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.
28 2.1 20100731: add full python module.
29 2.2 20101005: update for Blender2.54.
30 2.3 20101228: update for Blender2.55.
31 2.4 20110429: update for Blender2.57b.
35 'category': 'Import/Export',
36 'name': 'Export: MikuMikuDance Model Format (.pmd)',
40 'location': 'File > Export',
41 'description': 'Export to the MikuMikuDance Model Format (.pmd)',
42 'warning': '', # used for warning icon and text in addons panel
43 'wiki_url': 'http://sourceforge.jp/projects/meshio/wiki/FrontPage',
44 'tracker_url': 'http://sourceforge.jp/ticket/newticket.php?group_id=5081',
47 MMD_SHAPE_GROUP_NAME='_MMD_SHAPE'
49 MMD_MB_COMMENT='mb_comment'
51 BASE_SHAPE_NAME='Basis'
52 RIGID_NAME='rigid_name'
53 RIGID_SHAPE_TYPE='rigid_shape_type'
54 RIGID_PROCESS_TYPE='rigid_process_type'
55 RIGID_BONE_NAME='rigid_bone_name'
56 #RIGID_LOCATION='rigid_loation'
57 RIGID_GROUP='ribid_group'
58 RIGID_INTERSECTION_GROUP='rigid_intersection_group'
59 RIGID_WEIGHT='rigid_weight'
60 RIGID_LINEAR_DAMPING='rigid_linear_damping'
61 RIGID_ANGULAR_DAMPING='rigid_angular_damping'
62 RIGID_RESTITUTION='rigid_restitution'
63 RIGID_FRICTION='rigid_friction'
64 CONSTRAINT_NAME='constraint_name'
65 CONSTRAINT_A='const_a'
66 CONSTRAINT_B='const_b'
67 CONSTRAINT_POS_MIN='const_pos_min'
68 CONSTRAINT_POS_MAX='const_pos_max'
69 CONSTRAINT_ROT_MIN='const_rot_min'
70 CONSTRAINT_ROT_MAX='const_rot_max'
71 CONSTRAINT_SPRING_POS='const_spring_pos'
72 CONSTRAINT_SPRING_ROT='const_spring_rot'
73 TOON_TEXTURE_OBJECT='ToonTextures'
76 ###############################################################################
78 ###############################################################################
84 from meshio import pmd, englishmap
85 print('use meshio C module')
88 from .pymeshio import englishmap
89 from .pymeshio import pmd
97 from . import bl25 as bl
101 def setMaterialParams(material, m):
103 material.diffuse.r=m.diffuse_color[0]
104 material.diffuse.g=m.diffuse_color[1]
105 material.diffuse.b=m.diffuse_color[2]
106 material.diffuse.a=m.alpha
108 material.shinness=0 if m.specular_toon_size<1e-5 else m.specular_hardness*10
109 material.specular.r=m.specular_color[0]
110 material.specular.g=m.specular_color[1]
111 material.specular.b=m.specular_color[2]
113 material.ambient.r=m.mirror_color[0]
114 material.ambient.g=m.mirror_color[1]
115 material.ambient.b=m.mirror_color[2]
117 material.flag=1 if m.subsurface_scattering.use else 0
119 material.toon_index=7
122 return s.encode('cp932')
126 __slots__=['o', 'children']
127 def __init__(self, o):
132 ###############################################################################
133 # Blenderのメッシュをワンスキンメッシュ化する
134 ###############################################################################
135 def near(x, y, EPSILON=1e-5):
137 return d>=-EPSILON and d<=EPSILON
140 class VertexAttribute(object):
142 'nx', 'ny', 'nz', # normal
145 def __init__(self, nx, ny, nz, u, v):
153 return "<vkey: %f, %f, %f, %f, %f>" % (
154 self.nx, self.ny, self.nz, self.u, self.v)
157 return int(100*(self.nx + self.ny + self.nz + self.u + self.v))
159 def __eq__(self, rhs):
160 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
163 class VertexKey(object):
165 'obj_index', 'index',
168 def __init__(self, obj_index, index):
169 self.obj_index=obj_index
173 return "<vkey: %d, %d>" % (self.obj_index, self.index)
176 return self.index*100+self.obj_index
178 def __eq__(self, rhs):
179 return self.obj_index==rhs.obj_index and self.index==rhs.index
182 class VertexArray(object):
189 'attributes', # normal and uv
190 'b0', 'b1', 'weight',
195 # indexArrays split with each material
208 return "<VertexArray %d positions, %d indexArrays>" % (
209 len(self.positions), len(self.indexArrays))
213 self.positions, self.attributes,
214 self.b0, self.b1, self.weight)
217 keys=[key for key in self.indexArrays.keys()]
220 yield(key, self.indexArrays[key])
222 def __addOrGetIndex(self, obj_index, base_index, pos, normal, uv, b0, b1, weight0):
223 key=VertexKey(obj_index, base_index)
224 attribute=VertexAttribute(
225 normal[0], normal[1], normal[2],
227 if key in self.vertexMap:
228 if attribute in self.vertexMap[key]:
229 return self.vertexMap[key][attribute]
231 return self.__addVertex(self.vertexMap[key],
232 pos, attribute, b0, b1, weight0)
235 self.vertexMap[key]=vertexMapKey
236 return self.__addVertex(vertexMapKey,
237 pos, attribute, b0, b1, weight0)
239 def __addVertex(self, vertexMapKey, pos, attribute, b0, b1, weight0):
240 index=len(self.positions)
241 vertexMapKey[attribute]=index
243 self.positions.append((pos.x, pos.y, pos.z))
245 self.attributes.append(attribute)
249 self.weight.append(weight0)
253 def getMappedIndex(self, obj_name, base_index):
254 return self.vertexMap[VertexKey(self.objectMap[obj_name], base_index)]
256 def addTriangle(self,
257 object_name, material,
258 base_index0, base_index1, base_index2,
264 weight0, weight1, weight2
266 if object_name in self.objectMap:
267 obj_index=self.objectMap[object_name]
269 obj_index=len(self.objectMap)
270 self.objectMap[object_name]=obj_index
271 index0=self.__addOrGetIndex(obj_index, base_index0, pos0, n0, uv0, b0_0, b1_0, weight0)
272 index1=self.__addOrGetIndex(obj_index, base_index1, pos1, n1, uv1, b0_1, b1_1, weight1)
273 index2=self.__addOrGetIndex(obj_index, base_index2, pos2, n2, uv2, b0_2, b1_2, weight2)
275 if not material in self.indexArrays:
276 self.indexArrays[material]=[]
277 self.indexArrays[material]+=[index0, index1, index2]
281 __slots__=['name', 'type', 'offsets']
282 def __init__(self, name, type):
287 def add(self, index, offset):
288 self.offsets.append((index, offset))
291 self.offsets.sort(key=lambda e: e[0])
294 return "<Morph %s>" % self.name
296 class IKSolver(object):
297 __slots__=['target', 'effector', 'length', 'iterations', 'weight']
298 def __init__(self, target, effector, length, iterations, weight):
300 self.effector=effector
302 self.iterations=iterations
306 class OneSkinMesh(object):
307 __slots__=['vertexArray', 'morphList', 'rigidbodies', 'constraints', ]
309 self.vertexArray=VertexArray()
315 return "<OneSkinMesh %s, morph:%d>" % (
319 def addMesh(self, obj):
320 if not bl.object.isVisible(obj):
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)
354 # ToDo bone weightと関係ないvertex groupを除外する
355 for i, v in enumerate(mesh.vertices):
358 setWeight(i, obj.vertex_groups[g.group].name, g.weight)
360 setWeight(i, obj.vertex_groups[0].name, 1)
363 for i in xrange(len(mesh.vertices)):
364 if i in secondWeightMap:
365 secondWeightMap[i]=(secondWeightMap[i][0], 1.0-weightMap[i][1])
367 weightMap[i]=(weightMap[i][0], 1.0)
368 secondWeightMap[i]=("", 0)
370 print("no weight vertex")
372 secondWeightMap[i]=("", 0)
374 return weightMap, secondWeightMap
376 def __processFaces(self, obj_name, mesh, weightMap, secondWeightMap):
378 for i, face in enumerate(mesh.faces):
379 faceVertexCount=bl.face.getVertexCount(face)
380 material=mesh.materials[bl.face.getMaterialIndex(face)]
381 v=[mesh.vertices[index] for index in bl.face.getVertices(face)]
382 uv=bl.mesh.getFaceUV(
383 mesh, i, face, bl.face.getVertexCount(face))
385 if faceVertexCount==3:
387 self.vertexArray.addTriangle(
388 obj_name, material.name,
395 bl.vertex.getNormal(v[2]),
396 bl.vertex.getNormal(v[1]),
397 bl.vertex.getNormal(v[0]),
401 weightMap[v[2].index][0],
402 weightMap[v[1].index][0],
403 weightMap[v[0].index][0],
404 secondWeightMap[v[2].index][0],
405 secondWeightMap[v[1].index][0],
406 secondWeightMap[v[0].index][0],
407 weightMap[v[2].index][1],
408 weightMap[v[1].index][1],
409 weightMap[v[0].index][1]
411 elif faceVertexCount==4:
413 self.vertexArray.addTriangle(
414 obj_name, material.name,
421 bl.vertex.getNormal(v[2]),
422 bl.vertex.getNormal(v[1]),
423 bl.vertex.getNormal(v[0]),
427 weightMap[v[2].index][0],
428 weightMap[v[1].index][0],
429 weightMap[v[0].index][0],
430 secondWeightMap[v[2].index][0],
431 secondWeightMap[v[1].index][0],
432 secondWeightMap[v[0].index][0],
433 weightMap[v[2].index][1],
434 weightMap[v[1].index][1],
435 weightMap[v[0].index][1]
437 self.vertexArray.addTriangle(
438 obj_name, material.name,
445 bl.vertex.getNormal(v[0]),
446 bl.vertex.getNormal(v[3]),
447 bl.vertex.getNormal(v[2]),
451 weightMap[v[0].index][0],
452 weightMap[v[3].index][0],
453 weightMap[v[2].index][0],
454 secondWeightMap[v[0].index][0],
455 secondWeightMap[v[3].index][0],
456 secondWeightMap[v[2].index][0],
457 weightMap[v[0].index][1],
458 weightMap[v[3].index][1],
459 weightMap[v[2].index][1]
462 def __mesh(self, obj):
463 if RIGID_SHAPE_TYPE in obj:
465 if CONSTRAINT_A in obj:
468 #if not bl.modifier.hasType(obj, 'ARMATURE'):
471 bl.message("export: %s" % obj.name)
473 # メッシュのコピーを生成してオブジェクトの行列を適用する
474 copyMesh, copyObj=bl.object.duplicate(obj)
475 if len(copyMesh.vertices)>0:
477 copyObj.scale=obj.scale
478 bpy.ops.object.transform_apply(scale=True)
479 copyObj.rotation_euler=obj.rotation_euler
480 bpy.ops.object.transform_apply(rotation=True)
481 copyObj.location=obj.location
482 bpy.ops.object.transform_apply(location=True)
484 for m in [m for m in copyObj.modifiers]:
485 if m.type=='SOLIDFY':
487 elif m.type=='ARMATURE':
489 elif m.type=='MIRROR':
490 bpy.ops.object.modifier_apply(modifier=m.name)
494 weightMap, secondWeightMap=self.__getWeightMap(copyObj, copyMesh)
495 self.__processFaces(obj.name, copyMesh, weightMap, secondWeightMap)
496 bl.object.delete(copyObj)
498 def createEmptyBasicSkin(self):
499 self.__getOrCreateMorph('base', 0)
501 def __skin(self, obj):
502 if not bl.object.hasShapeKey(obj):
506 blenderMesh=bl.object.getData(obj)
510 vg=bl.object.getVertexGroup(obj, MMD_SHAPE_GROUP_NAME)
514 for b in bl.object.getShapeKeys(obj):
515 if b.name==BASE_SHAPE_NAME:
516 baseMorph=self.__getOrCreateMorph('base', 0)
521 v=bl.shapekey.getByIndex(b, index)
522 pos=[v[0], v[1], v[2]]
524 indices=self.vertexArray.getMappedIndex(obj.name, index)
525 for attribute, i in indices.items():
530 baseMorph.add(i, pos)
531 indexRelativeMap[i]=relativeIndex
536 print(basis.name, len(baseMorph.offsets))
538 if len(baseMorph.offsets)==0:
542 for b in bl.object.getShapeKeys(obj):
543 if b.name==BASE_SHAPE_NAME:
547 morph=self.__getOrCreateMorph(b.name, 4)
549 for index, src, dst in zip(
550 xrange(len(blenderMesh.vertices)),
551 bl.shapekey.get(basis),
553 offset=[dst[0]-src[0], dst[1]-src[1], dst[2]-src[2]]
554 if offset[0]==0 and offset[1]==0 and offset[2]==0:
557 indices=self.vertexArray.getMappedIndex(obj.name, index)
558 for attribute, i in indices.items():
562 morph.add(indexRelativeMap[i], offset)
563 assert(len(morph.offsets)<len(baseMorph.offsets))
566 original=self.morphList[:]
568 for i, v in enumerate(englishmap.skinMap):
572 return len(englishmap.skinMap)
573 self.morphList.sort(key=getIndex)
575 def __rigidbody(self, obj):
576 if not RIGID_SHAPE_TYPE in obj:
578 self.rigidbodies.append(obj)
580 def __constraint(self, obj):
581 if not CONSTRAINT_A in obj:
583 self.constraints.append(obj)
585 def __getOrCreateMorph(self, name, type):
586 for m in self.morphList:
590 self.morphList.append(m)
593 def getVertexCount(self):
594 return len(self.vertexArray.positions)
598 __slots__=['index', 'name', 'ik_index',
599 'pos', 'tail', 'parent_index', 'tail_index', 'type', 'isConnect']
600 def __init__(self, name, pos, tail, isConnect):
605 self.parent_index=None
608 self.isConnect=isConnect
611 def __eq__(self, rhs):
612 return self.index==rhs.index
615 return "<Bone %s %d>" % (self.name, self.type)
617 class BoneBuilder(object):
618 __slots__=['bones', 'boneMap', 'ik_list', 'bone_groups',]
625 def getBoneGroup(self, bone):
626 for i, g in enumerate(self.bone_groups):
630 print('no gorup', bone)
633 def build(self, armatureObj):
637 bl.message("build skeleton")
638 armature=bl.object.getData(armatureObj)
643 for g in bl.object.boneGroups(armatureObj):
644 self.bone_groups.append((g.name, []))
649 for b in armature.bones.values():
653 bl.bone.getHeadLocal(b),
654 bl.bone.getTailLocal(b),
657 self.__getBone(bone, b)
659 for b in armature.bones.values():
661 self.__checkConnection(b, None)
666 pose = bl.object.getPose(armatureObj)
667 for b in pose.bones.values():
671 self.__assignBoneGroup(b, b.bone_group)
672 for c in b.constraints:
673 if bl.constraint.isIKSolver(c):
677 target=self.__boneByName(bl.constraint.ikTarget(c))
684 link=self.__boneByName(b.name)
689 chainLength=bl.constraint.ikChainLen(c)
690 for i in range(chainLength):
692 chainBone=self.__boneByName(e.name)
694 chainBone.ik_index=target.index
697 IKSolver(target, link, chainLength,
698 int(bl.constraint.ikItration(c) * 0.1),
699 bl.constraint.ikRotationWeight(c)
709 for i, v in enumerate(englishmap.boneMap):
710 if v[0]==ik.target.name:
712 return len(englishmap.boneMap)
713 self.ik_list.sort(key=getIndex)
715 def __assignBoneGroup(self, poseBone, boneGroup):
717 for g in self.bone_groups:
718 if g[0]==boneGroup.name:
719 g[1].append(poseBone.name)
721 def __checkConnection(self, b, p):
722 if bl.bone.isConnected(b):
723 parent=self.__boneByName(p.name)
724 parent.isConnect=True
727 self.__checkConnection(c, b)
733 boneMap=englishmap.boneMap
734 original=self.bones[:]
736 for i, k_v in enumerate(boneMap):
737 if k_v[0]==bone.name:
742 self.bones.sort(key=getIndex)
745 for i, b in enumerate(self.bones):
746 src=original.index(b)
749 b.index=sortMap[b.index]
751 b.parent_index=sortMap[b.parent_index]
753 b.tail_index=sortMap[b.tail_index]
755 b.ik_index=sortMap[b.ik_index]
763 if b.parent_index==None:
764 b.parent_index=0xFFFF
766 if b.type==6 or b.type==7:
768 parent=self.bones[b.parent_index]
769 #print('parnet', parent.name)
770 parent.tail_index=b.index
773 if b.tail_index==None:
778 def getIndex(self, bone):
779 for i, b in enumerate(self.bones):
784 def indexByName(self, name):
788 return self.getIndex(self.__boneByName(name))
790 def __boneByName(self, name):
791 return self.boneMap[name]
793 def __getBone(self, parent, b):
794 if len(b.children)==0:
798 for i, c in enumerate(b.children):
800 bl.bone.getHeadLocal(c),
801 bl.bone.getTailLocal(c),
802 bl.bone.isConnected(c))
805 bone.parent_index=parent.index
807 if bone.isConnect or (not parent.tail_index and parent.tail==bone.pos):
808 parent.tail_index=bone.index
809 self.__getBone(bone, c)
811 def __addBone(self, bone):
812 bone.index=len(self.bones)
813 self.bones.append(bone)
814 self.boneMap[bone.name]=bone
817 class PmdExporter(object):
829 self.armatureObj=None
833 for o in bl.object.each():
834 object_node_map[o]=Node(o)
835 for o in bl.object.each():
836 node=object_node_map[o]
838 object_node_map[node.o.parent].children.append(node)
841 root=object_node_map[bl.object.getActive()]
843 self.englishName=o.name
844 self.englishComment=o[MMD_COMMENT] if MMD_COMMENT in o else 'blender export\n'
845 self.name=o[MMD_MB_NAME] if MMD_MB_NAME in o else 'Blenderエクスポート'
846 self.comment=o[MMD_MB_COMMENT] if MMD_MB_COMMENT in o else 'Blnderエクスポート\n'
849 self.oneSkinMesh=OneSkinMesh()
850 self.__createOneSkinMesh(root)
851 bl.message(self.oneSkinMesh)
852 if len(self.oneSkinMesh.morphList)==0:
854 self.oneSkinMesh.createEmptyBasicSkin()
857 self.skeleton=BoneBuilder()
858 self.skeleton.build(self.armatureObj)
860 def __createOneSkinMesh(self, node):
861 ############################################################
862 # search armature modifier
863 ############################################################
864 for m in node.o.modifiers:
865 if bl.modifier.isType(m, 'ARMATURE'):
866 armatureObj=bl.modifier.getArmatureObject(m)
867 if not self.armatureObj:
868 self.armatureObj=armatureObj
869 elif self.armatureObj!=armatureObj:
870 print("warning! found multiple armature. ignored.",
873 if node.o.type.upper()=='MESH':
874 self.oneSkinMesh.addMesh(node.o)
876 for child in node.children:
877 self.__createOneSkinMesh(child)
879 def write(self, path):
882 io.comment=self.comment
886 for pos, attribute, b0, b1, weight in self.oneSkinMesh.vertexArray.zip():
887 # convert right-handed z-up to left-handed y-up
892 v.normal.x=attribute.nx
893 v.normal.y=attribute.ny
894 v.normal.z=attribute.nz
896 v.uv.y=1.0-attribute.v # reverse vertical
897 v.bone0=self.skeleton.indexByName(b0)
898 v.bone1=self.skeleton.indexByName(b1)
899 v.weight0=int(100*weight)
900 v.edge_flag=0 # edge flag, 0: enable edge, 1: not edge
903 vertexCount=self.oneSkinMesh.getVertexCount()
904 for material_name, indices in self.oneSkinMesh.vertexArray.each():
905 #print('material:', material_name)
906 m=bl.material.get(material_name)
908 material=io.addMaterial()
909 setMaterialParams(material, m)
911 material.vertex_count=len(indices)
912 material.toon_index=0
913 textures=[os.path.basename(path)
914 for path in bl.material.eachEnalbeTexturePath(m)]
916 material.texture='*'.join(textures)
921 assert(i<vertexCount)
922 for i in xrange(0, len(indices), 3):
924 io.indices.append(indices[i])
925 io.indices.append(indices[i+1])
926 io.indices.append(indices[i+2])
930 for i, b in enumerate(self.skeleton.bones):
934 boneNameMap[b.name]=i
935 v=englishmap.getUnicodeBoneName(b.name)
942 bone_english_name=toCP932(b.name)
943 assert(len(bone_english_name)<20)
944 bone.english_name=bone_english_name
949 b.ik_index=self.skeleton.indexByName('eyes')
954 bone.parent_index=b.parent_index
955 bone.tail_index=b.tail_index
956 bone.ik_index=b.ik_index
958 # convert right-handed z-up to left-handed y-up
959 bone.pos.x=b.pos[0] if not near(b.pos[0], 0) else 0
960 bone.pos.y=b.pos[2] if not near(b.pos[2], 0) else 0
961 bone.pos.z=b.pos[1] if not near(b.pos[1], 0) else 0
964 for ik in self.skeleton.ik_list:
966 solver.index=self.skeleton.getIndex(ik.target)
967 solver.target=self.skeleton.getIndex(ik.effector)
968 solver.length=ik.length
969 b=self.skeleton.bones[ik.effector.parent_index]
970 for i in xrange(solver.length):
971 solver.children.append(self.skeleton.getIndex(b))
972 b=self.skeleton.bones[b.parent_index]
973 solver.iterations=ik.iterations
974 solver.weight=ik.weight
977 for i, m in enumerate(self.oneSkinMesh.morphList):
981 v=englishmap.getUnicodeSkinName(m.name)
983 v=[m.name, m.name, 0]
986 morph.english_name=m.name
989 for index, offset in m.offsets:
990 # convert right-handed z-up to left-handed y-up
991 morph.append(index, offset[0], offset[2], offset[1])
992 morph.vertex_count=len(m.offsets)
996 for i, m in enumerate(self.oneSkinMesh.morphList):
998 io.face_list.append(i)
999 for i, m in enumerate(self.oneSkinMesh.morphList):
1001 io.face_list.append(i)
1002 for i, m in enumerate(self.oneSkinMesh.morphList):
1004 io.face_list.append(i)
1005 for i, m in enumerate(self.oneSkinMesh.morphList):
1007 io.face_list.append(i)
1010 for g in self.skeleton.bone_groups:
1011 boneDisplayName=io.addBoneGroup()
1013 name=englishmap.getUnicodeBoneGroupName(g[0])
1016 boneDisplayName.name=name+'\n'
1019 boneDisplayName.english_name=englishName+'\n'
1022 for i, b in enumerate(self.skeleton.bones):
1025 if b.type in [6, 7]:
1027 io.addBoneDisplay(i, self.skeleton.getBoneGroup(b))
1029 #assert(len(io.bones)==len(io.bone_display_list)+1)
1032 io.english_name=self.englishName
1033 io.english_comment=self.englishComment
1037 for o in bl.object.each():
1039 if o.name.startswith(TOON_TEXTURE_OBJECT):
1045 toonMesh=bl.object.getData(toonMeshObject)
1046 toonMaterial=bl.mesh.getMaterial(toonMesh, 0)
1048 t=bl.material.getTexture(toonMaterial, i)
1050 io.toon_textures[i]=pmd.encode_string(t.name)
1052 io.toon_textures[i]=pmd.encode_string("toon%02d.bmp\n" % i)
1055 io.toon_textures[i]=pmd.encode_string("toon%02d.bmp\n" % i)
1059 for i, obj in enumerate(self.oneSkinMesh.rigidbodies):
1060 name=obj[RIGID_NAME] if RIGID_NAME in obj else obj.name
1062 rigidBody=pmd.RigidBody(name)
1063 rigidNameMap[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]
1093 elif obj[RIGID_SHAPE_TYPE]==1:
1094 rigidBody.shapeType=pmd.SHAPE_BOX
1095 rigidBody.w=obj.scale[0]
1096 rigidBody.d=obj.scale[1]
1097 rigidBody.h=obj.scale[2]
1098 elif obj[RIGID_SHAPE_TYPE]==2:
1099 rigidBody.shapeType=pmd.SHAPE_CAPSULE
1100 rigidBody.w=obj.scale[0]
1101 rigidBody.h=obj.scale[2]
1103 io.rigidbodies.append(rigidBody)
1106 for obj in self.oneSkinMesh.constraints:
1107 constraint=pmd.Constraint(obj[CONSTRAINT_NAME])
1108 constraint.rigidA=rigidNameMap[obj[CONSTRAINT_A]]
1109 constraint.rigidB=rigidNameMap[obj[CONSTRAINT_B]]
1110 constraint.pos.x=obj.location[0]
1111 constraint.pos.y=obj.location[2]
1112 constraint.pos.z=obj.location[1]
1113 constraint.rot.x=-obj.rotation_euler[0]
1114 constraint.rot.y=-obj.rotation_euler[2]
1115 constraint.rot.z=-obj.rotation_euler[1]
1116 constraint.constraintPosMin.x=obj[CONSTRAINT_POS_MIN][0]
1117 constraint.constraintPosMin.y=obj[CONSTRAINT_POS_MIN][1]
1118 constraint.constraintPosMin.z=obj[CONSTRAINT_POS_MIN][2]
1119 constraint.constraintPosMax.x=obj[CONSTRAINT_POS_MAX][0]
1120 constraint.constraintPosMax.y=obj[CONSTRAINT_POS_MAX][1]
1121 constraint.constraintPosMax.z=obj[CONSTRAINT_POS_MAX][2]
1122 constraint.constraintRotMin.x=obj[CONSTRAINT_ROT_MIN][0]
1123 constraint.constraintRotMin.y=obj[CONSTRAINT_ROT_MIN][1]
1124 constraint.constraintRotMin.z=obj[CONSTRAINT_ROT_MIN][2]
1125 constraint.constraintRotMax.x=obj[CONSTRAINT_ROT_MAX][0]
1126 constraint.constraintRotMax.y=obj[CONSTRAINT_ROT_MAX][1]
1127 constraint.constraintRotMax.z=obj[CONSTRAINT_ROT_MAX][2]
1128 constraint.springPos.x=obj[CONSTRAINT_SPRING_POS][0]
1129 constraint.springPos.y=obj[CONSTRAINT_SPRING_POS][1]
1130 constraint.springPos.z=obj[CONSTRAINT_SPRING_POS][2]
1131 constraint.springRot.x=obj[CONSTRAINT_SPRING_ROT][0]
1132 constraint.springRot.y=obj[CONSTRAINT_SPRING_ROT][1]
1133 constraint.springRot.z=obj[CONSTRAINT_SPRING_ROT][2]
1134 io.constraints.append(constraint)
1137 bl.message('write: %s' % path)
1138 return io.write(path)
1141 def _execute(filepath=''):
1142 active=bl.object.getActive()
1144 print("abort. no active object.")
1146 exporter=PmdExporter()
1149 exporter.write(filepath)
1150 bl.object.activate(active)