6 ###############################################################################
8 ###############################################################################
9 class ProgressBar(object):
10 def __init__(self, base):
11 print("#### %s ####" % base)
13 self.start=time.time()
14 self.set('<start>', 0)
16 def advance(self, message, progress):
17 self.progress+=float(progress)
20 def set(self, message, progress):
21 self.progress=float(progress)
24 def _print(self, message):
26 message="%s: %s" % (self.base, message)
27 #Blender.Window.DrawProgressBar(self.progress, message)
31 message='finished in %.2f sec' % (time.time()-self.start)
32 self.set(message, 1.0)
35 ###############################################################################
37 def __init__(self, path, encoding):
38 self.io=open(path, "wb")
39 self.encoding=encoding
42 self.io.write(s.encode(self.encoding))
50 ###############################################################################
51 def createEmptyObject(scene, name):
52 empty=bpy.data.objects.new(name, None)
53 scene.objects.link(empty)
57 def createMqoMaterial(m):
58 material = bpy.data.materials.new(m.getName())
59 material.diffuse_color=[m.color.r, m.color.g, m.color.b]
60 material.alpha=m.color.a
61 material.diffuse_intensity=m.diffuse
67 def createTexture(path):
68 texture=bpy.data.textures.new(os.path.basename(path))
70 texture=texture.recast_type()
71 image=bpy.data.images.load(path)
74 texture.interpolation = True
75 texture.use_alpha = True
79 def materialAddTexture(material, texture):
80 #material.add_texture(texture, "UV", {"COLOR", "ALPHA"})
81 material.add_texture(texture, "UV", "COLOR")
84 def createMesh(scene, name):
85 mesh=bpy.data.meshes.new("Mesh")
86 mesh_object= bpy.data.objects.new(name, mesh)
87 scene.objects.link(mesh_object)
88 return mesh, mesh_object
91 def objectMakeParent(parent, child):
95 def meshAddMqoGeometry(mesh, o, materials, imageMap, scale):
96 # count triangle and quadrangle
99 if f.index_count==3 or f.index_count==4:
101 mesh.add_geometry(len(o.vertices), 0, faceCount)
106 # convert right-handed y-up to right-handed z-up
107 unpackedVertices.extend(
108 (scale*v.x, scale*-v.z, scale*v.y))
109 mesh.verts.foreach_set("co", unpackedVertices)
117 for i in range(f.index_count):
118 face.append(f.getIndex(i))
123 if len(face) != 3 and len(face) != 4:
124 print("{0} vertices in face.".format(len(face)))
129 # rotate indices if the 4th is 0
130 face = [face[3], face[0], face[1], face[2]]
133 # rotate indices if the 3rd is 0
134 face = [face[2], face[0], face[1], 0]
138 unpackedFaces.extend(face)
139 usedMaterial.add(f.material_index)
141 mesh.faces.foreach_set("verts_raw", unpackedFaces)
143 #print([getFace(f) for f in o.faces])
144 print("fail to mesh.faces.foreach_set")
150 for i in usedMaterial:
151 mesh.add_material(materials[i])
152 meshMaterialMap[i]=materialIndex
156 mesh.add_uv_texture()
157 for mqo_face, blender_face, uv_face in zip(
158 o.faces, mesh.faces, mesh.uv_textures[0].data):
159 if mqo_face.index_count<3:
161 blender_face.material_index=meshMaterialMap[mqo_face.material_index]
162 if mqo_face.index_count>=3:
163 uv_face.uv1=[mqo_face.getUV(0).x, 1.0-mqo_face.getUV(0).y]
164 uv_face.uv2=[mqo_face.getUV(1).x, 1.0-mqo_face.getUV(1).y]
165 uv_face.uv3=[mqo_face.getUV(2).x, 1.0-mqo_face.getUV(2).y]
166 if mqo_face.index_count==4:
168 mqo_face.getUV(3).x, 1.0-mqo_face.getUV(3).y]
169 if materials[mqo_face.material_index] in imageMap:
170 uv_face.image=imageMap[mqo_face.material_index]
175 def getTexture(m, dirname):
179 for slot in m.texture_slots:
180 if slot and slot.texture:
182 if texture.type=="IMAGE":
186 imagePath=image.filename
187 if len(dirname)>0 and imagePath.startswith(dirname):
188 #
\e$BAjBP%Q%9$KJQ49$9$k
\e(B
189 imagePath=imagePath[len(dirname)+1:len(imagePath)]
190 #imagePath=Blender.sys.expandpath(
191 # imagePath).replace("\\", '/')
192 if slot.map_colordiff:
193 tex=" tex(\"%s\")" % imagePath
195 aplane=" aplane(\"%s\")" % imagePath
198 def objectDuplicate(scene, obj):
199 bpy.ops.object.select_all(action='DESELECT')
201 scene.objects.active=obj
202 bpy.ops.object.duplicate()
203 dumy=scene.objects.active
204 bpy.ops.object.rotation_apply()
205 bpy.ops.object.scale_apply()
206 bpy.ops.object.location_apply()
207 return dumy.data, dumy
209 def faceVertexCount(face):
210 return len(face.verts)
212 def faceVertices(face):
216 return mesh.active_uv_texture
218 def faceHasUV(mesh, i, face):
219 return mesh.active_uv_texture.data[i]
221 def faceGetUV(mesh, i, faces, count):
222 uvFace=mesh.active_uv_texture.data[i]
224 return (uvFace.uv1, uvFace.uv2, uvFace.uv3)
226 return (uvFace.uv1, uvFace.uv2, uvFace.uv3, uvFace.uv4)
231 def materialToMqo(m):
232 return "\"%s\" shader(3) col(%f %f %f %f)" % (
234 m.diffuse_color[0], m.diffuse_color[1], m.diffuse_color[2],
237 def faceMaterialIndex(face):
238 return face.material_index
240 def objectGetData(o):
243 def objectAddArmatureModifier(o, armature_object):
244 mod=o.modifiers.new("Modifier", "ARMATURE")
245 mod.object = armature_object
246 mod.use_bone_envelopes=False
251 def objectGetPose(o):
254 def poseBoneLimit(n, b):
257 if n.startswith("knee_"):
258 b.lock_rotation[1]=True
259 b.lock_rotation[2]=True
262 elif n.startswith("ankle_"):
263 b.lock_rotation[1]=True
266 bpy.ops.object.mode_set(mode='OBJECT', toggle=False)
269 bpy.ops.object.mode_set(mode='OBJECT', toggle=False)
271 def objectDeselectAll():
273 bpy.ops.object.select_all(action='DESELECT')
275 def objectActivate(scene, o):
278 scene.objects.active=o