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.
25 MMD_SHAPE_GROUP_NAME='_MMD_SHAPE'
26 BASE_SHAPE_NAME='Basis'
27 RIGID_SHAPE_TYPE='rigid_shape_type'
28 RIGID_PROCESS_TYPE='rigid_process_type'
29 RIGID_BONE_NAME='rigid_bone_name'
30 #RIGID_LOCATION='rigid_loation'
31 RIGID_GROUP='ribid_group'
32 RIGID_INTERSECTION_GROUP='rigid_intersection_group'
33 RIGID_WEIGHT='rigid_weight'
34 RIGID_LINEAR_DAMPING='rigid_linear_damping'
35 RIGID_ANGULAR_DAMPING='rigid_angular_damping'
36 RIGID_RESTITUTION='rigid_restitution'
37 RIGID_FRICTION='rigid_friction'
38 CONSTRAINT_A='const_a'
39 CONSTRAINT_B='const_b'
40 CONSTRAINT_POS_MIN='const_pos_min'
41 CONSTRAINT_POS_MAX='const_pos_max'
42 CONSTRAINT_ROT_MIN='const_rot_min'
43 CONSTRAINT_ROT_MAX='const_rot_max'
44 CONSTRAINT_SPRING_POS='const_spring_pos'
45 CONSTRAINT_SPRING_ROT='const_spring_rot'
48 ###############################################################################
50 ###############################################################################
55 from meshio import pmd, englishmap
58 return sys.version_info[0]<3
63 from Blender import Mathutils
71 from bpy.props import *
81 __slots__=['o', 'children']
82 def __init__(self, o):
87 ###############################################################################
88 # Blenderのメッシュをワンスキンメッシュ化する
89 ###############################################################################
90 def near(x, y, EPSILON=1e-5):
92 return d>=-EPSILON and d<=EPSILON
95 class VertexKey(object):
102 'nx', 'ny', 'nz', # 法線
106 def __init__(self, obj, index, x, y, z, nx, ny, nz, u, v):
119 return "<vkey: %f, %f, %f, %f, %f, %f, %f, %f>" % (
120 self.x, self.y, self.z, self.nx, self.ny, self.nz, self.u, self.v)
123 #return int((self.x+self.y+self.z+self.nx+self.ny+self.nz+self.u+self.v)*100)
124 return int((self.x+self.y+self.z)*100)
126 def __eq__(self, rhs):
127 #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)
128 #return near(self.x, rhs.x) and near(self.y, rhs.y) and near(self.z, rhs.z)
129 #return self.x==rhs.x and self.y==rhs.y and self.z==rhs.z
130 return self.obj==rhs.obj and self.index==rhs.index
133 class VertexArray(object):
138 # マテリアル毎に分割したインデックス配列
153 return "<VertexArray %d vertices, %d indexArrays>" % (
154 len(self.vertices), len(self.indexArrays))
158 self.vertices, self.normals, self.uvs,
159 self.b0, self.b1, self.weight)
161 def __getIndex(self, obj, base_index, pos, normal, uv, b0, b1, weight0):
167 pos[0], pos[1], pos[2],
168 normal[0], normal[1], normal[2],
170 if key in self.vertexMap:
172 index=self.vertexMap[key]
174 index=len(self.vertices)
176 self.vertexMap[key]=index
178 self.vertices.append((pos.x, pos.y, pos.z))
179 self.normals.append((normal.x, normal.y, normal.z))
180 self.uvs.append((uv[0], uv[1]))
183 self.weight.append(weight0)
186 if not base_index in self.indexMap:
187 self.indexMap[base_index]=set()
188 self.indexMap[base_index].add(index)
193 def getMappedIndices(self, base_index):
194 return self.indexMap[base_index]
196 def addTriangle(self,
198 base_index0, base_index1, base_index2,
204 weight0, weight1, weight2
206 if not material in self.indexArrays:
207 self.indexArrays[material]=[]
209 index0=self.__getIndex(obj, base_index0, pos0, n0, uv0, b0_0, b1_0, weight0)
210 index1=self.__getIndex(obj, base_index1, pos1, n1, uv1, b0_1, b1_1, weight1)
211 index2=self.__getIndex(obj, base_index2, pos2, n2, uv2, b0_2, b1_2, weight2)
213 self.indexArrays[material]+=[index0, index1, index2]
217 __slots__=['name', 'type', 'offsets']
218 def __init__(self, name, type):
223 def add(self, index, offset):
224 self.offsets.append((index, offset))
228 self.offsets.sort(lambda l, r: l[0]-r[0])
230 self.offsets.sort(key=lambda e: e[0])
233 return "<Morph %s>" % self.name
235 class IKSolver(object):
236 __slots__=['target', 'effector', 'length', 'iterations', 'weight']
237 def __init__(self, target, effector, length, iterations, weight):
239 self.effector=effector
241 self.iterations=iterations
245 class OneSkinMesh(object):
246 __slots__=['obj_index', 'scene', 'vertexArray', 'morphList',
250 def __init__(self, scene):
251 self.vertexArray=VertexArray()
259 return "<OneSkinMesh %s, morph:%d>" % (
263 def addMesh(self, obj):
264 if bl.objectIsVisible(obj):
269 self.__rigidbody(obj)
270 self.__constraint(obj)
272 def __mesh(self, obj):
276 if RIGID_SHAPE_TYPE in obj:
278 if CONSTRAINT_A in obj:
281 print("export", obj.name)
282 mesh=bl.objectGetData(obj)
285 def setWeight(i, name, w):
288 if i in secondWeightMap:
290 if w<secondWeightMap[i]:
294 secondWeightMap[i]=(name, w)
297 weightMap[i]=(name, w)
299 if w>weightMap[i][1]:
301 secondWeightMap[i]=weightMap[i]
302 weightMap[i]=(name, w)
304 secondWeightMap[i]=(name, w)
306 weightMap[i]=(name, w)
309 for name in bl.meshVertexGroupNames(obj):
310 for i, w in mesh.getVertsFromGroup(name, 1):
311 setWeight(i, name, w)
313 for i, v in enumerate(mesh.verts):
315 setWeight(i, obj.vertex_groups[g.group].name, g.weight)
318 for i in xrange(len(mesh.verts)):
319 #for i, name_weight in weightMap.items():
320 if i in secondWeightMap:
321 secondWeightMap[i]=(secondWeightMap[i][0], 1.0-weightMap[i][1])
323 weightMap[i]=(weightMap[i][0], 1.0)
324 secondWeightMap[i]=("", 0)
326 print("no weight vertex")
328 secondWeightMap[i]=("", 0)
330 # メッシュのコピーを生成してオブジェクトの行列を適用する
331 copyMesh, copyObj=bl.objectDuplicate(self.scene, obj)
332 if len(copyMesh.verts)==0:
335 for i, face in enumerate(copyMesh.faces):
336 faceVertexCount=bl.faceVertexCount(face)
337 material=copyMesh.materials[bl.faceMaterialIndex(face)]
338 v=[copyMesh.verts[index] for index in bl.faceVertices(face)]
339 uv=bl.meshFaceUv(copyMesh, i, face)
340 if faceVertexCount==3:
342 self.vertexArray.addTriangle(
343 self.obj_index, material.name,
344 v[0].index, v[1].index, v[2].index,
345 v[0].co, v[1].co, v[2].co,
347 #v0.no, v1.no, v2.no,
352 weightMap[v[0].index][0],
353 weightMap[v[1].index][0],
354 weightMap[v[2].index][0],
355 secondWeightMap[v[0].index][0],
356 secondWeightMap[v[1].index][0],
357 secondWeightMap[v[2].index][0],
358 weightMap[v[0].index][1],
359 weightMap[v[1].index][1],
360 weightMap[v[2].index][1]
362 elif faceVertexCount==4:
364 self.vertexArray.addTriangle(
365 self.obj_index, material.name,
366 v[0].index, v[1].index, v[2].index,
367 v[0].co, v[1].co, v[2].co,
368 #v0.no, v1.no, v2.no,
373 weightMap[v[0].index][0],
374 weightMap[v[1].index][0],
375 weightMap[v[2].index][0],
376 secondWeightMap[v[0].index][0],
377 secondWeightMap[v[1].index][0],
378 secondWeightMap[v[2].index][0],
379 weightMap[v[0].index][1],
380 weightMap[v[1].index][1],
381 weightMap[v[2].index][1]
383 self.vertexArray.addTriangle(
384 self.obj_index, material.name,
385 v[2].index, v[3].index, v[0].index,
386 v[2].co, v[3].co, v[0].co,
387 #v2.no, v3.no, v0.no,
392 weightMap[v[2].index][0],
393 weightMap[v[3].index][0],
394 weightMap[v[0].index][0],
395 secondWeightMap[v[2].index][0],
396 secondWeightMap[v[3].index][0],
397 secondWeightMap[v[0].index][0],
398 weightMap[v[2].index][1],
399 weightMap[v[3].index][1],
400 weightMap[v[0].index][1]
402 bl.objectDelete(self.scene, copyObj)
405 def __skin(self, obj):
406 if not bl.objectHasShapeKey(obj):
410 blenderMesh=bl.objectGetData(obj)
414 vg=bl.meshVertexGroup(obj, MMD_SHAPE_GROUP_NAME)
418 for b in bl.objectShapeKeys(obj):
419 if b.name==BASE_SHAPE_NAME:
420 baseMorph=self.__getOrCreateMorph('base', 0)
425 v=bl.shapeKeyGet(b, index)
426 pos=[v[0], v[1], v[2]]
427 indices=self.vertexArray.getMappedIndices(index)
433 baseMorph.add(i, pos)
434 indexRelativeMap[i]=relativeIndex
439 print(basis.name, len(baseMorph.offsets))
441 if len(baseMorph.offsets)==0:
445 for b in bl.objectShapeKeys(obj):
446 if b.name==BASE_SHAPE_NAME:
450 morph=self.__getOrCreateMorph(b.name, 4)
452 for index, src, dst in zip(
453 xrange(len(blenderMesh.verts)),
456 offset=[dst[0]-src[0], dst[1]-src[1], dst[2]-src[2]]
457 if offset[0]==0 and offset[1]==0 and offset[2]==0:
460 indices=self.vertexArray.getMappedIndices(index)
465 morph.add(indexRelativeMap[i], offset)
468 original=self.morphList[:]
470 for i, v in enumerate(englishmap.skinMap):
475 self.morphList.sort(lambda l, r: getIndex(l)-getIndex(r))
477 self.morphList.sort(key=getIndex)
479 def __rigidbody(self, obj):
482 if not RIGID_SHAPE_TYPE in obj:
484 self.rigidbodies.append(obj)
486 def __constraint(self, obj):
489 if not CONSTRAINT_A in obj:
491 self.constraints.append(obj)
493 def __getOrCreateMorph(self, name, type):
494 for m in self.morphList:
498 self.morphList.append(m)
501 def getVertexCount(self):
502 return len(self.vertexArray.vertices)
506 __slots__=['index', 'name', 'ik_index',
507 'pos', 'tail', 'parent_index', 'tail_index', 'type', 'isConnect']
508 def __init__(self, name, pos, tail):
513 self.parent_index=None
519 def __eq__(self, rhs):
520 return self.index==rhs.index
523 return "<Bone %s %d>" % (self.name, self.type)
525 class BoneBuilder(object):
526 __slots__=['bones', 'boneMap', 'ik_list', ]
532 def build(self, armatureObj):
536 print("gather bones")
537 armature=bl.objectGetData(armatureObj)
538 for b in armature.bones.values():
545 self.__getBone(bone, b)
547 for b in armature.bones.values():
548 if not b.parent and b.name!='center':
554 self.__getBone(bone, b)
556 print("check connection")
557 for b in armature.bones.values():
559 self.__checkConnection(b, None)
562 pose = bl.objectGetPose(armatureObj)
563 for b in pose.bones.values():
564 for c in b.constraints:
565 if bl.constraintIsIKSolver(c):
569 target=self.__boneByName(bl.ikTarget(c))
576 link=self.__boneByName(b.name)
581 chainLength=bl.ikChainLen(c)
582 for i in range(chainLength):
584 chainBone=self.__boneByName(e.name)
586 chainBone.ik_index=target.index
589 IKSolver(target, link, chainLength,
590 int(bl.ikItration(c) * 0.1),
591 bl.ikRotationWeight(c)
599 for i, v in enumerate(englishmap.boneMap):
600 if v[0]==ik.target.name:
602 return len(englishmap.boneMap)
604 self.ik_list.sort(lambda l, r: getIndex(l)-getIndex(r))
606 self.ik_list.sort(key=getIndex)
608 def __checkConnection(self, b, p):
609 if bl.boneIsConnected(b):
610 parent=self.__boneByName(p.name)
611 parent.isConnect=True
614 self.__checkConnection(c, b)
620 boneMap=englishmap.boneMap
621 original=self.bones[:]
623 for i, k_v in enumerate(boneMap):
624 if k_v[0]==bone.name:
629 self.bones.sort(lambda l, r: getIndex(l)-getIndex(r))
631 self.bones.sort(key=getIndex)
634 for i, b in enumerate(self.bones):
635 src=original.index(b)
638 b.index=sortMap[b.index]
640 b.parent_index=sortMap[b.parent_index]
642 b.tail_index=sortMap[b.tail_index]
644 b.ik_index=sortMap[b.ik_index]
652 if b.parent_index==None:
653 b.parent_index=0xFFFF
655 if b.type==6 or b.type==7:
657 parent=self.bones[b.parent_index]
658 parent.tail_index=b.index
661 if b.tail_index==None:
666 def getIndex(self, bone):
667 for i, b in enumerate(self.bones):
672 def indexByName(self, name):
676 return self.getIndex(self.__boneByName(name))
678 def __boneByName(self, name):
679 return self.boneMap[name]
681 def __getBone(self, parent, b):
682 if len(b.children)==0:
686 for i, c in enumerate(b.children):
692 bone.parent_index=parent.index
694 parent.tail_index=bone.index
695 self.__getBone(bone, c)
697 def __addBone(self, bone):
698 bone.index=len(self.bones)
699 self.bones.append(bone)
700 self.boneMap[bone.name]=bone
703 class PmdExporter(object):
705 def setup(self, scene):
706 self.armatureObj=None
711 for o in scene.objects:
712 object_node_map[o]=Node(o)
713 for o in scene.objects:
714 #for node in object_node_map.values():
715 node=object_node_map[o]
717 object_node_map[node.o.parent].children.append(node)
720 root=object_node_map[scene.objects.active]
723 self.oneSkinMesh=OneSkinMesh(scene)
724 self.__createOneSkinMesh(root)
725 print(self.oneSkinMesh)
726 self.name=root.o.name
729 self.builder=BoneBuilder()
730 self.builder.build(self.armatureObj)
732 def __createOneSkinMesh(self, node):
733 ############################################################
734 # search armature modifier
735 ############################################################
736 for m in node.o.modifiers:
737 if bl.modifierIsArmature(m):
738 armatureObj=bl.armatureModifierGetObject(m)
739 if not self.armatureObj:
740 self.armatureObj=armatureObj
741 elif self.armatureObj!=armatureObj:
742 print("warning! found multiple armature. ignored.",
745 if node.o.type.upper()=='MESH':
746 self.oneSkinMesh.addMesh(node.o)
748 for child in node.children:
749 self.__createOneSkinMesh(child)
751 def write(self, path):
754 io.comment="blender export"
758 for pos, normal, uv, b0, b1, weight in self.oneSkinMesh.vertexArray.zip():
759 # convert right-handed z-up to left-handed y-up
769 v.bone0=self.builder.indexByName(b0)
770 v.bone1=self.builder.indexByName(b1)
771 v.weight0=int(100*weight)
772 v.edge_flag=0 # edge flag, 0: enable edge, 1: not edge
775 vertexCount=self.oneSkinMesh.getVertexCount()
776 for material_name, indices in self.oneSkinMesh.vertexArray.indexArrays.items():
777 m=bl.materialGet(self.scene, material_name)
779 material=io.addMaterial()
781 material.diffuse.r=m.R
782 material.diffuse.g=m.G
783 material.diffuse.b=m.B
784 material.diffuse.a=m.alpha
785 material.sinness=0 if m.spec<1e-5 else m.spec*10
786 material.specular.r=m.specR
787 material.specular.g=m.specG
788 material.specular.b=m.specB
789 material.ambient.r=m.mirR
790 material.ambient.g=m.mirG
791 material.ambient.b=m.mirB
792 material.flag=1 if m.enableSSS else 0
794 material.diffuse.r=m.diffuse_color[0]
795 material.diffuse.g=m.diffuse_color[1]
796 material.diffuse.b=m.diffuse_color[2]
797 material.diffuse.a=m.alpha
798 material.sinness=0 if m.specular_hardness<1e-5 else m.specular_hardness*10
799 material.specular.r=m.specular_color[0]
800 material.specular.g=m.specular_color[1]
801 material.specular.b=m.specular_color[2]
802 material.ambient.r=m.mirror_color[0]
803 material.ambient.g=m.mirror_color[1]
804 material.ambient.b=m.mirror_color[2]
805 material.flag=1 if m.subsurface_scattering.enabled else 0
807 material.vertex_count=len(indices)
808 material.toon_index=0
813 assert(i<vertexCount)
814 for i in xrange(0, len(indices), 3):
816 io.indices.append(indices[i])
817 io.indices.append(indices[i+1])
818 io.indices.append(indices[i+2])
822 for i, b in enumerate(self.builder.bones):
826 boneNameMap[b.name]=i
827 v=englishmap.getUnicodeBoneName(b.name)
829 cp932=v[1].encode('cp932')
830 assert(len(cp932)<20)
834 bone_english_name=b.name
835 assert(len(bone_english_name)<20)
836 bone.english_name=bone_english_name
841 b.ik_index=self.builder.indexByName('eyes')
846 bone.parent_index=b.parent_index
847 bone.tail_index=b.tail_index
848 bone.ik_index=b.ik_index
850 # convert right-handed z-up to left-handed y-up
851 bone.pos.x=b.pos[0] if not near(b.pos[0], 0) else 0
852 bone.pos.y=b.pos[2] if not near(b.pos[2], 0) else 0
853 bone.pos.z=b.pos[1] if not near(b.pos[1], 0) else 0
856 for ik in self.builder.ik_list:
858 solver.index=self.builder.getIndex(ik.target)
859 solver.target=self.builder.getIndex(ik.effector)
860 solver.length=ik.length
861 b=self.builder.bones[ik.effector.parent_index]
862 for i in xrange(solver.length):
863 solver.children.append(self.builder.getIndex(b))
864 b=self.builder.bones[b.parent_index]
865 solver.iterations=ik.iterations
866 solver.weight=ik.weight
869 for i, m in enumerate(self.oneSkinMesh.morphList):
873 v=englishmap.getUnicodeSkinName(m.name)
875 cp932=v[1].encode('cp932')
877 morph.setEnglishName(m.name.encode('cp932'))
880 for index, offset in m.offsets:
881 # convert right-handed z-up to left-handed y-up
882 morph.append(index, offset[0], offset[2], offset[1])
883 morph.vertex_count=len(m.offsets)
887 for i, m in enumerate(self.oneSkinMesh.morphList):
889 io.face_list.append(i)
890 for i, m in enumerate(self.oneSkinMesh.morphList):
892 io.face_list.append(i)
893 for i, m in enumerate(self.oneSkinMesh.morphList):
895 io.face_list.append(i)
896 for i, m in enumerate(self.oneSkinMesh.morphList):
898 io.face_list.append(i)
901 def createBoneDisplayName(name, english):
902 boneDisplayName=io.addBoneDisplayName()
904 boneDisplayName.name=name.decode('utf-8').encode('cp932')
905 boneDisplayName.english_name=english
907 boneDisplayName.setName(name.encode('cp932'))
908 boneDisplayName.setEnglishName(english.encode('cp932'))
909 boneDisplayName=createBoneDisplayName("IK\n", "IK\n")
910 boneDisplayName=createBoneDisplayName("体(上)\n", "Body[u]\n")
911 boneDisplayName=createBoneDisplayName("髪\n", "Hair\n")
912 boneDisplayName=createBoneDisplayName("腕\n", "Arms\n")
913 boneDisplayName=createBoneDisplayName("指\n", "Fingers\n")
914 boneDisplayName=createBoneDisplayName("体(下)\n", "Body[l]\n")
915 boneDisplayName=createBoneDisplayName("足\n", "Legs\n")
916 for i, b in enumerate(self.builder.bones):
921 io.addBoneDisplay(i, getBoneDisplayGroup(b))
924 io.english_name="blender export"
925 io.english_coment="blender export"
929 io.getToonTexture(i).name="toon%02d.bmp\n" % i
933 for i, obj in enumerate(self.oneSkinMesh.rigidbodies):
934 rigidBody=io.addRigidBody()
935 rigidBody.setName(obj.name.encode('cp932'))
936 rigidNameMap[obj.name]=i
937 boneIndex=boneNameMap[obj[RIGID_BONE_NAME]]
940 bone=self.builder.bones[0]
942 bone=self.builder.bones[boneIndex]
943 rigidBody.boneIndex=boneIndex
944 #rigidBody.position.x=obj[RIGID_LOCATION][0]
945 #rigidBody.position.y=obj[RIGID_LOCATION][1]
946 #rigidBody.position.z=obj[RIGID_LOCATION][2]
947 rigidBody.position.x=obj.location.x-bone.pos[0]
948 rigidBody.position.y=obj.location.z-bone.pos[2]
949 rigidBody.position.z=obj.location.y-bone.pos[1]
950 rigidBody.rotation.x=-obj.rotation_euler[0]
951 rigidBody.rotation.y=-obj.rotation_euler[2]
952 rigidBody.rotation.z=-obj.rotation_euler[1]
953 rigidBody.processType=obj[RIGID_PROCESS_TYPE]
954 rigidBody.group=obj[RIGID_GROUP]
955 rigidBody.target=obj[RIGID_INTERSECTION_GROUP]
956 rigidBody.weight=obj[RIGID_WEIGHT]
957 rigidBody.linearDamping=obj[RIGID_LINEAR_DAMPING]
958 rigidBody.angularDamping=obj[RIGID_ANGULAR_DAMPING]
959 rigidBody.restitution=obj[RIGID_RESTITUTION]
960 rigidBody.friction=obj[RIGID_FRICTION]
961 if obj[RIGID_SHAPE_TYPE]==0:
962 rigidBody.shapeType=pmd.SHAPE_SPHERE
963 rigidBody.w=obj.scale[0]
964 elif obj[RIGID_SHAPE_TYPE]==1:
965 rigidBody.shapeType=pmd.SHAPE_BOX
966 rigidBody.w=obj.scale[0]
967 rigidBody.d=obj.scale[1]
968 rigidBody.h=obj.scale[2]
969 elif obj[RIGID_SHAPE_TYPE]==2:
970 rigidBody.shapeType=pmd.SHAPE_CAPSULE
971 rigidBody.w=obj.scale[0]
972 rigidBody.h=obj.scale[2]
975 for obj in self.oneSkinMesh.constraints:
976 constraint=io.addConstraint()
977 constraint.setName(obj.name[1:].encode('cp932'))
978 constraint.rigidA=rigidNameMap[obj[CONSTRAINT_A]]
979 constraint.rigidB=rigidNameMap[obj[CONSTRAINT_B]]
980 constraint.pos.x=obj.location[0]
981 constraint.pos.y=obj.location[2]
982 constraint.pos.z=obj.location[1]
983 constraint.rot.x=-obj.rotation_euler[0]
984 constraint.rot.y=-obj.rotation_euler[2]
985 constraint.rot.z=-obj.rotation_euler[1]
986 constraint.constraintPosMin.x=obj[CONSTRAINT_POS_MIN][0]
987 constraint.constraintPosMin.y=obj[CONSTRAINT_POS_MIN][1]
988 constraint.constraintPosMin.z=obj[CONSTRAINT_POS_MIN][2]
989 constraint.constraintPosMax.x=obj[CONSTRAINT_POS_MAX][0]
990 constraint.constraintPosMax.y=obj[CONSTRAINT_POS_MAX][1]
991 constraint.constraintPosMax.z=obj[CONSTRAINT_POS_MAX][2]
992 constraint.constraintRotMin.x=obj[CONSTRAINT_ROT_MIN][0]
993 constraint.constraintRotMin.y=obj[CONSTRAINT_ROT_MIN][1]
994 constraint.constraintRotMin.z=obj[CONSTRAINT_ROT_MIN][2]
995 constraint.constraintRotMax.x=obj[CONSTRAINT_ROT_MAX][0]
996 constraint.constraintRotMax.y=obj[CONSTRAINT_ROT_MAX][1]
997 constraint.constraintRotMax.z=obj[CONSTRAINT_ROT_MAX][2]
998 constraint.springPos.x=obj[CONSTRAINT_SPRING_POS][0]
999 constraint.springPos.y=obj[CONSTRAINT_SPRING_POS][1]
1000 constraint.springPos.z=obj[CONSTRAINT_SPRING_POS][2]
1001 constraint.springRot.x=obj[CONSTRAINT_SPRING_ROT][0]
1002 constraint.springRot.y=obj[CONSTRAINT_SPRING_ROT][1]
1003 constraint.springRot.z=obj[CONSTRAINT_SPRING_ROT][2]
1006 print('write', path)
1007 return io.write(path)
1010 def getBoneDisplayGroup(bone):
1013 "necktie IK", "hair IK_L", "hair IK_R", "leg IK_L", "leg IK_R",
1014 "toe IK_L", "toe IK_R",
1017 "upper body", "neck", "head", "eye_L", "eye_R",
1018 "necktie1", "necktie2", "necktie3", "eyes",
1019 "eyelight_L", "eyelight_R",
1022 "front hair1", "front hair2", "front hair3",
1023 "hair1_L", "hair2_L", "hair3_L",
1024 "hair4_L", "hair5_L", "hair6_L",
1025 "hair1_R", "hair2_R", "hair3_R",
1026 "hair4_R", "hair5_R", "hair6_R",
1029 "shoulder_L", "arm_L", "arm twist_L", "elbow_L",
1030 "wrist twist_L", "wrist_L", "sleeve_L",
1031 "shoulder_R", "arm_R", "arm twist_R", "elbow_R",
1032 "wrist twist_R", "wrist_R", "sleeve_R",
1035 "thumb1_L", "thumb2_L", "fore1_L", "fore2_L", "fore3_L",
1036 "middle1_L", "middle2_L", "middle3_L",
1037 "third1_L", "third2_L", "third3_L",
1038 "little1_L", "little2_L", "little3_L",
1039 "thumb1_R", "thumb2_R", "fore1_R", "fore2_R", "fore3_R",
1040 "middle1_R", "middle2_R", "middle3_R",
1041 "third1_R", "third2_R", "third3_R",
1042 "little1_R", "little2_R", "little3_R",
1045 "lower body", "waist accessory",
1046 "front skirt_L", "back skirt_L",
1047 "front skirt_R", "back skirt_R",
1050 "leg_L", "knee_L", "ankle_L",
1051 "leg_R", "knee_R", "ankle_R",
1055 for g in boneGroups:
1063 def __execute(filename, scene):
1064 if not scene.objects.active:
1065 print("abort. no active object.")
1068 bl.progress_start('pmd_export')
1069 active=bl.objectGetActive(scene)
1070 exporter=PmdExporter()
1071 exporter.setup(scene)
1072 exporter.write(filename)
1073 bl.objectActivate(scene, active)
1074 bl.progress_finish()
1079 def execute_24(filename):
1080 filename=filename.decode(bl.INTERNAL_ENCODING)
1081 print("pmd exporter: %s" % filename)
1083 Blender.Window.WaitCursor(1)
1084 t = Blender.sys.time()
1086 scene = bpy.data.scenes.active
1087 __execute(filename, scene)
1089 print('finished in %.2f seconds' % (Blender.sys.time()-t))
1091 Blender.Window.WaitCursor(0)
1093 Blender.Window.FileSelector(
1095 'Export Metasequoia PMD',
1096 Blender.sys.makename(ext='.pmd'))
1100 def execute_25(*args):
1104 class EXPORT_OT_pmd(bpy.types.Operator):
1105 '''Save a Metasequoia PMD file.'''
1106 bl_idname = "export_scene.pmd"
1107 bl_label = 'Export PMD'
1109 # List of operator properties, the attributes will be assigned
1110 # to the class instance from the operator settings before calling.
1112 path = StringProperty(
1114 description="File path used for exporting the PMD file",
1118 filename = StringProperty(
1120 description="Name of the file.")
1121 directory = StringProperty(
1123 description="Directory of the file.")
1125 check_existing = BoolProperty(
1126 name="Check Existing",
1127 description="Check and warn on overwriting existing files",
1129 options=set('HIDDEN'))
1131 def execute(self, context):
1133 self.properties.path,
1138 def invoke(self, context, event):
1140 wm.add_fileselect(self)
1141 return 'RUNNING_MODAL'
1144 def menu_func(self, context):
1145 #default_path=bpy.data.filename.replace(".blend", ".pmd")
1146 self.layout.operator(
1147 EXPORT_OT_pmd.bl_idname,
1148 text="Miku Miku Dance Model(.pmd)")#.path=default_path
1151 bpy.types.register(EXPORT_OT_pmd)
1152 bpy.types.INFO_MT_file_export.append(menu_func)
1155 bpy.types.unregister(EXPORT_OT_pmd)
1156 bpy.types.INFO_MT_file_export.remove(menu_func)
1158 if __name__ == "__main__":