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
66 def createPmdMaterial(m):
67 material = bpy.data.materials.new("Material")
68 material.diffuse_shader='TOON'
69 material.specular_shader='TOON'
70 material.diffuse_color=([m.diffuse.r, m.diffuse.g, m.diffuse.b])
71 material.alpha=m.diffuse.a
72 material.specular_hardness=int(m.shinness)
73 material.specular_color=([m.specular.r, m.specular.g, m.specular.b])
74 material.mirror_color=([m.ambient.r, m.ambient.g, m.ambient.b])
79 def createTexture(path):
80 texture=bpy.data.textures.new(os.path.basename(path))
82 texture=texture.recast_type()
83 image=bpy.data.images.load(path)
86 texture.interpolation = True
87 texture.use_alpha = True
91 def materialAddTexture(material, texture):
92 #material.add_texture(texture, "UV", {"COLOR", "ALPHA"})
93 material.add_texture(texture, "UV", "COLOR")
96 def meshAddMaterial(mesh, material):
97 mesh.add_material(material)
100 def createMesh(scene, name):
101 mesh=bpy.data.meshes.new("Mesh")
102 mesh_object= bpy.data.objects.new(name, mesh)
103 scene.objects.link(mesh_object)
104 return mesh, mesh_object
107 def objectMakeParent(parent, child):
111 def meshAddMqoGeometry(mesh, o, materials, imageMap, scale):
112 # count triangle and quadrangle
115 if f.index_count==3 or f.index_count==4:
117 mesh.add_geometry(len(o.vertices), 0, faceCount)
122 # convert right-handed y-up to right-handed z-up
123 unpackedVertices.extend(
124 (scale*v.x, scale*-v.z, scale*v.y))
125 mesh.verts.foreach_set("co", unpackedVertices)
133 for i in range(f.index_count):
134 face.append(f.getIndex(i))
139 if len(face) != 3 and len(face) != 4:
140 print("{0} vertices in face.".format(len(face)))
145 # rotate indices if the 4th is 0
146 face = [face[3], face[0], face[1], face[2]]
149 # rotate indices if the 3rd is 0
150 face = [face[2], face[0], face[1], 0]
154 unpackedFaces.extend(face)
155 usedMaterial.add(f.material_index)
157 mesh.faces.foreach_set("verts_raw", unpackedFaces)
159 #print([getFace(f) for f in o.faces])
160 print("fail to mesh.faces.foreach_set")
166 for i in usedMaterial:
167 mesh.add_material(materials[i])
168 meshMaterialMap[i]=materialIndex
172 mesh.add_uv_texture()
173 for mqo_face, blender_face, uv_face in zip(
174 o.faces, mesh.faces, mesh.uv_textures[0].data):
175 if mqo_face.index_count<3:
177 blender_face.material_index=meshMaterialMap[mqo_face.material_index]
178 if mqo_face.index_count>=3:
179 uv_face.uv1=[mqo_face.getUV(0).x, 1.0-mqo_face.getUV(0).y]
180 uv_face.uv2=[mqo_face.getUV(1).x, 1.0-mqo_face.getUV(1).y]
181 uv_face.uv3=[mqo_face.getUV(2).x, 1.0-mqo_face.getUV(2).y]
182 if mqo_face.index_count==4:
184 mqo_face.getUV(3).x, 1.0-mqo_face.getUV(3).y]
185 if materials[mqo_face.material_index] in imageMap:
186 uv_face.image=imageMap[mqo_face.material_index]
191 def getTexture(m, dirname):
195 for slot in m.texture_slots:
196 if slot and slot.texture:
198 if texture.type=="IMAGE":
202 imagePath=image.filename
203 if len(dirname)>0 and imagePath.startswith(dirname):
204 #
\e$BAjBP%Q%9$KJQ49$9$k
\e(B
205 imagePath=imagePath[len(dirname)+1:len(imagePath)]
206 #imagePath=Blender.sys.expandpath(
207 # imagePath).replace("\\", '/')
208 if slot.map_colordiff:
209 tex=" tex(\"%s\")" % imagePath
211 aplane=" aplane(\"%s\")" % imagePath
214 def objectDuplicate(scene, obj):
215 bpy.ops.object.select_all(action='DESELECT')
217 scene.objects.active=obj
218 bpy.ops.object.duplicate()
219 dumy=scene.objects.active
220 bpy.ops.object.rotation_apply()
221 bpy.ops.object.scale_apply()
222 bpy.ops.object.location_apply()
223 return dumy.data, dumy
225 def faceVertexCount(face):
226 return len(face.verts)
228 def faceVertices(face):
232 return mesh.active_uv_texture
234 def faceHasUV(mesh, i, face):
235 return mesh.active_uv_texture.data[i]
237 def faceGetUV(mesh, i, faces, count):
238 uvFace=mesh.active_uv_texture.data[i]
240 return (uvFace.uv1, uvFace.uv2, uvFace.uv3)
242 return (uvFace.uv1, uvFace.uv2, uvFace.uv3, uvFace.uv4)
247 def materialToMqo(m):
248 return "\"%s\" shader(3) col(%f %f %f %f)" % (
250 m.diffuse_color[0], m.diffuse_color[1], m.diffuse_color[2],
253 def faceMaterialIndex(face):
254 return face.material_index
256 def objectGetData(o):
259 def objectAddArmatureModifier(o, armature_object):
260 mod=o.modifiers.new("Modifier", "ARMATURE")
261 mod.object = armature_object
262 mod.use_bone_envelopes=False
267 def objectGetPose(o):
270 def poseBoneLimit(n, b):
273 if n.startswith("knee_"):
274 b.lock_rotation[1]=True
275 b.lock_rotation[2]=True
278 elif n.startswith("ankle_"):
279 b.lock_rotation[1]=True
282 bpy.ops.object.mode_set(mode='OBJECT', toggle=False)
285 bpy.ops.object.mode_set(mode='OBJECT', toggle=False)
287 def objectDeselectAll():
289 bpy.ops.object.select_all(action='DESELECT')
291 def objectActivate(scene, o):
294 scene.objects.active=o