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.
26 MMD_SHAPE_GROUP_NAME='_MMD_SHAPE'
27 BASE_SHAPE_NAME='Basis'
28 RIGID_SHAPE_TYPE='rigid_shape_type'
29 RIGID_PROCESS_TYPE='rigid_process_type'
30 RIGID_BONE_NAME='rigid_bone_name'
31 #RIGID_LOCATION='rigid_loation'
32 RIGID_GROUP='ribid_group'
33 RIGID_INTERSECTION_GROUP='rigid_intersection_group'
34 RIGID_WEIGHT='rigid_weight'
35 RIGID_LINEAR_DAMPING='rigid_linear_damping'
36 RIGID_ANGULAR_DAMPING='rigid_angular_damping'
37 RIGID_RESTITUTION='rigid_restitution'
38 RIGID_FRICTION='rigid_friction'
39 CONSTRAINT_NAME='constraint_name'
40 CONSTRAINT_A='const_a'
41 CONSTRAINT_B='const_b'
42 CONSTRAINT_POS_MIN='const_pos_min'
43 CONSTRAINT_POS_MAX='const_pos_max'
44 CONSTRAINT_ROT_MIN='const_rot_min'
45 CONSTRAINT_ROT_MAX='const_rot_max'
46 CONSTRAINT_SPRING_POS='const_spring_pos'
47 CONSTRAINT_SPRING_ROT='const_spring_rot'
50 ###############################################################################
52 ###############################################################################
57 from meshio import pmd, englishmap
60 return sys.version_info[0]<3
65 from Blender import Mathutils
71 def setMaterialParams(material, m):
73 material.diffuse.r=m.R
74 material.diffuse.g=m.G
75 material.diffuse.b=m.B
76 material.diffuse.a=m.alpha
78 material.sinness=0 if m.spec<1e-5 else m.spec*10
79 material.specular.r=m.specR
80 material.specular.g=m.specG
81 material.specular.b=m.specB
83 material.ambient.r=m.mirR
84 material.ambient.g=m.mirG
85 material.ambient.b=m.mirB
87 material.flag=1 if m.enableSSS else 0
92 from bpy.props import *
100 def setMaterialParams(material, m):
102 material.diffuse.r=m.diffuse_color[0]
103 material.diffuse.g=m.diffuse_color[1]
104 material.diffuse.b=m.diffuse_color[2]
105 material.diffuse.a=m.alpha
107 material.sinness=0 if m.specular_hardness<1e-5 else m.specular_hardness*10
108 material.specular.r=m.specular_color[0]
109 material.specular.g=m.specular_color[1]
110 material.specular.b=m.specular_color[2]
112 material.ambient.r=m.mirror_color[0]
113 material.ambient.g=m.mirror_color[1]
114 material.ambient.b=m.mirror_color[2]
116 material.flag=1 if m.subsurface_scattering.enabled else 0
119 __slots__=['o', 'children']
120 def __init__(self, o):
125 ###############################################################################
126 # Blenderのメッシュをワンスキンメッシュ化する
127 ###############################################################################
128 def near(x, y, EPSILON=1e-5):
130 return d>=-EPSILON and d<=EPSILON
133 class VertexKey(object):
140 'nx', 'ny', 'nz', # 法線
144 def __init__(self, obj, index, x, y, z, nx, ny, nz, u, v):
157 return "<vkey: %f, %f, %f, %f, %f, %f, %f, %f>" % (
158 self.x, self.y, self.z, self.nx, self.ny, self.nz, self.u, self.v)
161 #return int((self.x+self.y+self.z+self.nx+self.ny+self.nz+self.u+self.v)*100)
162 return int((self.x+self.y+self.z)*100)
164 def __eq__(self, rhs):
165 #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)
166 #return near(self.x, rhs.x) and near(self.y, rhs.y) and near(self.z, rhs.z)
167 #return self.x==rhs.x and self.y==rhs.y and self.z==rhs.z
168 return self.obj==rhs.obj and self.index==rhs.index
171 class VertexArray(object):
176 # マテリアル毎に分割したインデックス配列
191 return "<VertexArray %d vertices, %d indexArrays>" % (
192 len(self.vertices), len(self.indexArrays))
196 self.vertices, self.normals, self.uvs,
197 self.b0, self.b1, self.weight)
200 keys=[key for key in self.indexArrays.keys()]
203 yield(key, self.indexArrays[key])
205 def __getIndex(self, obj, base_index, pos, normal, uv, b0, b1, weight0):
211 pos[0], pos[1], pos[2],
212 normal[0], normal[1], normal[2],
214 if key in self.vertexMap:
216 index=self.vertexMap[key]
218 index=len(self.vertices)
220 self.vertexMap[key]=index
222 self.vertices.append((pos.x, pos.y, pos.z))
223 self.normals.append((normal.x, normal.y, normal.z))
224 self.uvs.append((uv[0], uv[1]))
227 self.weight.append(weight0)
230 if not base_index in self.indexMap:
231 self.indexMap[base_index]=set()
232 self.indexMap[base_index].add(index)
237 def getMappedIndices(self, base_index):
238 return self.indexMap[base_index]
240 def addTriangle(self,
242 base_index0, base_index1, base_index2,
248 weight0, weight1, weight2
250 if not material in self.indexArrays:
251 self.indexArrays[material]=[]
253 index0=self.__getIndex(obj, base_index0, pos0, n0, uv0, b0_0, b1_0, weight0)
254 index1=self.__getIndex(obj, base_index1, pos1, n1, uv1, b0_1, b1_1, weight1)
255 index2=self.__getIndex(obj, base_index2, pos2, n2, uv2, b0_2, b1_2, weight2)
257 self.indexArrays[material]+=[index0, index1, index2]
261 __slots__=['name', 'type', 'offsets']
262 def __init__(self, name, type):
267 def add(self, index, offset):
268 self.offsets.append((index, offset))
272 self.offsets.sort(lambda l, r: l[0]-r[0])
274 self.offsets.sort(key=lambda e: e[0])
277 return "<Morph %s>" % self.name
279 class IKSolver(object):
280 __slots__=['target', 'effector', 'length', 'iterations', 'weight']
281 def __init__(self, target, effector, length, iterations, weight):
283 self.effector=effector
285 self.iterations=iterations
289 class OneSkinMesh(object):
290 __slots__=['obj_index', 'scene', 'vertexArray', 'morphList',
294 def __init__(self, scene):
295 self.vertexArray=VertexArray()
303 return "<OneSkinMesh %s, morph:%d>" % (
307 def addMesh(self, obj):
308 if bl.object.isVisible(obj):
313 self.__rigidbody(obj)
314 self.__constraint(obj)
316 def __mesh(self, obj):
320 if RIGID_SHAPE_TYPE in obj:
322 if CONSTRAINT_A in obj:
325 print("export", obj.name)
326 mesh=bl.object.getData(obj)
329 def setWeight(i, name, w):
332 if i in secondWeightMap:
334 if w<secondWeightMap[i]:
338 secondWeightMap[i]=(name, w)
341 weightMap[i]=(name, w)
343 if w>weightMap[i][1]:
345 secondWeightMap[i]=weightMap[i]
346 weightMap[i]=(name, w)
348 secondWeightMap[i]=(name, w)
350 weightMap[i]=(name, w)
353 for name in bl.object.getVertexGroupNames(obj):
354 for i, w in mesh.getVertsFromGroup(name, 1):
355 setWeight(i, name, w)
357 for i, v in enumerate(mesh.verts):
359 setWeight(i, obj.vertex_groups[g.group].name, g.weight)
362 for i in xrange(len(mesh.verts)):
363 #for i, name_weight in weightMap.items():
364 if i in secondWeightMap:
365 secondWeightMap[i]=(secondWeightMap[i][0], 1.0-weightMap[i][1])
367 weightMap[i]=(weightMap[i][0], 1.0)
368 secondWeightMap[i]=("", 0)
370 print("no weight vertex")
372 secondWeightMap[i]=("", 0)
374 # メッシュのコピーを生成してオブジェクトの行列を適用する
375 copyMesh, copyObj=bl.object.duplicate(obj)
376 if len(copyMesh.verts)==0:
379 for i, face in enumerate(copyMesh.faces):
380 faceVertexCount=bl.face.getVertexCount(face)
381 material=copyMesh.materials[bl.face.getMaterialIndex(face)]
382 v=[copyMesh.verts[index] for index in bl.face.getVertices(face)]
383 uv=bl.mesh.getFaceUV(copyMesh, i, face)
385 if faceVertexCount==3:
387 self.vertexArray.addTriangle(
388 self.obj_index, material.name,
396 #v0.no, v1.no, v2.no,
397 bl.face.getNormal(face),
398 bl.face.getNormal(face),
399 bl.face.getNormal(face),
403 weightMap[v[2].index][0],
404 weightMap[v[1].index][0],
405 weightMap[v[0].index][0],
406 secondWeightMap[v[2].index][0],
407 secondWeightMap[v[1].index][0],
408 secondWeightMap[v[0].index][0],
409 weightMap[v[2].index][1],
410 weightMap[v[1].index][1],
411 weightMap[v[0].index][1]
413 elif faceVertexCount==4:
415 self.vertexArray.addTriangle(
416 self.obj_index, 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 self.vertexArray.addTriangle(
441 self.obj_index, material.name,
448 #v2.no, v3.no, v0.no,
449 bl.face.getNormal(face),
450 bl.face.getNormal(face),
451 bl.face.getNormal(face),
455 weightMap[v[0].index][0],
456 weightMap[v[3].index][0],
457 weightMap[v[2].index][0],
458 secondWeightMap[v[0].index][0],
459 secondWeightMap[v[3].index][0],
460 secondWeightMap[v[2].index][0],
461 weightMap[v[0].index][1],
462 weightMap[v[3].index][1],
463 weightMap[v[2].index][1]
465 bl.object.delete(copyObj)
468 def __skin(self, obj):
469 if not bl.object.hasShapeKey(obj):
473 blenderMesh=bl.object.getData(obj)
477 vg=bl.object.getVertexGroup(obj, MMD_SHAPE_GROUP_NAME)
481 for b in bl.object.getShapeKeys(obj):
482 if b.name==BASE_SHAPE_NAME:
483 baseMorph=self.__getOrCreateMorph('base', 0)
488 v=bl.shapekey.getByIndex(b, index)
489 pos=[v[0], v[1], v[2]]
490 indices=self.vertexArray.getMappedIndices(index)
496 baseMorph.add(i, pos)
497 indexRelativeMap[i]=relativeIndex
502 print(basis.name, len(baseMorph.offsets))
504 if len(baseMorph.offsets)==0:
508 for b in bl.object.getShapeKeys(obj):
509 if b.name==BASE_SHAPE_NAME:
513 morph=self.__getOrCreateMorph(b.name, 4)
515 for index, src, dst in zip(
516 xrange(len(blenderMesh.verts)),
517 bl.shapekey.get(basis),
519 offset=[dst[0]-src[0], dst[1]-src[1], dst[2]-src[2]]
520 if offset[0]==0 and offset[1]==0 and offset[2]==0:
523 indices=self.vertexArray.getMappedIndices(index)
528 morph.add(indexRelativeMap[i], offset)
531 original=self.morphList[:]
533 for i, v in enumerate(englishmap.skinMap):
538 self.morphList.sort(lambda l, r: getIndex(l)-getIndex(r))
540 self.morphList.sort(key=getIndex)
542 def __rigidbody(self, obj):
545 if not RIGID_SHAPE_TYPE in obj:
547 self.rigidbodies.append(obj)
549 def __constraint(self, obj):
552 if not CONSTRAINT_A in obj:
554 self.constraints.append(obj)
556 def __getOrCreateMorph(self, name, type):
557 for m in self.morphList:
561 self.morphList.append(m)
564 def getVertexCount(self):
565 return len(self.vertexArray.vertices)
569 __slots__=['index', 'name', 'ik_index',
570 'pos', 'tail', 'parent_index', 'tail_index', 'type', 'isConnect']
571 def __init__(self, name, pos, tail):
576 self.parent_index=None
582 def __eq__(self, rhs):
583 return self.index==rhs.index
586 return "<Bone %s %d>" % (self.name, self.type)
588 class BoneBuilder(object):
589 __slots__=['bones', 'boneMap', 'ik_list', ]
595 def build(self, armatureObj):
599 print("gather bones")
600 armature=bl.object.getData(armatureObj)
601 for b in armature.bones.values():
605 bl.bone.getHeadLocal(b),
606 bl.bone.getTailLocal(b))
608 self.__getBone(bone, b)
610 for b in armature.bones.values():
611 if not b.parent and b.name!='center':
614 bl.bone.getHeadLocal(b),
615 bl.bone.getTailLocal(b))
617 self.__getBone(bone, b)
619 print("check connection")
620 for b in armature.bones.values():
622 self.__checkConnection(b, None)
625 pose = bl.object.getPose(armatureObj)
626 for b in pose.bones.values():
627 for c in b.constraints:
628 if bl.constraint.isIKSolver(c):
632 target=self.__boneByName(bl.constraint.ikTarget(c))
639 link=self.__boneByName(b.name)
644 chainLength=bl.constraint.ikChainLen(c)
645 for i in range(chainLength):
647 chainBone=self.__boneByName(e.name)
649 chainBone.ik_index=target.index
652 IKSolver(target, link, chainLength,
653 int(bl.constraint.ikItration(c) * 0.1),
654 bl.constraint.ikRotationWeight(c)
662 for i, v in enumerate(englishmap.boneMap):
663 if v[0]==ik.target.name:
665 return len(englishmap.boneMap)
667 self.ik_list.sort(lambda l, r: getIndex(l)-getIndex(r))
669 self.ik_list.sort(key=getIndex)
671 def __checkConnection(self, b, p):
672 if bl.bone.isConnected(b):
673 parent=self.__boneByName(p.name)
674 parent.isConnect=True
677 self.__checkConnection(c, b)
683 boneMap=englishmap.boneMap
684 original=self.bones[:]
686 for i, k_v in enumerate(boneMap):
687 if k_v[0]==bone.name:
692 self.bones.sort(lambda l, r: getIndex(l)-getIndex(r))
694 self.bones.sort(key=getIndex)
697 for i, b in enumerate(self.bones):
698 src=original.index(b)
701 b.index=sortMap[b.index]
703 b.parent_index=sortMap[b.parent_index]
705 b.tail_index=sortMap[b.tail_index]
707 b.ik_index=sortMap[b.ik_index]
715 if b.parent_index==None:
716 b.parent_index=0xFFFF
718 if b.type==6 or b.type==7:
720 parent=self.bones[b.parent_index]
721 parent.tail_index=b.index
724 if b.tail_index==None:
729 def getIndex(self, bone):
730 for i, b in enumerate(self.bones):
735 def indexByName(self, name):
739 return self.getIndex(self.__boneByName(name))
741 def __boneByName(self, name):
742 return self.boneMap[name]
744 def __getBone(self, parent, b):
745 if len(b.children)==0:
749 for i, c in enumerate(b.children):
751 bl.bone.getHeadLocal(c),
752 bl.bone.getTailLocal(c))
755 bone.parent_index=parent.index
757 parent.tail_index=bone.index
758 self.__getBone(bone, c)
760 def __addBone(self, bone):
761 bone.index=len(self.bones)
762 self.bones.append(bone)
763 self.boneMap[bone.name]=bone
766 class PmdExporter(object):
768 def setup(self, scene):
769 self.armatureObj=None
774 for o in scene.objects:
775 object_node_map[o]=Node(o)
776 for o in scene.objects:
777 #for node in object_node_map.values():
778 node=object_node_map[o]
780 object_node_map[node.o.parent].children.append(node)
783 root=object_node_map[scene.objects.active]
786 self.oneSkinMesh=OneSkinMesh(scene)
787 self.__createOneSkinMesh(root)
788 print(self.oneSkinMesh)
789 self.name=root.o.name
792 self.builder=BoneBuilder()
793 self.builder.build(self.armatureObj)
795 def __createOneSkinMesh(self, node):
796 ############################################################
797 # search armature modifier
798 ############################################################
799 for m in node.o.modifiers:
800 if bl.modifier.isType(m, 'ARMATURE'):
801 armatureObj=bl.modifier.getArmatureObject(m)
802 if not self.armatureObj:
803 self.armatureObj=armatureObj
804 elif self.armatureObj!=armatureObj:
805 print("warning! found multiple armature. ignored.",
808 if node.o.type.upper()=='MESH':
809 self.oneSkinMesh.addMesh(node.o)
811 for child in node.children:
812 self.__createOneSkinMesh(child)
814 def write(self, path):
817 io.comment="blender export"
821 for pos, normal, uv, b0, b1, weight in self.oneSkinMesh.vertexArray.zip():
822 # convert right-handed z-up to left-handed y-up
832 v.bone0=self.builder.indexByName(b0)
833 v.bone1=self.builder.indexByName(b1)
834 v.weight0=int(100*weight)
835 v.edge_flag=0 # edge flag, 0: enable edge, 1: not edge
838 vertexCount=self.oneSkinMesh.getVertexCount()
839 for material_name, indices in self.oneSkinMesh.vertexArray.each():
840 m=bl.material.get(material_name)
842 material=io.addMaterial()
843 setMaterialParams(material, m)
845 material.vertex_count=len(indices)
846 material.toon_index=0
851 assert(i<vertexCount)
852 for i in xrange(0, len(indices), 3):
854 io.indices.append(indices[i])
855 io.indices.append(indices[i+1])
856 io.indices.append(indices[i+2])
860 for i, b in enumerate(self.builder.bones):
864 boneNameMap[b.name]=i
865 v=englishmap.getUnicodeBoneName(b.name)
867 cp932=v[1].encode('cp932')
868 assert(len(cp932)<20)
872 bone_english_name=b.name
873 assert(len(bone_english_name)<20)
874 bone.english_name=bone_english_name
879 b.ik_index=self.builder.indexByName('eyes')
884 bone.parent_index=b.parent_index
885 bone.tail_index=b.tail_index
886 bone.ik_index=b.ik_index
888 # convert right-handed z-up to left-handed y-up
889 bone.pos.x=b.pos[0] if not near(b.pos[0], 0) else 0
890 bone.pos.y=b.pos[2] if not near(b.pos[2], 0) else 0
891 bone.pos.z=b.pos[1] if not near(b.pos[1], 0) else 0
894 for ik in self.builder.ik_list:
896 solver.index=self.builder.getIndex(ik.target)
897 solver.target=self.builder.getIndex(ik.effector)
898 solver.length=ik.length
899 b=self.builder.bones[ik.effector.parent_index]
900 for i in xrange(solver.length):
901 solver.children.append(self.builder.getIndex(b))
902 b=self.builder.bones[b.parent_index]
903 solver.iterations=ik.iterations
904 solver.weight=ik.weight
907 for i, m in enumerate(self.oneSkinMesh.morphList):
911 v=englishmap.getUnicodeSkinName(m.name)
913 cp932=v[1].encode('cp932')
915 morph.setEnglishName(m.name.encode('cp932'))
918 for index, offset in m.offsets:
919 # convert right-handed z-up to left-handed y-up
920 morph.append(index, offset[0], offset[2], offset[1])
921 morph.vertex_count=len(m.offsets)
925 for i, m in enumerate(self.oneSkinMesh.morphList):
927 io.face_list.append(i)
928 for i, m in enumerate(self.oneSkinMesh.morphList):
930 io.face_list.append(i)
931 for i, m in enumerate(self.oneSkinMesh.morphList):
933 io.face_list.append(i)
934 for i, m in enumerate(self.oneSkinMesh.morphList):
936 io.face_list.append(i)
939 def createBoneDisplayName(name, english):
940 boneDisplayName=io.addBoneDisplayName()
942 boneDisplayName.name=name.decode('utf-8').encode('cp932')
943 boneDisplayName.english_name=english
945 boneDisplayName.setName(name.encode('cp932'))
946 boneDisplayName.setEnglishName(english.encode('cp932'))
947 boneDisplayName=createBoneDisplayName("IK\n", "IK\n")
948 boneDisplayName=createBoneDisplayName("体(上)\n", "Body[u]\n")
949 boneDisplayName=createBoneDisplayName("髪\n", "Hair\n")
950 boneDisplayName=createBoneDisplayName("腕\n", "Arms\n")
951 boneDisplayName=createBoneDisplayName("指\n", "Fingers\n")
952 boneDisplayName=createBoneDisplayName("体(下)\n", "Body[l]\n")
953 boneDisplayName=createBoneDisplayName("足\n", "Legs\n")
954 for i, b in enumerate(self.builder.bones):
959 io.addBoneDisplay(i, getBoneDisplayGroup(b))
962 io.english_name="blender export"
963 io.english_coment="blender export"
967 io.getToonTexture(i).name="toon%02d.bmp\n" % i
971 for i, obj in enumerate(self.oneSkinMesh.rigidbodies):
972 rigidBody=io.addRigidBody()
973 rigidBody.setName(obj.name.encode('cp932'))
974 rigidNameMap[obj.name]=i
975 boneIndex=boneNameMap[obj[RIGID_BONE_NAME]]
978 bone=self.builder.bones[0]
980 bone=self.builder.bones[boneIndex]
981 rigidBody.boneIndex=boneIndex
982 #rigidBody.position.x=obj[RIGID_LOCATION][0]
983 #rigidBody.position.y=obj[RIGID_LOCATION][1]
984 #rigidBody.position.z=obj[RIGID_LOCATION][2]
985 rigidBody.position.x=obj.location.x-bone.pos[0]
986 rigidBody.position.y=obj.location.z-bone.pos[2]
987 rigidBody.position.z=obj.location.y-bone.pos[1]
988 rigidBody.rotation.x=-obj.rotation_euler[0]
989 rigidBody.rotation.y=-obj.rotation_euler[2]
990 rigidBody.rotation.z=-obj.rotation_euler[1]
991 rigidBody.processType=obj[RIGID_PROCESS_TYPE]
992 rigidBody.group=obj[RIGID_GROUP]
993 rigidBody.target=obj[RIGID_INTERSECTION_GROUP]
994 rigidBody.weight=obj[RIGID_WEIGHT]
995 rigidBody.linearDamping=obj[RIGID_LINEAR_DAMPING]
996 rigidBody.angularDamping=obj[RIGID_ANGULAR_DAMPING]
997 rigidBody.restitution=obj[RIGID_RESTITUTION]
998 rigidBody.friction=obj[RIGID_FRICTION]
999 if obj[RIGID_SHAPE_TYPE]==0:
1000 rigidBody.shapeType=pmd.SHAPE_SPHERE
1001 rigidBody.w=obj.scale[0]
1002 elif obj[RIGID_SHAPE_TYPE]==1:
1003 rigidBody.shapeType=pmd.SHAPE_BOX
1004 rigidBody.w=obj.scale[0]
1005 rigidBody.d=obj.scale[1]
1006 rigidBody.h=obj.scale[2]
1007 elif obj[RIGID_SHAPE_TYPE]==2:
1008 rigidBody.shapeType=pmd.SHAPE_CAPSULE
1009 rigidBody.w=obj.scale[0]
1010 rigidBody.h=obj.scale[2]
1013 for obj in self.oneSkinMesh.constraints:
1014 constraint=io.addConstraint()
1015 constraint.setName(obj[CONSTRAINT_NAME].encode('cp932'))
1016 constraint.rigidA=rigidNameMap[obj[CONSTRAINT_A]]
1017 constraint.rigidB=rigidNameMap[obj[CONSTRAINT_B]]
1018 constraint.pos.x=obj.location[0]
1019 constraint.pos.y=obj.location[2]
1020 constraint.pos.z=obj.location[1]
1021 constraint.rot.x=-obj.rotation_euler[0]
1022 constraint.rot.y=-obj.rotation_euler[2]
1023 constraint.rot.z=-obj.rotation_euler[1]
1024 constraint.constraintPosMin.x=obj[CONSTRAINT_POS_MIN][0]
1025 constraint.constraintPosMin.y=obj[CONSTRAINT_POS_MIN][1]
1026 constraint.constraintPosMin.z=obj[CONSTRAINT_POS_MIN][2]
1027 constraint.constraintPosMax.x=obj[CONSTRAINT_POS_MAX][0]
1028 constraint.constraintPosMax.y=obj[CONSTRAINT_POS_MAX][1]
1029 constraint.constraintPosMax.z=obj[CONSTRAINT_POS_MAX][2]
1030 constraint.constraintRotMin.x=obj[CONSTRAINT_ROT_MIN][0]
1031 constraint.constraintRotMin.y=obj[CONSTRAINT_ROT_MIN][1]
1032 constraint.constraintRotMin.z=obj[CONSTRAINT_ROT_MIN][2]
1033 constraint.constraintRotMax.x=obj[CONSTRAINT_ROT_MAX][0]
1034 constraint.constraintRotMax.y=obj[CONSTRAINT_ROT_MAX][1]
1035 constraint.constraintRotMax.z=obj[CONSTRAINT_ROT_MAX][2]
1036 constraint.springPos.x=obj[CONSTRAINT_SPRING_POS][0]
1037 constraint.springPos.y=obj[CONSTRAINT_SPRING_POS][1]
1038 constraint.springPos.z=obj[CONSTRAINT_SPRING_POS][2]
1039 constraint.springRot.x=obj[CONSTRAINT_SPRING_ROT][0]
1040 constraint.springRot.y=obj[CONSTRAINT_SPRING_ROT][1]
1041 constraint.springRot.z=obj[CONSTRAINT_SPRING_ROT][2]
1044 print('write', path)
1045 return io.write(path)
1048 def getBoneDisplayGroup(bone):
1051 "necktie IK", "hair IK_L", "hair IK_R", "leg IK_L", "leg IK_R",
1052 "toe IK_L", "toe IK_R",
1055 "upper body", "neck", "head", "eye_L", "eye_R",
1056 "necktie1", "necktie2", "necktie3", "eyes",
1057 "eyelight_L", "eyelight_R",
1060 "front hair1", "front hair2", "front hair3",
1061 "hair1_L", "hair2_L", "hair3_L",
1062 "hair4_L", "hair5_L", "hair6_L",
1063 "hair1_R", "hair2_R", "hair3_R",
1064 "hair4_R", "hair5_R", "hair6_R",
1067 "shoulder_L", "arm_L", "arm twist_L", "elbow_L",
1068 "wrist twist_L", "wrist_L", "sleeve_L",
1069 "shoulder_R", "arm_R", "arm twist_R", "elbow_R",
1070 "wrist twist_R", "wrist_R", "sleeve_R",
1073 "thumb1_L", "thumb2_L", "fore1_L", "fore2_L", "fore3_L",
1074 "middle1_L", "middle2_L", "middle3_L",
1075 "third1_L", "third2_L", "third3_L",
1076 "little1_L", "little2_L", "little3_L",
1077 "thumb1_R", "thumb2_R", "fore1_R", "fore2_R", "fore3_R",
1078 "middle1_R", "middle2_R", "middle3_R",
1079 "third1_R", "third2_R", "third3_R",
1080 "little1_R", "little2_R", "little3_R",
1083 "lower body", "waist accessory",
1084 "front skirt_L", "back skirt_L",
1085 "front skirt_R", "back skirt_R",
1088 "leg_L", "knee_L", "ankle_L",
1089 "leg_R", "knee_R", "ankle_R",
1093 for g in boneGroups:
1101 def __execute(filename, scene):
1102 if not scene.objects.active:
1103 print("abort. no active object.")
1106 active=bl.object.getActive()
1107 exporter=PmdExporter()
1108 exporter.setup(scene)
1109 exporter.write(filename)
1110 bl.object.activate(active)
1115 def execute_24(filename):
1116 scene = bpy.data.scenes.active
1117 bl.initialize('pmd_export', scene)
1118 __execute(filename.decode(bl.INTERNAL_ENCODING), scene)
1121 Blender.Window.FileSelector(
1123 'Export Metasequoia PMD',
1124 Blender.sys.makename(ext='.pmd'))
1128 def execute_25(filename, scene):
1129 bl.initialize('pmd_export', scene)
1130 __execute(filename, scene)
1134 class EXPORT_OT_pmd(bpy.types.Operator):
1135 '''Save a Metasequoia PMD file.'''
1136 bl_idname = "export_scene.pmd"
1137 bl_label = 'Export PMD'
1139 # List of operator properties, the attributes will be assigned
1140 # to the class instance from the operator settings before calling.
1142 path = StringProperty(
1144 description="File path used for exporting the PMD file",
1148 filename = StringProperty(
1150 description="Name of the file.")
1151 directory = StringProperty(
1153 description="Directory of the file.")
1155 check_existing = BoolProperty(
1156 name="Check Existing",
1157 description="Check and warn on overwriting existing files",
1159 options=set('HIDDEN'))
1161 def execute(self, context):
1163 self.properties.path,
1168 def invoke(self, context, event):
1170 wm.add_fileselect(self)
1171 return 'RUNNING_MODAL'
1174 def menu_func(self, context):
1175 #default_path=bpy.data.filename.replace(".blend", ".pmd")
1176 self.layout.operator(
1177 EXPORT_OT_pmd.bl_idname,
1178 text="Miku Miku Dance Model(.pmd)")#.path=default_path
1181 bpy.types.register(EXPORT_OT_pmd)
1182 bpy.types.INFO_MT_file_export.append(menu_func)
1185 bpy.types.unregister(EXPORT_OT_pmd)
1186 bpy.types.INFO_MT_file_export.remove(menu_func)
1188 if __name__ == "__main__":