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
123 material.toon_index=7
126 return s.encode('cp932')
130 __slots__=['o', 'children']
131 def __init__(self, o):
136 ###############################################################################
137 # Blenderのメッシュをワンスキンメッシュ化する
138 ###############################################################################
139 def near(x, y, EPSILON=1e-5):
141 return d>=-EPSILON and d<=EPSILON
144 class VertexKey(object):
151 'nx', 'ny', 'nz', # 法線
155 def __init__(self, obj, index, x, y, z, nx, ny, nz, u, v):
168 return "<vkey: %f, %f, %f, %f, %f, %f, %f, %f>" % (
169 self.x, self.y, self.z, self.nx, self.ny, self.nz, self.u, self.v)
172 #return int((self.x+self.y+self.z+self.nx+self.ny+self.nz+self.u+self.v)*100)
173 return int((self.x+self.y+self.z)*100)
175 def __eq__(self, rhs):
176 #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)
177 #return near(self.x, rhs.x) and near(self.y, rhs.y) and near(self.z, rhs.z)
178 #return self.x==rhs.x and self.y==rhs.y and self.z==rhs.z
179 return self.obj==rhs.obj and self.index==rhs.index
182 class VertexArray(object):
187 # マテリアル毎に分割したインデックス配列
202 return "<VertexArray %d vertices, %d indexArrays>" % (
203 len(self.vertices), len(self.indexArrays))
207 self.vertices, self.normals, self.uvs,
208 self.b0, self.b1, self.weight)
211 keys=[key for key in self.indexArrays.keys()]
214 yield(key, self.indexArrays[key])
216 def __getIndex(self, obj, base_index, pos, normal, uv, b0, b1, weight0):
222 pos[0], pos[1], pos[2],
223 normal[0], normal[1], normal[2],
225 if key in self.vertexMap:
227 index=self.vertexMap[key]
229 index=len(self.vertices)
231 self.vertexMap[key]=index
233 self.vertices.append((pos.x, pos.y, pos.z))
234 self.normals.append((normal.x, normal.y, normal.z))
235 self.uvs.append((uv[0], uv[1]))
238 self.weight.append(weight0)
241 if not base_index in self.indexMap:
242 self.indexMap[base_index]=set()
243 self.indexMap[base_index].add(index)
248 def getMappedIndices(self, base_index):
249 return self.indexMap[base_index]
251 def addTriangle(self,
253 base_index0, base_index1, base_index2,
259 weight0, weight1, weight2
261 index0=self.__getIndex(obj, base_index0, pos0, n0, uv0, b0_0, b1_0, weight0)
262 index1=self.__getIndex(obj, base_index1, pos1, n1, uv1, b0_1, b1_1, weight1)
263 index2=self.__getIndex(obj, base_index2, pos2, n2, uv2, b0_2, b1_2, weight2)
265 if not material in self.indexArrays:
266 self.indexArrays[material]=[]
267 self.indexArrays[material]+=[index0, index1, index2]
271 __slots__=['name', 'type', 'offsets']
272 def __init__(self, name, type):
277 def add(self, index, offset):
278 self.offsets.append((index, offset))
282 self.offsets.sort(lambda l, r: l[0]-r[0])
284 self.offsets.sort(key=lambda e: e[0])
287 return "<Morph %s>" % self.name
289 class IKSolver(object):
290 __slots__=['target', 'effector', 'length', 'iterations', 'weight']
291 def __init__(self, target, effector, length, iterations, weight):
293 self.effector=effector
295 self.iterations=iterations
299 class OneSkinMesh(object):
300 __slots__=['obj_index', 'scene', 'vertexArray', 'morphList',
304 def __init__(self, scene):
305 self.vertexArray=VertexArray()
313 return "<OneSkinMesh %s, morph:%d>" % (
317 def addMesh(self, obj):
318 if bl.object.isVisible(obj):
320 if not bl.modifier.hasType(obj, 'ARMATURE'):
324 self.__rigidbody(obj)
325 self.__constraint(obj)
327 def __getWeightMap(self, obj, mesh):
331 def setWeight(i, name, w):
334 if i in secondWeightMap:
336 if w<secondWeightMap[i][1]:
338 elif w<weightMap[i][1]:
340 secondWeightMap[i]=(name, w)
343 weightMap[i]=(name, w)
345 if w>weightMap[i][1]:
347 secondWeightMap[i]=weightMap[i]
348 weightMap[i]=(name, w)
350 secondWeightMap[i]=(name, w)
352 weightMap[i]=(name, w)
355 for name in bl.object.getVertexGroupNames(obj):
356 for i, w in mesh.getVertsFromGroup(name, 1):
357 setWeight(i, name, w)
359 for i, v in enumerate(mesh.verts):
362 setWeight(i, obj.vertex_groups[g.group].name, g.weight)
364 setWeight(i, obj.vertex_groups[0].name, 1)
367 for i in xrange(len(mesh.verts)):
368 #for i, name_weight in weightMap.items():
369 if i in secondWeightMap:
370 secondWeightMap[i]=(secondWeightMap[i][0], 1.0-weightMap[i][1])
372 weightMap[i]=(weightMap[i][0], 1.0)
373 secondWeightMap[i]=("", 0)
375 print("no weight vertex")
377 secondWeightMap[i]=("", 0)
379 return weightMap, secondWeightMap
381 def __processFaces(self, mesh, weightMap, secondWeightMap):
383 for i, face in enumerate(mesh.faces):
384 faceVertexCount=bl.face.getVertexCount(face)
385 material=mesh.materials[bl.face.getMaterialIndex(face)]
386 v=[mesh.verts[index] for index in bl.face.getVertices(face)]
387 uv=bl.mesh.getFaceUV(
388 mesh, i, face, bl.face.getVertexCount(face))
390 if faceVertexCount==3:
392 self.vertexArray.addTriangle(
393 self.obj_index, material.name,
401 #v0.no, v1.no, v2.no,
402 bl.face.getNormal(face),
403 bl.face.getNormal(face),
404 bl.face.getNormal(face),
408 weightMap[v[2].index][0],
409 weightMap[v[1].index][0],
410 weightMap[v[0].index][0],
411 secondWeightMap[v[2].index][0],
412 secondWeightMap[v[1].index][0],
413 secondWeightMap[v[0].index][0],
414 weightMap[v[2].index][1],
415 weightMap[v[1].index][1],
416 weightMap[v[0].index][1]
418 elif faceVertexCount==4:
420 self.vertexArray.addTriangle(
421 self.obj_index, material.name,
428 #v0.no, v1.no, v2.no,
429 bl.face.getNormal(face),
430 bl.face.getNormal(face),
431 bl.face.getNormal(face),
435 weightMap[v[2].index][0],
436 weightMap[v[1].index][0],
437 weightMap[v[0].index][0],
438 secondWeightMap[v[2].index][0],
439 secondWeightMap[v[1].index][0],
440 secondWeightMap[v[0].index][0],
441 weightMap[v[2].index][1],
442 weightMap[v[1].index][1],
443 weightMap[v[0].index][1]
445 self.vertexArray.addTriangle(
446 self.obj_index, material.name,
453 #v2.no, v3.no, v0.no,
454 bl.face.getNormal(face),
455 bl.face.getNormal(face),
456 bl.face.getNormal(face),
460 weightMap[v[0].index][0],
461 weightMap[v[3].index][0],
462 weightMap[v[2].index][0],
463 secondWeightMap[v[0].index][0],
464 secondWeightMap[v[3].index][0],
465 secondWeightMap[v[2].index][0],
466 weightMap[v[0].index][1],
467 weightMap[v[3].index][1],
468 weightMap[v[2].index][1]
471 def __mesh(self, obj):
475 if RIGID_SHAPE_TYPE in obj:
477 if CONSTRAINT_A in obj:
480 bl.message("export: %s" % obj.name)
482 # メッシュのコピーを生成してオブジェクトの行列を適用する
483 copyMesh, copyObj=bl.object.duplicate(obj)
484 if len(copyMesh.verts)==0:
487 copyObj.scale=obj.scale
488 bpy.ops.object.scale_apply()
489 copyObj.rotation_euler=obj.rotation_euler
490 bpy.ops.object.rotation_apply()
491 copyObj.location=obj.location
492 bpy.ops.object.location_apply()
494 for m in [m for m in copyObj.modifiers]:
495 if m.type=='SOLIDFY':
497 elif m.type=='ARMATURE':
499 elif m.type=='MIRROR':
500 bpy.ops.object.modifier_apply(modifier=m.name)
504 weightMap, secondWeightMap=self.__getWeightMap(copyObj, copyMesh)
505 self.__processFaces(copyMesh, weightMap, secondWeightMap)
507 bl.object.delete(copyObj)
510 def createEmptyBasicSkin(self):
511 self.__getOrCreateMorph('base', 0)
513 def __skin(self, obj):
514 if not bl.object.hasShapeKey(obj):
518 blenderMesh=bl.object.getData(obj)
522 vg=bl.object.getVertexGroup(obj, MMD_SHAPE_GROUP_NAME)
526 for b in bl.object.getShapeKeys(obj):
527 if b.name==BASE_SHAPE_NAME:
528 baseMorph=self.__getOrCreateMorph('base', 0)
533 v=bl.shapekey.getByIndex(b, index)
534 pos=[v[0], v[1], v[2]]
535 indices=self.vertexArray.getMappedIndices(index)
541 baseMorph.add(i, pos)
542 indexRelativeMap[i]=relativeIndex
547 print(basis.name, len(baseMorph.offsets))
549 if len(baseMorph.offsets)==0:
553 for b in bl.object.getShapeKeys(obj):
554 if b.name==BASE_SHAPE_NAME:
558 morph=self.__getOrCreateMorph(b.name, 4)
560 for index, src, dst in zip(
561 xrange(len(blenderMesh.verts)),
562 bl.shapekey.get(basis),
564 offset=[dst[0]-src[0], dst[1]-src[1], dst[2]-src[2]]
565 if offset[0]==0 and offset[1]==0 and offset[2]==0:
568 indices=self.vertexArray.getMappedIndices(index)
573 morph.add(indexRelativeMap[i], offset)
576 original=self.morphList[:]
578 for i, v in enumerate(englishmap.skinMap):
582 return len(englishmap.skinMap)
584 self.morphList.sort(lambda l, r: getIndex(l)-getIndex(r))
586 self.morphList.sort(key=getIndex)
588 def __rigidbody(self, obj):
591 if not RIGID_SHAPE_TYPE in obj:
593 self.rigidbodies.append(obj)
595 def __constraint(self, obj):
598 if not CONSTRAINT_A in obj:
600 self.constraints.append(obj)
602 def __getOrCreateMorph(self, name, type):
603 for m in self.morphList:
607 self.morphList.append(m)
610 def getVertexCount(self):
611 return len(self.vertexArray.vertices)
615 __slots__=['index', 'name', 'ik_index',
616 'pos', 'tail', 'parent_index', 'tail_index', 'type', 'isConnect']
617 def __init__(self, name, pos, tail):
622 self.parent_index=None
628 def __eq__(self, rhs):
629 return self.index==rhs.index
632 return "<Bone %s %d>" % (self.name, self.type)
634 class BoneBuilder(object):
635 __slots__=['bones', 'boneMap', 'ik_list', 'bone_groups',]
642 def getBoneGroup(self, bone):
643 for i, g in enumerate(self.bone_groups):
647 print('no gorup', bone)
650 def build(self, armatureObj):
654 bl.message("build skeleton")
655 armature=bl.object.getData(armatureObj)
660 for g in bl.object.boneGroups(armatureObj):
661 self.bone_groups.append((g.name, []))
666 for b in armature.bones.values():
670 bl.bone.getHeadLocal(b),
671 bl.bone.getTailLocal(b))
673 self.__getBone(bone, b)
675 for b in armature.bones.values():
676 if not b.parent and b.name!='center':
679 bl.bone.getHeadLocal(b),
680 bl.bone.getTailLocal(b))
682 self.__getBone(bone, b)
684 for b in armature.bones.values():
686 self.__checkConnection(b, None)
691 pose = bl.object.getPose(armatureObj)
692 for b in pose.bones.values():
696 self.__assignBoneGroup(b, b.bone_group)
697 for c in b.constraints:
698 if bl.constraint.isIKSolver(c):
702 target=self.__boneByName(bl.constraint.ikTarget(c))
709 link=self.__boneByName(b.name)
714 chainLength=bl.constraint.ikChainLen(c)
715 for i in range(chainLength):
717 chainBone=self.__boneByName(e.name)
719 chainBone.ik_index=target.index
722 IKSolver(target, link, chainLength,
723 int(bl.constraint.ikItration(c) * 0.1),
724 bl.constraint.ikRotationWeight(c)
734 for i, v in enumerate(englishmap.boneMap):
735 if v[0]==ik.target.name:
737 return len(englishmap.boneMap)
739 self.ik_list.sort(lambda l, r: getIndex(l)-getIndex(r))
741 self.ik_list.sort(key=getIndex)
743 def __assignBoneGroup(self, poseBone, boneGroup):
745 for g in self.bone_groups:
746 if g[0]==boneGroup.name:
747 g[1].append(poseBone.name)
749 def __checkConnection(self, b, p):
750 if bl.bone.isConnected(b):
751 parent=self.__boneByName(p.name)
752 parent.isConnect=True
755 self.__checkConnection(c, b)
761 boneMap=englishmap.boneMap
762 original=self.bones[:]
764 for i, k_v in enumerate(boneMap):
765 if k_v[0]==bone.name:
771 self.bones.sort(lambda l, r: getIndex(l)-getIndex(r))
773 self.bones.sort(key=getIndex)
776 for i, b in enumerate(self.bones):
777 src=original.index(b)
780 b.index=sortMap[b.index]
782 b.parent_index=sortMap[b.parent_index]
784 b.tail_index=sortMap[b.tail_index]
786 b.ik_index=sortMap[b.ik_index]
794 if b.parent_index==None:
795 b.parent_index=0xFFFF
797 if b.type==6 or b.type==7:
799 parent=self.bones[b.parent_index]
800 parent.tail_index=b.index
803 if b.tail_index==None:
808 def getIndex(self, bone):
809 for i, b in enumerate(self.bones):
814 def indexByName(self, name):
818 return self.getIndex(self.__boneByName(name))
820 def __boneByName(self, name):
821 return self.boneMap[name]
823 def __getBone(self, parent, b):
824 if len(b.children)==0:
828 for i, c in enumerate(b.children):
830 bl.bone.getHeadLocal(c),
831 bl.bone.getTailLocal(c))
834 bone.parent_index=parent.index
836 parent.tail_index=bone.index
837 self.__getBone(bone, c)
839 def __addBone(self, bone):
840 bone.index=len(self.bones)
841 self.bones.append(bone)
842 self.boneMap[bone.name]=bone
845 class PmdExporter(object):
847 def setup(self, scene):
848 self.armatureObj=None
853 for o in scene.objects:
854 object_node_map[o]=Node(o)
855 for o in scene.objects:
856 #for node in object_node_map.values():
857 node=object_node_map[o]
859 object_node_map[node.o.parent].children.append(node)
862 root=object_node_map[scene.objects.active]
865 self.oneSkinMesh=OneSkinMesh(scene)
866 self.__createOneSkinMesh(root)
867 bl.message(self.oneSkinMesh)
868 if len(self.oneSkinMesh.morphList)==0:
870 self.oneSkinMesh.createEmptyBasicSkin()
872 self.name=root.o.name
875 self.skeleton=BoneBuilder()
876 self.skeleton.build(self.armatureObj)
878 def __createOneSkinMesh(self, node):
879 ############################################################
880 # search armature modifier
881 ############################################################
882 for m in node.o.modifiers:
883 if bl.modifier.isType(m, 'ARMATURE'):
884 armatureObj=bl.modifier.getArmatureObject(m)
885 if not self.armatureObj:
886 self.armatureObj=armatureObj
887 elif self.armatureObj!=armatureObj:
888 print("warning! found multiple armature. ignored.",
891 if node.o.type.upper()=='MESH':
892 self.oneSkinMesh.addMesh(node.o)
894 for child in node.children:
895 self.__createOneSkinMesh(child)
897 def write(self, path):
900 io.comment="blender export"
904 for pos, normal, uv, b0, b1, weight in self.oneSkinMesh.vertexArray.zip():
905 # convert right-handed z-up to left-handed y-up
914 v.uv.y=1.0-uv[1] # reverse vertical
915 v.bone0=self.skeleton.indexByName(b0)
916 v.bone1=self.skeleton.indexByName(b1)
917 v.weight0=int(100*weight)
918 v.edge_flag=0 # edge flag, 0: enable edge, 1: not edge
921 vertexCount=self.oneSkinMesh.getVertexCount()
922 for material_name, indices in self.oneSkinMesh.vertexArray.each():
923 #print('material:', material_name)
924 m=bl.material.get(material_name)
926 material=io.addMaterial()
927 setMaterialParams(material, m)
929 material.vertex_count=len(indices)
930 material.toon_index=0
931 textures=[os.path.basename(path)
932 for path in bl.material.eachTexturePath(m)]
934 material.setTexture(toCP932('*'.join(textures)))
936 material.setTexture(toCP932(""))
939 assert(i<vertexCount)
940 for i in xrange(0, len(indices), 3):
942 io.indices.append(indices[i])
943 io.indices.append(indices[i+1])
944 io.indices.append(indices[i+2])
948 for i, b in enumerate(self.skeleton.bones):
952 boneNameMap[b.name]=i
953 v=englishmap.getUnicodeBoneName(b.name)
957 cp932=v[1].encode('cp932')
958 assert(len(cp932)<20)
962 bone_english_name=toCP932(b.name)
963 assert(len(bone_english_name)<20)
964 bone.setEnglishName(bone_english_name)
969 b.ik_index=self.skeleton.indexByName('eyes')
974 bone.parent_index=b.parent_index
975 bone.tail_index=b.tail_index
976 bone.ik_index=b.ik_index
978 # convert right-handed z-up to left-handed y-up
979 bone.pos.x=b.pos[0] if not near(b.pos[0], 0) else 0
980 bone.pos.y=b.pos[2] if not near(b.pos[2], 0) else 0
981 bone.pos.z=b.pos[1] if not near(b.pos[1], 0) else 0
984 for ik in self.skeleton.ik_list:
986 solver.index=self.skeleton.getIndex(ik.target)
987 solver.target=self.skeleton.getIndex(ik.effector)
988 solver.length=ik.length
989 b=self.skeleton.bones[ik.effector.parent_index]
990 for i in xrange(solver.length):
991 solver.children.append(self.skeleton.getIndex(b))
992 b=self.skeleton.bones[b.parent_index]
993 solver.iterations=ik.iterations
994 solver.weight=ik.weight
997 for i, m in enumerate(self.oneSkinMesh.morphList):
1001 v=englishmap.getUnicodeSkinName(m.name)
1003 v=[m.name, m.name, 0]
1005 cp932=v[1].encode('cp932')
1006 morph.setName(cp932)
1007 morph.setEnglishName(m.name.encode('cp932'))
1010 for index, offset in m.offsets:
1011 # convert right-handed z-up to left-handed y-up
1012 morph.append(index, offset[0], offset[2], offset[1])
1013 morph.vertex_count=len(m.offsets)
1017 for i, m in enumerate(self.oneSkinMesh.morphList):
1019 io.face_list.append(i)
1020 for i, m in enumerate(self.oneSkinMesh.morphList):
1022 io.face_list.append(i)
1023 for i, m in enumerate(self.oneSkinMesh.morphList):
1025 io.face_list.append(i)
1026 for i, m in enumerate(self.oneSkinMesh.morphList):
1028 io.face_list.append(i)
1031 for g in self.skeleton.bone_groups:
1032 boneDisplayName=io.addBoneGroup()
1034 name=englishmap.getUnicodeBoneGroupName(g[0])
1037 boneDisplayName.setName(toCP932(name+'\n'))
1040 boneDisplayName.setEnglishName(toCP932(englishName+'\n'))
1043 for i, b in enumerate(self.skeleton.bones):
1046 if b.type in [6, 7]:
1048 io.addBoneDisplay(i, self.skeleton.getBoneGroup(b))
1051 io.english_name="blender export"
1052 io.english_coment="blender export"
1056 io.getToonTexture(i).name="toon%02d.bmp\n" % i
1060 for i, obj in enumerate(self.oneSkinMesh.rigidbodies):
1061 rigidBody=io.addRigidBody()
1062 rigidBody.setName(obj.name.encode('cp932'))
1063 rigidNameMap[obj.name]=i
1064 boneIndex=boneNameMap[obj[RIGID_BONE_NAME]]
1067 bone=self.skeleton.bones[0]
1069 bone=self.skeleton.bones[boneIndex]
1070 rigidBody.boneIndex=boneIndex
1071 #rigidBody.position.x=obj[RIGID_LOCATION][0]
1072 #rigidBody.position.y=obj[RIGID_LOCATION][1]
1073 #rigidBody.position.z=obj[RIGID_LOCATION][2]
1074 rigidBody.position.x=obj.location.x-bone.pos[0]
1075 rigidBody.position.y=obj.location.z-bone.pos[2]
1076 rigidBody.position.z=obj.location.y-bone.pos[1]
1077 rigidBody.rotation.x=-obj.rotation_euler[0]
1078 rigidBody.rotation.y=-obj.rotation_euler[2]
1079 rigidBody.rotation.z=-obj.rotation_euler[1]
1080 rigidBody.processType=obj[RIGID_PROCESS_TYPE]
1081 rigidBody.group=obj[RIGID_GROUP]
1082 rigidBody.target=obj[RIGID_INTERSECTION_GROUP]
1083 rigidBody.weight=obj[RIGID_WEIGHT]
1084 rigidBody.linearDamping=obj[RIGID_LINEAR_DAMPING]
1085 rigidBody.angularDamping=obj[RIGID_ANGULAR_DAMPING]
1086 rigidBody.restitution=obj[RIGID_RESTITUTION]
1087 rigidBody.friction=obj[RIGID_FRICTION]
1088 if obj[RIGID_SHAPE_TYPE]==0:
1089 rigidBody.shapeType=pmd.SHAPE_SPHERE
1090 rigidBody.w=obj.scale[0]
1091 elif obj[RIGID_SHAPE_TYPE]==1:
1092 rigidBody.shapeType=pmd.SHAPE_BOX
1093 rigidBody.w=obj.scale[0]
1094 rigidBody.d=obj.scale[1]
1095 rigidBody.h=obj.scale[2]
1096 elif obj[RIGID_SHAPE_TYPE]==2:
1097 rigidBody.shapeType=pmd.SHAPE_CAPSULE
1098 rigidBody.w=obj.scale[0]
1099 rigidBody.h=obj.scale[2]
1102 for obj in self.oneSkinMesh.constraints:
1103 constraint=io.addConstraint()
1104 constraint.setName(obj[CONSTRAINT_NAME].encode('cp932'))
1105 constraint.rigidA=rigidNameMap[obj[CONSTRAINT_A]]
1106 constraint.rigidB=rigidNameMap[obj[CONSTRAINT_B]]
1107 constraint.pos.x=obj.location[0]
1108 constraint.pos.y=obj.location[2]
1109 constraint.pos.z=obj.location[1]
1110 constraint.rot.x=-obj.rotation_euler[0]
1111 constraint.rot.y=-obj.rotation_euler[2]
1112 constraint.rot.z=-obj.rotation_euler[1]
1113 constraint.constraintPosMin.x=obj[CONSTRAINT_POS_MIN][0]
1114 constraint.constraintPosMin.y=obj[CONSTRAINT_POS_MIN][1]
1115 constraint.constraintPosMin.z=obj[CONSTRAINT_POS_MIN][2]
1116 constraint.constraintPosMax.x=obj[CONSTRAINT_POS_MAX][0]
1117 constraint.constraintPosMax.y=obj[CONSTRAINT_POS_MAX][1]
1118 constraint.constraintPosMax.z=obj[CONSTRAINT_POS_MAX][2]
1119 constraint.constraintRotMin.x=obj[CONSTRAINT_ROT_MIN][0]
1120 constraint.constraintRotMin.y=obj[CONSTRAINT_ROT_MIN][1]
1121 constraint.constraintRotMin.z=obj[CONSTRAINT_ROT_MIN][2]
1122 constraint.constraintRotMax.x=obj[CONSTRAINT_ROT_MAX][0]
1123 constraint.constraintRotMax.y=obj[CONSTRAINT_ROT_MAX][1]
1124 constraint.constraintRotMax.z=obj[CONSTRAINT_ROT_MAX][2]
1125 constraint.springPos.x=obj[CONSTRAINT_SPRING_POS][0]
1126 constraint.springPos.y=obj[CONSTRAINT_SPRING_POS][1]
1127 constraint.springPos.z=obj[CONSTRAINT_SPRING_POS][2]
1128 constraint.springRot.x=obj[CONSTRAINT_SPRING_ROT][0]
1129 constraint.springRot.y=obj[CONSTRAINT_SPRING_ROT][1]
1130 constraint.springRot.z=obj[CONSTRAINT_SPRING_ROT][2]
1133 bl.message('write %s' % path)
1134 return io.write(path)
1137 def __execute(filename, scene):
1138 if not scene.objects.active:
1139 print("abort. no active object.")
1142 active=bl.object.getActive()
1143 exporter=PmdExporter()
1144 exporter.setup(scene)
1145 exporter.write(filename)
1146 bl.object.activate(active)
1151 def execute_24(filename):
1152 scene = bpy.data.scenes.active
1153 bl.initialize('pmd_export', scene)
1154 __execute(filename.decode(bl.INTERNAL_ENCODING), scene)
1157 Blender.Window.FileSelector(
1159 'Export Metasequoia PMD',
1160 Blender.sys.makename(ext='.pmd'))
1164 def execute_25(filename, scene):
1165 bl.initialize('pmd_export', scene)
1166 __execute(filename, scene)
1170 class EXPORT_OT_pmd(bpy.types.Operator):
1171 '''Save a Metasequoia PMD file.'''
1172 bl_idname = "export_scene.pmd"
1173 bl_label = 'Export PMD'
1175 # List of operator properties, the attributes will be assigned
1176 # to the class instance from the operator settings before calling.
1178 path = StringProperty(
1180 description="File path used for exporting the PMD file",
1184 filename = StringProperty(
1186 description="Name of the file.")
1187 directory = StringProperty(
1189 description="Directory of the file.")
1191 check_existing = BoolProperty(
1192 name="Check Existing",
1193 description="Check and warn on overwriting existing files",
1195 options=set('HIDDEN'))
1197 def execute(self, context):
1199 self.properties.path,
1204 def invoke(self, context, event):
1206 wm.add_fileselect(self)
1207 return 'RUNNING_MODAL'
1210 def menu_func(self, context):
1211 #default_path=bpy.data.filename.replace(".blend", ".pmd")
1212 self.layout.operator(
1213 EXPORT_OT_pmd.bl_idname,
1214 text="Miku Miku Dance Model(.pmd)")#.path=default_path
1217 bpy.types.register(EXPORT_OT_pmd)
1218 bpy.types.INFO_MT_file_export.append(menu_func)
1221 bpy.types.unregister(EXPORT_OT_pmd)
1222 bpy.types.INFO_MT_file_export.remove(menu_func)
1224 if __name__ == "__main__":