9 ###############################################################################
11 ###############################################################################
12 class ProgressBar(object):
13 def __init__(self, base):
14 print("#### %s ####" % base)
16 self.start=time.time()
17 self.set('<start>', 0)
19 def advance(self, message, progress):
20 self.progress+=float(progress)
23 def set(self, message, progress):
24 self.progress=float(progress)
27 def _print(self, message):
29 message="%s: %s" % (self.base, message)
30 #Blender.Window.DrawProgressBar(self.progress, message)
34 message='finished in %.2f sec' % (time.time()-self.start)
35 self.set(message, 1.0)
38 ###############################################################################
40 def __init__(self, path, encoding):
41 self.io=open(path, "wb")
42 self.encoding=encoding
45 self.io.write(s.encode(self.encoding))
53 ###############################################################################
54 def createEmptyObject(scene, name):
55 empty=bpy.data.objects.new(name, None)
56 scene.objects.link(empty)
60 def createMqoMaterial(m):
61 material = bpy.data.materials.new(m.getName())
62 material.diffuse_color=[m.color.r, m.color.g, m.color.b]
63 material.alpha=m.color.a
64 material.diffuse_intensity=m.diffuse
69 def createPmdMaterial(m):
70 material = bpy.data.materials.new("Material")
71 material.diffuse_shader='TOON'
72 material.specular_shader='TOON'
73 material.diffuse_color=([m.diffuse.r, m.diffuse.g, m.diffuse.b])
74 material.alpha=m.diffuse.a
75 material.specular_hardness=int(m.shinness)
76 material.specular_color=([m.specular.r, m.specular.g, m.specular.b])
77 material.mirror_color=([m.ambient.r, m.ambient.g, m.ambient.b])
78 material.subsurface_scattering.enabled=True if m.flag==1 else False
83 def createTexture(path):
84 texture=bpy.data.textures.new(os.path.basename(path))
86 texture=texture.recast_type()
87 image=bpy.data.images.load(path)
90 texture.interpolation = True
91 texture.use_alpha = True
95 def materialAddTexture(material, texture):
96 #material.add_texture(texture, "UV", {"COLOR", "ALPHA"})
97 material.add_texture(texture, "UV", "COLOR")
100 def meshAddMaterial(mesh, material):
101 mesh.add_material(material)
104 def createMesh(scene, name):
105 mesh=bpy.data.meshes.new("Mesh")
106 mesh_object= bpy.data.objects.new(name, mesh)
107 scene.objects.link(mesh_object)
108 return mesh, mesh_object
111 def objectMakeParent(parent, child):
115 def meshAddMqoGeometry(mesh, o, materials, imageMap, scale):
116 # count triangle and quadrangle
119 if f.index_count==3 or f.index_count==4:
121 mesh.add_geometry(len(o.vertices), 0, faceCount)
126 # convert right-handed y-up to right-handed z-up
127 unpackedVertices.extend(
128 (scale*v.x, scale*-v.z, scale*v.y))
129 mesh.verts.foreach_set("co", unpackedVertices)
137 for i in range(f.index_count):
138 face.append(f.getIndex(i))
143 if len(face) != 3 and len(face) != 4:
144 print("{0} vertices in face.".format(len(face)))
149 # rotate indices if the 4th is 0
150 face = [face[3], face[0], face[1], face[2]]
153 # rotate indices if the 3rd is 0
154 face = [face[2], face[0], face[1], 0]
158 unpackedFaces.extend(face)
159 usedMaterial.add(f.material_index)
161 mesh.faces.foreach_set("verts_raw", unpackedFaces)
163 #print([getFace(f) for f in o.faces])
164 print("fail to mesh.faces.foreach_set")
170 for i in usedMaterial:
171 mesh.add_material(materials[i])
172 meshMaterialMap[i]=materialIndex
176 mesh.add_uv_texture()
177 for mqo_face, blender_face, uv_face in zip(
178 o.faces, mesh.faces, mesh.uv_textures[0].data):
179 if mqo_face.index_count<3:
181 blender_face.material_index=meshMaterialMap[mqo_face.material_index]
182 if mqo_face.index_count>=3:
183 uv_face.uv1=[mqo_face.getUV(0).x, 1.0-mqo_face.getUV(0).y]
184 uv_face.uv2=[mqo_face.getUV(1).x, 1.0-mqo_face.getUV(1).y]
185 uv_face.uv3=[mqo_face.getUV(2).x, 1.0-mqo_face.getUV(2).y]
186 if mqo_face.index_count==4:
188 mqo_face.getUV(3).x, 1.0-mqo_face.getUV(3).y]
189 if materials[mqo_face.material_index] in imageMap:
190 uv_face.image=imageMap[mqo_face.material_index]
195 def getTexture(m, dirname):
199 for slot in m.texture_slots:
200 if slot and slot.texture:
202 if texture.type=="IMAGE":
206 imagePath=image.filename
207 if len(dirname)>0 and imagePath.startswith(dirname):
208 #
\e$BAjBP%Q%9$KJQ49$9$k
\e(B
209 imagePath=imagePath[len(dirname)+1:len(imagePath)]
210 #imagePath=Blender.sys.expandpath(
211 # imagePath).replace("\\", '/')
212 if slot.map_colordiff:
213 tex=" tex(\"%s\")" % imagePath
215 aplane=" aplane(\"%s\")" % imagePath
218 def objectDuplicate(scene, obj):
219 bpy.ops.object.select_all(action='DESELECT')
221 scene.objects.active=obj
222 bpy.ops.object.duplicate()
223 dumy=scene.objects.active
224 bpy.ops.object.rotation_apply()
225 bpy.ops.object.scale_apply()
226 bpy.ops.object.location_apply()
227 return dumy.data, dumy
229 def objectDelete(scene, obj):
230 scene.objects.unlink(obj)
232 def faceVertexCount(face):
233 return len(face.verts)
235 def faceVertices(face):
239 return mesh.active_uv_texture
241 def faceHasUV(mesh, i, face):
242 return mesh.active_uv_texture.data[i]
244 def faceGetUV(mesh, i, faces, count):
245 uvFace=mesh.active_uv_texture.data[i]
247 return (uvFace.uv1, uvFace.uv2, uvFace.uv3)
249 return (uvFace.uv1, uvFace.uv2, uvFace.uv3, uvFace.uv4)
254 def materialToMqo(m):
255 return "\"%s\" shader(3) col(%f %f %f %f)" % (
257 m.diffuse_color[0], m.diffuse_color[1], m.diffuse_color[2],
260 def faceMaterialIndex(face):
261 return face.material_index
263 def objectGetData(o):
266 def objectAddArmatureModifier(o, armature_object):
267 mod=o.modifiers.new("Modifier", "ARMATURE")
268 mod.object = armature_object
269 mod.use_bone_envelopes=False
274 def objectGetPose(o):
277 def poseBoneLimit(n, b):
280 if n.startswith("knee_"):
287 elif n.startswith("ankle_"):
292 bpy.ops.object.mode_set(mode='EDIT', toggle=False)
295 bpy.ops.object.mode_set(mode='OBJECT', toggle=False)
297 def objectDeselectAll():
298 bpy.ops.object.select_all(action='DESELECT')
300 def objectActivate(scene, o):
302 scene.objects.active=o
304 def meshAddVertexGroup(meshObject, name):
305 meshObject.add_vertex_group(name)
307 def vertexSetNormal(mvert, normal):
308 mvert.normal=mathutils.Vector(normal)
310 def meshUseVertexUv(mesh):
313 def vertexSetUv(mvert, uv):
316 def meshAssignVertexGroup(meshObject, name, index, weight):
317 meshObject.add_vertex_to_group(index,
318 meshObject.vertex_groups[name], weight, 'ADD')
320 def meshCreateVerteicesAndFaces(mesh, vertices, faces):
321 vertexCount=int(len(vertices)/3)
322 faceCount=int(len(faces)/4)
323 mesh.add_geometry(vertexCount, 0, faceCount)
324 mesh.verts.foreach_set("co", vertices)
325 mesh.faces.foreach_set("verts_raw", faces)
326 assert(len(mesh.verts)==vertexCount)
329 mesh.add_uv_texture()
331 def meshVertsDelete(mesh, remove_vertices):
333 bpy.ops.mesh.select_all(action='DESELECT')
334 for i in remove_vertices:
335 mesh.verts[i].selected=True
336 bpy.ops.mesh.delete(type='VERT')
339 def createArmature(scene):
340 armature = bpy.data.armatures.new('Armature')
341 armature_object=bpy.data.objects.new('Armature', armature)
342 scene.objects.link(armature_object)
344 armature_object.x_ray=True
345 armature.draw_names=True
346 armature.drawtype='OCTAHEDRAL'
347 armature.deform_envelope=False
348 armature.deform_vertexgroups=True
349 armature.x_axis_mirror=True
351 return armature, armature_object
353 def armatureMakeEditable(scene, armature_object):
354 # select only armature object and set edit mode
355 scene.objects.active=armature_object
356 bpy.ops.object.mode_set(mode='OBJECT', toggle=False)
357 bpy.ops.object.mode_set(mode='EDIT', toggle=False)
359 def createIkConstraint(armature_object, p_bone, effector_name, ik):
360 constraint = p_bone.constraints.new('IK')
361 constraint.chain_length=len(ik.children)
362 constraint.target=armature_object
363 constraint.subtarget=effector_name
364 constraint.use_tail=False
367 def createArmatureBone(armature, name):
368 return armature.edit_bones.new(name)
370 def boneSetConnected(bone):
373 def createVector(x, y, z):
374 return mathutils.Vector([x, y, z])
376 def armatureUpdate(armature):
379 def boneLayerMask(bone, layers):
383 layer.append(True if layers[i]!=0 else False)
388 def objectPinShape(o):
389 o.shape_key_lock=True
391 def objectAddShapeKey(o, name):
392 return o.add_shape_key(name)
394 def objectActivateShapeKey(o, index):
395 o.active_shape_key_index=index
397 def shapeKeyAssign(shapeKey, index, pos):
398 shapeKey.data[index].co=pos
400 def objectIsVisible(obj):
401 return obj.restrict_view
403 def meshVertexGroupNames(meshObject):
404 for g in meshObject.vertex_groups:
407 def faceNormal(face):
410 def meshFaceUv(mesh, i, face):
411 return mesh.uv_textures[0].data[i].uv
413 def armatureModifierGetObject(m):
416 def objectHasShapeKey(o):
417 return o.data.shape_keys
419 def objectShapeKeys(o):
420 return o.data.shape_keys.keys
422 def meshVertexGroup(meshObject, name):
423 for i, v in enumerate(meshObject.data.verts):
425 if meshObject.vertex_groups[g.group].name==name:
428 def materialGet(scene, material_name):
429 return bpy.data.materials[material_name]