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'
49 TOON_TEXTURE_OBJECT='ToonTextures'
52 ###############################################################################
54 ###############################################################################
59 from meshio import pmd, englishmap
62 return sys.version_info[0]<3
67 from Blender import Mathutils
73 def setMaterialParams(material, m):
75 material.diffuse.r=m.R
76 material.diffuse.g=m.G
77 material.diffuse.b=m.B
78 material.diffuse.a=m.alpha
80 material.sinness=0 if m.spec<1e-5 else m.spec*10
81 material.specular.r=m.specR
82 material.specular.g=m.specG
83 material.specular.b=m.specB
85 material.ambient.r=m.mirR
86 material.ambient.g=m.mirG
87 material.ambient.b=m.mirB
89 material.flag=1 if m.enableSSS else 0
98 from bpy.props import *
106 def setMaterialParams(material, m):
108 material.diffuse.r=m.diffuse_color[0]
109 material.diffuse.g=m.diffuse_color[1]
110 material.diffuse.b=m.diffuse_color[2]
111 material.diffuse.a=m.alpha
113 material.sinness=0 if m.specular_toon_size<1e-5 else m.specular_hardness*10
114 material.specular.r=m.specular_color[0]
115 material.specular.g=m.specular_color[1]
116 material.specular.b=m.specular_color[2]
118 material.ambient.r=m.mirror_color[0]
119 material.ambient.g=m.mirror_color[1]
120 material.ambient.b=m.mirror_color[2]
122 material.flag=1 if m.subsurface_scattering.enabled else 0
124 material.toon_index=7
127 return s.encode('cp932')
131 __slots__=['o', 'children']
132 def __init__(self, o):
137 ###############################################################################
138 # Blenderのメッシュをワンスキンメッシュ化する
139 ###############################################################################
140 def near(x, y, EPSILON=1e-5):
142 return d>=-EPSILON and d<=EPSILON
145 class VertexKey(object):
152 'nx', 'ny', 'nz', # 法線
156 def __init__(self, obj, index, x, y, z, nx, ny, nz, u, v):
169 return "<vkey: %f, %f, %f, %f, %f, %f, %f, %f>" % (
170 self.x, self.y, self.z, self.nx, self.ny, self.nz, self.u, self.v)
173 #return int((self.x+self.y+self.z+self.nx+self.ny+self.nz+self.u+self.v)*100)
174 return int((self.x+self.y+self.z)*100)
176 def __eq__(self, rhs):
177 #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)
178 #return near(self.x, rhs.x) and near(self.y, rhs.y) and near(self.z, rhs.z)
179 #return self.x==rhs.x and self.y==rhs.y and self.z==rhs.z
180 return self.obj==rhs.obj and self.index==rhs.index
183 class VertexArray(object):
188 # マテリアル毎に分割したインデックス配列
203 return "<VertexArray %d vertices, %d indexArrays>" % (
204 len(self.vertices), len(self.indexArrays))
208 self.vertices, self.normals, self.uvs,
209 self.b0, self.b1, self.weight)
212 keys=[key for key in self.indexArrays.keys()]
215 yield(key, self.indexArrays[key])
217 def __getIndex(self, obj, base_index, pos, normal, uv, b0, b1, weight0):
223 pos[0], pos[1], pos[2],
224 normal[0], normal[1], normal[2],
226 if key in self.vertexMap:
228 index=self.vertexMap[key]
230 index=len(self.vertices)
232 self.vertexMap[key]=index
234 self.vertices.append((pos.x, pos.y, pos.z))
235 self.normals.append((normal.x, normal.y, normal.z))
236 self.uvs.append((uv[0], uv[1]))
239 self.weight.append(weight0)
242 if not base_index in self.indexMap:
243 self.indexMap[base_index]=set()
244 self.indexMap[base_index].add(index)
249 def getMappedIndices(self, base_index):
250 return self.indexMap[base_index]
252 def addTriangle(self,
254 base_index0, base_index1, base_index2,
260 weight0, weight1, weight2
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 if not material in self.indexArrays:
267 self.indexArrays[material]=[]
268 self.indexArrays[material]+=[index0, index1, index2]
272 __slots__=['name', 'type', 'offsets']
273 def __init__(self, name, type):
278 def add(self, index, offset):
279 self.offsets.append((index, offset))
283 self.offsets.sort(lambda l, r: l[0]-r[0])
285 self.offsets.sort(key=lambda e: e[0])
288 return "<Morph %s>" % self.name
290 class IKSolver(object):
291 __slots__=['target', 'effector', 'length', 'iterations', 'weight']
292 def __init__(self, target, effector, length, iterations, weight):
294 self.effector=effector
296 self.iterations=iterations
300 class OneSkinMesh(object):
301 __slots__=['obj_index', 'scene', 'vertexArray', 'morphList',
305 def __init__(self, scene):
306 self.vertexArray=VertexArray()
314 return "<OneSkinMesh %s, morph:%d>" % (
318 def addMesh(self, obj):
319 if bl.object.isVisible(obj):
323 self.__rigidbody(obj)
324 self.__constraint(obj)
326 def __getWeightMap(self, obj, mesh):
330 def setWeight(i, name, w):
333 if i in secondWeightMap:
335 if w<secondWeightMap[i][1]:
337 elif w<weightMap[i][1]:
339 secondWeightMap[i]=(name, w)
342 weightMap[i]=(name, w)
344 if w>weightMap[i][1]:
346 secondWeightMap[i]=weightMap[i]
347 weightMap[i]=(name, w)
349 secondWeightMap[i]=(name, w)
351 weightMap[i]=(name, w)
354 for name in bl.object.getVertexGroupNames(obj):
355 for i, w in mesh.getVertsFromGroup(name, 1):
356 setWeight(i, name, w)
358 for i, v in enumerate(mesh.verts):
361 setWeight(i, obj.vertex_groups[g.group].name, g.weight)
363 setWeight(i, obj.vertex_groups[0].name, 1)
366 for i in xrange(len(mesh.verts)):
367 #for i, name_weight in weightMap.items():
368 if i in secondWeightMap:
369 secondWeightMap[i]=(secondWeightMap[i][0], 1.0-weightMap[i][1])
371 weightMap[i]=(weightMap[i][0], 1.0)
372 secondWeightMap[i]=("", 0)
374 print("no weight vertex")
376 secondWeightMap[i]=("", 0)
378 return weightMap, secondWeightMap
380 def __processFaces(self, mesh, weightMap, secondWeightMap):
382 for i, face in enumerate(mesh.faces):
383 faceVertexCount=bl.face.getVertexCount(face)
384 material=mesh.materials[bl.face.getMaterialIndex(face)]
385 v=[mesh.verts[index] for index in bl.face.getVertices(face)]
386 uv=bl.mesh.getFaceUV(
387 mesh, i, face, bl.face.getVertexCount(face))
389 if faceVertexCount==3:
391 self.vertexArray.addTriangle(
392 self.obj_index, material.name,
400 #v0.no, v1.no, v2.no,
401 bl.face.getNormal(face),
402 bl.face.getNormal(face),
403 bl.face.getNormal(face),
407 weightMap[v[2].index][0],
408 weightMap[v[1].index][0],
409 weightMap[v[0].index][0],
410 secondWeightMap[v[2].index][0],
411 secondWeightMap[v[1].index][0],
412 secondWeightMap[v[0].index][0],
413 weightMap[v[2].index][1],
414 weightMap[v[1].index][1],
415 weightMap[v[0].index][1]
417 elif faceVertexCount==4:
419 self.vertexArray.addTriangle(
420 self.obj_index, material.name,
427 #v0.no, v1.no, v2.no,
428 bl.face.getNormal(face),
429 bl.face.getNormal(face),
430 bl.face.getNormal(face),
434 weightMap[v[2].index][0],
435 weightMap[v[1].index][0],
436 weightMap[v[0].index][0],
437 secondWeightMap[v[2].index][0],
438 secondWeightMap[v[1].index][0],
439 secondWeightMap[v[0].index][0],
440 weightMap[v[2].index][1],
441 weightMap[v[1].index][1],
442 weightMap[v[0].index][1]
444 self.vertexArray.addTriangle(
445 self.obj_index, material.name,
452 #v2.no, v3.no, v0.no,
453 bl.face.getNormal(face),
454 bl.face.getNormal(face),
455 bl.face.getNormal(face),
459 weightMap[v[0].index][0],
460 weightMap[v[3].index][0],
461 weightMap[v[2].index][0],
462 secondWeightMap[v[0].index][0],
463 secondWeightMap[v[3].index][0],
464 secondWeightMap[v[2].index][0],
465 weightMap[v[0].index][1],
466 weightMap[v[3].index][1],
467 weightMap[v[2].index][1]
470 def __mesh(self, obj):
474 if RIGID_SHAPE_TYPE in obj:
476 if CONSTRAINT_A in obj:
479 #if not bl.modifier.hasType(obj, 'ARMATURE'):
482 bl.message("export: %s" % obj.name)
484 # メッシュのコピーを生成してオブジェクトの行列を適用する
485 copyMesh, copyObj=bl.object.duplicate(obj)
486 if len(copyMesh.verts)==0:
489 copyObj.scale=obj.scale
490 bpy.ops.object.scale_apply()
491 copyObj.rotation_euler=obj.rotation_euler
492 bpy.ops.object.rotation_apply()
493 copyObj.location=obj.location
494 bpy.ops.object.location_apply()
496 for m in [m for m in copyObj.modifiers]:
497 if m.type=='SOLIDFY':
499 elif m.type=='ARMATURE':
501 elif m.type=='MIRROR':
502 bpy.ops.object.modifier_apply(modifier=m.name)
506 weightMap, secondWeightMap=self.__getWeightMap(copyObj, copyMesh)
507 self.__processFaces(copyMesh, weightMap, secondWeightMap)
509 bl.object.delete(copyObj)
512 def createEmptyBasicSkin(self):
513 self.__getOrCreateMorph('base', 0)
515 def __skin(self, obj):
516 if not bl.object.hasShapeKey(obj):
520 blenderMesh=bl.object.getData(obj)
524 vg=bl.object.getVertexGroup(obj, MMD_SHAPE_GROUP_NAME)
528 for b in bl.object.getShapeKeys(obj):
529 if b.name==BASE_SHAPE_NAME:
530 baseMorph=self.__getOrCreateMorph('base', 0)
535 v=bl.shapekey.getByIndex(b, index)
536 pos=[v[0], v[1], v[2]]
537 indices=self.vertexArray.getMappedIndices(index)
543 baseMorph.add(i, pos)
544 indexRelativeMap[i]=relativeIndex
549 print(basis.name, len(baseMorph.offsets))
551 if len(baseMorph.offsets)==0:
555 for b in bl.object.getShapeKeys(obj):
556 if b.name==BASE_SHAPE_NAME:
560 morph=self.__getOrCreateMorph(b.name, 4)
562 for index, src, dst in zip(
563 xrange(len(blenderMesh.verts)),
564 bl.shapekey.get(basis),
566 offset=[dst[0]-src[0], dst[1]-src[1], dst[2]-src[2]]
567 if offset[0]==0 and offset[1]==0 and offset[2]==0:
570 indices=self.vertexArray.getMappedIndices(index)
575 morph.add(indexRelativeMap[i], offset)
578 original=self.morphList[:]
580 for i, v in enumerate(englishmap.skinMap):
584 return len(englishmap.skinMap)
586 self.morphList.sort(lambda l, r: getIndex(l)-getIndex(r))
588 self.morphList.sort(key=getIndex)
590 def __rigidbody(self, obj):
593 if not RIGID_SHAPE_TYPE in obj:
595 self.rigidbodies.append(obj)
597 def __constraint(self, obj):
600 if not CONSTRAINT_A in obj:
602 self.constraints.append(obj)
604 def __getOrCreateMorph(self, name, type):
605 for m in self.morphList:
609 self.morphList.append(m)
612 def getVertexCount(self):
613 return len(self.vertexArray.vertices)
617 __slots__=['index', 'name', 'ik_index',
618 'pos', 'tail', 'parent_index', 'tail_index', 'type', 'isConnect']
619 def __init__(self, name, pos, tail):
624 self.parent_index=None
630 def __eq__(self, rhs):
631 return self.index==rhs.index
634 return "<Bone %s %d>" % (self.name, self.type)
636 class BoneBuilder(object):
637 __slots__=['bones', 'boneMap', 'ik_list', 'bone_groups',]
644 def getBoneGroup(self, bone):
645 for i, g in enumerate(self.bone_groups):
649 print('no gorup', bone)
652 def build(self, armatureObj):
656 bl.message("build skeleton")
657 armature=bl.object.getData(armatureObj)
662 for g in bl.object.boneGroups(armatureObj):
663 self.bone_groups.append((g.name, []))
668 for b in armature.bones.values():
672 bl.bone.getHeadLocal(b),
673 bl.bone.getTailLocal(b))
675 self.__getBone(bone, b)
677 for b in armature.bones.values():
678 if not b.parent and b.name!='center':
681 bl.bone.getHeadLocal(b),
682 bl.bone.getTailLocal(b))
684 self.__getBone(bone, b)
686 for b in armature.bones.values():
688 self.__checkConnection(b, None)
693 pose = bl.object.getPose(armatureObj)
694 for b in pose.bones.values():
698 self.__assignBoneGroup(b, b.bone_group)
699 for c in b.constraints:
700 if bl.constraint.isIKSolver(c):
704 target=self.__boneByName(bl.constraint.ikTarget(c))
711 link=self.__boneByName(b.name)
716 chainLength=bl.constraint.ikChainLen(c)
717 for i in range(chainLength):
719 chainBone=self.__boneByName(e.name)
721 chainBone.ik_index=target.index
724 IKSolver(target, link, chainLength,
725 int(bl.constraint.ikItration(c) * 0.1),
726 bl.constraint.ikRotationWeight(c)
736 for i, v in enumerate(englishmap.boneMap):
737 if v[0]==ik.target.name:
739 return len(englishmap.boneMap)
741 self.ik_list.sort(lambda l, r: getIndex(l)-getIndex(r))
743 self.ik_list.sort(key=getIndex)
745 def __assignBoneGroup(self, poseBone, boneGroup):
747 for g in self.bone_groups:
748 if g[0]==boneGroup.name:
749 g[1].append(poseBone.name)
751 def __checkConnection(self, b, p):
752 if bl.bone.isConnected(b):
753 parent=self.__boneByName(p.name)
754 parent.isConnect=True
757 self.__checkConnection(c, b)
763 boneMap=englishmap.boneMap
764 original=self.bones[:]
766 for i, k_v in enumerate(boneMap):
767 if k_v[0]==bone.name:
773 self.bones.sort(lambda l, r: getIndex(l)-getIndex(r))
775 self.bones.sort(key=getIndex)
778 for i, b in enumerate(self.bones):
779 src=original.index(b)
782 b.index=sortMap[b.index]
784 b.parent_index=sortMap[b.parent_index]
786 b.tail_index=sortMap[b.tail_index]
788 b.ik_index=sortMap[b.ik_index]
796 if b.parent_index==None:
797 b.parent_index=0xFFFF
799 if b.type==6 or b.type==7:
801 parent=self.bones[b.parent_index]
802 parent.tail_index=b.index
805 if b.tail_index==None:
810 def getIndex(self, bone):
811 for i, b in enumerate(self.bones):
816 def indexByName(self, name):
820 return self.getIndex(self.__boneByName(name))
822 def __boneByName(self, name):
823 return self.boneMap[name]
825 def __getBone(self, parent, b):
826 if len(b.children)==0:
830 for i, c in enumerate(b.children):
832 bl.bone.getHeadLocal(c),
833 bl.bone.getTailLocal(c))
836 bone.parent_index=parent.index
838 parent.tail_index=bone.index
839 self.__getBone(bone, c)
841 def __addBone(self, bone):
842 bone.index=len(self.bones)
843 self.bones.append(bone)
844 self.boneMap[bone.name]=bone
847 class PmdExporter(object):
849 def setup(self, scene):
850 self.armatureObj=None
855 for o in scene.objects:
856 object_node_map[o]=Node(o)
857 for o in scene.objects:
858 #for node in object_node_map.values():
859 node=object_node_map[o]
861 object_node_map[node.o.parent].children.append(node)
864 root=object_node_map[scene.objects.active]
867 self.oneSkinMesh=OneSkinMesh(scene)
868 self.__createOneSkinMesh(root)
869 bl.message(self.oneSkinMesh)
870 if len(self.oneSkinMesh.morphList)==0:
872 self.oneSkinMesh.createEmptyBasicSkin()
874 self.name=root.o.name
877 self.skeleton=BoneBuilder()
878 self.skeleton.build(self.armatureObj)
880 def __createOneSkinMesh(self, node):
881 ############################################################
882 # search armature modifier
883 ############################################################
884 for m in node.o.modifiers:
885 if bl.modifier.isType(m, 'ARMATURE'):
886 armatureObj=bl.modifier.getArmatureObject(m)
887 if not self.armatureObj:
888 self.armatureObj=armatureObj
889 elif self.armatureObj!=armatureObj:
890 print("warning! found multiple armature. ignored.",
893 if node.o.type.upper()=='MESH':
894 self.oneSkinMesh.addMesh(node.o)
896 for child in node.children:
897 self.__createOneSkinMesh(child)
899 def write(self, path, scene):
902 io.comment="blender export"
906 for pos, normal, uv, b0, b1, weight in self.oneSkinMesh.vertexArray.zip():
907 # convert right-handed z-up to left-handed y-up
916 v.uv.y=1.0-uv[1] # reverse vertical
917 v.bone0=self.skeleton.indexByName(b0)
918 v.bone1=self.skeleton.indexByName(b1)
919 v.weight0=int(100*weight)
920 v.edge_flag=0 # edge flag, 0: enable edge, 1: not edge
923 vertexCount=self.oneSkinMesh.getVertexCount()
924 for material_name, indices in self.oneSkinMesh.vertexArray.each():
925 #print('material:', material_name)
926 m=bl.material.get(material_name)
928 material=io.addMaterial()
929 setMaterialParams(material, m)
931 material.vertex_count=len(indices)
932 material.toon_index=0
933 textures=[os.path.basename(path)
934 for path in bl.material.eachTexturePath(m)]
936 material.setTexture(toCP932('*'.join(textures)))
938 material.setTexture(toCP932(""))
941 assert(i<vertexCount)
942 for i in xrange(0, len(indices), 3):
944 io.indices.append(indices[i])
945 io.indices.append(indices[i+1])
946 io.indices.append(indices[i+2])
950 for i, b in enumerate(self.skeleton.bones):
954 boneNameMap[b.name]=i
955 v=englishmap.getUnicodeBoneName(b.name)
959 cp932=v[1].encode('cp932')
960 assert(len(cp932)<20)
964 bone_english_name=toCP932(b.name)
965 assert(len(bone_english_name)<20)
966 bone.setEnglishName(bone_english_name)
971 b.ik_index=self.skeleton.indexByName('eyes')
976 bone.parent_index=b.parent_index
977 bone.tail_index=b.tail_index
978 bone.ik_index=b.ik_index
980 # convert right-handed z-up to left-handed y-up
981 bone.pos.x=b.pos[0] if not near(b.pos[0], 0) else 0
982 bone.pos.y=b.pos[2] if not near(b.pos[2], 0) else 0
983 bone.pos.z=b.pos[1] if not near(b.pos[1], 0) else 0
986 for ik in self.skeleton.ik_list:
988 solver.index=self.skeleton.getIndex(ik.target)
989 solver.target=self.skeleton.getIndex(ik.effector)
990 solver.length=ik.length
991 b=self.skeleton.bones[ik.effector.parent_index]
992 for i in xrange(solver.length):
993 solver.children.append(self.skeleton.getIndex(b))
994 b=self.skeleton.bones[b.parent_index]
995 solver.iterations=ik.iterations
996 solver.weight=ik.weight
999 for i, m in enumerate(self.oneSkinMesh.morphList):
1003 v=englishmap.getUnicodeSkinName(m.name)
1005 v=[m.name, m.name, 0]
1007 cp932=v[1].encode('cp932')
1008 morph.setName(cp932)
1009 morph.setEnglishName(m.name.encode('cp932'))
1012 for index, offset in m.offsets:
1013 # convert right-handed z-up to left-handed y-up
1014 morph.append(index, offset[0], offset[2], offset[1])
1015 morph.vertex_count=len(m.offsets)
1019 for i, m in enumerate(self.oneSkinMesh.morphList):
1021 io.face_list.append(i)
1022 for i, m in enumerate(self.oneSkinMesh.morphList):
1024 io.face_list.append(i)
1025 for i, m in enumerate(self.oneSkinMesh.morphList):
1027 io.face_list.append(i)
1028 for i, m in enumerate(self.oneSkinMesh.morphList):
1030 io.face_list.append(i)
1033 for g in self.skeleton.bone_groups:
1034 boneDisplayName=io.addBoneGroup()
1036 name=englishmap.getUnicodeBoneGroupName(g[0])
1039 boneDisplayName.setName(toCP932(name+'\n'))
1042 boneDisplayName.setEnglishName(toCP932(englishName+'\n'))
1045 for i, b in enumerate(self.skeleton.bones):
1048 if b.type in [6, 7]:
1050 io.addBoneDisplay(i, self.skeleton.getBoneGroup(b))
1052 #assert(len(io.bones)==len(io.bone_display_list)+1)
1055 io.setEnglishName(toCP932("blender export\n"))
1056 io.setEnglishComment(toCP932("blender export comment"))
1060 for o in scene.objects:
1061 if o.name.startswith(TOON_TEXTURE_OBJECT):
1065 toonMesh=bl.object.getData(toonMeshObject)
1066 toonMaterial=bl.mesh.getMaterial(toonMesh, 0)
1068 t=bl.material.getTexture(toonMaterial, i)
1070 io.getToonTexture(i).setName(toCP932(t.name))
1072 io.getToonTexture(i).setName(toCP932("toon%02d.bmp\n" % i))
1075 io.getToonTexture(i).setName(toCP932("toon%02d.bmp\n" % i))
1079 for i, obj in enumerate(self.oneSkinMesh.rigidbodies):
1080 rigidBody=io.addRigidBody()
1081 rigidBody.setName(obj.name.encode('cp932'))
1082 rigidNameMap[obj.name]=i
1083 boneIndex=boneNameMap[obj[RIGID_BONE_NAME]]
1086 bone=self.skeleton.bones[0]
1088 bone=self.skeleton.bones[boneIndex]
1089 rigidBody.boneIndex=boneIndex
1090 #rigidBody.position.x=obj[RIGID_LOCATION][0]
1091 #rigidBody.position.y=obj[RIGID_LOCATION][1]
1092 #rigidBody.position.z=obj[RIGID_LOCATION][2]
1093 rigidBody.position.x=obj.location.x-bone.pos[0]
1094 rigidBody.position.y=obj.location.z-bone.pos[2]
1095 rigidBody.position.z=obj.location.y-bone.pos[1]
1096 rigidBody.rotation.x=-obj.rotation_euler[0]
1097 rigidBody.rotation.y=-obj.rotation_euler[2]
1098 rigidBody.rotation.z=-obj.rotation_euler[1]
1099 rigidBody.processType=obj[RIGID_PROCESS_TYPE]
1100 rigidBody.group=obj[RIGID_GROUP]
1101 rigidBody.target=obj[RIGID_INTERSECTION_GROUP]
1102 rigidBody.weight=obj[RIGID_WEIGHT]
1103 rigidBody.linearDamping=obj[RIGID_LINEAR_DAMPING]
1104 rigidBody.angularDamping=obj[RIGID_ANGULAR_DAMPING]
1105 rigidBody.restitution=obj[RIGID_RESTITUTION]
1106 rigidBody.friction=obj[RIGID_FRICTION]
1107 if obj[RIGID_SHAPE_TYPE]==0:
1108 rigidBody.shapeType=pmd.SHAPE_SPHERE
1109 rigidBody.w=obj.scale[0]
1110 elif obj[RIGID_SHAPE_TYPE]==1:
1111 rigidBody.shapeType=pmd.SHAPE_BOX
1112 rigidBody.w=obj.scale[0]
1113 rigidBody.d=obj.scale[1]
1114 rigidBody.h=obj.scale[2]
1115 elif obj[RIGID_SHAPE_TYPE]==2:
1116 rigidBody.shapeType=pmd.SHAPE_CAPSULE
1117 rigidBody.w=obj.scale[0]
1118 rigidBody.h=obj.scale[2]
1121 for obj in self.oneSkinMesh.constraints:
1122 constraint=io.addConstraint()
1123 constraint.setName(obj[CONSTRAINT_NAME].encode('cp932'))
1124 constraint.rigidA=rigidNameMap[obj[CONSTRAINT_A]]
1125 constraint.rigidB=rigidNameMap[obj[CONSTRAINT_B]]
1126 constraint.pos.x=obj.location[0]
1127 constraint.pos.y=obj.location[2]
1128 constraint.pos.z=obj.location[1]
1129 constraint.rot.x=-obj.rotation_euler[0]
1130 constraint.rot.y=-obj.rotation_euler[2]
1131 constraint.rot.z=-obj.rotation_euler[1]
1132 constraint.constraintPosMin.x=obj[CONSTRAINT_POS_MIN][0]
1133 constraint.constraintPosMin.y=obj[CONSTRAINT_POS_MIN][1]
1134 constraint.constraintPosMin.z=obj[CONSTRAINT_POS_MIN][2]
1135 constraint.constraintPosMax.x=obj[CONSTRAINT_POS_MAX][0]
1136 constraint.constraintPosMax.y=obj[CONSTRAINT_POS_MAX][1]
1137 constraint.constraintPosMax.z=obj[CONSTRAINT_POS_MAX][2]
1138 constraint.constraintRotMin.x=obj[CONSTRAINT_ROT_MIN][0]
1139 constraint.constraintRotMin.y=obj[CONSTRAINT_ROT_MIN][1]
1140 constraint.constraintRotMin.z=obj[CONSTRAINT_ROT_MIN][2]
1141 constraint.constraintRotMax.x=obj[CONSTRAINT_ROT_MAX][0]
1142 constraint.constraintRotMax.y=obj[CONSTRAINT_ROT_MAX][1]
1143 constraint.constraintRotMax.z=obj[CONSTRAINT_ROT_MAX][2]
1144 constraint.springPos.x=obj[CONSTRAINT_SPRING_POS][0]
1145 constraint.springPos.y=obj[CONSTRAINT_SPRING_POS][1]
1146 constraint.springPos.z=obj[CONSTRAINT_SPRING_POS][2]
1147 constraint.springRot.x=obj[CONSTRAINT_SPRING_ROT][0]
1148 constraint.springRot.y=obj[CONSTRAINT_SPRING_ROT][1]
1149 constraint.springRot.z=obj[CONSTRAINT_SPRING_ROT][2]
1152 bl.message('write %s' % path)
1153 return io.write(path)
1156 def __execute(filename, scene):
1157 if not scene.objects.active:
1158 print("abort. no active object.")
1161 active=bl.object.getActive()
1162 exporter=PmdExporter()
1163 exporter.setup(scene)
1164 exporter.write(filename, scene)
1165 bl.object.activate(active)
1170 def execute_24(filename):
1171 scene = bpy.data.scenes.active
1172 bl.initialize('pmd_export', scene)
1173 __execute(filename.decode(bl.INTERNAL_ENCODING), scene)
1176 Blender.Window.FileSelector(
1178 'Export Metasequoia PMD',
1179 Blender.sys.makename(ext='.pmd'))
1183 def execute_25(filename, scene):
1184 bl.initialize('pmd_export', scene)
1185 __execute(filename, scene)
1189 class EXPORT_OT_pmd(bpy.types.Operator):
1190 '''Save a Metasequoia PMD file.'''
1191 bl_idname = "export_scene.pmd"
1192 bl_label = 'Export PMD'
1194 # List of operator properties, the attributes will be assigned
1195 # to the class instance from the operator settings before calling.
1197 path = StringProperty(
1199 description="File path used for exporting the PMD file",
1203 filename = StringProperty(
1205 description="Name of the file.")
1206 directory = StringProperty(
1208 description="Directory of the file.")
1210 check_existing = BoolProperty(
1211 name="Check Existing",
1212 description="Check and warn on overwriting existing files",
1214 options=set('HIDDEN'))
1216 def execute(self, context):
1218 self.properties.path,
1223 def invoke(self, context, event):
1225 wm.add_fileselect(self)
1226 return 'RUNNING_MODAL'
1229 def menu_func(self, context):
1230 #default_path=bpy.data.filename.replace(".blend", ".pmd")
1231 self.layout.operator(
1232 EXPORT_OT_pmd.bl_idname,
1233 text="Miku Miku Dance Model(.pmd)")#.path=default_path
1236 bpy.types.register(EXPORT_OT_pmd)
1237 bpy.types.INFO_MT_file_export.append(menu_func)
1240 bpy.types.unregister(EXPORT_OT_pmd)
1241 bpy.types.INFO_MT_file_export.remove(menu_func)
1243 if __name__ == "__main__":