4 Name: 'MikuMikuDance model (.pmd)...'
7 Tooltip: 'Export PMD file for MikuMikuDance.'
9 __author__= ["ousttrue"]
15 This script exports a pmd model.
17 0.1 20100318: first implementation.
18 0.2 20100519: refactoring. use C extension.
19 1.0 20100530: implement basic features.
20 1.1 20100612: integrate 2.4 and 2.5.
21 1.2 20100616: implement rigid body.
22 1.3 20100619: fix rigid body, bone weight.
23 1.4 20100626: refactoring.
24 1.5 20100629: sphere map.
27 MMD_SHAPE_GROUP_NAME='_MMD_SHAPE'
28 BASE_SHAPE_NAME='Basis'
29 RIGID_SHAPE_TYPE='rigid_shape_type'
30 RIGID_PROCESS_TYPE='rigid_process_type'
31 RIGID_BONE_NAME='rigid_bone_name'
32 #RIGID_LOCATION='rigid_loation'
33 RIGID_GROUP='ribid_group'
34 RIGID_INTERSECTION_GROUP='rigid_intersection_group'
35 RIGID_WEIGHT='rigid_weight'
36 RIGID_LINEAR_DAMPING='rigid_linear_damping'
37 RIGID_ANGULAR_DAMPING='rigid_angular_damping'
38 RIGID_RESTITUTION='rigid_restitution'
39 RIGID_FRICTION='rigid_friction'
40 CONSTRAINT_NAME='constraint_name'
41 CONSTRAINT_A='const_a'
42 CONSTRAINT_B='const_b'
43 CONSTRAINT_POS_MIN='const_pos_min'
44 CONSTRAINT_POS_MAX='const_pos_max'
45 CONSTRAINT_ROT_MIN='const_rot_min'
46 CONSTRAINT_ROT_MAX='const_rot_max'
47 CONSTRAINT_SPRING_POS='const_spring_pos'
48 CONSTRAINT_SPRING_ROT='const_spring_rot'
51 ###############################################################################
53 ###############################################################################
58 from meshio import pmd, englishmap
61 return sys.version_info[0]<3
66 from Blender import Mathutils
72 def setMaterialParams(material, m):
74 material.diffuse.r=m.R
75 material.diffuse.g=m.G
76 material.diffuse.b=m.B
77 material.diffuse.a=m.alpha
79 material.sinness=0 if m.spec<1e-5 else m.spec*10
80 material.specular.r=m.specR
81 material.specular.g=m.specG
82 material.specular.b=m.specB
84 material.ambient.r=m.mirR
85 material.ambient.g=m.mirG
86 material.ambient.b=m.mirB
88 material.flag=1 if m.enableSSS else 0
97 from bpy.props import *
105 def setMaterialParams(material, m):
107 material.diffuse.r=m.diffuse_color[0]
108 material.diffuse.g=m.diffuse_color[1]
109 material.diffuse.b=m.diffuse_color[2]
110 material.diffuse.a=m.alpha
112 material.sinness=0 if m.specular_toon_size<1e-5 else m.specular_hardness*10
113 material.specular.r=m.specular_color[0]
114 material.specular.g=m.specular_color[1]
115 material.specular.b=m.specular_color[2]
117 material.ambient.r=m.mirror_color[0]
118 material.ambient.g=m.mirror_color[1]
119 material.ambient.b=m.mirror_color[2]
121 material.flag=1 if m.subsurface_scattering.enabled else 0
124 return s.encode('cp932')
128 __slots__=['o', 'children']
129 def __init__(self, o):
134 ###############################################################################
135 # Blenderのメッシュをワンスキンメッシュ化する
136 ###############################################################################
137 def near(x, y, EPSILON=1e-5):
139 return d>=-EPSILON and d<=EPSILON
142 class VertexKey(object):
149 'nx', 'ny', 'nz', # 法線
153 def __init__(self, obj, index, x, y, z, nx, ny, nz, u, v):
166 return "<vkey: %f, %f, %f, %f, %f, %f, %f, %f>" % (
167 self.x, self.y, self.z, self.nx, self.ny, self.nz, self.u, self.v)
170 #return int((self.x+self.y+self.z+self.nx+self.ny+self.nz+self.u+self.v)*100)
171 return int((self.x+self.y+self.z)*100)
173 def __eq__(self, rhs):
174 #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)
175 #return near(self.x, rhs.x) and near(self.y, rhs.y) and near(self.z, rhs.z)
176 #return self.x==rhs.x and self.y==rhs.y and self.z==rhs.z
177 return self.obj==rhs.obj and self.index==rhs.index
180 class VertexArray(object):
185 # マテリアル毎に分割したインデックス配列
200 return "<VertexArray %d vertices, %d indexArrays>" % (
201 len(self.vertices), len(self.indexArrays))
205 self.vertices, self.normals, self.uvs,
206 self.b0, self.b1, self.weight)
209 keys=[key for key in self.indexArrays.keys()]
212 yield(key, self.indexArrays[key])
214 def __getIndex(self, obj, base_index, pos, normal, uv, b0, b1, weight0):
220 pos[0], pos[1], pos[2],
221 normal[0], normal[1], normal[2],
223 if key in self.vertexMap:
225 index=self.vertexMap[key]
227 index=len(self.vertices)
229 self.vertexMap[key]=index
231 self.vertices.append((pos.x, pos.y, pos.z))
232 self.normals.append((normal.x, normal.y, normal.z))
233 self.uvs.append((uv[0], uv[1]))
236 self.weight.append(weight0)
239 if not base_index in self.indexMap:
240 self.indexMap[base_index]=set()
241 self.indexMap[base_index].add(index)
246 def getMappedIndices(self, base_index):
247 return self.indexMap[base_index]
249 def addTriangle(self,
251 base_index0, base_index1, base_index2,
257 weight0, weight1, weight2
259 if not material in self.indexArrays:
260 self.indexArrays[material]=[]
262 index0=self.__getIndex(obj, base_index0, pos0, n0, uv0, b0_0, b1_0, weight0)
263 index1=self.__getIndex(obj, base_index1, pos1, n1, uv1, b0_1, b1_1, weight1)
264 index2=self.__getIndex(obj, base_index2, pos2, n2, uv2, b0_2, b1_2, weight2)
266 self.indexArrays[material]+=[index0, index1, index2]
270 __slots__=['name', 'type', 'offsets']
271 def __init__(self, name, type):
276 def add(self, index, offset):
277 self.offsets.append((index, offset))
281 self.offsets.sort(lambda l, r: l[0]-r[0])
283 self.offsets.sort(key=lambda e: e[0])
286 return "<Morph %s>" % self.name
288 class IKSolver(object):
289 __slots__=['target', 'effector', 'length', 'iterations', 'weight']
290 def __init__(self, target, effector, length, iterations, weight):
292 self.effector=effector
294 self.iterations=iterations
298 class OneSkinMesh(object):
299 __slots__=['obj_index', 'scene', 'vertexArray', 'morphList',
303 def __init__(self, scene):
304 self.vertexArray=VertexArray()
312 return "<OneSkinMesh %s, morph:%d>" % (
316 def addMesh(self, obj):
317 if bl.object.isVisible(obj):
322 self.__rigidbody(obj)
323 self.__constraint(obj)
325 def __mesh(self, obj):
329 if RIGID_SHAPE_TYPE in obj:
331 if CONSTRAINT_A in obj:
334 print("export", obj.name)
335 mesh=bl.object.getData(obj)
338 def setWeight(i, name, w):
341 if i in secondWeightMap:
343 if w<secondWeightMap[i]:
347 secondWeightMap[i]=(name, w)
350 weightMap[i]=(name, w)
352 if w>weightMap[i][1]:
354 secondWeightMap[i]=weightMap[i]
355 weightMap[i]=(name, w)
357 secondWeightMap[i]=(name, w)
359 weightMap[i]=(name, w)
362 for name in bl.object.getVertexGroupNames(obj):
363 for i, w in mesh.getVertsFromGroup(name, 1):
364 setWeight(i, name, w)
366 for i, v in enumerate(mesh.verts):
368 setWeight(i, obj.vertex_groups[g.group].name, g.weight)
371 for i in xrange(len(mesh.verts)):
372 #for i, name_weight in weightMap.items():
373 if i in secondWeightMap:
374 secondWeightMap[i]=(secondWeightMap[i][0], 1.0-weightMap[i][1])
376 weightMap[i]=(weightMap[i][0], 1.0)
377 secondWeightMap[i]=("", 0)
379 print("no weight vertex")
381 secondWeightMap[i]=("", 0)
383 # メッシュのコピーを生成してオブジェクトの行列を適用する
384 copyMesh, copyObj=bl.object.duplicate(obj)
385 if len(copyMesh.verts)==0:
388 for i, face in enumerate(copyMesh.faces):
389 faceVertexCount=bl.face.getVertexCount(face)
390 material=copyMesh.materials[bl.face.getMaterialIndex(face)]
391 v=[copyMesh.verts[index] for index in bl.face.getVertices(face)]
392 uv=bl.mesh.getFaceUV(copyMesh, i, face)
394 if faceVertexCount==3:
396 self.vertexArray.addTriangle(
397 self.obj_index, material.name,
405 #v0.no, v1.no, v2.no,
406 bl.face.getNormal(face),
407 bl.face.getNormal(face),
408 bl.face.getNormal(face),
412 weightMap[v[2].index][0],
413 weightMap[v[1].index][0],
414 weightMap[v[0].index][0],
415 secondWeightMap[v[2].index][0],
416 secondWeightMap[v[1].index][0],
417 secondWeightMap[v[0].index][0],
418 weightMap[v[2].index][1],
419 weightMap[v[1].index][1],
420 weightMap[v[0].index][1]
422 elif faceVertexCount==4:
424 self.vertexArray.addTriangle(
425 self.obj_index, material.name,
432 #v0.no, v1.no, v2.no,
433 bl.face.getNormal(face),
434 bl.face.getNormal(face),
435 bl.face.getNormal(face),
439 weightMap[v[2].index][0],
440 weightMap[v[1].index][0],
441 weightMap[v[0].index][0],
442 secondWeightMap[v[2].index][0],
443 secondWeightMap[v[1].index][0],
444 secondWeightMap[v[0].index][0],
445 weightMap[v[2].index][1],
446 weightMap[v[1].index][1],
447 weightMap[v[0].index][1]
449 self.vertexArray.addTriangle(
450 self.obj_index, material.name,
457 #v2.no, v3.no, v0.no,
458 bl.face.getNormal(face),
459 bl.face.getNormal(face),
460 bl.face.getNormal(face),
464 weightMap[v[0].index][0],
465 weightMap[v[3].index][0],
466 weightMap[v[2].index][0],
467 secondWeightMap[v[0].index][0],
468 secondWeightMap[v[3].index][0],
469 secondWeightMap[v[2].index][0],
470 weightMap[v[0].index][1],
471 weightMap[v[3].index][1],
472 weightMap[v[2].index][1]
474 bl.object.delete(copyObj)
477 def __skin(self, obj):
478 if not bl.object.hasShapeKey(obj):
482 blenderMesh=bl.object.getData(obj)
486 vg=bl.object.getVertexGroup(obj, MMD_SHAPE_GROUP_NAME)
490 for b in bl.object.getShapeKeys(obj):
491 if b.name==BASE_SHAPE_NAME:
492 baseMorph=self.__getOrCreateMorph('base', 0)
497 v=bl.shapekey.getByIndex(b, index)
498 pos=[v[0], v[1], v[2]]
499 indices=self.vertexArray.getMappedIndices(index)
505 baseMorph.add(i, pos)
506 indexRelativeMap[i]=relativeIndex
511 print(basis.name, len(baseMorph.offsets))
513 if len(baseMorph.offsets)==0:
517 for b in bl.object.getShapeKeys(obj):
518 if b.name==BASE_SHAPE_NAME:
522 morph=self.__getOrCreateMorph(b.name, 4)
524 for index, src, dst in zip(
525 xrange(len(blenderMesh.verts)),
526 bl.shapekey.get(basis),
528 offset=[dst[0]-src[0], dst[1]-src[1], dst[2]-src[2]]
529 if offset[0]==0 and offset[1]==0 and offset[2]==0:
532 indices=self.vertexArray.getMappedIndices(index)
537 morph.add(indexRelativeMap[i], offset)
540 original=self.morphList[:]
542 for i, v in enumerate(englishmap.skinMap):
546 return len(englishmap.skinMap)
548 self.morphList.sort(lambda l, r: getIndex(l)-getIndex(r))
550 self.morphList.sort(key=getIndex)
552 def __rigidbody(self, obj):
555 if not RIGID_SHAPE_TYPE in obj:
557 self.rigidbodies.append(obj)
559 def __constraint(self, obj):
562 if not CONSTRAINT_A in obj:
564 self.constraints.append(obj)
566 def __getOrCreateMorph(self, name, type):
567 for m in self.morphList:
571 self.morphList.append(m)
574 def getVertexCount(self):
575 return len(self.vertexArray.vertices)
579 __slots__=['index', 'name', 'ik_index',
580 'pos', 'tail', 'parent_index', 'tail_index', 'type', 'isConnect']
581 def __init__(self, name, pos, tail):
586 self.parent_index=None
592 def __eq__(self, rhs):
593 return self.index==rhs.index
596 return "<Bone %s %d>" % (self.name, self.type)
598 class BoneBuilder(object):
599 __slots__=['bones', 'boneMap', 'ik_list', ]
605 def build(self, armatureObj):
609 print("gather bones")
610 armature=bl.object.getData(armatureObj)
611 for b in armature.bones.values():
615 bl.bone.getHeadLocal(b),
616 bl.bone.getTailLocal(b))
618 self.__getBone(bone, b)
620 for b in armature.bones.values():
621 if not b.parent and b.name!='center':
624 bl.bone.getHeadLocal(b),
625 bl.bone.getTailLocal(b))
627 self.__getBone(bone, b)
629 print("check connection")
630 for b in armature.bones.values():
632 self.__checkConnection(b, None)
635 pose = bl.object.getPose(armatureObj)
636 for b in pose.bones.values():
637 for c in b.constraints:
638 if bl.constraint.isIKSolver(c):
642 target=self.__boneByName(bl.constraint.ikTarget(c))
649 link=self.__boneByName(b.name)
654 chainLength=bl.constraint.ikChainLen(c)
655 for i in range(chainLength):
657 chainBone=self.__boneByName(e.name)
659 chainBone.ik_index=target.index
662 IKSolver(target, link, chainLength,
663 int(bl.constraint.ikItration(c) * 0.1),
664 bl.constraint.ikRotationWeight(c)
672 for i, v in enumerate(englishmap.boneMap):
673 if v[0]==ik.target.name:
675 return len(englishmap.boneMap)
677 self.ik_list.sort(lambda l, r: getIndex(l)-getIndex(r))
679 self.ik_list.sort(key=getIndex)
681 def __checkConnection(self, b, p):
682 if bl.bone.isConnected(b):
683 parent=self.__boneByName(p.name)
684 parent.isConnect=True
687 self.__checkConnection(c, b)
693 boneMap=englishmap.boneMap
694 original=self.bones[:]
696 for i, k_v in enumerate(boneMap):
697 if k_v[0]==bone.name:
703 self.bones.sort(lambda l, r: getIndex(l)-getIndex(r))
705 self.bones.sort(key=getIndex)
708 for i, b in enumerate(self.bones):
709 src=original.index(b)
712 b.index=sortMap[b.index]
714 b.parent_index=sortMap[b.parent_index]
716 b.tail_index=sortMap[b.tail_index]
718 b.ik_index=sortMap[b.ik_index]
726 if b.parent_index==None:
727 b.parent_index=0xFFFF
729 if b.type==6 or b.type==7:
731 parent=self.bones[b.parent_index]
732 parent.tail_index=b.index
735 if b.tail_index==None:
740 def getIndex(self, bone):
741 for i, b in enumerate(self.bones):
746 def indexByName(self, name):
750 return self.getIndex(self.__boneByName(name))
752 def __boneByName(self, name):
753 return self.boneMap[name]
755 def __getBone(self, parent, b):
756 if len(b.children)==0:
760 for i, c in enumerate(b.children):
762 bl.bone.getHeadLocal(c),
763 bl.bone.getTailLocal(c))
766 bone.parent_index=parent.index
768 parent.tail_index=bone.index
769 self.__getBone(bone, c)
771 def __addBone(self, bone):
772 bone.index=len(self.bones)
773 self.bones.append(bone)
774 self.boneMap[bone.name]=bone
777 class PmdExporter(object):
779 def setup(self, scene):
780 self.armatureObj=None
785 for o in scene.objects:
786 object_node_map[o]=Node(o)
787 for o in scene.objects:
788 #for node in object_node_map.values():
789 node=object_node_map[o]
791 object_node_map[node.o.parent].children.append(node)
794 root=object_node_map[scene.objects.active]
797 self.oneSkinMesh=OneSkinMesh(scene)
798 self.__createOneSkinMesh(root)
799 print(self.oneSkinMesh)
800 self.name=root.o.name
803 self.builder=BoneBuilder()
804 self.builder.build(self.armatureObj)
806 def __createOneSkinMesh(self, node):
807 ############################################################
808 # search armature modifier
809 ############################################################
810 for m in node.o.modifiers:
811 if bl.modifier.isType(m, 'ARMATURE'):
812 armatureObj=bl.modifier.getArmatureObject(m)
813 if not self.armatureObj:
814 self.armatureObj=armatureObj
815 elif self.armatureObj!=armatureObj:
816 print("warning! found multiple armature. ignored.",
819 if node.o.type.upper()=='MESH':
820 self.oneSkinMesh.addMesh(node.o)
822 for child in node.children:
823 self.__createOneSkinMesh(child)
825 def write(self, path):
828 io.comment="blender export"
832 for pos, normal, uv, b0, b1, weight in self.oneSkinMesh.vertexArray.zip():
833 # convert right-handed z-up to left-handed y-up
843 v.bone0=self.builder.indexByName(b0)
844 v.bone1=self.builder.indexByName(b1)
845 v.weight0=int(100*weight)
846 v.edge_flag=0 # edge flag, 0: enable edge, 1: not edge
849 vertexCount=self.oneSkinMesh.getVertexCount()
850 for material_name, indices in self.oneSkinMesh.vertexArray.each():
851 m=bl.material.get(material_name)
853 material=io.addMaterial()
854 setMaterialParams(material, m)
856 material.vertex_count=len(indices)
857 material.toon_index=0
858 textures=[os.path.basename(path)
859 for path in bl.material.eachTexturePath(m)]
861 material.setTexture(toCP932('*'.join(textures)))
864 assert(i<vertexCount)
865 for i in xrange(0, len(indices), 3):
867 io.indices.append(indices[i])
868 io.indices.append(indices[i+1])
869 io.indices.append(indices[i+2])
873 for i, b in enumerate(self.builder.bones):
877 boneNameMap[b.name]=i
878 v=englishmap.getUnicodeBoneName(b.name)
882 cp932=v[1].encode('cp932')
883 assert(len(cp932)<20)
887 bone_english_name=b.name
888 assert(len(bone_english_name)<20)
889 bone.english_name=bone_english_name
894 b.ik_index=self.builder.indexByName('eyes')
899 bone.parent_index=b.parent_index
900 bone.tail_index=b.tail_index
901 bone.ik_index=b.ik_index
903 # convert right-handed z-up to left-handed y-up
904 bone.pos.x=b.pos[0] if not near(b.pos[0], 0) else 0
905 bone.pos.y=b.pos[2] if not near(b.pos[2], 0) else 0
906 bone.pos.z=b.pos[1] if not near(b.pos[1], 0) else 0
909 for ik in self.builder.ik_list:
911 solver.index=self.builder.getIndex(ik.target)
912 solver.target=self.builder.getIndex(ik.effector)
913 solver.length=ik.length
914 b=self.builder.bones[ik.effector.parent_index]
915 for i in xrange(solver.length):
916 solver.children.append(self.builder.getIndex(b))
917 b=self.builder.bones[b.parent_index]
918 solver.iterations=ik.iterations
919 solver.weight=ik.weight
922 for i, m in enumerate(self.oneSkinMesh.morphList):
926 v=englishmap.getUnicodeSkinName(m.name)
928 v=[m.name, m.name, 0]
930 cp932=v[1].encode('cp932')
932 morph.setEnglishName(m.name.encode('cp932'))
935 for index, offset in m.offsets:
936 # convert right-handed z-up to left-handed y-up
937 morph.append(index, offset[0], offset[2], offset[1])
938 morph.vertex_count=len(m.offsets)
942 for i, m in enumerate(self.oneSkinMesh.morphList):
944 io.face_list.append(i)
945 for i, m in enumerate(self.oneSkinMesh.morphList):
947 io.face_list.append(i)
948 for i, m in enumerate(self.oneSkinMesh.morphList):
950 io.face_list.append(i)
951 for i, m in enumerate(self.oneSkinMesh.morphList):
953 io.face_list.append(i)
956 def createBoneDisplayName(name, english):
957 boneDisplayName=io.addBoneDisplayName()
959 boneDisplayName.name=name.decode('utf-8').encode('cp932')
960 boneDisplayName.english_name=english
962 boneDisplayName.setName(name.encode('cp932'))
963 boneDisplayName.setEnglishName(english.encode('cp932'))
964 boneDisplayName=createBoneDisplayName("IK\n", "IK\n")
965 boneDisplayName=createBoneDisplayName("体(上)\n", "Body[u]\n")
966 boneDisplayName=createBoneDisplayName("髪\n", "Hair\n")
967 boneDisplayName=createBoneDisplayName("腕\n", "Arms\n")
968 boneDisplayName=createBoneDisplayName("指\n", "Fingers\n")
969 boneDisplayName=createBoneDisplayName("体(下)\n", "Body[l]\n")
970 boneDisplayName=createBoneDisplayName("足\n", "Legs\n")
971 for i, b in enumerate(self.builder.bones):
976 io.addBoneDisplay(i, getBoneDisplayGroup(b))
979 io.english_name="blender export"
980 io.english_coment="blender export"
984 io.getToonTexture(i).name="toon%02d.bmp\n" % i
988 for i, obj in enumerate(self.oneSkinMesh.rigidbodies):
989 rigidBody=io.addRigidBody()
990 rigidBody.setName(obj.name.encode('cp932'))
991 rigidNameMap[obj.name]=i
992 boneIndex=boneNameMap[obj[RIGID_BONE_NAME]]
995 bone=self.builder.bones[0]
997 bone=self.builder.bones[boneIndex]
998 rigidBody.boneIndex=boneIndex
999 #rigidBody.position.x=obj[RIGID_LOCATION][0]
1000 #rigidBody.position.y=obj[RIGID_LOCATION][1]
1001 #rigidBody.position.z=obj[RIGID_LOCATION][2]
1002 rigidBody.position.x=obj.location.x-bone.pos[0]
1003 rigidBody.position.y=obj.location.z-bone.pos[2]
1004 rigidBody.position.z=obj.location.y-bone.pos[1]
1005 rigidBody.rotation.x=-obj.rotation_euler[0]
1006 rigidBody.rotation.y=-obj.rotation_euler[2]
1007 rigidBody.rotation.z=-obj.rotation_euler[1]
1008 rigidBody.processType=obj[RIGID_PROCESS_TYPE]
1009 rigidBody.group=obj[RIGID_GROUP]
1010 rigidBody.target=obj[RIGID_INTERSECTION_GROUP]
1011 rigidBody.weight=obj[RIGID_WEIGHT]
1012 rigidBody.linearDamping=obj[RIGID_LINEAR_DAMPING]
1013 rigidBody.angularDamping=obj[RIGID_ANGULAR_DAMPING]
1014 rigidBody.restitution=obj[RIGID_RESTITUTION]
1015 rigidBody.friction=obj[RIGID_FRICTION]
1016 if obj[RIGID_SHAPE_TYPE]==0:
1017 rigidBody.shapeType=pmd.SHAPE_SPHERE
1018 rigidBody.w=obj.scale[0]
1019 elif obj[RIGID_SHAPE_TYPE]==1:
1020 rigidBody.shapeType=pmd.SHAPE_BOX
1021 rigidBody.w=obj.scale[0]
1022 rigidBody.d=obj.scale[1]
1023 rigidBody.h=obj.scale[2]
1024 elif obj[RIGID_SHAPE_TYPE]==2:
1025 rigidBody.shapeType=pmd.SHAPE_CAPSULE
1026 rigidBody.w=obj.scale[0]
1027 rigidBody.h=obj.scale[2]
1030 for obj in self.oneSkinMesh.constraints:
1031 constraint=io.addConstraint()
1032 constraint.setName(obj[CONSTRAINT_NAME].encode('cp932'))
1033 constraint.rigidA=rigidNameMap[obj[CONSTRAINT_A]]
1034 constraint.rigidB=rigidNameMap[obj[CONSTRAINT_B]]
1035 constraint.pos.x=obj.location[0]
1036 constraint.pos.y=obj.location[2]
1037 constraint.pos.z=obj.location[1]
1038 constraint.rot.x=-obj.rotation_euler[0]
1039 constraint.rot.y=-obj.rotation_euler[2]
1040 constraint.rot.z=-obj.rotation_euler[1]
1041 constraint.constraintPosMin.x=obj[CONSTRAINT_POS_MIN][0]
1042 constraint.constraintPosMin.y=obj[CONSTRAINT_POS_MIN][1]
1043 constraint.constraintPosMin.z=obj[CONSTRAINT_POS_MIN][2]
1044 constraint.constraintPosMax.x=obj[CONSTRAINT_POS_MAX][0]
1045 constraint.constraintPosMax.y=obj[CONSTRAINT_POS_MAX][1]
1046 constraint.constraintPosMax.z=obj[CONSTRAINT_POS_MAX][2]
1047 constraint.constraintRotMin.x=obj[CONSTRAINT_ROT_MIN][0]
1048 constraint.constraintRotMin.y=obj[CONSTRAINT_ROT_MIN][1]
1049 constraint.constraintRotMin.z=obj[CONSTRAINT_ROT_MIN][2]
1050 constraint.constraintRotMax.x=obj[CONSTRAINT_ROT_MAX][0]
1051 constraint.constraintRotMax.y=obj[CONSTRAINT_ROT_MAX][1]
1052 constraint.constraintRotMax.z=obj[CONSTRAINT_ROT_MAX][2]
1053 constraint.springPos.x=obj[CONSTRAINT_SPRING_POS][0]
1054 constraint.springPos.y=obj[CONSTRAINT_SPRING_POS][1]
1055 constraint.springPos.z=obj[CONSTRAINT_SPRING_POS][2]
1056 constraint.springRot.x=obj[CONSTRAINT_SPRING_ROT][0]
1057 constraint.springRot.y=obj[CONSTRAINT_SPRING_ROT][1]
1058 constraint.springRot.z=obj[CONSTRAINT_SPRING_ROT][2]
1061 print('write', path)
1062 return io.write(path)
1065 def getBoneDisplayGroup(bone):
1068 "necktie IK", "hair IK_L", "hair IK_R", "leg IK_L", "leg IK_R",
1069 "toe IK_L", "toe IK_R",
1072 "upper body", "neck", "head", "eye_L", "eye_R",
1073 "necktie1", "necktie2", "necktie3", "eyes",
1074 "eyelight_L", "eyelight_R",
1077 "front hair1", "front hair2", "front hair3",
1078 "hair1_L", "hair2_L", "hair3_L",
1079 "hair4_L", "hair5_L", "hair6_L",
1080 "hair1_R", "hair2_R", "hair3_R",
1081 "hair4_R", "hair5_R", "hair6_R",
1084 "shoulder_L", "arm_L", "arm twist_L", "elbow_L",
1085 "wrist twist_L", "wrist_L", "sleeve_L",
1086 "shoulder_R", "arm_R", "arm twist_R", "elbow_R",
1087 "wrist twist_R", "wrist_R", "sleeve_R",
1090 "thumb1_L", "thumb2_L", "fore1_L", "fore2_L", "fore3_L",
1091 "middle1_L", "middle2_L", "middle3_L",
1092 "third1_L", "third2_L", "third3_L",
1093 "little1_L", "little2_L", "little3_L",
1094 "thumb1_R", "thumb2_R", "fore1_R", "fore2_R", "fore3_R",
1095 "middle1_R", "middle2_R", "middle3_R",
1096 "third1_R", "third2_R", "third3_R",
1097 "little1_R", "little2_R", "little3_R",
1100 "lower body", "waist accessory",
1101 "front skirt_L", "back skirt_L",
1102 "front skirt_R", "back skirt_R",
1105 "leg_L", "knee_L", "ankle_L",
1106 "leg_R", "knee_R", "ankle_R",
1110 for g in boneGroups:
1118 def __execute(filename, scene):
1119 if not scene.objects.active:
1120 print("abort. no active object.")
1123 active=bl.object.getActive()
1124 exporter=PmdExporter()
1125 exporter.setup(scene)
1126 exporter.write(filename)
1127 bl.object.activate(active)
1132 def execute_24(filename):
1133 scene = bpy.data.scenes.active
1134 bl.initialize('pmd_export', scene)
1135 __execute(filename.decode(bl.INTERNAL_ENCODING), scene)
1138 Blender.Window.FileSelector(
1140 'Export Metasequoia PMD',
1141 Blender.sys.makename(ext='.pmd'))
1145 def execute_25(filename, scene):
1146 bl.initialize('pmd_export', scene)
1147 __execute(filename, scene)
1151 class EXPORT_OT_pmd(bpy.types.Operator):
1152 '''Save a Metasequoia PMD file.'''
1153 bl_idname = "export_scene.pmd"
1154 bl_label = 'Export PMD'
1156 # List of operator properties, the attributes will be assigned
1157 # to the class instance from the operator settings before calling.
1159 path = StringProperty(
1161 description="File path used for exporting the PMD file",
1165 filename = StringProperty(
1167 description="Name of the file.")
1168 directory = StringProperty(
1170 description="Directory of the file.")
1172 check_existing = BoolProperty(
1173 name="Check Existing",
1174 description="Check and warn on overwriting existing files",
1176 options=set('HIDDEN'))
1178 def execute(self, context):
1180 self.properties.path,
1185 def invoke(self, context, event):
1187 wm.add_fileselect(self)
1188 return 'RUNNING_MODAL'
1191 def menu_func(self, context):
1192 #default_path=bpy.data.filename.replace(".blend", ".pmd")
1193 self.layout.operator(
1194 EXPORT_OT_pmd.bl_idname,
1195 text="Miku Miku Dance Model(.pmd)")#.path=default_path
1198 bpy.types.register(EXPORT_OT_pmd)
1199 bpy.types.INFO_MT_file_export.append(menu_func)
1202 bpy.types.unregister(EXPORT_OT_pmd)
1203 bpy.types.INFO_MT_file_export.remove(menu_func)
1205 if __name__ == "__main__":