goat3d

view exporters/blendgoat/src/export_goat.py @ 69:66cd8266f078

fixed animation export
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 21 Apr 2014 03:44:10 +0300
parents 0e48907847ad
children
line source
1 # Goat3D Blender >2.63 exporter
2 import bpy
3 import ctypes
4 from ctypes import *
5 from ctypes.util import find_library
7 GOAT3D_MAT_ATTR_DIFFUSE = "diffuse".encode('utf-8')
8 GOAT3D_MAT_ATTR_SPECULAR = "specular".encode('utf-8')
9 GOAT3D_MAT_ATTR_SHININESS = "shininess".encode('utf-8')
10 GOAT3D_MAT_ATTR_NORMAL = "normal".encode('utf-8')
11 GOAT3D_MAT_ATTR_BUMP = "bump".encode('utf-8')
12 GOAT3D_MAT_ATTR_REFLECTION = "reflection".encode('utf-8')
13 GOAT3D_MAT_ATTR_TRANSMISSION = "transmission".encode('utf-8')
14 GOAT3D_MAT_ATTR_IOR = "ior".encode('utf-8')
15 GOAT3D_MAT_ATTR_ALPHA = "alpha".encode('utf-8')
17 # these must match the goat3d_vertex_attr enumeration values in goat3d.h
18 GOAT3D_MESH_ATTR_VERTEX = 0
19 GOAT3D_MESH_ATTR_NORMAL = 1
20 GOAT3D_MESH_ATTR_TANGENT = 2
21 GOAT3D_MESH_ATTR_TEXCOORD = 3
22 GOAT3D_MESH_ATTR_SKIN_WEIGHT = 4
23 GOAT3D_MESH_ATTR_SKIN_MATRIX = 5
24 GOAT3D_MESH_ATTR_COLOR = 6
27 def export(oper, ctx, fname):
28 print("Exporting goat3d file: " + fname)
30 lib = init_libgoat()
31 if not lib:
32 return False
34 # initiaize a goat3d object and start filling it up with data
35 goat = lib.goat3d_create()
36 if not goat:
37 oper.report({'ERROR'}, "Failed to create goat3d object")
38 return False
40 lib.goat3d_set_name(goat, fname.encode('utf-8'))
42 export_env(ctx, goat, lib)
43 export_meshes(ctx, goat, lib)
44 export_nodes(ctx, goat, lib)
46 lib.goat3d_save(goat, fname.encode('utf-8'))
47 lib.goat3d_free(goat)
48 return True
50 def export_env(ctx, goat, lib):
51 return False
53 def export_material(bmtl, goat, lib):
54 name = bmtl.name.encode("utf-8")
56 mtl = lib.goat3d_create_mtl()
57 lib.goat3d_set_mtl_name(mtl, name)
59 s = bmtl.diffuse_intensity
60 col = bmtl.diffuse_color
61 lib.goat3d_set_mtl_attrib4f(mtl, GOAT3D_MAT_ATTR_DIFFUSE, c_float(col[0] * s), c_float(col[1] * s), c_float(col[2] * s), 1.0)
63 s = bmtl.specular_intensity
64 col = bmtl.specular_color
65 lib.goat3d_set_mtl_attrib4f(mtl, GOAT3D_MAT_ATTR_SPECULAR, c_float(col[0] * s), c_float(col[1] * s), c_float(col[2] * s), 1.0)
66 lib.goat3d_set_mtl_attrib1f(mtl, GOAT3D_MAT_ATTR_SHININESS, c_float(bmtl.specular_hardness))
67 lib.goat3d_set_mtl_attrib1f(mtl, GOAT3D_MAT_ATTR_ALPHA, c_float(bmtl.alpha))
69 if bmtl.raytrace_mirror.use:
70 lib.goat3d_set_mtl_attrib1f(mtl, GOAT3D_MAT_ATTR_REFLECTION, c_float(bmtl.raytrace_mirror.reflect_factor))
71 if bmtl.use_transparency and bmtl.transparency_method == 'RAYTRACE':
72 lib.goat3d_set_mtl_attrib1f(mtl, GOAT3D_MAT_ATTR_IOR, c_float(bmtl.raytrace_transparency.ior))
73 lib.goat3d_set_mtl_attrib1f(mtl, GOAT3D_MAT_ATTR_TRANSMISSION, 1.0)
75 # grab all the textures and apply them to the correct attributes
76 for texslot in bmtl.texture_slots:
77 if not texslot:
78 continue
80 tex = texslot.texture
81 if tex.type != 'IMAGE':
82 print("ignoring texture " + tex.name + ": not an image!")
83 continue
85 fname = tex.image.filepath.encode('ascii')
87 attr = ""
88 if texslot.use_map_color_diffuse or texslot.use_map_diffuse:
89 attr = GOAT3D_MAT_ATTR_DIFFUSE
90 elif texslot.use_map_color_spec or texslot.use_map_specular:
91 attr = GOAT3D_MAT_ATTR_SPECULAR
92 elif texslot.use_map_color_reflection or texslot.use_map_reflect:
93 attr = GOAT3D_MAT_ATTR_REFLECTION
94 elif texslot.use_map_hardness:
95 attr = GOAT3D_MAT_ATTR_SHININESS
96 elif texslot.use_map_alpha:
97 attr = GOAT3D_MAT_ATTR_ALPHA
98 elif texslot.use_map_normal:
99 attr = GOAT3D_MAT_ATTR_NORMAL
101 if attr != "":
102 lib.goat3d_set_mtl_attrib_map(mtl, attr, fname)
104 lib.goat3d_add_mtl(goat, mtl)
105 return mtl
108 def export_meshes(ctx, goat, lib):
109 print("exporting " + str(len(ctx.scene.objects)) + " objects")
110 for obj in ctx.scene.objects:
111 if obj.type != 'MESH':
112 continue
114 bmesh = obj.data
115 # make sure we get a tesselated mesh
116 bmesh.update(calc_tessface = True)
118 # create goat3d mesh and set the data
119 mesh = lib.goat3d_create_mesh()
120 lib.goat3d_set_mesh_name(mesh, bmesh.name.encode("utf-8"))
122 # get the material, add it to the scene and apply it to this mesh
123 for bmtl in bmesh.materials:
124 mtl = export_material(bmtl, goat, lib)
125 lib.goat3d_set_mesh_mtl(mesh, mtl)
126 break # we only care about one material
128 for vert in bmesh.vertices:
129 v = vert.co
130 n = vert.normal
131 lib.goat3d_add_mesh_attrib3f(mesh, GOAT3D_MESH_ATTR_VERTEX, v[0], v[1], v[2])
132 lib.goat3d_add_mesh_attrib3f(mesh, GOAT3D_MESH_ATTR_NORMAL, n[0], n[1], n[2])
134 for face in bmesh.tessfaces:
135 fverts = face.vertices
136 lib.goat3d_add_mesh_face(mesh, fverts[0], fverts[1], fverts[2])
138 lib.goat3d_add_mesh(goat, mesh)
139 return False
142 def export_nodes(ctx, goat, lib):
143 return False
145 def init_libgoat():
146 # load all relevant functions from libgoat3d
147 libname = find_library("goat3d")
148 if not libname:
149 oper.report({'ERROR'}, "Could not find the goat3d library! make sure it's installed.")
150 return None
152 lib = CDLL(libname)
153 if not lib:
154 oper.report({'ERROR'}, "Could not open goat3d library!")
155 return None
157 lib.goat3d_create.argtypes = None
158 lib.goat3d_create.restype = c_void_p
160 lib.goat3d_free.argtypes = [c_void_p]
161 lib.goat3d_free.restype = None
163 lib.goat3d_set_name.argtypes = [c_void_p, c_char_p]
165 lib.goat3d_set_ambient3f.argtypes = [c_void_p, c_float, c_float, c_float]
166 lib.goat3d_set_ambient3f.restype = None
168 lib.goat3d_add_mtl.argtypes = [c_void_p, c_void_p]
169 lib.goat3d_add_mtl.restype = None
171 lib.goat3d_create_mtl.argtypes = None
172 lib.goat3d_create_mtl.restype = c_void_p
174 lib.goat3d_set_mtl_name.argtypes = [c_void_p, c_char_p]
175 lib.goat3d_set_mtl_name.restype = None
177 lib.goat3d_set_mtl_attrib1f.argtypes = [c_void_p, c_char_p, c_float]
178 lib.goat3d_set_mtl_attrib1f.restype = None
180 lib.goat3d_set_mtl_attrib4f.argtypes = [c_void_p, c_char_p, c_float, c_float, c_float, c_float]
181 lib.goat3d_set_mtl_attrib4f.restype = None
183 lib.goat3d_set_mtl_attrib_map.argtypes = [c_void_p, c_char_p, c_char_p]
184 lib.goat3d_set_mtl_attrib_map.restype = None
186 lib.goat3d_add_mesh.argtypes = [c_void_p, c_void_p]
187 lib.goat3d_add_mesh.restype = None
189 lib.goat3d_create_mesh.argtypes = None
190 lib.goat3d_create_mesh.restype = c_void_p
192 lib.goat3d_set_mesh_name.argtypes = [c_void_p, c_char_p]
193 lib.goat3d_set_mesh_name.restype = None
195 lib.goat3d_set_mesh_mtl.argtypes = [c_void_p, c_void_p]
196 lib.goat3d_set_mesh_mtl.restype = None
198 lib.goat3d_add_mesh_attrib3f.argtypes = [c_void_p, c_int, c_float, c_float, c_float]
199 lib.goat3d_add_mesh_attrib3f.restype = None
201 lib.goat3d_add_mesh_attrib4f.argtypes = [c_void_p, c_int, c_float, c_float, c_float, c_float]
202 lib.goat3d_add_mesh_attrib4f.restype = None
204 lib.goat3d_add_mesh_face.argtypes = [c_void_p, c_int, c_int, c_int]
205 lib.goat3d_add_mesh_face.restype = None
207 lib.goat3d_add_node.argtypes = [c_void_p, c_void_p]
208 lib.goat3d_add_node.restype = None
210 lib.goat3d_create_node.argtypes = None
211 lib.goat3d_create_node.restype = c_void_p
213 lib.goat3d_set_node_name.argtypes = [c_void_p, c_char_p]
214 lib.goat3d_set_node_name.restype = None
216 lib.goat3d_set_node_object.argtypes = [c_void_p, c_int, c_void_p]
217 lib.goat3d_set_node_object.restype = None
219 lib.goat3d_add_node_child.argtypes = [c_void_p, c_void_p]
220 lib.goat3d_add_node_child.restype = None
222 lib.goat3d_set_node_position.argtypes = [c_void_p, c_float, c_float, c_float, c_long]
223 lib.goat3d_set_node_position.restype = None
225 lib.goat3d_set_node_rotation.argtypes = [c_void_p, c_float, c_float, c_float, c_float, c_long]
226 lib.goat3d_set_node_rotation.restype = None
228 lib.goat3d_set_node_scaling.argtypes = [c_void_p, c_float, c_float, c_float, c_long]
229 lib.goat3d_set_node_scaling.restype = None
231 lib.goat3d_set_node_pivot.argtypes = [c_void_p, c_float, c_float, c_float]
232 lib.goat3d_set_node_pivot.restype = None
234 lib.goat3d_save.argtypes = [c_void_p, c_char_p]
235 return lib