4 Name: 'MikuMikuDance model (.pmd)...'
7 Tooltip: 'Export PMD file for MikuMikuDance.'
9 __author__= ["ousttrue"]
15 This script exports a pmd model.
17 0.1 20100318: first implementation.
18 0.2 20100519: refactoring. use C extension.
19 1.0 20100530: implement basic features.
20 1.1 20100612: integrate 2.4 and 2.5.
21 1.2 20100616: implement rigid body.
22 1.3 20100619: fix rigid body, bone weight.
23 1.4 20100626: refactoring.
24 1.5 20100629: sphere map.
27 MMD_SHAPE_GROUP_NAME='_MMD_SHAPE'
28 BASE_SHAPE_NAME='Basis'
29 RIGID_SHAPE_TYPE='rigid_shape_type'
30 RIGID_PROCESS_TYPE='rigid_process_type'
31 RIGID_BONE_NAME='rigid_bone_name'
32 #RIGID_LOCATION='rigid_loation'
33 RIGID_GROUP='ribid_group'
34 RIGID_INTERSECTION_GROUP='rigid_intersection_group'
35 RIGID_WEIGHT='rigid_weight'
36 RIGID_LINEAR_DAMPING='rigid_linear_damping'
37 RIGID_ANGULAR_DAMPING='rigid_angular_damping'
38 RIGID_RESTITUTION='rigid_restitution'
39 RIGID_FRICTION='rigid_friction'
40 CONSTRAINT_NAME='constraint_name'
41 CONSTRAINT_A='const_a'
42 CONSTRAINT_B='const_b'
43 CONSTRAINT_POS_MIN='const_pos_min'
44 CONSTRAINT_POS_MAX='const_pos_max'
45 CONSTRAINT_ROT_MIN='const_rot_min'
46 CONSTRAINT_ROT_MAX='const_rot_max'
47 CONSTRAINT_SPRING_POS='const_spring_pos'
48 CONSTRAINT_SPRING_ROT='const_spring_rot'
49 TOON_TEXTURE_OBJECT='ToonTextures'
52 ###############################################################################
54 ###############################################################################
59 from meshio import pmd, englishmap
62 return sys.version_info[0]<3
67 from Blender import Mathutils
73 def setMaterialParams(material, m):
75 material.diffuse.r=m.R
76 material.diffuse.g=m.G
77 material.diffuse.b=m.B
78 material.diffuse.a=m.alpha
80 material.sinness=0 if m.spec<1e-5 else m.spec*10
81 material.specular.r=m.specR
82 material.specular.g=m.specG
83 material.specular.b=m.specB
85 material.ambient.r=m.mirR
86 material.ambient.g=m.mirG
87 material.ambient.b=m.mirB
89 material.flag=1 if m.enableSSS else 0
98 from bpy.props import *
106 def setMaterialParams(material, m):
108 material.diffuse.r=m.diffuse_color[0]
109 material.diffuse.g=m.diffuse_color[1]
110 material.diffuse.b=m.diffuse_color[2]
111 material.diffuse.a=m.alpha
113 material.sinness=0 if m.specular_toon_size<1e-5 else m.specular_hardness*10
114 material.specular.r=m.specular_color[0]
115 material.specular.g=m.specular_color[1]
116 material.specular.b=m.specular_color[2]
118 material.ambient.r=m.mirror_color[0]
119 material.ambient.g=m.mirror_color[1]
120 material.ambient.b=m.mirror_color[2]
122 material.flag=1 if m.subsurface_scattering.enabled else 0
124 material.toon_index=7
127 return s.encode('cp932')
131 __slots__=['o', 'children']
132 def __init__(self, o):
137 ###############################################################################
138 # Blenderのメッシュをワンスキンメッシュ化する
139 ###############################################################################
140 def near(x, y, EPSILON=1e-5):
142 return d>=-EPSILON and d<=EPSILON
145 class VertexKey(object):
152 'nx', 'ny', 'nz', # 法線
156 def __init__(self, obj, index, x, y, z, nx, ny, nz, u, v):
169 return "<vkey: %f, %f, %f, %f, %f, %f, %f, %f>" % (
170 self.x, self.y, self.z, self.nx, self.ny, self.nz, self.u, self.v)
173 #return int((self.x+self.y+self.z+self.nx+self.ny+self.nz+self.u+self.v)*100)
174 #return int((self.x+self.y+self.z)*100)
175 return self.index*100+self.obj
177 def __eq__(self, rhs):
178 #return near(self.x, rhs.x) and near(self.y, rhs.y) and near(self.z, rhs.z) and near(self.nx, rhs.nx) and near(self.ny, rhs.ny) and near(self.nz, rhs.nz) and near(self.u, rhs.u) and near(self.v, rhs.v)
179 #return near(self.x, rhs.x) and near(self.y, rhs.y) and near(self.z, rhs.z)
180 #return self.x==rhs.x and self.y==rhs.y and self.z==rhs.z
181 return self.obj==rhs.obj and self.index==rhs.index
184 class VertexArray(object):
193 'b0', 'b1', 'weight',
198 # マテリアル毎に分割したインデックス配列
214 return "<VertexArray %d vertices, %d indexArrays>" % (
215 len(self.vertices), len(self.indexArrays))
219 self.vertices, self.normals, self.uvs,
220 self.b0, self.b1, self.weight)
223 keys=[key for key in self.indexArrays.keys()]
226 yield(key, self.indexArrays[key])
228 def __getIndex(self, obj, base_index, pos, normal, uv, b0, b1, weight0):
234 pos[0], pos[1], pos[2],
235 normal[0], normal[1], normal[2],
237 if key in self.vertexMap:
239 index=self.vertexMap[key]
241 index=len(self.vertices)
243 self.vertexMap[key]=index
245 self.vertices.append((pos.x, pos.y, pos.z))
246 self.normals.append((normal.x, normal.y, normal.z))
247 self.uvs.append((uv[0], uv[1]))
250 self.weight.append(weight0)
253 #if not base_index in self.indexMap:
254 # self.indexMap[base_index]=set()
255 #self.indexMap[base_index].add(index)
260 def getMappedIndex(self, obj_name, base_index):
262 self.objectMap[obj_name], base_index,
266 return self.vertexMap[key]
268 def getMappedIndices(self, obj, base_index):
269 return self.indexMap[base_index]
271 def addTriangle(self,
272 object_name, material,
273 base_index0, base_index1, base_index2,
279 weight0, weight1, weight2
281 if object_name in self.objectMap:
282 obj=self.objectMap[object_name]
284 obj=len(self.objectMap)
285 self.objectMap[object_name]=obj
286 index0=self.__getIndex(obj, base_index0, pos0, n0, uv0, b0_0, b1_0, weight0)
287 index1=self.__getIndex(obj, base_index1, pos1, n1, uv1, b0_1, b1_1, weight1)
288 index2=self.__getIndex(obj, base_index2, pos2, n2, uv2, b0_2, b1_2, weight2)
290 if not material in self.indexArrays:
291 self.indexArrays[material]=[]
292 self.indexArrays[material]+=[index0, index1, index2]
296 __slots__=['name', 'type', 'offsets']
297 def __init__(self, name, type):
302 def add(self, index, offset):
303 self.offsets.append((index, offset))
307 self.offsets.sort(lambda l, r: l[0]-r[0])
309 self.offsets.sort(key=lambda e: e[0])
312 return "<Morph %s>" % self.name
314 class IKSolver(object):
315 __slots__=['target', 'effector', 'length', 'iterations', 'weight']
316 def __init__(self, target, effector, length, iterations, weight):
318 self.effector=effector
320 self.iterations=iterations
324 class OneSkinMesh(object):
325 __slots__=['scene', 'vertexArray', 'morphList',
329 def __init__(self, scene):
330 self.vertexArray=VertexArray()
337 return "<OneSkinMesh %s, morph:%d>" % (
341 def addMesh(self, obj):
342 if bl.object.isVisible(obj):
346 self.__rigidbody(obj)
347 self.__constraint(obj)
349 def __getWeightMap(self, obj, mesh):
353 def setWeight(i, name, w):
356 if i in secondWeightMap:
358 if w<secondWeightMap[i][1]:
360 elif w<weightMap[i][1]:
362 secondWeightMap[i]=(name, w)
365 weightMap[i]=(name, w)
367 if w>weightMap[i][1]:
369 secondWeightMap[i]=weightMap[i]
370 weightMap[i]=(name, w)
372 secondWeightMap[i]=(name, w)
374 weightMap[i]=(name, w)
377 for name in bl.object.getVertexGroupNames(obj):
378 for i, w in mesh.getVertsFromGroup(name, 1):
379 setWeight(i, name, w)
381 for i, v in enumerate(mesh.verts):
384 setWeight(i, obj.vertex_groups[g.group].name, g.weight)
386 setWeight(i, obj.vertex_groups[0].name, 1)
389 for i in xrange(len(mesh.verts)):
390 #for i, name_weight in weightMap.items():
391 if i in secondWeightMap:
392 secondWeightMap[i]=(secondWeightMap[i][0], 1.0-weightMap[i][1])
394 weightMap[i]=(weightMap[i][0], 1.0)
395 secondWeightMap[i]=("", 0)
397 print("no weight vertex")
399 secondWeightMap[i]=("", 0)
401 return weightMap, secondWeightMap
403 def __processFaces(self, obj_name, mesh, weightMap, secondWeightMap):
405 for i, face in enumerate(mesh.faces):
406 faceVertexCount=bl.face.getVertexCount(face)
407 material=mesh.materials[bl.face.getMaterialIndex(face)]
408 v=[mesh.verts[index] for index in bl.face.getVertices(face)]
409 uv=bl.mesh.getFaceUV(
410 mesh, i, face, bl.face.getVertexCount(face))
412 if faceVertexCount==3:
414 self.vertexArray.addTriangle(
415 obj_name, material.name,
423 #v0.no, v1.no, v2.no,
424 bl.face.getNormal(face),
425 bl.face.getNormal(face),
426 bl.face.getNormal(face),
430 weightMap[v[2].index][0],
431 weightMap[v[1].index][0],
432 weightMap[v[0].index][0],
433 secondWeightMap[v[2].index][0],
434 secondWeightMap[v[1].index][0],
435 secondWeightMap[v[0].index][0],
436 weightMap[v[2].index][1],
437 weightMap[v[1].index][1],
438 weightMap[v[0].index][1]
440 elif faceVertexCount==4:
442 self.vertexArray.addTriangle(
443 obj_name, material.name,
450 #v0.no, v1.no, v2.no,
451 bl.face.getNormal(face),
452 bl.face.getNormal(face),
453 bl.face.getNormal(face),
457 weightMap[v[2].index][0],
458 weightMap[v[1].index][0],
459 weightMap[v[0].index][0],
460 secondWeightMap[v[2].index][0],
461 secondWeightMap[v[1].index][0],
462 secondWeightMap[v[0].index][0],
463 weightMap[v[2].index][1],
464 weightMap[v[1].index][1],
465 weightMap[v[0].index][1]
467 self.vertexArray.addTriangle(
468 obj_name, material.name,
475 #v2.no, v3.no, v0.no,
476 bl.face.getNormal(face),
477 bl.face.getNormal(face),
478 bl.face.getNormal(face),
482 weightMap[v[0].index][0],
483 weightMap[v[3].index][0],
484 weightMap[v[2].index][0],
485 secondWeightMap[v[0].index][0],
486 secondWeightMap[v[3].index][0],
487 secondWeightMap[v[2].index][0],
488 weightMap[v[0].index][1],
489 weightMap[v[3].index][1],
490 weightMap[v[2].index][1]
493 def __mesh(self, obj):
497 if RIGID_SHAPE_TYPE in obj:
499 if CONSTRAINT_A in obj:
502 #if not bl.modifier.hasType(obj, 'ARMATURE'):
505 bl.message("export: %s" % obj.name)
507 # メッシュのコピーを生成してオブジェクトの行列を適用する
508 copyMesh, copyObj=bl.object.duplicate(obj)
509 if len(copyMesh.verts)==0:
512 copyObj.scale=obj.scale
513 bpy.ops.object.scale_apply()
514 copyObj.rotation_euler=obj.rotation_euler
515 bpy.ops.object.rotation_apply()
516 copyObj.location=obj.location
517 bpy.ops.object.location_apply()
519 for m in [m for m in copyObj.modifiers]:
520 if m.type=='SOLIDFY':
522 elif m.type=='ARMATURE':
524 elif m.type=='MIRROR':
525 bpy.ops.object.modifier_apply(modifier=m.name)
529 weightMap, secondWeightMap=self.__getWeightMap(copyObj, copyMesh)
530 self.__processFaces(obj.name, copyMesh, weightMap, secondWeightMap)
532 bl.object.delete(copyObj)
534 def createEmptyBasicSkin(self):
535 self.__getOrCreateMorph('base', 0)
537 def __skin(self, obj):
538 if not bl.object.hasShapeKey(obj):
542 blenderMesh=bl.object.getData(obj)
546 vg=bl.object.getVertexGroup(obj, MMD_SHAPE_GROUP_NAME)
550 for b in bl.object.getShapeKeys(obj):
551 if b.name==BASE_SHAPE_NAME:
552 baseMorph=self.__getOrCreateMorph('base', 0)
557 v=bl.shapekey.getByIndex(b, index)
558 pos=[v[0], v[1], v[2]]
559 i=self.vertexArray.getMappedIndex(obj.name, index)
560 baseMorph.add(i, pos)
561 indexRelativeMap[i]=relativeIndex
563 #indices=self.vertexArray.getMappedIndices(index)
569 # baseMorph.add(i, pos)
570 # indexRelativeMap[i]=relativeIndex
575 print(basis.name, len(baseMorph.offsets))
577 if len(baseMorph.offsets)==0:
581 for b in bl.object.getShapeKeys(obj):
582 if b.name==BASE_SHAPE_NAME:
586 morph=self.__getOrCreateMorph(b.name, 4)
588 for index, src, dst in zip(
589 xrange(len(blenderMesh.verts)),
590 bl.shapekey.get(basis),
592 offset=[dst[0]-src[0], dst[1]-src[1], dst[2]-src[2]]
593 if offset[0]==0 and offset[1]==0 and offset[2]==0:
596 i=self.vertexArray.getMappedIndex(obj.name, index)
600 morph.add(indexRelativeMap[i], offset)
601 #indices=self.vertexArray.getMappedIndices(index)
606 # morph.add(indexRelativeMap[i], offset)
607 assert(len(morph.offsets)<len(baseMorph.offsets))
610 original=self.morphList[:]
612 for i, v in enumerate(englishmap.skinMap):
616 return len(englishmap.skinMap)
618 self.morphList.sort(lambda l, r: getIndex(l)-getIndex(r))
620 self.morphList.sort(key=getIndex)
622 def __rigidbody(self, obj):
625 if not RIGID_SHAPE_TYPE in obj:
627 self.rigidbodies.append(obj)
629 def __constraint(self, obj):
632 if not CONSTRAINT_A in obj:
634 self.constraints.append(obj)
636 def __getOrCreateMorph(self, name, type):
637 for m in self.morphList:
641 self.morphList.append(m)
644 def getVertexCount(self):
645 return len(self.vertexArray.vertices)
649 __slots__=['index', 'name', 'ik_index',
650 'pos', 'tail', 'parent_index', 'tail_index', 'type', 'isConnect']
651 def __init__(self, name, pos, tail):
656 self.parent_index=None
662 def __eq__(self, rhs):
663 return self.index==rhs.index
666 return "<Bone %s %d>" % (self.name, self.type)
668 class BoneBuilder(object):
669 __slots__=['bones', 'boneMap', 'ik_list', 'bone_groups',]
676 def getBoneGroup(self, bone):
677 for i, g in enumerate(self.bone_groups):
681 print('no gorup', bone)
684 def build(self, armatureObj):
688 bl.message("build skeleton")
689 armature=bl.object.getData(armatureObj)
694 for g in bl.object.boneGroups(armatureObj):
695 self.bone_groups.append((g.name, []))
700 for b in armature.bones.values():
704 bl.bone.getHeadLocal(b),
705 bl.bone.getTailLocal(b))
707 self.__getBone(bone, b)
709 for b in armature.bones.values():
710 if not b.parent and b.name!='center':
713 bl.bone.getHeadLocal(b),
714 bl.bone.getTailLocal(b))
716 self.__getBone(bone, b)
718 for b in armature.bones.values():
720 self.__checkConnection(b, None)
725 pose = bl.object.getPose(armatureObj)
726 for b in pose.bones.values():
730 self.__assignBoneGroup(b, b.bone_group)
731 for c in b.constraints:
732 if bl.constraint.isIKSolver(c):
736 target=self.__boneByName(bl.constraint.ikTarget(c))
743 link=self.__boneByName(b.name)
748 chainLength=bl.constraint.ikChainLen(c)
749 for i in range(chainLength):
751 chainBone=self.__boneByName(e.name)
753 chainBone.ik_index=target.index
756 IKSolver(target, link, chainLength,
757 int(bl.constraint.ikItration(c) * 0.1),
758 bl.constraint.ikRotationWeight(c)
768 for i, v in enumerate(englishmap.boneMap):
769 if v[0]==ik.target.name:
771 return len(englishmap.boneMap)
773 self.ik_list.sort(lambda l, r: getIndex(l)-getIndex(r))
775 self.ik_list.sort(key=getIndex)
777 def __assignBoneGroup(self, poseBone, boneGroup):
779 for g in self.bone_groups:
780 if g[0]==boneGroup.name:
781 g[1].append(poseBone.name)
783 def __checkConnection(self, b, p):
784 if bl.bone.isConnected(b):
785 parent=self.__boneByName(p.name)
786 parent.isConnect=True
789 self.__checkConnection(c, b)
795 boneMap=englishmap.boneMap
796 original=self.bones[:]
798 for i, k_v in enumerate(boneMap):
799 if k_v[0]==bone.name:
805 self.bones.sort(lambda l, r: getIndex(l)-getIndex(r))
807 self.bones.sort(key=getIndex)
810 for i, b in enumerate(self.bones):
811 src=original.index(b)
814 b.index=sortMap[b.index]
816 b.parent_index=sortMap[b.parent_index]
818 b.tail_index=sortMap[b.tail_index]
820 b.ik_index=sortMap[b.ik_index]
828 if b.parent_index==None:
829 b.parent_index=0xFFFF
831 if b.type==6 or b.type==7:
833 parent=self.bones[b.parent_index]
834 parent.tail_index=b.index
837 if b.tail_index==None:
842 def getIndex(self, bone):
843 for i, b in enumerate(self.bones):
848 def indexByName(self, name):
852 return self.getIndex(self.__boneByName(name))
854 def __boneByName(self, name):
855 return self.boneMap[name]
857 def __getBone(self, parent, b):
858 if len(b.children)==0:
862 for i, c in enumerate(b.children):
864 bl.bone.getHeadLocal(c),
865 bl.bone.getTailLocal(c))
868 bone.parent_index=parent.index
870 parent.tail_index=bone.index
871 self.__getBone(bone, c)
873 def __addBone(self, bone):
874 bone.index=len(self.bones)
875 self.bones.append(bone)
876 self.boneMap[bone.name]=bone
879 class PmdExporter(object):
881 def setup(self, scene):
882 self.armatureObj=None
887 for o in scene.objects:
888 object_node_map[o]=Node(o)
889 for o in scene.objects:
890 #for node in object_node_map.values():
891 node=object_node_map[o]
893 object_node_map[node.o.parent].children.append(node)
896 root=object_node_map[scene.objects.active]
899 self.oneSkinMesh=OneSkinMesh(scene)
900 self.__createOneSkinMesh(root)
901 bl.message(self.oneSkinMesh)
902 if len(self.oneSkinMesh.morphList)==0:
904 self.oneSkinMesh.createEmptyBasicSkin()
906 self.name=root.o.name
909 self.skeleton=BoneBuilder()
910 self.skeleton.build(self.armatureObj)
912 def __createOneSkinMesh(self, node):
913 ############################################################
914 # search armature modifier
915 ############################################################
916 for m in node.o.modifiers:
917 if bl.modifier.isType(m, 'ARMATURE'):
918 armatureObj=bl.modifier.getArmatureObject(m)
919 if not self.armatureObj:
920 self.armatureObj=armatureObj
921 elif self.armatureObj!=armatureObj:
922 print("warning! found multiple armature. ignored.",
925 if node.o.type.upper()=='MESH':
926 self.oneSkinMesh.addMesh(node.o)
928 for child in node.children:
929 self.__createOneSkinMesh(child)
931 def write(self, path, scene):
934 io.comment="blender export"
938 for pos, normal, uv, b0, b1, weight in self.oneSkinMesh.vertexArray.zip():
939 # convert right-handed z-up to left-handed y-up
948 v.uv.y=1.0-uv[1] # reverse vertical
949 v.bone0=self.skeleton.indexByName(b0)
950 v.bone1=self.skeleton.indexByName(b1)
951 v.weight0=int(100*weight)
952 v.edge_flag=0 # edge flag, 0: enable edge, 1: not edge
955 vertexCount=self.oneSkinMesh.getVertexCount()
956 for material_name, indices in self.oneSkinMesh.vertexArray.each():
957 #print('material:', material_name)
958 m=bl.material.get(material_name)
960 material=io.addMaterial()
961 setMaterialParams(material, m)
963 material.vertex_count=len(indices)
964 material.toon_index=0
965 textures=[os.path.basename(path)
966 for path in bl.material.eachTexturePath(m)]
968 material.setTexture(toCP932('*'.join(textures)))
970 material.setTexture(toCP932(""))
973 assert(i<vertexCount)
974 for i in xrange(0, len(indices), 3):
976 io.indices.append(indices[i])
977 io.indices.append(indices[i+1])
978 io.indices.append(indices[i+2])
982 for i, b in enumerate(self.skeleton.bones):
986 boneNameMap[b.name]=i
987 v=englishmap.getUnicodeBoneName(b.name)
991 cp932=v[1].encode('cp932')
992 assert(len(cp932)<20)
996 bone_english_name=toCP932(b.name)
997 assert(len(bone_english_name)<20)
998 bone.setEnglishName(bone_english_name)
1003 b.ik_index=self.skeleton.indexByName('eyes')
1008 bone.parent_index=b.parent_index
1009 bone.tail_index=b.tail_index
1010 bone.ik_index=b.ik_index
1012 # convert right-handed z-up to left-handed y-up
1013 bone.pos.x=b.pos[0] if not near(b.pos[0], 0) else 0
1014 bone.pos.y=b.pos[2] if not near(b.pos[2], 0) else 0
1015 bone.pos.z=b.pos[1] if not near(b.pos[1], 0) else 0
1018 for ik in self.skeleton.ik_list:
1020 solver.index=self.skeleton.getIndex(ik.target)
1021 solver.target=self.skeleton.getIndex(ik.effector)
1022 solver.length=ik.length
1023 b=self.skeleton.bones[ik.effector.parent_index]
1024 for i in xrange(solver.length):
1025 solver.children.append(self.skeleton.getIndex(b))
1026 b=self.skeleton.bones[b.parent_index]
1027 solver.iterations=ik.iterations
1028 solver.weight=ik.weight
1031 for i, m in enumerate(self.oneSkinMesh.morphList):
1035 v=englishmap.getUnicodeSkinName(m.name)
1037 v=[m.name, m.name, 0]
1039 cp932=v[1].encode('cp932')
1040 morph.setName(cp932)
1041 morph.setEnglishName(m.name.encode('cp932'))
1044 for index, offset in m.offsets:
1045 # convert right-handed z-up to left-handed y-up
1046 morph.append(index, offset[0], offset[2], offset[1])
1047 morph.vertex_count=len(m.offsets)
1051 for i, m in enumerate(self.oneSkinMesh.morphList):
1053 io.face_list.append(i)
1054 for i, m in enumerate(self.oneSkinMesh.morphList):
1056 io.face_list.append(i)
1057 for i, m in enumerate(self.oneSkinMesh.morphList):
1059 io.face_list.append(i)
1060 for i, m in enumerate(self.oneSkinMesh.morphList):
1062 io.face_list.append(i)
1065 for g in self.skeleton.bone_groups:
1066 boneDisplayName=io.addBoneGroup()
1068 name=englishmap.getUnicodeBoneGroupName(g[0])
1071 boneDisplayName.setName(toCP932(name+'\n'))
1074 boneDisplayName.setEnglishName(toCP932(englishName+'\n'))
1077 for i, b in enumerate(self.skeleton.bones):
1080 if b.type in [6, 7]:
1082 io.addBoneDisplay(i, self.skeleton.getBoneGroup(b))
1084 #assert(len(io.bones)==len(io.bone_display_list)+1)
1087 io.setEnglishName(toCP932("blender export\n"))
1088 io.setEnglishComment(toCP932("blender export comment"))
1092 for o in scene.objects:
1093 if o.name.startswith(TOON_TEXTURE_OBJECT):
1097 toonMesh=bl.object.getData(toonMeshObject)
1098 toonMaterial=bl.mesh.getMaterial(toonMesh, 0)
1100 t=bl.material.getTexture(toonMaterial, i)
1102 io.getToonTexture(i).setName(toCP932(t.name))
1104 io.getToonTexture(i).setName(toCP932("toon%02d.bmp\n" % i))
1107 io.getToonTexture(i).setName(toCP932("toon%02d.bmp\n" % i))
1111 for i, obj in enumerate(self.oneSkinMesh.rigidbodies):
1112 rigidBody=io.addRigidBody()
1113 rigidBody.setName(obj.name.encode('cp932'))
1114 rigidNameMap[obj.name]=i
1115 boneIndex=boneNameMap[obj[RIGID_BONE_NAME]]
1118 bone=self.skeleton.bones[0]
1120 bone=self.skeleton.bones[boneIndex]
1121 rigidBody.boneIndex=boneIndex
1122 #rigidBody.position.x=obj[RIGID_LOCATION][0]
1123 #rigidBody.position.y=obj[RIGID_LOCATION][1]
1124 #rigidBody.position.z=obj[RIGID_LOCATION][2]
1125 rigidBody.position.x=obj.location.x-bone.pos[0]
1126 rigidBody.position.y=obj.location.z-bone.pos[2]
1127 rigidBody.position.z=obj.location.y-bone.pos[1]
1128 rigidBody.rotation.x=-obj.rotation_euler[0]
1129 rigidBody.rotation.y=-obj.rotation_euler[2]
1130 rigidBody.rotation.z=-obj.rotation_euler[1]
1131 rigidBody.processType=obj[RIGID_PROCESS_TYPE]
1132 rigidBody.group=obj[RIGID_GROUP]
1133 rigidBody.target=obj[RIGID_INTERSECTION_GROUP]
1134 rigidBody.weight=obj[RIGID_WEIGHT]
1135 rigidBody.linearDamping=obj[RIGID_LINEAR_DAMPING]
1136 rigidBody.angularDamping=obj[RIGID_ANGULAR_DAMPING]
1137 rigidBody.restitution=obj[RIGID_RESTITUTION]
1138 rigidBody.friction=obj[RIGID_FRICTION]
1139 if obj[RIGID_SHAPE_TYPE]==0:
1140 rigidBody.shapeType=pmd.SHAPE_SPHERE
1141 rigidBody.w=obj.scale[0]
1142 elif obj[RIGID_SHAPE_TYPE]==1:
1143 rigidBody.shapeType=pmd.SHAPE_BOX
1144 rigidBody.w=obj.scale[0]
1145 rigidBody.d=obj.scale[1]
1146 rigidBody.h=obj.scale[2]
1147 elif obj[RIGID_SHAPE_TYPE]==2:
1148 rigidBody.shapeType=pmd.SHAPE_CAPSULE
1149 rigidBody.w=obj.scale[0]
1150 rigidBody.h=obj.scale[2]
1153 for obj in self.oneSkinMesh.constraints:
1154 constraint=io.addConstraint()
1155 constraint.setName(obj[CONSTRAINT_NAME].encode('cp932'))
1156 constraint.rigidA=rigidNameMap[obj[CONSTRAINT_A]]
1157 constraint.rigidB=rigidNameMap[obj[CONSTRAINT_B]]
1158 constraint.pos.x=obj.location[0]
1159 constraint.pos.y=obj.location[2]
1160 constraint.pos.z=obj.location[1]
1161 constraint.rot.x=-obj.rotation_euler[0]
1162 constraint.rot.y=-obj.rotation_euler[2]
1163 constraint.rot.z=-obj.rotation_euler[1]
1164 constraint.constraintPosMin.x=obj[CONSTRAINT_POS_MIN][0]
1165 constraint.constraintPosMin.y=obj[CONSTRAINT_POS_MIN][1]
1166 constraint.constraintPosMin.z=obj[CONSTRAINT_POS_MIN][2]
1167 constraint.constraintPosMax.x=obj[CONSTRAINT_POS_MAX][0]
1168 constraint.constraintPosMax.y=obj[CONSTRAINT_POS_MAX][1]
1169 constraint.constraintPosMax.z=obj[CONSTRAINT_POS_MAX][2]
1170 constraint.constraintRotMin.x=obj[CONSTRAINT_ROT_MIN][0]
1171 constraint.constraintRotMin.y=obj[CONSTRAINT_ROT_MIN][1]
1172 constraint.constraintRotMin.z=obj[CONSTRAINT_ROT_MIN][2]
1173 constraint.constraintRotMax.x=obj[CONSTRAINT_ROT_MAX][0]
1174 constraint.constraintRotMax.y=obj[CONSTRAINT_ROT_MAX][1]
1175 constraint.constraintRotMax.z=obj[CONSTRAINT_ROT_MAX][2]
1176 constraint.springPos.x=obj[CONSTRAINT_SPRING_POS][0]
1177 constraint.springPos.y=obj[CONSTRAINT_SPRING_POS][1]
1178 constraint.springPos.z=obj[CONSTRAINT_SPRING_POS][2]
1179 constraint.springRot.x=obj[CONSTRAINT_SPRING_ROT][0]
1180 constraint.springRot.y=obj[CONSTRAINT_SPRING_ROT][1]
1181 constraint.springRot.z=obj[CONSTRAINT_SPRING_ROT][2]
1184 bl.message('write %s' % path)
1185 return io.write(path)
1188 def __execute(filename, scene):
1189 if not scene.objects.active:
1190 print("abort. no active object.")
1193 active=bl.object.getActive()
1194 exporter=PmdExporter()
1195 exporter.setup(scene)
1196 exporter.write(filename, scene)
1197 bl.object.activate(active)
1202 def execute_24(filename):
1203 scene = bpy.data.scenes.active
1204 bl.initialize('pmd_export', scene)
1205 __execute(filename.decode(bl.INTERNAL_ENCODING), scene)
1208 Blender.Window.FileSelector(
1210 'Export Metasequoia PMD',
1211 Blender.sys.makename(ext='.pmd'))
1215 def execute_25(filename, scene):
1216 bl.initialize('pmd_export', scene)
1217 __execute(filename, scene)
1221 class EXPORT_OT_pmd(bpy.types.Operator):
1222 '''Save a Metasequoia PMD file.'''
1223 bl_idname = "export_scene.pmd"
1224 bl_label = 'Export PMD'
1226 # List of operator properties, the attributes will be assigned
1227 # to the class instance from the operator settings before calling.
1229 path = StringProperty(
1231 description="File path used for exporting the PMD file",
1235 filename = StringProperty(
1237 description="Name of the file.")
1238 directory = StringProperty(
1240 description="Directory of the file.")
1242 check_existing = BoolProperty(
1243 name="Check Existing",
1244 description="Check and warn on overwriting existing files",
1246 options=set('HIDDEN'))
1248 def execute(self, context):
1250 self.properties.path,
1255 def invoke(self, context, event):
1257 wm.add_fileselect(self)
1258 return 'RUNNING_MODAL'
1261 def menu_func(self, context):
1262 #default_path=bpy.data.filename.replace(".blend", ".pmd")
1263 self.layout.operator(
1264 EXPORT_OT_pmd.bl_idname,
1265 text="Miku Miku Dance Model(.pmd)")#.path=default_path
1268 bpy.types.register(EXPORT_OT_pmd)
1269 bpy.types.INFO_MT_file_export.append(menu_func)
1272 bpy.types.unregister(EXPORT_OT_pmd)
1273 bpy.types.INFO_MT_file_export.remove(menu_func)
1275 if __name__ == "__main__":