10 #
\e$B%U%!%$%k%7%9%F%`$NJ8;z%3!<%I
\e(B
11 #
\e$B2~B$HG$H$N6&MQ$N$?$a
\e(B
12 FS_ENCODING=sys.getfilesystemencoding()
13 if os.path.exists(os.path.dirname(sys.argv[0])+"/utf8"):
14 INTERNAL_ENCODING='utf-8'
16 INTERNAL_ENCODING=FS_ENCODING
19 ###############################################################################
21 ###############################################################################
22 class ProgressBar(object):
23 def __init__(self, base):
24 print("#### %s ####" % base)
26 self.start=time.time()
27 self.set('<start>', 0)
29 def advance(self, message, progress):
30 self.progress+=float(progress)
33 def set(self, message, progress):
34 self.progress=float(progress)
37 def _print(self, message):
39 message="%s: %s" % (self.base, message)
40 #Blender.Window.DrawProgressBar(self.progress, message)
44 message='finished in %.2f sec' % (time.time()-self.start)
45 self.set(message, 1.0)
48 ###############################################################################
50 def __init__(self, path, encoding):
51 self.io=open(path, "wb")
52 self.encoding=encoding
55 self.io.write(s.encode(self.encoding))
63 ###############################################################################
64 def createEmptyObject(scene, name):
65 empty=bpy.data.objects.new(name, None)
66 scene.objects.link(empty)
70 def createMqoMaterial(m):
71 material = bpy.data.materials.new(m.getName())
72 material.diffuse_color=[m.color.r, m.color.g, m.color.b]
73 material.alpha=m.color.a
74 material.diffuse_intensity=m.diffuse
79 def createPmdMaterial(m):
80 material = bpy.data.materials.new("Material")
81 material.diffuse_shader='TOON'
82 material.specular_shader='TOON'
83 material.diffuse_color=([m.diffuse.r, m.diffuse.g, m.diffuse.b])
84 material.alpha=m.diffuse.a
85 material.specular_hardness=int(m.shinness)
86 material.specular_color=([m.specular.r, m.specular.g, m.specular.b])
87 material.mirror_color=([m.ambient.r, m.ambient.g, m.ambient.b])
88 material.subsurface_scattering.enabled=True if m.flag==1 else False
93 def createTexture(path):
94 texture=bpy.data.textures.new(os.path.basename(path))
96 texture=texture.recast_type()
97 image=bpy.data.images.load(path)
100 texture.interpolation = True
101 texture.use_alpha = True
102 return texture, image
105 def materialAddTexture(material, texture):
106 #material.add_texture(texture, "UV", {"COLOR", "ALPHA"})
107 material.add_texture(texture, "UV", "COLOR")
110 def meshAddMaterial(mesh, material):
111 mesh.add_material(material)
114 def createMesh(scene, name):
115 mesh=bpy.data.meshes.new("Mesh")
116 mesh_object= bpy.data.objects.new(name, mesh)
117 scene.objects.link(mesh_object)
118 return mesh, mesh_object
121 def objectMakeParent(parent, child):
125 def meshAddMqoGeometry(mesh, o, materials, imageMap, scale):
126 # count triangle and quadrangle
129 if f.index_count==3 or f.index_count==4:
131 mesh.add_geometry(len(o.vertices), 0, faceCount)
136 # convert right-handed y-up to right-handed z-up
137 unpackedVertices.extend(
138 (scale*v.x, scale*-v.z, scale*v.y))
139 mesh.verts.foreach_set("co", unpackedVertices)
147 for i in range(f.index_count):
148 face.append(f.getIndex(i))
153 if len(face) != 3 and len(face) != 4:
154 print("{0} vertices in face.".format(len(face)))
159 # rotate indices if the 4th is 0
160 face = [face[3], face[0], face[1], face[2]]
163 # rotate indices if the 3rd is 0
164 face = [face[2], face[0], face[1], 0]
168 unpackedFaces.extend(face)
169 usedMaterial.add(f.material_index)
171 mesh.faces.foreach_set("verts_raw", unpackedFaces)
173 #print([getFace(f) for f in o.faces])
174 print("fail to mesh.faces.foreach_set")
180 for i in usedMaterial:
181 mesh.add_material(materials[i])
182 meshMaterialMap[i]=materialIndex
186 mesh.add_uv_texture()
187 for mqo_face, blender_face, uv_face in zip(
188 o.faces, mesh.faces, mesh.uv_textures[0].data):
189 if mqo_face.index_count<3:
191 blender_face.material_index=meshMaterialMap[mqo_face.material_index]
192 if mqo_face.index_count>=3:
193 uv_face.uv1=[mqo_face.getUV(0).x, 1.0-mqo_face.getUV(0).y]
194 uv_face.uv2=[mqo_face.getUV(1).x, 1.0-mqo_face.getUV(1).y]
195 uv_face.uv3=[mqo_face.getUV(2).x, 1.0-mqo_face.getUV(2).y]
196 if mqo_face.index_count==4:
198 mqo_face.getUV(3).x, 1.0-mqo_face.getUV(3).y]
199 if materials[mqo_face.material_index] in imageMap:
200 uv_face.image=imageMap[mqo_face.material_index]
205 def getTexture(m, dirname):
209 for slot in m.texture_slots:
210 if slot and slot.texture:
212 if texture.type=="IMAGE":
216 imagePath=image.filename
217 if len(dirname)>0 and imagePath.startswith(dirname):
218 #
\e$BAjBP%Q%9$KJQ49$9$k
\e(B
219 imagePath=imagePath[len(dirname)+1:len(imagePath)]
220 #imagePath=Blender.sys.expandpath(
221 # imagePath).replace("\\", '/')
222 if slot.map_colordiff:
223 tex=" tex(\"%s\")" % imagePath
225 aplane=" aplane(\"%s\")" % imagePath
228 def objectDuplicate(scene, obj):
229 bpy.ops.object.select_all(action='DESELECT')
231 scene.objects.active=obj
232 bpy.ops.object.duplicate()
233 dumy=scene.objects.active
234 bpy.ops.object.rotation_apply()
235 bpy.ops.object.scale_apply()
236 bpy.ops.object.location_apply()
237 return dumy.data, dumy
239 def objectDelete(scene, obj):
240 scene.objects.unlink(obj)
242 def faceVertexCount(face):
243 return len(face.verts)
245 def faceVertices(face):
249 return mesh.active_uv_texture
251 def faceHasUV(mesh, i, face):
252 return mesh.active_uv_texture.data[i]
254 def faceGetUV(mesh, i, faces, count):
255 uvFace=mesh.active_uv_texture.data[i]
257 return (uvFace.uv1, uvFace.uv2, uvFace.uv3)
259 return (uvFace.uv1, uvFace.uv2, uvFace.uv3, uvFace.uv4)
264 def materialToMqo(m):
265 return "\"%s\" shader(3) col(%f %f %f %f)" % (
267 m.diffuse_color[0], m.diffuse_color[1], m.diffuse_color[2],
270 def faceMaterialIndex(face):
271 return face.material_index
273 def objectGetData(o):
276 def objectAddArmatureModifier(o, armature_object):
277 mod=o.modifiers.new("Modifier", "ARMATURE")
278 mod.object = armature_object
279 mod.use_bone_envelopes=False
284 def objectGetPose(o):
287 def poseBoneLimit(n, b):
290 if n.startswith("knee_"):
297 elif n.startswith("ankle_"):
302 bpy.ops.object.mode_set(mode='EDIT', toggle=False)
305 bpy.ops.object.mode_set(mode='OBJECT', toggle=False)
307 def objectDeselectAll():
308 bpy.ops.object.select_all(action='DESELECT')
310 def objectActivate(scene, o):
312 scene.objects.active=o
314 def meshAddVertexGroup(meshObject, name):
315 meshObject.add_vertex_group(name)
317 def vertexSetNormal(mvert, normal):
318 mvert.normal=mathutils.Vector(normal)
320 def meshUseVertexUv(mesh):
323 def vertexSetUv(mvert, uv):
326 def meshAssignVertexGroup(meshObject, name, index, weight):
327 meshObject.add_vertex_to_group(index,
328 meshObject.vertex_groups[name], weight, 'ADD')
330 def meshCreateVerteicesAndFaces(mesh, vertices, faces):
331 vertexCount=int(len(vertices)/3)
332 faceCount=int(len(faces)/4)
333 mesh.add_geometry(vertexCount, 0, faceCount)
334 mesh.verts.foreach_set("co", vertices)
335 mesh.faces.foreach_set("verts_raw", faces)
336 assert(len(mesh.verts)==vertexCount)
339 mesh.add_uv_texture()
341 def meshVertsDelete(mesh, remove_vertices):
343 bpy.ops.mesh.select_all(action='DESELECT')
344 for i in remove_vertices:
345 mesh.verts[i].selected=True
346 bpy.ops.mesh.delete(type='VERT')
349 def createArmature(scene):
350 armature = bpy.data.armatures.new('Armature')
351 armature_object=bpy.data.objects.new('Armature', armature)
352 scene.objects.link(armature_object)
354 armature_object.x_ray=True
355 armature.draw_names=True
356 armature.drawtype='OCTAHEDRAL'
357 armature.deform_envelope=False
358 armature.deform_vertexgroups=True
359 armature.x_axis_mirror=True
361 return armature, armature_object
363 def armatureMakeEditable(scene, armature_object):
364 # select only armature object and set edit mode
365 scene.objects.active=armature_object
366 bpy.ops.object.mode_set(mode='OBJECT', toggle=False)
367 bpy.ops.object.mode_set(mode='EDIT', toggle=False)
369 def createIkConstraint(armature_object, p_bone, effector_name, ik):
370 constraint = p_bone.constraints.new('IK')
371 constraint.chain_length=len(ik.children)
372 constraint.target=armature_object
373 constraint.subtarget=effector_name
374 constraint.use_tail=False
377 def createArmatureBone(armature, name):
378 return armature.edit_bones.new(name)
380 def boneSetConnected(bone):
383 def createVector(x, y, z):
384 return mathutils.Vector([x, y, z])
386 def armatureUpdate(armature):
389 def boneLayerMask(bone, layers):
393 layer.append(True if layers[i]!=0 else False)
398 def objectPinShape(o):
399 o.shape_key_lock=True
401 def objectAddShapeKey(o, name):
402 return o.add_shape_key(name)
404 def objectActivateShapeKey(o, index):
405 o.active_shape_key_index=index
407 def shapeKeyAssign(shapeKey, index, pos):
408 shapeKey.data[index].co=pos
410 def objectIsVisible(obj):
411 return obj.restrict_view
413 def meshVertexGroupNames(meshObject):
414 for g in meshObject.vertex_groups:
417 def faceNormal(face):
420 def meshFaceUv(mesh, i, face):
421 return mesh.uv_textures[0].data[i].uv
423 def armatureModifierGetObject(m):
426 def objectHasShapeKey(o):
427 return o.data.shape_keys
429 def objectShapeKeys(o):
430 return o.data.shape_keys.keys
432 def meshVertexGroup(meshObject, name):
433 for i, v in enumerate(meshObject.data.verts):
435 if meshObject.vertex_groups[g.group].name==name:
438 def materialGet(scene, material_name):
439 return bpy.data.materials[material_name]