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.
25 1.6 20100710: toon texture & bone group.
26 1.7 20100711: separate vertex with normal or uv.
27 2.0 20100724: update for Blender2.53.
31 'category': 'Import/Export',
32 'name': 'Export: MikuMikuDance Model Format (.pmd)',
36 'location': 'File > Export',
37 'description': 'Export to the MikuMikuDance Model Format (.pmd)',
38 'warning': '', # used for warning icon and text in addons panel
39 'wiki_url': 'http://sourceforge.jp/projects/meshio/wiki/FrontPage',
42 MMD_SHAPE_GROUP_NAME='_MMD_SHAPE'
44 MMD_MB_COMMENT='mb_comment'
46 BASE_SHAPE_NAME='Basis'
47 RIGID_NAME='rigid_name'
48 RIGID_SHAPE_TYPE='rigid_shape_type'
49 RIGID_PROCESS_TYPE='rigid_process_type'
50 RIGID_BONE_NAME='rigid_bone_name'
51 #RIGID_LOCATION='rigid_loation'
52 RIGID_GROUP='ribid_group'
53 RIGID_INTERSECTION_GROUP='rigid_intersection_group'
54 RIGID_WEIGHT='rigid_weight'
55 RIGID_LINEAR_DAMPING='rigid_linear_damping'
56 RIGID_ANGULAR_DAMPING='rigid_angular_damping'
57 RIGID_RESTITUTION='rigid_restitution'
58 RIGID_FRICTION='rigid_friction'
59 CONSTRAINT_NAME='constraint_name'
60 CONSTRAINT_A='const_a'
61 CONSTRAINT_B='const_b'
62 CONSTRAINT_POS_MIN='const_pos_min'
63 CONSTRAINT_POS_MAX='const_pos_max'
64 CONSTRAINT_ROT_MIN='const_rot_min'
65 CONSTRAINT_ROT_MAX='const_rot_max'
66 CONSTRAINT_SPRING_POS='const_spring_pos'
67 CONSTRAINT_SPRING_ROT='const_spring_rot'
68 TOON_TEXTURE_OBJECT='ToonTextures'
71 ###############################################################################
73 ###############################################################################
78 from meshio import pmd, englishmap
81 return sys.version_info[0]<3
86 from Blender import Mathutils
92 def setMaterialParams(material, m):
94 material.diffuse.r=m.R
95 material.diffuse.g=m.G
96 material.diffuse.b=m.B
97 material.diffuse.a=m.alpha
99 material.sinness=0 if m.spec<1e-5 else m.spec*10
100 material.specular.r=m.specR
101 material.specular.g=m.specG
102 material.specular.b=m.specB
104 material.ambient.r=m.mirR
105 material.ambient.g=m.mirG
106 material.ambient.b=m.mirB
108 material.flag=1 if m.enableSSS else 0
124 def setMaterialParams(material, m):
126 material.diffuse.r=m.diffuse_color[0]
127 material.diffuse.g=m.diffuse_color[1]
128 material.diffuse.b=m.diffuse_color[2]
129 material.diffuse.a=m.alpha
131 material.sinness=0 if m.specular_toon_size<1e-5 else m.specular_hardness*10
132 material.specular.r=m.specular_color[0]
133 material.specular.g=m.specular_color[1]
134 material.specular.b=m.specular_color[2]
136 material.ambient.r=m.mirror_color[0]
137 material.ambient.g=m.mirror_color[1]
138 material.ambient.b=m.mirror_color[2]
140 material.flag=1 if m.subsurface_scattering.enabled else 0
142 material.toon_index=7
145 return s.encode('cp932')
149 __slots__=['o', 'children']
150 def __init__(self, o):
155 ###############################################################################
156 # Blenderのメッシュをワンスキンメッシュ化する
157 ###############################################################################
158 def near(x, y, EPSILON=1e-5):
160 return d>=-EPSILON and d<=EPSILON
163 class VertexAttribute(object):
165 'nx', 'ny', 'nz', # normal
168 def __init__(self, nx, ny, nz, u, v):
176 return "<vkey: %f, %f, %f, %f, %f>" % (
177 self.nx, self.ny, self.nz, self.u, self.v)
180 return self.nx + self.ny + self.nz + self.u + self.v
182 def __eq__(self, rhs):
183 return self.nx==rhs.nx and self.ny==rhs.ny and self.nz==rhs.nz and self.u==rhs.u and self.v==rhs.v
186 class VertexKey(object):
188 'obj_index', 'index',
191 def __init__(self, obj_index, index):
192 self.obj_index=obj_index
196 return "<vkey: %d, %d>" % (self.obj_index, self.index)
199 return self.index*100+self.obj_index
201 def __eq__(self, rhs):
202 return self.obj_index==rhs.obj_index and self.index==rhs.index
205 class VertexArray(object):
212 'attributes', # normal and uv
213 'b0', 'b1', 'weight',
218 # indexArrays split with each material
231 return "<VertexArray %d positions, %d indexArrays>" % (
232 len(self.positions), len(self.indexArrays))
236 self.positions, self.attributes,
237 self.b0, self.b1, self.weight)
240 keys=[key for key in self.indexArrays.keys()]
243 yield(key, self.indexArrays[key])
245 def __addOrGetIndex(self, obj_index, base_index, pos, normal, uv, b0, b1, weight0):
246 key=VertexKey(obj_index, base_index)
247 attribute=VertexAttribute(
248 normal[0], normal[1], normal[2],
250 if key in self.vertexMap:
251 if attribute in self.vertexMap[key]:
252 return self.vertexMap[key][attribute]
254 return self.__addVertex(self.vertexMap[key],
255 pos, attribute, b0, b1, weight0)
258 self.vertexMap[key]=vertexMapKey
259 return self.__addVertex(vertexMapKey,
260 pos, attribute, b0, b1, weight0)
262 def __addVertex(self, vertexMapKey, pos, attribute, b0, b1, weight0):
263 index=len(self.positions)
264 vertexMapKey[attribute]=index
266 self.positions.append((pos.x, pos.y, pos.z))
268 self.attributes.append(attribute)
272 self.weight.append(weight0)
276 def getMappedIndex(self, obj_name, base_index):
277 return self.vertexMap[VertexKey(self.objectMap[obj_name], base_index)]
279 def addTriangle(self,
280 object_name, material,
281 base_index0, base_index1, base_index2,
287 weight0, weight1, weight2
289 if object_name in self.objectMap:
290 obj_index=self.objectMap[object_name]
292 obj_index=len(self.objectMap)
293 self.objectMap[object_name]=obj_index
294 index0=self.__addOrGetIndex(obj_index, base_index0, pos0, n0, uv0, b0_0, b1_0, weight0)
295 index1=self.__addOrGetIndex(obj_index, base_index1, pos1, n1, uv1, b0_1, b1_1, weight1)
296 index2=self.__addOrGetIndex(obj_index, base_index2, pos2, n2, uv2, b0_2, b1_2, weight2)
298 if not material in self.indexArrays:
299 self.indexArrays[material]=[]
300 self.indexArrays[material]+=[index0, index1, index2]
304 __slots__=['name', 'type', 'offsets']
305 def __init__(self, name, type):
310 def add(self, index, offset):
311 self.offsets.append((index, offset))
315 self.offsets.sort(lambda l, r: l[0]-r[0])
317 self.offsets.sort(key=lambda e: e[0])
320 return "<Morph %s>" % self.name
322 class IKSolver(object):
323 __slots__=['target', 'effector', 'length', 'iterations', 'weight']
324 def __init__(self, target, effector, length, iterations, weight):
326 self.effector=effector
328 self.iterations=iterations
332 class OneSkinMesh(object):
333 __slots__=['vertexArray', 'morphList', 'rigidbodies', 'constraints', ]
335 self.vertexArray=VertexArray()
341 return "<OneSkinMesh %s, morph:%d>" % (
345 def addMesh(self, obj):
346 if not bl.object.isVisible(obj):
350 self.__rigidbody(obj)
351 self.__constraint(obj)
353 def __getWeightMap(self, obj, mesh):
357 def setWeight(i, name, w):
360 if i in secondWeightMap:
362 if w<secondWeightMap[i][1]:
364 elif w<weightMap[i][1]:
366 secondWeightMap[i]=(name, w)
369 weightMap[i]=(name, w)
371 if w>weightMap[i][1]:
373 secondWeightMap[i]=weightMap[i]
374 weightMap[i]=(name, w)
376 secondWeightMap[i]=(name, w)
378 weightMap[i]=(name, w)
380 # ToDo bone weightと関係ないvertex groupを除外する
382 for name in bl.object.getVertexGroupNames(obj):
383 for i, w in mesh.getVertsFromGroup(name, 1):
384 setWeight(i, name, w)
386 for i, v in enumerate(mesh.verts):
389 setWeight(i, obj.vertex_groups[g.group].name, g.weight)
391 setWeight(i, obj.vertex_groups[0].name, 1)
394 for i in xrange(len(mesh.verts)):
395 if i in secondWeightMap:
396 secondWeightMap[i]=(secondWeightMap[i][0], 1.0-weightMap[i][1])
398 weightMap[i]=(weightMap[i][0], 1.0)
399 secondWeightMap[i]=("", 0)
401 print("no weight vertex")
403 secondWeightMap[i]=("", 0)
405 return weightMap, secondWeightMap
407 def __processFaces(self, obj_name, mesh, weightMap, secondWeightMap):
409 for i, face in enumerate(mesh.faces):
410 faceVertexCount=bl.face.getVertexCount(face)
411 material=mesh.materials[bl.face.getMaterialIndex(face)]
412 v=[mesh.verts[index] for index in bl.face.getVertices(face)]
413 uv=bl.mesh.getFaceUV(
414 mesh, i, face, bl.face.getVertexCount(face))
416 if faceVertexCount==3:
418 self.vertexArray.addTriangle(
419 obj_name, material.name,
426 bl.vertex.getNormal(v[2]),
427 bl.vertex.getNormal(v[1]),
428 bl.vertex.getNormal(v[0]),
432 weightMap[v[2].index][0],
433 weightMap[v[1].index][0],
434 weightMap[v[0].index][0],
435 secondWeightMap[v[2].index][0],
436 secondWeightMap[v[1].index][0],
437 secondWeightMap[v[0].index][0],
438 weightMap[v[2].index][1],
439 weightMap[v[1].index][1],
440 weightMap[v[0].index][1]
442 elif faceVertexCount==4:
444 self.vertexArray.addTriangle(
445 obj_name, material.name,
452 bl.vertex.getNormal(v[2]),
453 bl.vertex.getNormal(v[1]),
454 bl.vertex.getNormal(v[0]),
458 weightMap[v[2].index][0],
459 weightMap[v[1].index][0],
460 weightMap[v[0].index][0],
461 secondWeightMap[v[2].index][0],
462 secondWeightMap[v[1].index][0],
463 secondWeightMap[v[0].index][0],
464 weightMap[v[2].index][1],
465 weightMap[v[1].index][1],
466 weightMap[v[0].index][1]
468 self.vertexArray.addTriangle(
469 obj_name, material.name,
476 bl.vertex.getNormal(v[0]),
477 bl.vertex.getNormal(v[3]),
478 bl.vertex.getNormal(v[2]),
482 weightMap[v[0].index][0],
483 weightMap[v[3].index][0],
484 weightMap[v[2].index][0],
485 secondWeightMap[v[0].index][0],
486 secondWeightMap[v[3].index][0],
487 secondWeightMap[v[2].index][0],
488 weightMap[v[0].index][1],
489 weightMap[v[3].index][1],
490 weightMap[v[2].index][1]
493 def __mesh(self, obj):
497 if RIGID_SHAPE_TYPE in obj:
499 if CONSTRAINT_A in obj:
502 #if not bl.modifier.hasType(obj, 'ARMATURE'):
505 bl.message("export: %s" % obj.name)
507 # メッシュのコピーを生成してオブジェクトの行列を適用する
508 copyMesh, copyObj=bl.object.duplicate(obj)
509 if len(copyMesh.verts)>0:
511 copyObj.scale=obj.scale
512 bpy.ops.object.scale_apply()
513 copyObj.rotation_euler=obj.rotation_euler
514 bpy.ops.object.rotation_apply()
515 copyObj.location=obj.location
516 bpy.ops.object.location_apply()
518 for m in [m for m in copyObj.modifiers]:
519 if m.type=='SOLIDFY':
521 elif m.type=='ARMATURE':
523 elif m.type=='MIRROR':
524 bpy.ops.object.modifier_apply(modifier=m.name)
528 weightMap, secondWeightMap=self.__getWeightMap(copyObj, copyMesh)
529 self.__processFaces(obj.name, copyMesh, weightMap, secondWeightMap)
530 bl.object.delete(copyObj)
532 def createEmptyBasicSkin(self):
533 self.__getOrCreateMorph('base', 0)
535 def __skin(self, obj):
536 if not bl.object.hasShapeKey(obj):
540 blenderMesh=bl.object.getData(obj)
544 vg=bl.object.getVertexGroup(obj, MMD_SHAPE_GROUP_NAME)
548 for b in bl.object.getShapeKeys(obj):
549 if b.name==BASE_SHAPE_NAME:
550 baseMorph=self.__getOrCreateMorph('base', 0)
555 v=bl.shapekey.getByIndex(b, index)
556 pos=[v[0], v[1], v[2]]
558 indices=self.vertexArray.getMappedIndex(obj.name, index)
559 for attribute, i in indices.items():
564 baseMorph.add(i, pos)
565 indexRelativeMap[i]=relativeIndex
570 print(basis.name, len(baseMorph.offsets))
572 if len(baseMorph.offsets)==0:
576 for b in bl.object.getShapeKeys(obj):
577 if b.name==BASE_SHAPE_NAME:
581 morph=self.__getOrCreateMorph(b.name, 4)
583 for index, src, dst in zip(
584 xrange(len(blenderMesh.verts)),
585 bl.shapekey.get(basis),
587 offset=[dst[0]-src[0], dst[1]-src[1], dst[2]-src[2]]
588 if offset[0]==0 and offset[1]==0 and offset[2]==0:
591 indices=self.vertexArray.getMappedIndex(obj.name, index)
592 for attribute, i in indices.items():
596 morph.add(indexRelativeMap[i], offset)
597 assert(len(morph.offsets)<len(baseMorph.offsets))
600 original=self.morphList[:]
602 for i, v in enumerate(englishmap.skinMap):
606 return len(englishmap.skinMap)
608 self.morphList.sort(lambda l, r: getIndex(l)-getIndex(r))
610 self.morphList.sort(key=getIndex)
612 def __rigidbody(self, obj):
615 if not RIGID_SHAPE_TYPE in obj:
617 self.rigidbodies.append(obj)
619 def __constraint(self, obj):
622 if not CONSTRAINT_A in obj:
624 self.constraints.append(obj)
626 def __getOrCreateMorph(self, name, type):
627 for m in self.morphList:
631 self.morphList.append(m)
634 def getVertexCount(self):
635 return len(self.vertexArray.positions)
639 __slots__=['index', 'name', 'ik_index',
640 'pos', 'tail', 'parent_index', 'tail_index', 'type', 'isConnect']
641 def __init__(self, name, pos, tail, isConnect):
646 self.parent_index=None
649 self.isConnect=isConnect
652 def __eq__(self, rhs):
653 return self.index==rhs.index
656 return "<Bone %s %d>" % (self.name, self.type)
658 class BoneBuilder(object):
659 __slots__=['bones', 'boneMap', 'ik_list', 'bone_groups',]
666 def getBoneGroup(self, bone):
667 for i, g in enumerate(self.bone_groups):
671 print('no gorup', bone)
674 def build(self, armatureObj):
678 bl.message("build skeleton")
679 armature=bl.object.getData(armatureObj)
684 for g in bl.object.boneGroups(armatureObj):
685 self.bone_groups.append((g.name, []))
690 for b in armature.bones.values():
694 bl.bone.getHeadLocal(b),
695 bl.bone.getTailLocal(b),
698 self.__getBone(bone, b)
700 for b in armature.bones.values():
702 self.__checkConnection(b, None)
707 pose = bl.object.getPose(armatureObj)
708 for b in pose.bones.values():
712 self.__assignBoneGroup(b, b.bone_group)
713 for c in b.constraints:
714 if bl.constraint.isIKSolver(c):
718 target=self.__boneByName(bl.constraint.ikTarget(c))
725 link=self.__boneByName(b.name)
730 chainLength=bl.constraint.ikChainLen(c)
731 for i in range(chainLength):
733 chainBone=self.__boneByName(e.name)
735 chainBone.ik_index=target.index
738 IKSolver(target, link, chainLength,
739 int(bl.constraint.ikItration(c) * 0.1),
740 bl.constraint.ikRotationWeight(c)
750 for i, v in enumerate(englishmap.boneMap):
751 if v[0]==ik.target.name:
753 return len(englishmap.boneMap)
755 self.ik_list.sort(lambda l, r: getIndex(l)-getIndex(r))
757 self.ik_list.sort(key=getIndex)
759 def __assignBoneGroup(self, poseBone, boneGroup):
761 for g in self.bone_groups:
762 if g[0]==boneGroup.name:
763 g[1].append(poseBone.name)
765 def __checkConnection(self, b, p):
766 if bl.bone.isConnected(b):
767 parent=self.__boneByName(p.name)
768 parent.isConnect=True
771 self.__checkConnection(c, b)
777 boneMap=englishmap.boneMap
778 original=self.bones[:]
780 for i, k_v in enumerate(boneMap):
781 if k_v[0]==bone.name:
787 self.bones.sort(lambda l, r: getIndex(l)-getIndex(r))
789 self.bones.sort(key=getIndex)
792 for i, b in enumerate(self.bones):
793 src=original.index(b)
796 b.index=sortMap[b.index]
798 b.parent_index=sortMap[b.parent_index]
800 b.tail_index=sortMap[b.tail_index]
802 b.ik_index=sortMap[b.ik_index]
810 if b.parent_index==None:
811 b.parent_index=0xFFFF
813 if b.type==6 or b.type==7:
815 parent=self.bones[b.parent_index]
816 #print('parnet', parent.name)
817 parent.tail_index=b.index
820 if b.tail_index==None:
825 def getIndex(self, bone):
826 for i, b in enumerate(self.bones):
831 def indexByName(self, name):
835 return self.getIndex(self.__boneByName(name))
837 def __boneByName(self, name):
838 return self.boneMap[name]
840 def __getBone(self, parent, b):
841 if len(b.children)==0:
845 for i, c in enumerate(b.children):
847 bl.bone.getHeadLocal(c),
848 bl.bone.getTailLocal(c),
849 bl.bone.isConnected(c))
852 bone.parent_index=parent.index
854 if bone.isConnect or (not parent.tail_index and parent.tail==bone.pos):
855 parent.tail_index=bone.index
856 self.__getBone(bone, c)
858 def __addBone(self, bone):
859 bone.index=len(self.bones)
860 self.bones.append(bone)
861 self.boneMap[bone.name]=bone
864 class PmdExporter(object):
876 self.armatureObj=None
880 for o in bl.object.each():
881 object_node_map[o]=Node(o)
882 for o in bl.object.each():
883 node=object_node_map[o]
885 object_node_map[node.o.parent].children.append(node)
888 root=object_node_map[bl.object.getActive()]
890 self.englishName=o.name
891 self.englishComment=o[MMD_COMMENT] if MMD_COMMENT in o else 'blender export\n'
892 self.name=o[MMD_MB_NAME] if MMD_MB_NAME in o else 'Blenderエクスポート'
893 self.comment=o[MMD_MB_COMMENT] if MMD_MB_COMMENT in o else 'Blnderエクスポート\n'
896 self.oneSkinMesh=OneSkinMesh()
897 self.__createOneSkinMesh(root)
898 bl.message(self.oneSkinMesh)
899 if len(self.oneSkinMesh.morphList)==0:
901 self.oneSkinMesh.createEmptyBasicSkin()
904 self.skeleton=BoneBuilder()
905 self.skeleton.build(self.armatureObj)
907 def __createOneSkinMesh(self, node):
908 ############################################################
909 # search armature modifier
910 ############################################################
911 for m in node.o.modifiers:
912 if bl.modifier.isType(m, 'ARMATURE'):
913 armatureObj=bl.modifier.getArmatureObject(m)
914 if not self.armatureObj:
915 self.armatureObj=armatureObj
916 elif self.armatureObj!=armatureObj:
917 print("warning! found multiple armature. ignored.",
920 if node.o.type.upper()=='MESH':
921 self.oneSkinMesh.addMesh(node.o)
923 for child in node.children:
924 self.__createOneSkinMesh(child)
926 def write(self, path):
928 io.setName(toCP932(self.name))
929 io.setComment(toCP932(self.comment))
933 for pos, attribute, b0, b1, weight in self.oneSkinMesh.vertexArray.zip():
934 # convert right-handed z-up to left-handed y-up
939 v.normal.x=attribute.nx
940 v.normal.y=attribute.ny
941 v.normal.z=attribute.nz
943 v.uv.y=1.0-attribute.v # reverse vertical
944 v.bone0=self.skeleton.indexByName(b0)
945 v.bone1=self.skeleton.indexByName(b1)
946 v.weight0=int(100*weight)
947 v.edge_flag=0 # edge flag, 0: enable edge, 1: not edge
950 vertexCount=self.oneSkinMesh.getVertexCount()
951 for material_name, indices in self.oneSkinMesh.vertexArray.each():
952 #print('material:', material_name)
953 m=bl.material.get(material_name)
955 material=io.addMaterial()
956 setMaterialParams(material, m)
958 material.vertex_count=len(indices)
959 material.toon_index=0
960 textures=[os.path.basename(path)
961 for path in bl.material.eachEnalbeTexturePath(m)]
963 material.setTexture(toCP932('*'.join(textures)))
965 material.setTexture(toCP932(""))
968 assert(i<vertexCount)
969 for i in xrange(0, len(indices), 3):
971 io.indices.append(indices[i])
972 io.indices.append(indices[i+1])
973 io.indices.append(indices[i+2])
977 for i, b in enumerate(self.skeleton.bones):
981 boneNameMap[b.name]=i
982 v=englishmap.getUnicodeBoneName(b.name)
986 cp932=v[1].encode('cp932')
987 assert(len(cp932)<20)
991 bone_english_name=toCP932(b.name)
992 assert(len(bone_english_name)<20)
993 bone.setEnglishName(bone_english_name)
998 b.ik_index=self.skeleton.indexByName('eyes')
1003 bone.parent_index=b.parent_index
1004 bone.tail_index=b.tail_index
1005 bone.ik_index=b.ik_index
1007 # convert right-handed z-up to left-handed y-up
1008 bone.pos.x=b.pos[0] if not near(b.pos[0], 0) else 0
1009 bone.pos.y=b.pos[2] if not near(b.pos[2], 0) else 0
1010 bone.pos.z=b.pos[1] if not near(b.pos[1], 0) else 0
1013 for ik in self.skeleton.ik_list:
1015 solver.index=self.skeleton.getIndex(ik.target)
1016 solver.target=self.skeleton.getIndex(ik.effector)
1017 solver.length=ik.length
1018 b=self.skeleton.bones[ik.effector.parent_index]
1019 for i in xrange(solver.length):
1020 solver.children.append(self.skeleton.getIndex(b))
1021 b=self.skeleton.bones[b.parent_index]
1022 solver.iterations=ik.iterations
1023 solver.weight=ik.weight
1026 for i, m in enumerate(self.oneSkinMesh.morphList):
1030 v=englishmap.getUnicodeSkinName(m.name)
1032 v=[m.name, m.name, 0]
1034 cp932=v[1].encode('cp932')
1035 morph.setName(cp932)
1036 morph.setEnglishName(m.name.encode('cp932'))
1039 for index, offset in m.offsets:
1040 # convert right-handed z-up to left-handed y-up
1041 morph.append(index, offset[0], offset[2], offset[1])
1042 morph.vertex_count=len(m.offsets)
1046 for i, m in enumerate(self.oneSkinMesh.morphList):
1048 io.face_list.append(i)
1049 for i, m in enumerate(self.oneSkinMesh.morphList):
1051 io.face_list.append(i)
1052 for i, m in enumerate(self.oneSkinMesh.morphList):
1054 io.face_list.append(i)
1055 for i, m in enumerate(self.oneSkinMesh.morphList):
1057 io.face_list.append(i)
1060 for g in self.skeleton.bone_groups:
1061 boneDisplayName=io.addBoneGroup()
1063 name=englishmap.getUnicodeBoneGroupName(g[0])
1066 boneDisplayName.setName(toCP932(name+'\n'))
1069 boneDisplayName.setEnglishName(toCP932(englishName+'\n'))
1072 for i, b in enumerate(self.skeleton.bones):
1075 if b.type in [6, 7]:
1077 io.addBoneDisplay(i, self.skeleton.getBoneGroup(b))
1079 #assert(len(io.bones)==len(io.bone_display_list)+1)
1082 io.setEnglishName(toCP932(self.englishName))
1083 io.setEnglishComment(toCP932(self.englishComment))
1087 for o in bl.object.each():
1089 if o.name.startswith(TOON_TEXTURE_OBJECT):
1095 toonMesh=bl.object.getData(toonMeshObject)
1096 toonMaterial=bl.mesh.getMaterial(toonMesh, 0)
1098 t=bl.material.getTexture(toonMaterial, i)
1100 io.getToonTexture(i).setName(toCP932(t.name))
1102 io.getToonTexture(i).setName(toCP932("toon%02d.bmp\n" % i))
1105 io.getToonTexture(i).setName(toCP932("toon%02d.bmp\n" % i))
1109 for i, obj in enumerate(self.oneSkinMesh.rigidbodies):
1110 name=obj[RIGID_NAME] if RIGID_NAME in obj else obj.name
1112 rigidBody=io.addRigidBody()
1113 rigidBody.setName(name.encode('cp932'))
1114 rigidNameMap[name]=i
1115 boneIndex=boneNameMap[obj[RIGID_BONE_NAME]]
1118 bone=self.skeleton.bones[0]
1120 bone=self.skeleton.bones[boneIndex]
1121 rigidBody.boneIndex=boneIndex
1122 #rigidBody.position.x=obj[RIGID_LOCATION][0]
1123 #rigidBody.position.y=obj[RIGID_LOCATION][1]
1124 #rigidBody.position.z=obj[RIGID_LOCATION][2]
1125 rigidBody.position.x=obj.location.x-bone.pos[0]
1126 rigidBody.position.y=obj.location.z-bone.pos[2]
1127 rigidBody.position.z=obj.location.y-bone.pos[1]
1128 rigidBody.rotation.x=-obj.rotation_euler[0]
1129 rigidBody.rotation.y=-obj.rotation_euler[2]
1130 rigidBody.rotation.z=-obj.rotation_euler[1]
1131 rigidBody.processType=obj[RIGID_PROCESS_TYPE]
1132 rigidBody.group=obj[RIGID_GROUP]
1133 rigidBody.target=obj[RIGID_INTERSECTION_GROUP]
1134 rigidBody.weight=obj[RIGID_WEIGHT]
1135 rigidBody.linearDamping=obj[RIGID_LINEAR_DAMPING]
1136 rigidBody.angularDamping=obj[RIGID_ANGULAR_DAMPING]
1137 rigidBody.restitution=obj[RIGID_RESTITUTION]
1138 rigidBody.friction=obj[RIGID_FRICTION]
1139 if obj[RIGID_SHAPE_TYPE]==0:
1140 rigidBody.shapeType=pmd.SHAPE_SPHERE
1141 rigidBody.w=obj.scale[0]
1142 elif obj[RIGID_SHAPE_TYPE]==1:
1143 rigidBody.shapeType=pmd.SHAPE_BOX
1144 rigidBody.w=obj.scale[0]
1145 rigidBody.d=obj.scale[1]
1146 rigidBody.h=obj.scale[2]
1147 elif obj[RIGID_SHAPE_TYPE]==2:
1148 rigidBody.shapeType=pmd.SHAPE_CAPSULE
1149 rigidBody.w=obj.scale[0]
1150 rigidBody.h=obj.scale[2]
1153 for obj in self.oneSkinMesh.constraints:
1154 constraint=io.addConstraint()
1155 constraint.setName(obj[CONSTRAINT_NAME].encode('cp932'))
1156 constraint.rigidA=rigidNameMap[obj[CONSTRAINT_A]]
1157 constraint.rigidB=rigidNameMap[obj[CONSTRAINT_B]]
1158 constraint.pos.x=obj.location[0]
1159 constraint.pos.y=obj.location[2]
1160 constraint.pos.z=obj.location[1]
1161 constraint.rot.x=-obj.rotation_euler[0]
1162 constraint.rot.y=-obj.rotation_euler[2]
1163 constraint.rot.z=-obj.rotation_euler[1]
1164 constraint.constraintPosMin.x=obj[CONSTRAINT_POS_MIN][0]
1165 constraint.constraintPosMin.y=obj[CONSTRAINT_POS_MIN][1]
1166 constraint.constraintPosMin.z=obj[CONSTRAINT_POS_MIN][2]
1167 constraint.constraintPosMax.x=obj[CONSTRAINT_POS_MAX][0]
1168 constraint.constraintPosMax.y=obj[CONSTRAINT_POS_MAX][1]
1169 constraint.constraintPosMax.z=obj[CONSTRAINT_POS_MAX][2]
1170 constraint.constraintRotMin.x=obj[CONSTRAINT_ROT_MIN][0]
1171 constraint.constraintRotMin.y=obj[CONSTRAINT_ROT_MIN][1]
1172 constraint.constraintRotMin.z=obj[CONSTRAINT_ROT_MIN][2]
1173 constraint.constraintRotMax.x=obj[CONSTRAINT_ROT_MAX][0]
1174 constraint.constraintRotMax.y=obj[CONSTRAINT_ROT_MAX][1]
1175 constraint.constraintRotMax.z=obj[CONSTRAINT_ROT_MAX][2]
1176 constraint.springPos.x=obj[CONSTRAINT_SPRING_POS][0]
1177 constraint.springPos.y=obj[CONSTRAINT_SPRING_POS][1]
1178 constraint.springPos.z=obj[CONSTRAINT_SPRING_POS][2]
1179 constraint.springRot.x=obj[CONSTRAINT_SPRING_ROT][0]
1180 constraint.springRot.y=obj[CONSTRAINT_SPRING_ROT][1]
1181 constraint.springRot.z=obj[CONSTRAINT_SPRING_ROT][2]
1184 bl.message('write %s' % path)
1185 return io.write(path)
1188 def __execute(filename):
1189 active=bl.object.getActive()
1191 print("abort. no active object.")
1193 exporter=PmdExporter()
1196 exporter.write(filename)
1197 bl.object.activate(active)
1202 def execute_24(filename):
1203 bl.initialize('pmd_export', bpy.data.scenes.active)
1204 __execute(filename.decode(bl.INTERNAL_ENCODING))
1207 Blender.Window.FileSelector(
1209 'Export Metasequoia PMD',
1210 Blender.sys.makename(ext='.pmd'))
1214 def execute_25(filename, scene):
1215 bl.initialize('pmd_export', scene)
1220 class EXPORT_OT_pmd(bpy.types.Operator):
1221 '''Save a Metasequoia PMD file.'''
1222 bl_idname = "export_scene.pmd"
1223 bl_label = 'Export PMD'
1225 # List of operator properties, the attributes will be assigned
1226 # to the class instance from the operator settings before calling.
1227 filepath = bpy.props.StringProperty()
1228 filename = bpy.props.StringProperty()
1229 directory = bpy.props.StringProperty()
1231 def execute(self, context):
1233 self.properties.filepath,
1238 def invoke(self, context, event):
1240 wm.add_fileselect(self)
1241 return 'RUNNING_MODAL'
1244 def menu_func(self, context):
1245 default_path=bpy.data.filepath.replace(".blend", ".pmd")
1246 self.layout.operator(
1247 EXPORT_OT_pmd.bl_idname,
1248 text="Miku Miku Dance Model(.pmd)",
1250 ).filepath=default_path
1253 bpy.types.register(EXPORT_OT_pmd)
1254 bpy.types.INFO_MT_file_export.append(menu_func)
1257 bpy.types.unregister(EXPORT_OT_pmd)
1258 bpy.types.INFO_MT_file_export.remove(menu_func)
1260 if __name__ == "__main__":