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'
51 ###############################################################################
53 ###############################################################################
58 from meshio import pmd, englishmap
61 return sys.version_info[0]<3
66 from Blender import Mathutils
72 def setMaterialParams(material, m):
74 material.diffuse.r=m.R
75 material.diffuse.g=m.G
76 material.diffuse.b=m.B
77 material.diffuse.a=m.alpha
79 material.sinness=0 if m.spec<1e-5 else m.spec*10
80 material.specular.r=m.specR
81 material.specular.g=m.specG
82 material.specular.b=m.specB
84 material.ambient.r=m.mirR
85 material.ambient.g=m.mirG
86 material.ambient.b=m.mirB
88 material.flag=1 if m.enableSSS else 0
97 from bpy.props import *
105 def setMaterialParams(material, m):
107 material.diffuse.r=m.diffuse_color[0]
108 material.diffuse.g=m.diffuse_color[1]
109 material.diffuse.b=m.diffuse_color[2]
110 material.diffuse.a=m.alpha
112 material.sinness=0 if m.specular_toon_size<1e-5 else m.specular_hardness*10
113 material.specular.r=m.specular_color[0]
114 material.specular.g=m.specular_color[1]
115 material.specular.b=m.specular_color[2]
117 material.ambient.r=m.mirror_color[0]
118 material.ambient.g=m.mirror_color[1]
119 material.ambient.b=m.mirror_color[2]
121 material.flag=1 if m.subsurface_scattering.enabled else 0
123 material.toon_index=7
126 return s.encode('cp932')
130 __slots__=['o', 'children']
131 def __init__(self, o):
136 ###############################################################################
137 # Blenderのメッシュをワンスキンメッシュ化する
138 ###############################################################################
139 def near(x, y, EPSILON=1e-5):
141 return d>=-EPSILON and d<=EPSILON
144 class VertexKey(object):
151 'nx', 'ny', 'nz', # 法線
155 def __init__(self, obj, index, x, y, z, nx, ny, nz, u, v):
168 return "<vkey: %f, %f, %f, %f, %f, %f, %f, %f>" % (
169 self.x, self.y, self.z, self.nx, self.ny, self.nz, self.u, self.v)
172 #return int((self.x+self.y+self.z+self.nx+self.ny+self.nz+self.u+self.v)*100)
173 return int((self.x+self.y+self.z)*100)
175 def __eq__(self, rhs):
176 #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)
177 #return near(self.x, rhs.x) and near(self.y, rhs.y) and near(self.z, rhs.z)
178 #return self.x==rhs.x and self.y==rhs.y and self.z==rhs.z
179 return self.obj==rhs.obj and self.index==rhs.index
182 class VertexArray(object):
187 # マテリアル毎に分割したインデックス配列
202 return "<VertexArray %d vertices, %d indexArrays>" % (
203 len(self.vertices), len(self.indexArrays))
207 self.vertices, self.normals, self.uvs,
208 self.b0, self.b1, self.weight)
211 keys=[key for key in self.indexArrays.keys()]
214 yield(key, self.indexArrays[key])
216 def __getIndex(self, obj, base_index, pos, normal, uv, b0, b1, weight0):
222 pos[0], pos[1], pos[2],
223 normal[0], normal[1], normal[2],
225 if key in self.vertexMap:
227 index=self.vertexMap[key]
229 index=len(self.vertices)
231 self.vertexMap[key]=index
233 self.vertices.append((pos.x, pos.y, pos.z))
234 self.normals.append((normal.x, normal.y, normal.z))
235 self.uvs.append((uv[0], uv[1]))
238 self.weight.append(weight0)
241 if not base_index in self.indexMap:
242 self.indexMap[base_index]=set()
243 self.indexMap[base_index].add(index)
248 def getMappedIndices(self, base_index):
249 return self.indexMap[base_index]
251 def addTriangle(self,
253 base_index0, base_index1, base_index2,
259 weight0, weight1, weight2
261 index0=self.__getIndex(obj, base_index0, pos0, n0, uv0, b0_0, b1_0, weight0)
262 index1=self.__getIndex(obj, base_index1, pos1, n1, uv1, b0_1, b1_1, weight1)
263 index2=self.__getIndex(obj, base_index2, pos2, n2, uv2, b0_2, b1_2, weight2)
265 if not material in self.indexArrays:
266 self.indexArrays[material]=[]
267 self.indexArrays[material]+=[index0, index1, index2]
271 __slots__=['name', 'type', 'offsets']
272 def __init__(self, name, type):
277 def add(self, index, offset):
278 self.offsets.append((index, offset))
282 self.offsets.sort(lambda l, r: l[0]-r[0])
284 self.offsets.sort(key=lambda e: e[0])
287 return "<Morph %s>" % self.name
289 class IKSolver(object):
290 __slots__=['target', 'effector', 'length', 'iterations', 'weight']
291 def __init__(self, target, effector, length, iterations, weight):
293 self.effector=effector
295 self.iterations=iterations
299 class OneSkinMesh(object):
300 __slots__=['obj_index', 'scene', 'vertexArray', 'morphList',
304 def __init__(self, scene):
305 self.vertexArray=VertexArray()
313 return "<OneSkinMesh %s, morph:%d>" % (
317 def addMesh(self, obj):
318 if bl.object.isVisible(obj):
320 if not bl.modifier.hasType(obj, 'ARMATURE'):
324 self.__rigidbody(obj)
325 self.__constraint(obj)
327 def __getWeightMap(self, obj, mesh):
331 def setWeight(i, name, w):
334 if i in secondWeightMap:
336 if w<secondWeightMap[i][1]:
338 elif w<weightMap[i][1]:
340 secondWeightMap[i]=(name, w)
343 weightMap[i]=(name, w)
345 if w>weightMap[i][1]:
347 secondWeightMap[i]=weightMap[i]
348 weightMap[i]=(name, w)
350 secondWeightMap[i]=(name, w)
352 weightMap[i]=(name, w)
355 for name in bl.object.getVertexGroupNames(obj):
356 for i, w in mesh.getVertsFromGroup(name, 1):
357 setWeight(i, name, w)
359 for i, v in enumerate(mesh.verts):
362 setWeight(i, obj.vertex_groups[g.group].name, g.weight)
364 setWeight(i, obj.vertex_groups[0].name, 1)
367 for i in xrange(len(mesh.verts)):
368 #for i, name_weight in weightMap.items():
369 if i in secondWeightMap:
370 secondWeightMap[i]=(secondWeightMap[i][0], 1.0-weightMap[i][1])
372 weightMap[i]=(weightMap[i][0], 1.0)
373 secondWeightMap[i]=("", 0)
375 print("no weight vertex")
377 secondWeightMap[i]=("", 0)
379 return weightMap, secondWeightMap
381 def __processFaces(self, mesh, weightMap, secondWeightMap):
383 for i, face in enumerate(mesh.faces):
384 faceVertexCount=bl.face.getVertexCount(face)
385 material=mesh.materials[bl.face.getMaterialIndex(face)]
386 v=[mesh.verts[index] for index in bl.face.getVertices(face)]
387 uv=bl.mesh.getFaceUV(
388 mesh, i, face, bl.face.getVertexCount(face))
390 if faceVertexCount==3:
392 self.vertexArray.addTriangle(
393 self.obj_index, material.name,
401 #v0.no, v1.no, v2.no,
402 bl.face.getNormal(face),
403 bl.face.getNormal(face),
404 bl.face.getNormal(face),
408 weightMap[v[2].index][0],
409 weightMap[v[1].index][0],
410 weightMap[v[0].index][0],
411 secondWeightMap[v[2].index][0],
412 secondWeightMap[v[1].index][0],
413 secondWeightMap[v[0].index][0],
414 weightMap[v[2].index][1],
415 weightMap[v[1].index][1],
416 weightMap[v[0].index][1]
418 elif faceVertexCount==4:
420 self.vertexArray.addTriangle(
421 self.obj_index, material.name,
428 #v0.no, v1.no, v2.no,
429 bl.face.getNormal(face),
430 bl.face.getNormal(face),
431 bl.face.getNormal(face),
435 weightMap[v[2].index][0],
436 weightMap[v[1].index][0],
437 weightMap[v[0].index][0],
438 secondWeightMap[v[2].index][0],
439 secondWeightMap[v[1].index][0],
440 secondWeightMap[v[0].index][0],
441 weightMap[v[2].index][1],
442 weightMap[v[1].index][1],
443 weightMap[v[0].index][1]
445 self.vertexArray.addTriangle(
446 self.obj_index, material.name,
453 #v2.no, v3.no, v0.no,
454 bl.face.getNormal(face),
455 bl.face.getNormal(face),
456 bl.face.getNormal(face),
460 weightMap[v[0].index][0],
461 weightMap[v[3].index][0],
462 weightMap[v[2].index][0],
463 secondWeightMap[v[0].index][0],
464 secondWeightMap[v[3].index][0],
465 secondWeightMap[v[2].index][0],
466 weightMap[v[0].index][1],
467 weightMap[v[3].index][1],
468 weightMap[v[2].index][1]
471 def __mesh(self, obj):
475 if RIGID_SHAPE_TYPE in obj:
477 if CONSTRAINT_A in obj:
480 print("export", obj.name)
482 # メッシュのコピーを生成してオブジェクトの行列を適用する
483 copyMesh, copyObj=bl.object.duplicate(obj)
484 if len(copyMesh.verts)==0:
487 copyObj.scale=obj.scale
488 bpy.ops.object.scale_apply()
489 copyObj.rotation_euler=obj.rotation_euler
490 bpy.ops.object.rotation_apply()
491 copyObj.location=obj.location
492 bpy.ops.object.location_apply()
494 for m in [m for m in copyObj.modifiers]:
495 if m.type=='SOLIDFY':
497 bpy.ops.object.modifier_apply(modifier=m.name)
499 weightMap, secondWeightMap=self.__getWeightMap(copyObj, copyMesh)
500 self.__processFaces(copyMesh, weightMap, secondWeightMap)
502 bl.object.delete(copyObj)
505 def createEmptyBasicSkin(self):
506 self.__getOrCreateMorph('base', 0)
508 def __skin(self, obj):
509 if not bl.object.hasShapeKey(obj):
513 blenderMesh=bl.object.getData(obj)
517 vg=bl.object.getVertexGroup(obj, MMD_SHAPE_GROUP_NAME)
521 for b in bl.object.getShapeKeys(obj):
522 if b.name==BASE_SHAPE_NAME:
523 baseMorph=self.__getOrCreateMorph('base', 0)
528 v=bl.shapekey.getByIndex(b, index)
529 pos=[v[0], v[1], v[2]]
530 indices=self.vertexArray.getMappedIndices(index)
536 baseMorph.add(i, pos)
537 indexRelativeMap[i]=relativeIndex
542 print(basis.name, len(baseMorph.offsets))
544 if len(baseMorph.offsets)==0:
548 for b in bl.object.getShapeKeys(obj):
549 if b.name==BASE_SHAPE_NAME:
553 morph=self.__getOrCreateMorph(b.name, 4)
555 for index, src, dst in zip(
556 xrange(len(blenderMesh.verts)),
557 bl.shapekey.get(basis),
559 offset=[dst[0]-src[0], dst[1]-src[1], dst[2]-src[2]]
560 if offset[0]==0 and offset[1]==0 and offset[2]==0:
563 indices=self.vertexArray.getMappedIndices(index)
568 morph.add(indexRelativeMap[i], offset)
571 original=self.morphList[:]
573 for i, v in enumerate(englishmap.skinMap):
577 return len(englishmap.skinMap)
579 self.morphList.sort(lambda l, r: getIndex(l)-getIndex(r))
581 self.morphList.sort(key=getIndex)
583 def __rigidbody(self, obj):
586 if not RIGID_SHAPE_TYPE in obj:
588 self.rigidbodies.append(obj)
590 def __constraint(self, obj):
593 if not CONSTRAINT_A in obj:
595 self.constraints.append(obj)
597 def __getOrCreateMorph(self, name, type):
598 for m in self.morphList:
602 self.morphList.append(m)
605 def getVertexCount(self):
606 return len(self.vertexArray.vertices)
610 __slots__=['index', 'name', 'ik_index',
611 'pos', 'tail', 'parent_index', 'tail_index', 'type', 'isConnect']
612 def __init__(self, name, pos, tail):
617 self.parent_index=None
623 def __eq__(self, rhs):
624 return self.index==rhs.index
627 return "<Bone %s %d>" % (self.name, self.type)
629 class BoneBuilder(object):
630 __slots__=['bones', 'boneMap', 'ik_list', ]
636 def build(self, armatureObj):
640 print("gather bones")
641 armature=bl.object.getData(armatureObj)
642 for b in armature.bones.values():
646 bl.bone.getHeadLocal(b),
647 bl.bone.getTailLocal(b))
649 self.__getBone(bone, b)
651 for b in armature.bones.values():
652 if not b.parent and b.name!='center':
655 bl.bone.getHeadLocal(b),
656 bl.bone.getTailLocal(b))
658 self.__getBone(bone, b)
660 print("check connection")
661 for b in armature.bones.values():
663 self.__checkConnection(b, None)
666 pose = bl.object.getPose(armatureObj)
667 for b in pose.bones.values():
668 for c in b.constraints:
669 if bl.constraint.isIKSolver(c):
673 target=self.__boneByName(bl.constraint.ikTarget(c))
680 link=self.__boneByName(b.name)
685 chainLength=bl.constraint.ikChainLen(c)
686 for i in range(chainLength):
688 chainBone=self.__boneByName(e.name)
690 chainBone.ik_index=target.index
693 IKSolver(target, link, chainLength,
694 int(bl.constraint.ikItration(c) * 0.1),
695 bl.constraint.ikRotationWeight(c)
703 for i, v in enumerate(englishmap.boneMap):
704 if v[0]==ik.target.name:
706 return len(englishmap.boneMap)
708 self.ik_list.sort(lambda l, r: getIndex(l)-getIndex(r))
710 self.ik_list.sort(key=getIndex)
712 def __checkConnection(self, b, p):
713 if bl.bone.isConnected(b):
714 parent=self.__boneByName(p.name)
715 parent.isConnect=True
718 self.__checkConnection(c, b)
724 boneMap=englishmap.boneMap
725 original=self.bones[:]
727 for i, k_v in enumerate(boneMap):
728 if k_v[0]==bone.name:
734 self.bones.sort(lambda l, r: getIndex(l)-getIndex(r))
736 self.bones.sort(key=getIndex)
739 for i, b in enumerate(self.bones):
740 src=original.index(b)
743 b.index=sortMap[b.index]
745 b.parent_index=sortMap[b.parent_index]
747 b.tail_index=sortMap[b.tail_index]
749 b.ik_index=sortMap[b.ik_index]
757 if b.parent_index==None:
758 b.parent_index=0xFFFF
760 if b.type==6 or b.type==7:
762 parent=self.bones[b.parent_index]
763 parent.tail_index=b.index
766 if b.tail_index==None:
771 def getIndex(self, bone):
772 for i, b in enumerate(self.bones):
777 def indexByName(self, name):
781 return self.getIndex(self.__boneByName(name))
783 def __boneByName(self, name):
784 return self.boneMap[name]
786 def __getBone(self, parent, b):
787 if len(b.children)==0:
791 for i, c in enumerate(b.children):
793 bl.bone.getHeadLocal(c),
794 bl.bone.getTailLocal(c))
797 bone.parent_index=parent.index
799 parent.tail_index=bone.index
800 self.__getBone(bone, c)
802 def __addBone(self, bone):
803 bone.index=len(self.bones)
804 self.bones.append(bone)
805 self.boneMap[bone.name]=bone
808 class PmdExporter(object):
810 def setup(self, scene):
811 self.armatureObj=None
816 for o in scene.objects:
817 object_node_map[o]=Node(o)
818 for o in scene.objects:
819 #for node in object_node_map.values():
820 node=object_node_map[o]
822 object_node_map[node.o.parent].children.append(node)
825 root=object_node_map[scene.objects.active]
828 self.oneSkinMesh=OneSkinMesh(scene)
829 self.__createOneSkinMesh(root)
830 print(self.oneSkinMesh)
831 if len(self.oneSkinMesh.morphList)==0:
833 self.oneSkinMesh.createEmptyBasicSkin()
835 self.name=root.o.name
838 self.builder=BoneBuilder()
839 self.builder.build(self.armatureObj)
841 def __createOneSkinMesh(self, node):
842 ############################################################
843 # search armature modifier
844 ############################################################
845 for m in node.o.modifiers:
846 if bl.modifier.isType(m, 'ARMATURE'):
847 armatureObj=bl.modifier.getArmatureObject(m)
848 if not self.armatureObj:
849 self.armatureObj=armatureObj
850 elif self.armatureObj!=armatureObj:
851 print("warning! found multiple armature. ignored.",
854 if node.o.type.upper()=='MESH':
855 self.oneSkinMesh.addMesh(node.o)
857 for child in node.children:
858 self.__createOneSkinMesh(child)
860 def write(self, path):
863 io.comment="blender export"
867 for pos, normal, uv, b0, b1, weight in self.oneSkinMesh.vertexArray.zip():
868 # convert right-handed z-up to left-handed y-up
877 v.uv.y=1.0-uv[1] # reverse vertical
878 v.bone0=self.builder.indexByName(b0)
879 v.bone1=self.builder.indexByName(b1)
880 v.weight0=int(100*weight)
881 v.edge_flag=0 # edge flag, 0: enable edge, 1: not edge
884 vertexCount=self.oneSkinMesh.getVertexCount()
885 for material_name, indices in self.oneSkinMesh.vertexArray.each():
886 print('material:', material_name)
887 m=bl.material.get(material_name)
889 material=io.addMaterial()
890 setMaterialParams(material, m)
892 material.vertex_count=len(indices)
893 material.toon_index=0
894 textures=[os.path.basename(path)
895 for path in bl.material.eachTexturePath(m)]
897 material.setTexture(toCP932('*'.join(textures)))
899 material.setTexture(toCP932(""))
902 assert(i<vertexCount)
903 for i in xrange(0, len(indices), 3):
905 io.indices.append(indices[i])
906 io.indices.append(indices[i+1])
907 io.indices.append(indices[i+2])
911 for i, b in enumerate(self.builder.bones):
915 boneNameMap[b.name]=i
916 v=englishmap.getUnicodeBoneName(b.name)
920 cp932=v[1].encode('cp932')
921 assert(len(cp932)<20)
925 bone_english_name=toCP932(b.name)
926 assert(len(bone_english_name)<20)
927 bone.setEnglishName(bone_english_name)
932 b.ik_index=self.builder.indexByName('eyes')
937 bone.parent_index=b.parent_index
938 bone.tail_index=b.tail_index
939 bone.ik_index=b.ik_index
941 # convert right-handed z-up to left-handed y-up
942 bone.pos.x=b.pos[0] if not near(b.pos[0], 0) else 0
943 bone.pos.y=b.pos[2] if not near(b.pos[2], 0) else 0
944 bone.pos.z=b.pos[1] if not near(b.pos[1], 0) else 0
947 for ik in self.builder.ik_list:
949 solver.index=self.builder.getIndex(ik.target)
950 solver.target=self.builder.getIndex(ik.effector)
951 solver.length=ik.length
952 b=self.builder.bones[ik.effector.parent_index]
953 for i in xrange(solver.length):
954 solver.children.append(self.builder.getIndex(b))
955 b=self.builder.bones[b.parent_index]
956 solver.iterations=ik.iterations
957 solver.weight=ik.weight
960 for i, m in enumerate(self.oneSkinMesh.morphList):
964 v=englishmap.getUnicodeSkinName(m.name)
966 v=[m.name, m.name, 0]
968 cp932=v[1].encode('cp932')
970 morph.setEnglishName(m.name.encode('cp932'))
973 for index, offset in m.offsets:
974 # convert right-handed z-up to left-handed y-up
975 morph.append(index, offset[0], offset[2], offset[1])
976 morph.vertex_count=len(m.offsets)
980 for i, m in enumerate(self.oneSkinMesh.morphList):
982 io.face_list.append(i)
983 for i, m in enumerate(self.oneSkinMesh.morphList):
985 io.face_list.append(i)
986 for i, m in enumerate(self.oneSkinMesh.morphList):
988 io.face_list.append(i)
989 for i, m in enumerate(self.oneSkinMesh.morphList):
991 io.face_list.append(i)
994 def createBoneDisplayName(name, english):
995 boneDisplayName=io.addBoneGroup()
997 boneDisplayName.name=name.decode('utf-8').encode('cp932')
998 boneDisplayName.english_name=english
1000 boneDisplayName.setName(name.encode('cp932'))
1001 boneDisplayName.setEnglishName(english.encode('cp932'))
1002 boneDisplayName=createBoneDisplayName("IK\n", "IK\n")
1003 boneDisplayName=createBoneDisplayName("体(上)\n", "Body[u]\n")
1004 boneDisplayName=createBoneDisplayName("髪\n", "Hair\n")
1005 boneDisplayName=createBoneDisplayName("腕\n", "Arms\n")
1006 boneDisplayName=createBoneDisplayName("指\n", "Fingers\n")
1007 boneDisplayName=createBoneDisplayName("体(下)\n", "Body[l]\n")
1008 boneDisplayName=createBoneDisplayName("足\n", "Legs\n")
1009 for i, b in enumerate(self.builder.bones):
1012 if b.type in [6, 7]:
1014 io.addBoneDisplay(i, getBoneDisplayGroup(b))
1017 io.english_name="blender export"
1018 io.english_coment="blender export"
1022 io.getToonTexture(i).name="toon%02d.bmp\n" % i
1026 for i, obj in enumerate(self.oneSkinMesh.rigidbodies):
1027 rigidBody=io.addRigidBody()
1028 rigidBody.setName(obj.name.encode('cp932'))
1029 rigidNameMap[obj.name]=i
1030 boneIndex=boneNameMap[obj[RIGID_BONE_NAME]]
1033 bone=self.builder.bones[0]
1035 bone=self.builder.bones[boneIndex]
1036 rigidBody.boneIndex=boneIndex
1037 #rigidBody.position.x=obj[RIGID_LOCATION][0]
1038 #rigidBody.position.y=obj[RIGID_LOCATION][1]
1039 #rigidBody.position.z=obj[RIGID_LOCATION][2]
1040 rigidBody.position.x=obj.location.x-bone.pos[0]
1041 rigidBody.position.y=obj.location.z-bone.pos[2]
1042 rigidBody.position.z=obj.location.y-bone.pos[1]
1043 rigidBody.rotation.x=-obj.rotation_euler[0]
1044 rigidBody.rotation.y=-obj.rotation_euler[2]
1045 rigidBody.rotation.z=-obj.rotation_euler[1]
1046 rigidBody.processType=obj[RIGID_PROCESS_TYPE]
1047 rigidBody.group=obj[RIGID_GROUP]
1048 rigidBody.target=obj[RIGID_INTERSECTION_GROUP]
1049 rigidBody.weight=obj[RIGID_WEIGHT]
1050 rigidBody.linearDamping=obj[RIGID_LINEAR_DAMPING]
1051 rigidBody.angularDamping=obj[RIGID_ANGULAR_DAMPING]
1052 rigidBody.restitution=obj[RIGID_RESTITUTION]
1053 rigidBody.friction=obj[RIGID_FRICTION]
1054 if obj[RIGID_SHAPE_TYPE]==0:
1055 rigidBody.shapeType=pmd.SHAPE_SPHERE
1056 rigidBody.w=obj.scale[0]
1057 elif obj[RIGID_SHAPE_TYPE]==1:
1058 rigidBody.shapeType=pmd.SHAPE_BOX
1059 rigidBody.w=obj.scale[0]
1060 rigidBody.d=obj.scale[1]
1061 rigidBody.h=obj.scale[2]
1062 elif obj[RIGID_SHAPE_TYPE]==2:
1063 rigidBody.shapeType=pmd.SHAPE_CAPSULE
1064 rigidBody.w=obj.scale[0]
1065 rigidBody.h=obj.scale[2]
1068 for obj in self.oneSkinMesh.constraints:
1069 constraint=io.addConstraint()
1070 constraint.setName(obj[CONSTRAINT_NAME].encode('cp932'))
1071 constraint.rigidA=rigidNameMap[obj[CONSTRAINT_A]]
1072 constraint.rigidB=rigidNameMap[obj[CONSTRAINT_B]]
1073 constraint.pos.x=obj.location[0]
1074 constraint.pos.y=obj.location[2]
1075 constraint.pos.z=obj.location[1]
1076 constraint.rot.x=-obj.rotation_euler[0]
1077 constraint.rot.y=-obj.rotation_euler[2]
1078 constraint.rot.z=-obj.rotation_euler[1]
1079 constraint.constraintPosMin.x=obj[CONSTRAINT_POS_MIN][0]
1080 constraint.constraintPosMin.y=obj[CONSTRAINT_POS_MIN][1]
1081 constraint.constraintPosMin.z=obj[CONSTRAINT_POS_MIN][2]
1082 constraint.constraintPosMax.x=obj[CONSTRAINT_POS_MAX][0]
1083 constraint.constraintPosMax.y=obj[CONSTRAINT_POS_MAX][1]
1084 constraint.constraintPosMax.z=obj[CONSTRAINT_POS_MAX][2]
1085 constraint.constraintRotMin.x=obj[CONSTRAINT_ROT_MIN][0]
1086 constraint.constraintRotMin.y=obj[CONSTRAINT_ROT_MIN][1]
1087 constraint.constraintRotMin.z=obj[CONSTRAINT_ROT_MIN][2]
1088 constraint.constraintRotMax.x=obj[CONSTRAINT_ROT_MAX][0]
1089 constraint.constraintRotMax.y=obj[CONSTRAINT_ROT_MAX][1]
1090 constraint.constraintRotMax.z=obj[CONSTRAINT_ROT_MAX][2]
1091 constraint.springPos.x=obj[CONSTRAINT_SPRING_POS][0]
1092 constraint.springPos.y=obj[CONSTRAINT_SPRING_POS][1]
1093 constraint.springPos.z=obj[CONSTRAINT_SPRING_POS][2]
1094 constraint.springRot.x=obj[CONSTRAINT_SPRING_ROT][0]
1095 constraint.springRot.y=obj[CONSTRAINT_SPRING_ROT][1]
1096 constraint.springRot.z=obj[CONSTRAINT_SPRING_ROT][2]
1099 print('write', path)
1100 return io.write(path)
1103 def getBoneDisplayGroup(bone):
1106 "necktie IK", "hair IK_L", "hair IK_R", "leg IK_L", "leg IK_R",
1107 "toe IK_L", "toe IK_R",
1110 "upper body", "neck", "head", "eye_L", "eye_R",
1111 "necktie1", "necktie2", "necktie3", "eyes",
1112 "eyelight_L", "eyelight_R",
1115 "front hair1", "front hair2", "front hair3",
1116 "hair1_L", "hair2_L", "hair3_L",
1117 "hair4_L", "hair5_L", "hair6_L",
1118 "hair1_R", "hair2_R", "hair3_R",
1119 "hair4_R", "hair5_R", "hair6_R",
1122 "shoulder_L", "arm_L", "arm twist_L", "elbow_L",
1123 "wrist twist_L", "wrist_L", "sleeve_L",
1124 "shoulder_R", "arm_R", "arm twist_R", "elbow_R",
1125 "wrist twist_R", "wrist_R", "sleeve_R",
1128 "thumb1_L", "thumb2_L", "fore1_L", "fore2_L", "fore3_L",
1129 "middle1_L", "middle2_L", "middle3_L",
1130 "third1_L", "third2_L", "third3_L",
1131 "little1_L", "little2_L", "little3_L",
1132 "thumb1_R", "thumb2_R", "fore1_R", "fore2_R", "fore3_R",
1133 "middle1_R", "middle2_R", "middle3_R",
1134 "third1_R", "third2_R", "third3_R",
1135 "little1_R", "little2_R", "little3_R",
1138 "lower body", "waist accessory",
1139 "front skirt_L", "back skirt_L",
1140 "front skirt_R", "back skirt_R",
1143 "leg_L", "knee_L", "ankle_L",
1144 "leg_R", "knee_R", "ankle_R",
1148 for g in boneGroups:
1156 def __execute(filename, scene):
1157 if not scene.objects.active:
1158 print("abort. no active object.")
1161 active=bl.object.getActive()
1162 exporter=PmdExporter()
1163 exporter.setup(scene)
1164 exporter.write(filename)
1165 bl.object.activate(active)
1170 def execute_24(filename):
1171 scene = bpy.data.scenes.active
1172 bl.initialize('pmd_export', scene)
1173 __execute(filename.decode(bl.INTERNAL_ENCODING), scene)
1176 Blender.Window.FileSelector(
1178 'Export Metasequoia PMD',
1179 Blender.sys.makename(ext='.pmd'))
1183 def execute_25(filename, scene):
1184 bl.initialize('pmd_export', scene)
1185 __execute(filename, scene)
1189 class EXPORT_OT_pmd(bpy.types.Operator):
1190 '''Save a Metasequoia PMD file.'''
1191 bl_idname = "export_scene.pmd"
1192 bl_label = 'Export PMD'
1194 # List of operator properties, the attributes will be assigned
1195 # to the class instance from the operator settings before calling.
1197 path = StringProperty(
1199 description="File path used for exporting the PMD file",
1203 filename = StringProperty(
1205 description="Name of the file.")
1206 directory = StringProperty(
1208 description="Directory of the file.")
1210 check_existing = BoolProperty(
1211 name="Check Existing",
1212 description="Check and warn on overwriting existing files",
1214 options=set('HIDDEN'))
1216 def execute(self, context):
1218 self.properties.path,
1223 def invoke(self, context, event):
1225 wm.add_fileselect(self)
1226 return 'RUNNING_MODAL'
1229 def menu_func(self, context):
1230 #default_path=bpy.data.filename.replace(".blend", ".pmd")
1231 self.layout.operator(
1232 EXPORT_OT_pmd.bl_idname,
1233 text="Miku Miku Dance Model(.pmd)")#.path=default_path
1236 bpy.types.register(EXPORT_OT_pmd)
1237 bpy.types.INFO_MT_file_export.append(menu_func)
1240 bpy.types.unregister(EXPORT_OT_pmd)
1241 bpy.types.INFO_MT_file_export.remove(menu_func)
1243 if __name__ == "__main__":