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.
24 MMD_SHAPE_GROUP_NAME='_MMD_SHAPE'
25 BASE_SHAPE_NAME='Basis'
26 RIGID_SHAPE_TYPE='rigid_shape_type'
27 RIGID_PROCESS_TYPE='rigid_process_type'
28 RIGID_BONE_NAME='rigid_bone_name'
29 #RIGID_LOCATION='rigid_loation'
30 RIGID_GROUP='ribid_group'
31 RIGID_INTERSECTION_GROUP='rigid_intersection_group'
32 RIGID_WEIGHT='rigid_weight'
33 RIGID_LINEAR_DAMPING='rigid_linear_damping'
34 RIGID_ANGULAR_DAMPING='rigid_angular_damping'
35 RIGID_RESTITUTION='rigid_restitution'
36 RIGID_FRICTION='rigid_friction'
37 CONSTRAINT_A='const_a'
38 CONSTRAINT_B='const_b'
39 CONSTRAINT_POS_MIN='const_pos_min'
40 CONSTRAINT_POS_MAX='const_pos_max'
41 CONSTRAINT_ROT_MIN='const_rot_min'
42 CONSTRAINT_ROT_MAX='const_rot_max'
43 CONSTRAINT_SPRING_POS='const_spring_pos'
44 CONSTRAINT_SPRING_ROT='const_spring_rot'
47 ###############################################################################
49 ###############################################################################
54 from meshio import pmd, englishmap
57 return sys.version_info[0]<3
62 from Blender import Mathutils
70 from bpy.props import *
80 __slots__=['o', 'children']
81 def __init__(self, o):
86 ###############################################################################
87 # Blenderのメッシュをワンスキンメッシュ化する
88 ###############################################################################
89 def near(x, y, EPSILON=1e-5):
91 return d>=-EPSILON and d<=EPSILON
94 class VertexKey(object):
101 'nx', 'ny', 'nz', # 法線
105 def __init__(self, obj, index, x, y, z, nx, ny, nz, u, v):
118 return "<vkey: %f, %f, %f, %f, %f, %f, %f, %f>" % (
119 self.x, self.y, self.z, self.nx, self.ny, self.nz, self.u, self.v)
122 #return int((self.x+self.y+self.z+self.nx+self.ny+self.nz+self.u+self.v)*100)
123 return int((self.x+self.y+self.z)*100)
125 def __eq__(self, rhs):
126 #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)
127 #return near(self.x, rhs.x) and near(self.y, rhs.y) and near(self.z, rhs.z)
128 #return self.x==rhs.x and self.y==rhs.y and self.z==rhs.z
129 return self.obj==rhs.obj and self.index==rhs.index
132 class VertexArray(object):
137 # マテリアル毎に分割したインデックス配列
152 return "<VertexArray %d vertices, %d indexArrays>" % (
153 len(self.vertices), len(self.indexArrays))
157 self.vertices, self.normals, self.uvs,
158 self.b0, self.b1, self.weight)
160 def __getIndex(self, obj, base_index, pos, normal, uv, b0, b1, weight0):
166 pos[0], pos[1], pos[2],
167 normal[0], normal[1], normal[2],
169 if key in self.vertexMap:
171 index=self.vertexMap[key]
173 index=len(self.vertices)
175 self.vertexMap[key]=index
177 self.vertices.append((pos.x, pos.y, pos.z))
178 self.normals.append((normal.x, normal.y, normal.z))
179 self.uvs.append((uv[0], uv[1]))
182 self.weight.append(weight0)
185 if not base_index in self.indexMap:
186 self.indexMap[base_index]=set()
187 self.indexMap[base_index].add(index)
192 def getMappedIndices(self, base_index):
193 return self.indexMap[base_index]
195 def addTriangle(self,
197 base_index0, base_index1, base_index2,
203 weight0, weight1, weight2
205 if not material in self.indexArrays:
206 self.indexArrays[material]=[]
208 index0=self.__getIndex(obj, base_index0, pos0, n0, uv0, b0_0, b1_0, weight0)
209 index1=self.__getIndex(obj, base_index1, pos1, n1, uv1, b0_1, b1_1, weight1)
210 index2=self.__getIndex(obj, base_index2, pos2, n2, uv2, b0_2, b1_2, weight2)
212 self.indexArrays[material]+=[index0, index1, index2]
216 __slots__=['name', 'type', 'offsets']
217 def __init__(self, name, type):
222 def add(self, index, offset):
223 self.offsets.append((index, offset))
227 self.offsets.sort(lambda l, r: l[0]-r[0])
229 self.offsets.sort(key=lambda e: e[0])
232 return "<Morph %s>" % self.name
234 class IKSolver(object):
235 __slots__=['target', 'effector', 'length', 'iterations', 'weight']
236 def __init__(self, target, effector, length, iterations, weight):
238 self.effector=effector
240 self.iterations=iterations
244 class OneSkinMesh(object):
245 __slots__=['obj_index', 'scene', 'vertexArray', 'morphList',
249 def __init__(self, scene):
250 self.vertexArray=VertexArray()
258 return "<OneSkinMesh %s, morph:%d>" % (
262 def addMesh(self, obj):
263 if bl.objectIsVisible(obj):
268 self.__rigidbody(obj)
269 self.__constraint(obj)
271 def __mesh(self, obj):
275 if RIGID_SHAPE_TYPE in obj:
277 if CONSTRAINT_A in obj:
280 print("export", obj.name)
281 mesh=bl.objectGetData(obj)
284 def setWeight(i, name, w):
287 if i in secondWeightMap:
289 if w<secondWeightMap[i]:
293 secondWeightMap[i]=(name, w)
296 weightMap[i]=(name, w)
298 if w>weightMap[i][1]:
300 secondWeightMap[i]=weightMap[i]
301 weightMap[i]=(name, w)
303 secondWeightMap[i]=(name, w)
305 weightMap[i]=(name, w)
308 for name in bl.meshVertexGroupNames(obj):
309 for i, w in mesh.getVertsFromGroup(name, 1):
310 setWeight(i, name, w)
312 for i, v in enumerate(mesh.verts):
314 setWeight(i, obj.vertex_groups[g.group].name, g.weight)
317 for i in xrange(len(mesh.verts)):
318 #for i, name_weight in weightMap.items():
319 if i in secondWeightMap:
320 secondWeightMap[i]=(secondWeightMap[i][0], 1.0-weightMap[i][1])
322 weightMap[i]=(weightMap[i][0], 1.0)
323 secondWeightMap[i]=("", 0)
325 print("no weight vertex")
327 secondWeightMap[i]=("", 0)
329 # メッシュのコピーを生成してオブジェクトの行列を適用する
330 copyMesh, copyObj=bl.objectDuplicate(self.scene, obj)
331 if len(copyMesh.verts)==0:
334 for i, face in enumerate(copyMesh.faces):
335 faceVertexCount=bl.faceVertexCount(face)
336 material=copyMesh.materials[bl.faceMaterialIndex(face)]
337 v=[copyMesh.verts[index] for index in bl.faceVertices(face)]
338 uv=bl.meshFaceUv(copyMesh, i, face)
339 if faceVertexCount==3:
341 self.vertexArray.addTriangle(
342 self.obj_index, material.name,
343 v[0].index, v[1].index, v[2].index,
344 v[0].co, v[1].co, v[2].co,
346 #v0.no, v1.no, v2.no,
351 weightMap[v[0].index][0],
352 weightMap[v[1].index][0],
353 weightMap[v[2].index][0],
354 secondWeightMap[v[0].index][0],
355 secondWeightMap[v[1].index][0],
356 secondWeightMap[v[2].index][0],
357 weightMap[v[0].index][1],
358 weightMap[v[1].index][1],
359 weightMap[v[2].index][1]
361 elif faceVertexCount==4:
363 self.vertexArray.addTriangle(
364 self.obj_index, material.name,
365 v[0].index, v[1].index, v[2].index,
366 v[0].co, v[1].co, v[2].co,
367 #v0.no, v1.no, v2.no,
372 weightMap[v[0].index][0],
373 weightMap[v[1].index][0],
374 weightMap[v[2].index][0],
375 secondWeightMap[v[0].index][0],
376 secondWeightMap[v[1].index][0],
377 secondWeightMap[v[2].index][0],
378 weightMap[v[0].index][1],
379 weightMap[v[1].index][1],
380 weightMap[v[2].index][1]
382 self.vertexArray.addTriangle(
383 self.obj_index, material.name,
384 v[2].index, v[3].index, v[0].index,
385 v[2].co, v[3].co, v[0].co,
386 #v2.no, v3.no, v0.no,
391 weightMap[v[2].index][0],
392 weightMap[v[3].index][0],
393 weightMap[v[0].index][0],
394 secondWeightMap[v[2].index][0],
395 secondWeightMap[v[3].index][0],
396 secondWeightMap[v[0].index][0],
397 weightMap[v[2].index][1],
398 weightMap[v[3].index][1],
399 weightMap[v[0].index][1]
401 bl.objectDelete(self.scene, copyObj)
404 def __skin(self, obj):
405 if not bl.objectHasShapeKey(obj):
409 blenderMesh=bl.objectGetData(obj)
413 vg=bl.meshVertexGroup(obj, MMD_SHAPE_GROUP_NAME)
417 for b in bl.objectShapeKeys(obj):
418 if b.name==BASE_SHAPE_NAME:
419 baseMorph=self.__getOrCreateMorph('base', 0)
424 v=bl.shapeKeyGet(b, index)
425 pos=[v[0], v[1], v[2]]
426 indices=self.vertexArray.getMappedIndices(index)
432 baseMorph.add(i, pos)
433 indexRelativeMap[i]=relativeIndex
438 print(basis.name, len(baseMorph.offsets))
440 if len(baseMorph.offsets)==0:
444 for b in bl.objectShapeKeys(obj):
445 if b.name==BASE_SHAPE_NAME:
449 morph=self.__getOrCreateMorph(b.name, 4)
451 for index, src, dst in zip(
452 xrange(len(blenderMesh.verts)),
455 offset=[dst[0]-src[0], dst[1]-src[1], dst[2]-src[2]]
456 if offset[0]==0 and offset[1]==0 and offset[2]==0:
459 indices=self.vertexArray.getMappedIndices(index)
464 morph.add(indexRelativeMap[i], offset)
467 original=self.morphList[:]
469 for i, v in enumerate(englishmap.skinMap):
474 self.morphList.sort(lambda l, r: getIndex(l)-getIndex(r))
476 self.morphList.sort(key=getIndex)
478 def __rigidbody(self, obj):
481 if not RIGID_SHAPE_TYPE in obj:
483 self.rigidbodies.append(obj)
485 def __constraint(self, obj):
488 if not CONSTRAINT_A in obj:
490 self.constraints.append(obj)
492 def __getOrCreateMorph(self, name, type):
493 for m in self.morphList:
497 self.morphList.append(m)
500 def getVertexCount(self):
501 return len(self.vertexArray.vertices)
505 __slots__=['index', 'name', 'ik_index',
506 'pos', 'tail', 'parent_index', 'tail_index', 'type', 'isConnect']
507 def __init__(self, name, pos, tail):
512 self.parent_index=None
518 def __eq__(self, rhs):
519 return self.index==rhs.index
522 return "<Bone %s %d>" % (self.name, self.type)
524 class BoneBuilder(object):
525 __slots__=['bones', 'boneMap', 'ik_list']
531 def build(self, armatureObj):
535 print("gather bones")
536 armature=bl.objectGetData(armatureObj)
537 for b in armature.bones.values():
544 self.__getBone(bone, b)
546 for b in armature.bones.values():
547 if not b.parent and b.name!='center':
553 self.__getBone(bone, b)
555 print("check connection")
556 for b in armature.bones.values():
558 self.__checkConnection(b, None)
561 pose = bl.objectGetPose(armatureObj)
562 for b in pose.bones.values():
563 for c in b.constraints:
564 if bl.constraintIsIKSolver(c):
568 target=self.__boneByName(bl.ikTarget(c))
575 link=self.__boneByName(b.name)
580 chainLength=bl.ikChainLen(c)
581 for i in range(chainLength):
583 chainBone=self.__boneByName(e.name)
585 chainBone.ik_index=target.index
588 IKSolver(target, link, chainLength,
589 int(bl.ikItration(c) * 0.1),
590 bl.ikRotationWeight(c)
593 def __checkConnection(self, b, p):
594 if bl.boneIsConnected(b):
595 parent=self.__boneByName(p.name)
596 parent.isConnect=True
599 self.__checkConnection(c, b)
601 def sortBy(self, boneMap):
605 original=self.bones[:]
607 for i, k_v in enumerate(boneMap):
608 if k_v[0]==bone.name:
613 self.bones.sort(lambda l, r: getIndex(l)-getIndex(r))
615 self.bones.sort(key=getIndex)
618 for i, b in enumerate(self.bones):
619 src=original.index(b)
622 b.index=sortMap[b.index]
624 b.parent_index=sortMap[b.parent_index]
626 b.tail_index=sortMap[b.tail_index]
628 b.ik_index=sortMap[b.ik_index]
630 def getIndex(self, bone):
631 for i, b in enumerate(self.bones):
636 def indexByName(self, name):
637 return self.getIndex(self.__boneByName(name))
639 def __boneByName(self, name):
640 return self.bones[self.boneMap[name]]
642 def __getBone(self, parent, b):
643 if len(b.children)==0:
647 for i, c in enumerate(b.children):
653 bone.parent_index=parent.index
655 parent.tail_index=bone.index
656 self.__getBone(bone, c)
658 def __addBone(self, bone):
659 bone.index=len(self.bones)
660 self.bones.append(bone)
661 self.boneMap[bone.name]=bone.index
664 class PmdExporter(object):
666 def setup(self, scene):
667 self.armatureObj=None
672 for o in scene.objects:
673 object_node_map[o]=Node(o)
674 for o in scene.objects:
675 #for node in object_node_map.values():
676 node=object_node_map[o]
678 object_node_map[node.o.parent].children.append(node)
681 root=object_node_map[scene.objects.active]
684 self.oneSkinMesh=OneSkinMesh(scene)
685 self.__createOneSkinMesh(root)
686 print(self.oneSkinMesh)
687 self.name=root.o.name
690 self.builder=BoneBuilder()
691 self.builder.build(self.armatureObj)
692 self.builder.sortBy(englishmap.boneMap)
694 for i, v in enumerate(englishmap.boneMap):
695 if v[0]==ik.target.name:
697 return len(englishmap.boneMap)
699 self.builder.ik_list.sort(lambda l, r: getIndex(l)-getIndex(r))
701 self.builder.ik_list.sort(key=getIndex)
703 def __createOneSkinMesh(self, node):
704 ############################################################
705 # search armature modifier
706 ############################################################
707 for m in node.o.modifiers:
708 if bl.modifierIsArmature(m):
709 armatureObj=bl.armatureModifierGetObject(m)
710 if not self.armatureObj:
711 self.armatureObj=armatureObj
712 elif self.armatureObj!=armatureObj:
713 print("warning! found multiple armature. ignored.",
716 if node.o.type.upper()=='MESH':
717 self.oneSkinMesh.addMesh(node.o)
719 for child in node.children:
720 self.__createOneSkinMesh(child)
722 def write(self, path):
725 io.comment="blender export"
729 for pos, normal, uv, b0, b1, weight in self.oneSkinMesh.vertexArray.zip():
730 # convert right-handed z-up to left-handed y-up
740 v.bone0=self.builder.boneMap[b0] if b0 in self.builder.boneMap else 0
741 v.bone1=self.builder.boneMap[b1] if b1 in self.builder.boneMap else 0
742 v.weight0=int(100*weight)
743 v.edge_flag=0 # edge flag, 0: enable edge, 1: not edge
746 vertexCount=self.oneSkinMesh.getVertexCount()
747 for material_name, indices in self.oneSkinMesh.vertexArray.indexArrays.items():
748 m=bl.materialGet(self.scene, material_name)
750 material=io.addMaterial()
752 material.diffuse.r=m.R
753 material.diffuse.g=m.G
754 material.diffuse.b=m.B
755 material.diffuse.a=m.alpha
756 material.sinness=0 if m.spec<1e-5 else m.spec*10
757 material.specular.r=m.specR
758 material.specular.g=m.specG
759 material.specular.b=m.specB
760 material.ambient.r=m.mirR
761 material.ambient.g=m.mirG
762 material.ambient.b=m.mirB
763 material.flag=1 if m.enableSSS else 0
765 material.diffuse.r=m.diffuse_color[0]
766 material.diffuse.g=m.diffuse_color[1]
767 material.diffuse.b=m.diffuse_color[2]
768 material.diffuse.a=m.alpha
769 material.sinness=0 if m.specular_hardness<1e-5 else m.specular_hardness*10
770 material.specular.r=m.specular_color[0]
771 material.specular.g=m.specular_color[1]
772 material.specular.b=m.specular_color[2]
773 material.ambient.r=m.mirror_color[0]
774 material.ambient.g=m.mirror_color[1]
775 material.ambient.b=m.mirror_color[2]
776 material.flag=1 if m.subsurface_scattering.enabled else 0
778 material.vertex_count=len(indices)
779 material.toon_index=0
784 assert(i<vertexCount)
785 for i in xrange(0, len(indices), 3):
787 io.indices.append(indices[i])
788 io.indices.append(indices[i+1])
789 io.indices.append(indices[i+2])
793 for i, b in enumerate(self.builder.bones):
797 boneNameMap[b.name]=i
798 v=englishmap.getUnicodeBoneName(b.name)
800 cp932=v[1].encode('cp932')
801 assert(len(cp932)<20)
805 bone_english_name=b.name
806 assert(len(bone_english_name)<20)
807 bone.english_name=bone_english_name
812 b.ik_index=self.builder.indexByName('eyes')
818 bone.parent_index=b.parent_index if b.parent_index!=None else 0xFFFF
821 if b.tail_index!=None:
825 bone.tail_index=b.tail_index
829 bone.ik_index=b.ik_index
831 # convert right-handed z-up to left-handed y-up
832 bone.pos.x=b.pos[0] if not near(b.pos[0], 0) else 0
833 bone.pos.y=b.pos[2] if not near(b.pos[2], 0) else 0
834 bone.pos.z=b.pos[1] if not near(b.pos[1], 0) else 0
837 for ik in self.builder.ik_list:
839 solver.index=self.builder.getIndex(ik.target)
840 solver.target=self.builder.getIndex(ik.effector)
841 solver.length=ik.length
842 b=self.builder.bones[ik.effector.parent_index]
843 for i in xrange(solver.length):
844 solver.children.append(self.builder.getIndex(b))
845 b=self.builder.bones[b.parent_index]
846 solver.iterations=ik.iterations
847 solver.weight=ik.weight
850 for i, m in enumerate(self.oneSkinMesh.morphList):
854 v=englishmap.getUnicodeSkinName(m.name)
856 cp932=v[1].encode('cp932')
858 morph.setEnglishName(m.name.encode('cp932'))
861 for index, offset in m.offsets:
862 # convert right-handed z-up to left-handed y-up
863 morph.append(index, offset[0], offset[2], offset[1])
864 morph.vertex_count=len(m.offsets)
868 for i, m in enumerate(self.oneSkinMesh.morphList):
870 io.face_list.append(i)
871 for i, m in enumerate(self.oneSkinMesh.morphList):
873 io.face_list.append(i)
874 for i, m in enumerate(self.oneSkinMesh.morphList):
876 io.face_list.append(i)
877 for i, m in enumerate(self.oneSkinMesh.morphList):
879 io.face_list.append(i)
882 def createBoneDisplayName(name, english):
883 boneDisplayName=io.addBoneDisplayName()
885 boneDisplayName.name=name.decode('utf-8').encode('cp932')
886 boneDisplayName.english_name=english
888 boneDisplayName.setName(name.encode('cp932'))
889 boneDisplayName.setEnglishName(english.encode('cp932'))
890 boneDisplayName=createBoneDisplayName("IK\n", "IK\n")
891 boneDisplayName=createBoneDisplayName("体(上)\n", "Body[u]\n")
892 boneDisplayName=createBoneDisplayName("髪\n", "Hair\n")
893 boneDisplayName=createBoneDisplayName("腕\n", "Arms\n")
894 boneDisplayName=createBoneDisplayName("指\n", "Fingers\n")
895 boneDisplayName=createBoneDisplayName("体(下)\n", "Body[l]\n")
896 boneDisplayName=createBoneDisplayName("足\n", "Legs\n")
897 for i, b in enumerate(self.builder.bones):
902 io.addBoneDisplay(i, getBoneDisplayGroup(b))
905 io.english_name="blender export"
906 io.english_coment="blender export"
910 io.getToonTexture(i).name="toon%02d.bmp\n" % i
914 for i, obj in enumerate(self.oneSkinMesh.rigidbodies):
915 rigidBody=io.addRigidBody()
916 rigidBody.setName(obj.name.encode('cp932'))
917 rigidNameMap[obj.name]=i
918 boneIndex=boneNameMap[obj[RIGID_BONE_NAME]]
921 bone=self.builder.bones[0]
923 bone=self.builder.bones[boneIndex]
924 rigidBody.boneIndex=boneIndex
925 #rigidBody.position.x=obj[RIGID_LOCATION][0]
926 #rigidBody.position.y=obj[RIGID_LOCATION][1]
927 #rigidBody.position.z=obj[RIGID_LOCATION][2]
928 rigidBody.position.x=obj.location.x-bone.pos[0]
929 rigidBody.position.y=obj.location.z-bone.pos[2]
930 rigidBody.position.z=obj.location.y-bone.pos[2]
931 rigidBody.rotation.x=-obj.rotation_euler[0]
932 rigidBody.rotation.y=-obj.rotation_euler[2]
933 rigidBody.rotation.z=-obj.rotation_euler[1]
934 rigidBody.processType=obj[RIGID_PROCESS_TYPE]
935 rigidBody.group=obj[RIGID_GROUP]
936 rigidBody.target=obj[RIGID_INTERSECTION_GROUP]
937 rigidBody.weight=obj[RIGID_WEIGHT]
938 rigidBody.linearDamping=obj[RIGID_LINEAR_DAMPING]
939 rigidBody.angularDamping=obj[RIGID_ANGULAR_DAMPING]
940 rigidBody.restitution=obj[RIGID_RESTITUTION]
941 rigidBody.friction=obj[RIGID_FRICTION]
942 if obj[RIGID_SHAPE_TYPE]==0:
943 rigidBody.shapeType=pmd.SHAPE_SPHERE
944 rigidBody.w=obj.scale[0]
945 elif obj[RIGID_SHAPE_TYPE]==1:
946 rigidBody.shapeType=pmd.SHAPE_CAPSULE
947 rigidBody.w=obj.scale[0]
948 rigidBody.h=obj.scale[2]
949 elif obj[RIGID_SHAPE_TYPE]==2:
950 rigidBody.shapeType=pmd.SHAPE_BOX
951 rigidBody.w=obj.scale[0]
952 rigidBody.d=obj.scale[1]
953 rigidBody.h=obj.scale[2]
956 for obj in self.oneSkinMesh.constraints:
957 constraint=io.addConstraint()
958 constraint.setName(obj.name[1:].encode('cp932'))
959 constraint.rigidA=rigidNameMap[obj[CONSTRAINT_A]]
960 constraint.rigidB=rigidNameMap[obj[CONSTRAINT_B]]
961 constraint.pos.x=obj.location[0]
962 constraint.pos.y=obj.location[2]
963 constraint.pos.z=obj.location[1]
964 constraint.rot.x=-obj.rotation_euler[0]
965 constraint.rot.y=-obj.rotation_euler[2]
966 constraint.rot.z=-obj.rotation_euler[1]
967 constraint.constraintPosMin.x=obj[CONSTRAINT_POS_MIN][0]
968 constraint.constraintPosMin.y=obj[CONSTRAINT_POS_MIN][1]
969 constraint.constraintPosMin.z=obj[CONSTRAINT_POS_MIN][2]
970 constraint.constraintPosMax.x=obj[CONSTRAINT_POS_MAX][0]
971 constraint.constraintPosMax.y=obj[CONSTRAINT_POS_MAX][1]
972 constraint.constraintPosMax.z=obj[CONSTRAINT_POS_MAX][2]
973 constraint.constraintRotMin.x=obj[CONSTRAINT_ROT_MIN][0]
974 constraint.constraintRotMin.y=obj[CONSTRAINT_ROT_MIN][1]
975 constraint.constraintRotMin.z=obj[CONSTRAINT_ROT_MIN][2]
976 constraint.constraintRotMax.x=obj[CONSTRAINT_ROT_MAX][0]
977 constraint.constraintRotMax.y=obj[CONSTRAINT_ROT_MAX][1]
978 constraint.constraintRotMax.z=obj[CONSTRAINT_ROT_MAX][2]
979 constraint.springPos.x=obj[CONSTRAINT_SPRING_POS][0]
980 constraint.springPos.y=obj[CONSTRAINT_SPRING_POS][1]
981 constraint.springPos.z=obj[CONSTRAINT_SPRING_POS][2]
982 constraint.springRot.x=obj[CONSTRAINT_SPRING_ROT][0]
983 constraint.springRot.y=obj[CONSTRAINT_SPRING_ROT][1]
984 constraint.springRot.z=obj[CONSTRAINT_SPRING_ROT][2]
988 return io.write(path)
991 def getBoneDisplayGroup(bone):
994 "necktie IK", "hair IK_L", "hair IK_R", "leg IK_L", "leg IK_R",
995 "toe IK_L", "toe IK_R",
998 "upper body", "neck", "head", "eye_L", "eye_R",
999 "necktie1", "necktie2", "necktie3", "eyes",
1000 "eyelight_L", "eyelight_R",
1003 "front hair1", "front hair2", "front hair3",
1004 "hair1_L", "hair2_L", "hair3_L",
1005 "hair4_L", "hair5_L", "hair6_L",
1006 "hair1_R", "hair2_R", "hair3_R",
1007 "hair4_R", "hair5_R", "hair6_R",
1010 "shoulder_L", "arm_L", "arm twist_L", "elbow_L",
1011 "wrist twist_L", "wrist_L", "sleeve_L",
1012 "shoulder_R", "arm_R", "arm twist_R", "elbow_R",
1013 "wrist twist_R", "wrist_R", "sleeve_R",
1016 "thumb1_L", "thumb2_L", "fore1_L", "fore2_L", "fore3_L",
1017 "middle1_L", "middle2_L", "middle3_L",
1018 "third1_L", "third2_L", "third3_L",
1019 "little1_L", "little2_L", "little3_L",
1020 "thumb1_R", "thumb2_R", "fore1_R", "fore2_R", "fore3_R",
1021 "middle1_R", "middle2_R", "middle3_R",
1022 "third1_R", "third2_R", "third3_R",
1023 "little1_R", "little2_R", "little3_R",
1026 "lower body", "waist accessory",
1027 "front skirt_L", "back skirt_L",
1028 "front skirt_R", "back skirt_R",
1031 "leg_L", "knee_L", "ankle_L",
1032 "leg_R", "knee_R", "ankle_R",
1036 for g in boneGroups:
1044 def __execute(filename, scene):
1045 if not scene.objects.active:
1046 print("abort. no active object.")
1049 bl.progress_start('pmd_export')
1050 active=bl.objectGetActive(scene)
1051 exporter=PmdExporter()
1052 exporter.setup(scene)
1053 exporter.write(filename)
1054 bl.objectActivate(scene, active)
1055 bl.progress_finish()
1060 def execute_24(filename):
1061 filename=filename.decode(bl.INTERNAL_ENCODING)
1062 print("pmd exporter: %s" % filename)
1064 Blender.Window.WaitCursor(1)
1065 t = Blender.sys.time()
1067 scene = bpy.data.scenes.active
1068 __execute(filename, scene)
1070 print('finished in %.2f seconds' % (Blender.sys.time()-t))
1072 Blender.Window.WaitCursor(0)
1074 Blender.Window.FileSelector(
1076 'Export Metasequoia PMD',
1077 Blender.sys.makename(ext='.pmd'))
1081 def execute_25(*args):
1085 class EXPORT_OT_pmd(bpy.types.Operator):
1086 '''Save a Metasequoia PMD file.'''
1087 bl_idname = "export_scene.pmd"
1088 bl_label = 'Export PMD'
1090 # List of operator properties, the attributes will be assigned
1091 # to the class instance from the operator settings before calling.
1093 path = StringProperty(
1095 description="File path used for exporting the PMD file",
1099 filename = StringProperty(
1101 description="Name of the file.")
1102 directory = StringProperty(
1104 description="Directory of the file.")
1106 check_existing = BoolProperty(
1107 name="Check Existing",
1108 description="Check and warn on overwriting existing files",
1110 options=set('HIDDEN'))
1112 def execute(self, context):
1114 self.properties.path,
1119 def invoke(self, context, event):
1121 wm.add_fileselect(self)
1122 return 'RUNNING_MODAL'
1125 def menu_func(self, context):
1126 #default_path=bpy.data.filename.replace(".blend", ".pmd")
1127 self.layout.operator(
1128 EXPORT_OT_pmd.bl_idname,
1129 text="Miku Miku Dance Model(.pmd)")#.path=default_path
1132 bpy.types.register(EXPORT_OT_pmd)
1133 bpy.types.INFO_MT_file_export.append(menu_func)
1136 bpy.types.unregister(EXPORT_OT_pmd)
1137 bpy.types.INFO_MT_file_export.remove(menu_func)
1139 if __name__ == "__main__":