summaryrefslogtreecommitdiff
path: root/libs/ode-0.16.1/tools/ode_convex_export.py
blob: 17375b5f4c87a43a7866f9f33ae23812dc827e71 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!BPY
"""
Name: 'ODE Convex...'
Blender: 244
Group: 'Export'
Tooltip: 'Export to Open Dynamics.'
"""
__author__ = "Rodrigo Hernandez"
__url__ = ("http://www.ode.org")
__version__ = "0.1"
__bpydoc__ = """\
ODE Convex Exporter
This script Exports a Blender scene as a series of ODE Convex Geom data stored in a C header file.
"""
import Blender
import bpy

def WriteMesh(file,ob):
	mesh = ob.getData(mesh=1)
	# Write Point Count
	file.write("unsigned int %s_pointcount = %d;\n" % (ob.getName(),len(mesh.verts)))
	# Write Plane Count
	file.write("unsigned int %s_planecount = %d;\n" % (ob.getName(),len(mesh.faces)))
	# Write Points
	file.write("dReal %s_points[%d]={\n" % (ob.getName(),(len(mesh.verts)*3)))
	for vert in mesh.verts:
		if vert.index==(len(mesh.verts)-1):
			file.write("%f,%f,%f\n};\n" % (vert.co[0],vert.co[1],vert.co[2]))
		else:
			file.write("%f,%f,%f,\n" % (vert.co[0],vert.co[1],vert.co[2]))
	# Write Polygons
	file.write("unsigned int %s_polygons[]={\n" % ob.getName())
	for face in mesh.faces:
		file.write("%d," % len(face.verts))
		for vert in face.verts:
			file.write("%d," % vert.index)
		if face.index==(len(mesh.faces)-1):
			file.write("\n};\n");
		else:
			file.write("\n");
	# Write Planes
	file.write("dReal %s_planes[]={\n" % ob.getName())
	for face in mesh.faces:
		# d calculated separatelly for code readability
		d = face.no[0]*face.verts[0].co[0]+face.no[1]*face.verts[0].co[1]+face.no[2]*face.verts[0].co[2]
		file.write("%f,%f,%f,%f,\n" % (face.no[0],face.no[1],face.no[2],d))
	file.write("};\n");
	
# Entry Point
sce = bpy.data.scenes.active
in_editmode = Blender.Window.EditMode()
if in_editmode: Blender.Window.EditMode(0)
file = open("convex.h",mode='wt')
for ob in sce.objects:
	if ob.getType()=='Mesh':
		WriteMesh(file,ob)
file.close()
if in_editmode:
	Blender.Window.EditMode(1)
print "ODE Export Done"