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_NAME='constraint_name'
39 CONSTRAINT_A='const_a'
40 CONSTRAINT_B='const_b'
41 CONSTRAINT_POS_MIN='const_pos_min'
42 CONSTRAINT_POS_MAX='const_pos_max'
43 CONSTRAINT_ROT_MIN='const_rot_min'
44 CONSTRAINT_ROT_MAX='const_rot_max'
45 CONSTRAINT_SPRING_POS='const_spring_pos'
46 CONSTRAINT_SPRING_ROT='const_spring_rot'
49 ###############################################################################
51 ###############################################################################
56 from meshio import pmd, englishmap
59 return sys.version_info[0]<3
64 from Blender import Mathutils
72 from bpy.props import *
82 __slots__=['o', 'children']
83 def __init__(self, o):
88 ###############################################################################
89 # Blenderのメッシュをワンスキンメッシュ化する
90 ###############################################################################
91 def near(x, y, EPSILON=1e-5):
93 return d>=-EPSILON and d<=EPSILON
96 class VertexKey(object):
103 'nx', 'ny', 'nz', # 法線
107 def __init__(self, obj, index, x, y, z, nx, ny, nz, u, v):
120 return "<vkey: %f, %f, %f, %f, %f, %f, %f, %f>" % (
121 self.x, self.y, self.z, self.nx, self.ny, self.nz, self.u, self.v)
124 #return int((self.x+self.y+self.z+self.nx+self.ny+self.nz+self.u+self.v)*100)
125 return int((self.x+self.y+self.z)*100)
127 def __eq__(self, rhs):
128 #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)
129 #return near(self.x, rhs.x) and near(self.y, rhs.y) and near(self.z, rhs.z)
130 #return self.x==rhs.x and self.y==rhs.y and self.z==rhs.z
131 return self.obj==rhs.obj and self.index==rhs.index
134 class VertexArray(object):
139 # マテリアル毎に分割したインデックス配列
154 return "<VertexArray %d vertices, %d indexArrays>" % (
155 len(self.vertices), len(self.indexArrays))
159 self.vertices, self.normals, self.uvs,
160 self.b0, self.b1, self.weight)
162 def __getIndex(self, obj, base_index, pos, normal, uv, b0, b1, weight0):
168 pos[0], pos[1], pos[2],
169 normal[0], normal[1], normal[2],
171 if key in self.vertexMap:
173 index=self.vertexMap[key]
175 index=len(self.vertices)
177 self.vertexMap[key]=index
179 self.vertices.append((pos.x, pos.y, pos.z))
180 self.normals.append((normal.x, normal.y, normal.z))
181 self.uvs.append((uv[0], uv[1]))
184 self.weight.append(weight0)
187 if not base_index in self.indexMap:
188 self.indexMap[base_index]=set()
189 self.indexMap[base_index].add(index)
194 def getMappedIndices(self, base_index):
195 return self.indexMap[base_index]
197 def addTriangle(self,
199 base_index0, base_index1, base_index2,
205 weight0, weight1, weight2
207 if not material in self.indexArrays:
208 self.indexArrays[material]=[]
210 index0=self.__getIndex(obj, base_index0, pos0, n0, uv0, b0_0, b1_0, weight0)
211 index1=self.__getIndex(obj, base_index1, pos1, n1, uv1, b0_1, b1_1, weight1)
212 index2=self.__getIndex(obj, base_index2, pos2, n2, uv2, b0_2, b1_2, weight2)
214 self.indexArrays[material]+=[index0, index1, index2]
218 __slots__=['name', 'type', 'offsets']
219 def __init__(self, name, type):
224 def add(self, index, offset):
225 self.offsets.append((index, offset))
229 self.offsets.sort(lambda l, r: l[0]-r[0])
231 self.offsets.sort(key=lambda e: e[0])
234 return "<Morph %s>" % self.name
236 class IKSolver(object):
237 __slots__=['target', 'effector', 'length', 'iterations', 'weight']
238 def __init__(self, target, effector, length, iterations, weight):
240 self.effector=effector
242 self.iterations=iterations
246 class OneSkinMesh(object):
247 __slots__=['obj_index', 'scene', 'vertexArray', 'morphList',
251 def __init__(self, scene):
252 self.vertexArray=VertexArray()
260 return "<OneSkinMesh %s, morph:%d>" % (
264 def addMesh(self, obj):
265 if bl.objectIsVisible(obj):
270 self.__rigidbody(obj)
271 self.__constraint(obj)
273 def __mesh(self, obj):
277 if RIGID_SHAPE_TYPE in obj:
279 if CONSTRAINT_A in obj:
282 print("export", obj.name)
283 mesh=bl.objectGetData(obj)
286 def setWeight(i, name, w):
289 if i in secondWeightMap:
291 if w<secondWeightMap[i]:
295 secondWeightMap[i]=(name, w)
298 weightMap[i]=(name, w)
300 if w>weightMap[i][1]:
302 secondWeightMap[i]=weightMap[i]
303 weightMap[i]=(name, w)
305 secondWeightMap[i]=(name, w)
307 weightMap[i]=(name, w)
310 for name in bl.meshVertexGroupNames(obj):
311 for i, w in mesh.getVertsFromGroup(name, 1):
312 setWeight(i, name, w)
314 for i, v in enumerate(mesh.verts):
316 setWeight(i, obj.vertex_groups[g.group].name, g.weight)
319 for i in xrange(len(mesh.verts)):
320 #for i, name_weight in weightMap.items():
321 if i in secondWeightMap:
322 secondWeightMap[i]=(secondWeightMap[i][0], 1.0-weightMap[i][1])
324 weightMap[i]=(weightMap[i][0], 1.0)
325 secondWeightMap[i]=("", 0)
327 print("no weight vertex")
329 secondWeightMap[i]=("", 0)
331 # メッシュのコピーを生成してオブジェクトの行列を適用する
332 copyMesh, copyObj=bl.objectDuplicate(self.scene, obj)
333 if len(copyMesh.verts)==0:
336 for i, face in enumerate(copyMesh.faces):
337 faceVertexCount=bl.faceVertexCount(face)
338 material=copyMesh.materials[bl.faceMaterialIndex(face)]
339 v=[copyMesh.verts[index] for index in bl.faceVertices(face)]
340 uv=bl.meshFaceUv(copyMesh, i, face)
341 if faceVertexCount==3:
343 self.vertexArray.addTriangle(
344 self.obj_index, material.name,
345 v[0].index, v[1].index, v[2].index,
346 v[0].co, v[1].co, v[2].co,
348 #v0.no, v1.no, v2.no,
353 weightMap[v[0].index][0],
354 weightMap[v[1].index][0],
355 weightMap[v[2].index][0],
356 secondWeightMap[v[0].index][0],
357 secondWeightMap[v[1].index][0],
358 secondWeightMap[v[2].index][0],
359 weightMap[v[0].index][1],
360 weightMap[v[1].index][1],
361 weightMap[v[2].index][1]
363 elif faceVertexCount==4:
365 self.vertexArray.addTriangle(
366 self.obj_index, material.name,
367 v[0].index, v[1].index, v[2].index,
368 v[0].co, v[1].co, v[2].co,
369 #v0.no, v1.no, v2.no,
374 weightMap[v[0].index][0],
375 weightMap[v[1].index][0],
376 weightMap[v[2].index][0],
377 secondWeightMap[v[0].index][0],
378 secondWeightMap[v[1].index][0],
379 secondWeightMap[v[2].index][0],
380 weightMap[v[0].index][1],
381 weightMap[v[1].index][1],
382 weightMap[v[2].index][1]
384 self.vertexArray.addTriangle(
385 self.obj_index, material.name,
386 v[2].index, v[3].index, v[0].index,
387 v[2].co, v[3].co, v[0].co,
388 #v2.no, v3.no, v0.no,
393 weightMap[v[2].index][0],
394 weightMap[v[3].index][0],
395 weightMap[v[0].index][0],
396 secondWeightMap[v[2].index][0],
397 secondWeightMap[v[3].index][0],
398 secondWeightMap[v[0].index][0],
399 weightMap[v[2].index][1],
400 weightMap[v[3].index][1],
401 weightMap[v[0].index][1]
403 bl.objectDelete(self.scene, copyObj)
406 def __skin(self, obj):
407 if not bl.objectHasShapeKey(obj):
411 blenderMesh=bl.objectGetData(obj)
415 vg=bl.meshVertexGroup(obj, MMD_SHAPE_GROUP_NAME)
419 for b in bl.objectShapeKeys(obj):
420 if b.name==BASE_SHAPE_NAME:
421 baseMorph=self.__getOrCreateMorph('base', 0)
426 v=bl.shapeKeyGet(b, index)
427 pos=[v[0], v[1], v[2]]
428 indices=self.vertexArray.getMappedIndices(index)
434 baseMorph.add(i, pos)
435 indexRelativeMap[i]=relativeIndex
440 print(basis.name, len(baseMorph.offsets))
442 if len(baseMorph.offsets)==0:
446 for b in bl.objectShapeKeys(obj):
447 if b.name==BASE_SHAPE_NAME:
451 morph=self.__getOrCreateMorph(b.name, 4)
453 for index, src, dst in zip(
454 xrange(len(blenderMesh.verts)),
457 offset=[dst[0]-src[0], dst[1]-src[1], dst[2]-src[2]]
458 if offset[0]==0 and offset[1]==0 and offset[2]==0:
461 indices=self.vertexArray.getMappedIndices(index)
466 morph.add(indexRelativeMap[i], offset)
469 original=self.morphList[:]
471 for i, v in enumerate(englishmap.skinMap):
476 self.morphList.sort(lambda l, r: getIndex(l)-getIndex(r))
478 self.morphList.sort(key=getIndex)
480 def __rigidbody(self, obj):
483 if not RIGID_SHAPE_TYPE in obj:
485 self.rigidbodies.append(obj)
487 def __constraint(self, obj):
490 if not CONSTRAINT_A in obj:
492 self.constraints.append(obj)
494 def __getOrCreateMorph(self, name, type):
495 for m in self.morphList:
499 self.morphList.append(m)
502 def getVertexCount(self):
503 return len(self.vertexArray.vertices)
507 __slots__=['index', 'name', 'ik_index',
508 'pos', 'tail', 'parent_index', 'tail_index', 'type', 'isConnect']
509 def __init__(self, name, pos, tail):
514 self.parent_index=None
520 def __eq__(self, rhs):
521 return self.index==rhs.index
524 return "<Bone %s %d>" % (self.name, self.type)
526 class BoneBuilder(object):
527 __slots__=['bones', 'boneMap', 'ik_list', ]
533 def build(self, armatureObj):
537 print("gather bones")
538 armature=bl.objectGetData(armatureObj)
539 for b in armature.bones.values():
546 self.__getBone(bone, b)
548 for b in armature.bones.values():
549 if not b.parent and b.name!='center':
555 self.__getBone(bone, b)
557 print("check connection")
558 for b in armature.bones.values():
560 self.__checkConnection(b, None)
563 pose = bl.objectGetPose(armatureObj)
564 for b in pose.bones.values():
565 for c in b.constraints:
566 if bl.constraintIsIKSolver(c):
570 target=self.__boneByName(bl.ikTarget(c))
577 link=self.__boneByName(b.name)
582 chainLength=bl.ikChainLen(c)
583 for i in range(chainLength):
585 chainBone=self.__boneByName(e.name)
587 chainBone.ik_index=target.index
590 IKSolver(target, link, chainLength,
591 int(bl.ikItration(c) * 0.1),
592 bl.ikRotationWeight(c)
600 for i, v in enumerate(englishmap.boneMap):
601 if v[0]==ik.target.name:
603 return len(englishmap.boneMap)
605 self.ik_list.sort(lambda l, r: getIndex(l)-getIndex(r))
607 self.ik_list.sort(key=getIndex)
609 def __checkConnection(self, b, p):
610 if bl.boneIsConnected(b):
611 parent=self.__boneByName(p.name)
612 parent.isConnect=True
615 self.__checkConnection(c, b)
621 boneMap=englishmap.boneMap
622 original=self.bones[:]
624 for i, k_v in enumerate(boneMap):
625 if k_v[0]==bone.name:
630 self.bones.sort(lambda l, r: getIndex(l)-getIndex(r))
632 self.bones.sort(key=getIndex)
635 for i, b in enumerate(self.bones):
636 src=original.index(b)
639 b.index=sortMap[b.index]
641 b.parent_index=sortMap[b.parent_index]
643 b.tail_index=sortMap[b.tail_index]
645 b.ik_index=sortMap[b.ik_index]
653 if b.parent_index==None:
654 b.parent_index=0xFFFF
656 if b.type==6 or b.type==7:
658 parent=self.bones[b.parent_index]
659 parent.tail_index=b.index
662 if b.tail_index==None:
667 def getIndex(self, bone):
668 for i, b in enumerate(self.bones):
673 def indexByName(self, name):
677 return self.getIndex(self.__boneByName(name))
679 def __boneByName(self, name):
680 return self.boneMap[name]
682 def __getBone(self, parent, b):
683 if len(b.children)==0:
687 for i, c in enumerate(b.children):
693 bone.parent_index=parent.index
695 parent.tail_index=bone.index
696 self.__getBone(bone, c)
698 def __addBone(self, bone):
699 bone.index=len(self.bones)
700 self.bones.append(bone)
701 self.boneMap[bone.name]=bone
704 class PmdExporter(object):
706 def setup(self, scene):
707 self.armatureObj=None
712 for o in scene.objects:
713 object_node_map[o]=Node(o)
714 for o in scene.objects:
715 #for node in object_node_map.values():
716 node=object_node_map[o]
718 object_node_map[node.o.parent].children.append(node)
721 root=object_node_map[scene.objects.active]
724 self.oneSkinMesh=OneSkinMesh(scene)
725 self.__createOneSkinMesh(root)
726 print(self.oneSkinMesh)
727 self.name=root.o.name
730 self.builder=BoneBuilder()
731 self.builder.build(self.armatureObj)
733 def __createOneSkinMesh(self, node):
734 ############################################################
735 # search armature modifier
736 ############################################################
737 for m in node.o.modifiers:
738 if bl.modifierIsArmature(m):
739 armatureObj=bl.armatureModifierGetObject(m)
740 if not self.armatureObj:
741 self.armatureObj=armatureObj
742 elif self.armatureObj!=armatureObj:
743 print("warning! found multiple armature. ignored.",
746 if node.o.type.upper()=='MESH':
747 self.oneSkinMesh.addMesh(node.o)
749 for child in node.children:
750 self.__createOneSkinMesh(child)
752 def write(self, path):
755 io.comment="blender export"
759 for pos, normal, uv, b0, b1, weight in self.oneSkinMesh.vertexArray.zip():
760 # convert right-handed z-up to left-handed y-up
770 v.bone0=self.builder.indexByName(b0)
771 v.bone1=self.builder.indexByName(b1)
772 v.weight0=int(100*weight)
773 v.edge_flag=0 # edge flag, 0: enable edge, 1: not edge
776 vertexCount=self.oneSkinMesh.getVertexCount()
777 for material_name, indices in self.oneSkinMesh.vertexArray.indexArrays.items():
778 m=bl.materialGet(self.scene, material_name)
780 material=io.addMaterial()
782 material.diffuse.r=m.R
783 material.diffuse.g=m.G
784 material.diffuse.b=m.B
785 material.diffuse.a=m.alpha
786 material.sinness=0 if m.spec<1e-5 else m.spec*10
787 material.specular.r=m.specR
788 material.specular.g=m.specG
789 material.specular.b=m.specB
790 material.ambient.r=m.mirR
791 material.ambient.g=m.mirG
792 material.ambient.b=m.mirB
793 material.flag=1 if m.enableSSS else 0
795 material.diffuse.r=m.diffuse_color[0]
796 material.diffuse.g=m.diffuse_color[1]
797 material.diffuse.b=m.diffuse_color[2]
798 material.diffuse.a=m.alpha
799 material.sinness=0 if m.specular_hardness<1e-5 else m.specular_hardness*10
800 material.specular.r=m.specular_color[0]
801 material.specular.g=m.specular_color[1]
802 material.specular.b=m.specular_color[2]
803 material.ambient.r=m.mirror_color[0]
804 material.ambient.g=m.mirror_color[1]
805 material.ambient.b=m.mirror_color[2]
806 material.flag=1 if m.subsurface_scattering.enabled else 0
808 material.vertex_count=len(indices)
809 material.toon_index=0
814 assert(i<vertexCount)
815 for i in xrange(0, len(indices), 3):
817 io.indices.append(indices[i])
818 io.indices.append(indices[i+1])
819 io.indices.append(indices[i+2])
823 for i, b in enumerate(self.builder.bones):
827 boneNameMap[b.name]=i
828 v=englishmap.getUnicodeBoneName(b.name)
830 cp932=v[1].encode('cp932')
831 assert(len(cp932)<20)
835 bone_english_name=b.name
836 assert(len(bone_english_name)<20)
837 bone.english_name=bone_english_name
842 b.ik_index=self.builder.indexByName('eyes')
847 bone.parent_index=b.parent_index
848 bone.tail_index=b.tail_index
849 bone.ik_index=b.ik_index
851 # convert right-handed z-up to left-handed y-up
852 bone.pos.x=b.pos[0] if not near(b.pos[0], 0) else 0
853 bone.pos.y=b.pos[2] if not near(b.pos[2], 0) else 0
854 bone.pos.z=b.pos[1] if not near(b.pos[1], 0) else 0
857 for ik in self.builder.ik_list:
859 solver.index=self.builder.getIndex(ik.target)
860 solver.target=self.builder.getIndex(ik.effector)
861 solver.length=ik.length
862 b=self.builder.bones[ik.effector.parent_index]
863 for i in xrange(solver.length):
864 solver.children.append(self.builder.getIndex(b))
865 b=self.builder.bones[b.parent_index]
866 solver.iterations=ik.iterations
867 solver.weight=ik.weight
870 for i, m in enumerate(self.oneSkinMesh.morphList):
874 v=englishmap.getUnicodeSkinName(m.name)
876 cp932=v[1].encode('cp932')
878 morph.setEnglishName(m.name.encode('cp932'))
881 for index, offset in m.offsets:
882 # convert right-handed z-up to left-handed y-up
883 morph.append(index, offset[0], offset[2], offset[1])
884 morph.vertex_count=len(m.offsets)
888 for i, m in enumerate(self.oneSkinMesh.morphList):
890 io.face_list.append(i)
891 for i, m in enumerate(self.oneSkinMesh.morphList):
893 io.face_list.append(i)
894 for i, m in enumerate(self.oneSkinMesh.morphList):
896 io.face_list.append(i)
897 for i, m in enumerate(self.oneSkinMesh.morphList):
899 io.face_list.append(i)
902 def createBoneDisplayName(name, english):
903 boneDisplayName=io.addBoneDisplayName()
905 boneDisplayName.name=name.decode('utf-8').encode('cp932')
906 boneDisplayName.english_name=english
908 boneDisplayName.setName(name.encode('cp932'))
909 boneDisplayName.setEnglishName(english.encode('cp932'))
910 boneDisplayName=createBoneDisplayName("IK\n", "IK\n")
911 boneDisplayName=createBoneDisplayName("体(上)\n", "Body[u]\n")
912 boneDisplayName=createBoneDisplayName("髪\n", "Hair\n")
913 boneDisplayName=createBoneDisplayName("腕\n", "Arms\n")
914 boneDisplayName=createBoneDisplayName("指\n", "Fingers\n")
915 boneDisplayName=createBoneDisplayName("体(下)\n", "Body[l]\n")
916 boneDisplayName=createBoneDisplayName("足\n", "Legs\n")
917 for i, b in enumerate(self.builder.bones):
922 io.addBoneDisplay(i, getBoneDisplayGroup(b))
925 io.english_name="blender export"
926 io.english_coment="blender export"
930 io.getToonTexture(i).name="toon%02d.bmp\n" % i
934 for i, obj in enumerate(self.oneSkinMesh.rigidbodies):
935 rigidBody=io.addRigidBody()
936 rigidBody.setName(obj.name.encode('cp932'))
937 rigidNameMap[obj.name]=i
938 boneIndex=boneNameMap[obj[RIGID_BONE_NAME]]
941 bone=self.builder.bones[0]
943 bone=self.builder.bones[boneIndex]
944 rigidBody.boneIndex=boneIndex
945 #rigidBody.position.x=obj[RIGID_LOCATION][0]
946 #rigidBody.position.y=obj[RIGID_LOCATION][1]
947 #rigidBody.position.z=obj[RIGID_LOCATION][2]
948 rigidBody.position.x=obj.location.x-bone.pos[0]
949 rigidBody.position.y=obj.location.z-bone.pos[2]
950 rigidBody.position.z=obj.location.y-bone.pos[1]
951 rigidBody.rotation.x=-obj.rotation_euler[0]
952 rigidBody.rotation.y=-obj.rotation_euler[2]
953 rigidBody.rotation.z=-obj.rotation_euler[1]
954 rigidBody.processType=obj[RIGID_PROCESS_TYPE]
955 rigidBody.group=obj[RIGID_GROUP]
956 rigidBody.target=obj[RIGID_INTERSECTION_GROUP]
957 rigidBody.weight=obj[RIGID_WEIGHT]
958 rigidBody.linearDamping=obj[RIGID_LINEAR_DAMPING]
959 rigidBody.angularDamping=obj[RIGID_ANGULAR_DAMPING]
960 rigidBody.restitution=obj[RIGID_RESTITUTION]
961 rigidBody.friction=obj[RIGID_FRICTION]
962 if obj[RIGID_SHAPE_TYPE]==0:
963 rigidBody.shapeType=pmd.SHAPE_SPHERE
964 rigidBody.w=obj.scale[0]
965 elif obj[RIGID_SHAPE_TYPE]==1:
966 rigidBody.shapeType=pmd.SHAPE_BOX
967 rigidBody.w=obj.scale[0]
968 rigidBody.d=obj.scale[1]
969 rigidBody.h=obj.scale[2]
970 elif obj[RIGID_SHAPE_TYPE]==2:
971 rigidBody.shapeType=pmd.SHAPE_CAPSULE
972 rigidBody.w=obj.scale[0]
973 rigidBody.h=obj.scale[2]
976 for obj in self.oneSkinMesh.constraints:
977 constraint=io.addConstraint()
978 constraint.setName(obj[CONSTRAINT_NAME].encode('cp932'))
979 constraint.rigidA=rigidNameMap[obj[CONSTRAINT_A]]
980 constraint.rigidB=rigidNameMap[obj[CONSTRAINT_B]]
981 constraint.pos.x=obj.location[0]
982 constraint.pos.y=obj.location[2]
983 constraint.pos.z=obj.location[1]
984 constraint.rot.x=-obj.rotation_euler[0]
985 constraint.rot.y=-obj.rotation_euler[2]
986 constraint.rot.z=-obj.rotation_euler[1]
987 constraint.constraintPosMin.x=obj[CONSTRAINT_POS_MIN][0]
988 constraint.constraintPosMin.y=obj[CONSTRAINT_POS_MIN][1]
989 constraint.constraintPosMin.z=obj[CONSTRAINT_POS_MIN][2]
990 constraint.constraintPosMax.x=obj[CONSTRAINT_POS_MAX][0]
991 constraint.constraintPosMax.y=obj[CONSTRAINT_POS_MAX][1]
992 constraint.constraintPosMax.z=obj[CONSTRAINT_POS_MAX][2]
993 constraint.constraintRotMin.x=obj[CONSTRAINT_ROT_MIN][0]
994 constraint.constraintRotMin.y=obj[CONSTRAINT_ROT_MIN][1]
995 constraint.constraintRotMin.z=obj[CONSTRAINT_ROT_MIN][2]
996 constraint.constraintRotMax.x=obj[CONSTRAINT_ROT_MAX][0]
997 constraint.constraintRotMax.y=obj[CONSTRAINT_ROT_MAX][1]
998 constraint.constraintRotMax.z=obj[CONSTRAINT_ROT_MAX][2]
999 constraint.springPos.x=obj[CONSTRAINT_SPRING_POS][0]
1000 constraint.springPos.y=obj[CONSTRAINT_SPRING_POS][1]
1001 constraint.springPos.z=obj[CONSTRAINT_SPRING_POS][2]
1002 constraint.springRot.x=obj[CONSTRAINT_SPRING_ROT][0]
1003 constraint.springRot.y=obj[CONSTRAINT_SPRING_ROT][1]
1004 constraint.springRot.z=obj[CONSTRAINT_SPRING_ROT][2]
1007 print('write', path)
1008 return io.write(path)
1011 def getBoneDisplayGroup(bone):
1014 "necktie IK", "hair IK_L", "hair IK_R", "leg IK_L", "leg IK_R",
1015 "toe IK_L", "toe IK_R",
1018 "upper body", "neck", "head", "eye_L", "eye_R",
1019 "necktie1", "necktie2", "necktie3", "eyes",
1020 "eyelight_L", "eyelight_R",
1023 "front hair1", "front hair2", "front hair3",
1024 "hair1_L", "hair2_L", "hair3_L",
1025 "hair4_L", "hair5_L", "hair6_L",
1026 "hair1_R", "hair2_R", "hair3_R",
1027 "hair4_R", "hair5_R", "hair6_R",
1030 "shoulder_L", "arm_L", "arm twist_L", "elbow_L",
1031 "wrist twist_L", "wrist_L", "sleeve_L",
1032 "shoulder_R", "arm_R", "arm twist_R", "elbow_R",
1033 "wrist twist_R", "wrist_R", "sleeve_R",
1036 "thumb1_L", "thumb2_L", "fore1_L", "fore2_L", "fore3_L",
1037 "middle1_L", "middle2_L", "middle3_L",
1038 "third1_L", "third2_L", "third3_L",
1039 "little1_L", "little2_L", "little3_L",
1040 "thumb1_R", "thumb2_R", "fore1_R", "fore2_R", "fore3_R",
1041 "middle1_R", "middle2_R", "middle3_R",
1042 "third1_R", "third2_R", "third3_R",
1043 "little1_R", "little2_R", "little3_R",
1046 "lower body", "waist accessory",
1047 "front skirt_L", "back skirt_L",
1048 "front skirt_R", "back skirt_R",
1051 "leg_L", "knee_L", "ankle_L",
1052 "leg_R", "knee_R", "ankle_R",
1056 for g in boneGroups:
1064 def __execute(filename, scene):
1065 if not scene.objects.active:
1066 print("abort. no active object.")
1069 bl.progress_start('pmd_export')
1070 active=bl.objectGetActive(scene)
1071 exporter=PmdExporter()
1072 exporter.setup(scene)
1073 exporter.write(filename)
1074 bl.objectActivate(scene, active)
1075 bl.progress_finish()
1080 def execute_24(filename):
1081 filename=filename.decode(bl.INTERNAL_ENCODING)
1082 print("pmd exporter: %s" % filename)
1084 Blender.Window.WaitCursor(1)
1085 t = Blender.sys.time()
1087 scene = bpy.data.scenes.active
1088 __execute(filename, scene)
1090 print('finished in %.2f seconds' % (Blender.sys.time()-t))
1092 Blender.Window.WaitCursor(0)
1094 Blender.Window.FileSelector(
1096 'Export Metasequoia PMD',
1097 Blender.sys.makename(ext='.pmd'))
1101 def execute_25(*args):
1105 class EXPORT_OT_pmd(bpy.types.Operator):
1106 '''Save a Metasequoia PMD file.'''
1107 bl_idname = "export_scene.pmd"
1108 bl_label = 'Export PMD'
1110 # List of operator properties, the attributes will be assigned
1111 # to the class instance from the operator settings before calling.
1113 path = StringProperty(
1115 description="File path used for exporting the PMD file",
1119 filename = StringProperty(
1121 description="Name of the file.")
1122 directory = StringProperty(
1124 description="Directory of the file.")
1126 check_existing = BoolProperty(
1127 name="Check Existing",
1128 description="Check and warn on overwriting existing files",
1130 options=set('HIDDEN'))
1132 def execute(self, context):
1134 self.properties.path,
1139 def invoke(self, context, event):
1141 wm.add_fileselect(self)
1142 return 'RUNNING_MODAL'
1145 def menu_func(self, context):
1146 #default_path=bpy.data.filename.replace(".blend", ".pmd")
1147 self.layout.operator(
1148 EXPORT_OT_pmd.bl_idname,
1149 text="Miku Miku Dance Model(.pmd)")#.path=default_path
1152 bpy.types.register(EXPORT_OT_pmd)
1153 bpy.types.INFO_MT_file_export.append(menu_func)
1156 bpy.types.unregister(EXPORT_OT_pmd)
1157 bpy.types.INFO_MT_file_export.remove(menu_func)
1159 if __name__ == "__main__":