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.
32 2.5 20110522: implement RigidBody and Constraint.
36 'category': 'Import/Export',
37 'name': 'Export: MikuMikuDance Model Format (.pmd)',
41 'location': 'File > Export',
42 'description': 'Export to the MikuMikuDance Model Format (.pmd)',
43 'warning': '', # used for warning icon and text in addons panel
44 'wiki_url': 'http://sourceforge.jp/projects/meshio/wiki/FrontPage',
45 'tracker_url': 'http://sourceforge.jp/ticket/newticket.php?group_id=5081',
48 MMD_SHAPE_GROUP_NAME='_MMD_SHAPE'
50 MMD_MB_COMMENT='mb_comment'
52 BASE_SHAPE_NAME='Basis'
53 RIGID_NAME='rigid_name'
54 RIGID_SHAPE_TYPE='rigid_shape_type'
55 RIGID_PROCESS_TYPE='rigid_process_type'
56 RIGID_BONE_NAME='rigid_bone_name'
57 #RIGID_LOCATION='rigid_loation'
58 RIGID_GROUP='ribid_group'
59 RIGID_INTERSECTION_GROUP='rigid_intersection_group'
60 RIGID_WEIGHT='rigid_weight'
61 RIGID_LINEAR_DAMPING='rigid_linear_damping'
62 RIGID_ANGULAR_DAMPING='rigid_angular_damping'
63 RIGID_RESTITUTION='rigid_restitution'
64 RIGID_FRICTION='rigid_friction'
65 CONSTRAINT_NAME='constraint_name'
66 CONSTRAINT_A='const_a'
67 CONSTRAINT_B='const_b'
68 CONSTRAINT_POS_MIN='const_pos_min'
69 CONSTRAINT_POS_MAX='const_pos_max'
70 CONSTRAINT_ROT_MIN='const_rot_min'
71 CONSTRAINT_ROT_MAX='const_rot_max'
72 CONSTRAINT_SPRING_POS='const_spring_pos'
73 CONSTRAINT_SPRING_ROT='const_spring_rot'
74 TOON_TEXTURE_OBJECT='ToonTextures'
77 ###############################################################################
79 ###############################################################################
85 from meshio import pmd, englishmap
86 print('use meshio C module')
89 from .pymeshio import englishmap
90 from .pymeshio import pmd
98 from . import bl25 as bl
102 def setMaterialParams(material, m):
104 material.diffuse.r=m.diffuse_color[0]
105 material.diffuse.g=m.diffuse_color[1]
106 material.diffuse.b=m.diffuse_color[2]
107 material.diffuse.a=m.alpha
109 material.shinness=0 if m.specular_toon_size<1e-5 else m.specular_hardness*10
110 material.specular.r=m.specular_color[0]
111 material.specular.g=m.specular_color[1]
112 material.specular.b=m.specular_color[2]
114 material.ambient.r=m.mirror_color[0]
115 material.ambient.g=m.mirror_color[1]
116 material.ambient.b=m.mirror_color[2]
118 material.flag=1 if m.subsurface_scattering.use else 0
120 material.toon_index=7
123 return s.encode('cp932')
127 __slots__=['o', 'children']
128 def __init__(self, o):
133 ###############################################################################
134 # Blenderのメッシュをワンスキンメッシュ化する
135 ###############################################################################
136 def near(x, y, EPSILON=1e-5):
138 return d>=-EPSILON and d<=EPSILON
141 class VertexAttribute(object):
143 'nx', 'ny', 'nz', # normal
146 def __init__(self, nx, ny, nz, u, v):
154 return "<vkey: %f, %f, %f, %f, %f>" % (
155 self.nx, self.ny, self.nz, self.u, self.v)
158 return int(100*(self.nx + self.ny + self.nz + self.u + self.v))
160 def __eq__(self, rhs):
161 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
164 class VertexKey(object):
166 'obj_index', 'index',
169 def __init__(self, obj_index, index):
170 self.obj_index=obj_index
174 return "<vkey: %d, %d>" % (self.obj_index, self.index)
177 return self.index*100+self.obj_index
179 def __eq__(self, rhs):
180 return self.obj_index==rhs.obj_index and self.index==rhs.index
183 class VertexArray(object):
190 'attributes', # normal and uv
191 'b0', 'b1', 'weight',
196 # indexArrays split with each material
209 return "<VertexArray %d positions, %d indexArrays>" % (
210 len(self.positions), len(self.indexArrays))
214 self.positions, self.attributes,
215 self.b0, self.b1, self.weight)
218 keys=[key for key in self.indexArrays.keys()]
221 yield(key, self.indexArrays[key])
223 def __addOrGetIndex(self, obj_index, base_index, pos, normal, uv, b0, b1, weight0):
224 key=VertexKey(obj_index, base_index)
225 attribute=VertexAttribute(
226 normal[0], normal[1], normal[2],
228 if key in self.vertexMap:
229 if attribute in self.vertexMap[key]:
230 return self.vertexMap[key][attribute]
232 return self.__addVertex(self.vertexMap[key],
233 pos, attribute, b0, b1, weight0)
236 self.vertexMap[key]=vertexMapKey
237 return self.__addVertex(vertexMapKey,
238 pos, attribute, b0, b1, weight0)
240 def __addVertex(self, vertexMapKey, pos, attribute, b0, b1, weight0):
241 index=len(self.positions)
242 vertexMapKey[attribute]=index
244 self.positions.append((pos.x, pos.y, pos.z))
246 self.attributes.append(attribute)
250 self.weight.append(weight0)
254 def getMappedIndex(self, obj_name, base_index):
255 return self.vertexMap[VertexKey(self.objectMap[obj_name], base_index)]
257 def addTriangle(self,
258 object_name, material,
259 base_index0, base_index1, base_index2,
265 weight0, weight1, weight2
267 if object_name in self.objectMap:
268 obj_index=self.objectMap[object_name]
270 obj_index=len(self.objectMap)
271 self.objectMap[object_name]=obj_index
272 index0=self.__addOrGetIndex(obj_index, base_index0, pos0, n0, uv0, b0_0, b1_0, weight0)
273 index1=self.__addOrGetIndex(obj_index, base_index1, pos1, n1, uv1, b0_1, b1_1, weight1)
274 index2=self.__addOrGetIndex(obj_index, base_index2, pos2, n2, uv2, b0_2, b1_2, weight2)
276 if not material in self.indexArrays:
277 self.indexArrays[material]=[]
278 self.indexArrays[material]+=[index0, index1, index2]
282 __slots__=['name', 'type', 'offsets']
283 def __init__(self, name, type):
288 def add(self, index, offset):
289 self.offsets.append((index, offset))
292 self.offsets.sort(key=lambda e: e[0])
295 return "<Morph %s>" % self.name
297 class IKSolver(object):
298 __slots__=['target', 'effector', 'length', 'iterations', 'weight']
299 def __init__(self, target, effector, length, iterations, weight):
301 self.effector=effector
303 self.iterations=iterations
307 class OneSkinMesh(object):
308 __slots__=['vertexArray', 'morphList', 'rigidbodies', 'constraints', ]
310 self.vertexArray=VertexArray()
316 return "<OneSkinMesh %s, morph:%d>" % (
320 def addMesh(self, obj):
321 if not bl.object.isVisible(obj):
325 self.__rigidbody(obj)
326 self.__constraint(obj)
328 def __getWeightMap(self, obj, mesh):
332 def setWeight(i, name, w):
335 if i in secondWeightMap:
337 if w<secondWeightMap[i][1]:
339 elif w<weightMap[i][1]:
341 secondWeightMap[i]=(name, w)
344 weightMap[i]=(name, w)
346 if w>weightMap[i][1]:
348 secondWeightMap[i]=weightMap[i]
349 weightMap[i]=(name, w)
351 secondWeightMap[i]=(name, w)
353 weightMap[i]=(name, w)
355 # ToDo bone weightと関係ないvertex groupを除外する
356 for i, v in enumerate(mesh.vertices):
359 setWeight(i, obj.vertex_groups[g.group].name, g.weight)
361 setWeight(i, obj.vertex_groups[0].name, 1)
364 for i in xrange(len(mesh.vertices)):
365 if i in secondWeightMap:
366 secondWeightMap[i]=(secondWeightMap[i][0], 1.0-weightMap[i][1])
368 weightMap[i]=(weightMap[i][0], 1.0)
369 secondWeightMap[i]=("", 0)
371 print("no weight vertex")
373 secondWeightMap[i]=("", 0)
375 return weightMap, secondWeightMap
377 def __processFaces(self, obj_name, mesh, weightMap, secondWeightMap):
379 for i, face in enumerate(mesh.faces):
380 faceVertexCount=bl.face.getVertexCount(face)
381 material=mesh.materials[bl.face.getMaterialIndex(face)]
382 v=[mesh.vertices[index] for index in bl.face.getVertices(face)]
383 uv=bl.mesh.getFaceUV(
384 mesh, i, face, bl.face.getVertexCount(face))
386 if faceVertexCount==3:
388 self.vertexArray.addTriangle(
389 obj_name, material.name,
396 bl.vertex.getNormal(v[2]),
397 bl.vertex.getNormal(v[1]),
398 bl.vertex.getNormal(v[0]),
402 weightMap[v[2].index][0],
403 weightMap[v[1].index][0],
404 weightMap[v[0].index][0],
405 secondWeightMap[v[2].index][0],
406 secondWeightMap[v[1].index][0],
407 secondWeightMap[v[0].index][0],
408 weightMap[v[2].index][1],
409 weightMap[v[1].index][1],
410 weightMap[v[0].index][1]
412 elif faceVertexCount==4:
414 self.vertexArray.addTriangle(
415 obj_name, material.name,
422 bl.vertex.getNormal(v[2]),
423 bl.vertex.getNormal(v[1]),
424 bl.vertex.getNormal(v[0]),
428 weightMap[v[2].index][0],
429 weightMap[v[1].index][0],
430 weightMap[v[0].index][0],
431 secondWeightMap[v[2].index][0],
432 secondWeightMap[v[1].index][0],
433 secondWeightMap[v[0].index][0],
434 weightMap[v[2].index][1],
435 weightMap[v[1].index][1],
436 weightMap[v[0].index][1]
438 self.vertexArray.addTriangle(
439 obj_name, material.name,
446 bl.vertex.getNormal(v[0]),
447 bl.vertex.getNormal(v[3]),
448 bl.vertex.getNormal(v[2]),
452 weightMap[v[0].index][0],
453 weightMap[v[3].index][0],
454 weightMap[v[2].index][0],
455 secondWeightMap[v[0].index][0],
456 secondWeightMap[v[3].index][0],
457 secondWeightMap[v[2].index][0],
458 weightMap[v[0].index][1],
459 weightMap[v[3].index][1],
460 weightMap[v[2].index][1]
463 def __mesh(self, obj):
464 if RIGID_SHAPE_TYPE in obj:
466 if CONSTRAINT_A in obj:
469 #if not bl.modifier.hasType(obj, 'ARMATURE'):
472 bl.message("export: %s" % obj.name)
474 # メッシュのコピーを生成してオブジェクトの行列を適用する
475 copyMesh, copyObj=bl.object.duplicate(obj)
476 if len(copyMesh.vertices)>0:
480 copyObj.scale=obj.scale
481 bpy.ops.object.transform_apply(scale=True)
482 copyObj.rotation_euler=obj.rotation_euler
483 bpy.ops.object.transform_apply(rotation=True)
484 copyObj.location=obj.location
485 bpy.ops.object.transform_apply(location=True)
486 except AttributeError as e:
488 copyObj.scale=obj.scale
489 bpy.ops.object.scale_apply()
490 copyObj.rotation_euler=obj.rotation_euler
491 bpy.ops.object.rotation_apply()
492 copyObj.location=obj.location
493 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(obj.name, copyMesh, weightMap, secondWeightMap)
508 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]]
536 indices=self.vertexArray.getMappedIndex(obj.name, index)
537 for attribute, i in indices.items():
542 baseMorph.add(i, pos)
543 indexRelativeMap[i]=relativeIndex
548 #print(basis.name, len(baseMorph.offsets))
550 if len(baseMorph.offsets)==0:
554 for b in bl.object.getShapeKeys(obj):
555 if b.name==BASE_SHAPE_NAME:
559 morph=self.__getOrCreateMorph(b.name, 4)
561 for index, src, dst in zip(
562 xrange(len(blenderMesh.vertices)),
563 bl.shapekey.get(basis),
565 offset=[dst[0]-src[0], dst[1]-src[1], dst[2]-src[2]]
566 if offset[0]==0 and offset[1]==0 and offset[2]==0:
569 indices=self.vertexArray.getMappedIndex(obj.name, index)
570 for attribute, i in indices.items():
574 morph.add(indexRelativeMap[i], offset)
575 assert(len(morph.offsets)<len(baseMorph.offsets))
578 original=self.morphList[:]
580 for i, v in enumerate(englishmap.skinMap):
584 return len(englishmap.skinMap)
585 self.morphList.sort(key=getIndex)
587 def __rigidbody(self, obj):
588 if not RIGID_SHAPE_TYPE in obj:
590 self.rigidbodies.append(obj)
592 def __constraint(self, obj):
593 if not CONSTRAINT_A in obj:
595 self.constraints.append(obj)
597 def __getOrCreateMorph(self, name, type):
598 for m in self.morphList:
602 self.morphList.append(m)
605 def getVertexCount(self):
606 return len(self.vertexArray.positions)
610 __slots__=['index', 'name', 'ik_index',
611 'pos', 'tail', 'parent_index', 'tail_index', 'type', 'isConnect']
612 def __init__(self, name, pos, tail, isConnect):
617 self.parent_index=None
620 self.isConnect=isConnect
623 def __eq__(self, rhs):
624 return self.index==rhs.index
627 return "<Bone %s %d>" % (self.name, self.type)
629 class BoneBuilder(object):
630 __slots__=['bones', 'boneMap', 'ik_list', 'bone_groups',]
637 def getBoneGroup(self, bone):
638 for i, g in enumerate(self.bone_groups):
642 print('no gorup', bone)
645 def build(self, armatureObj):
649 bl.message("build skeleton")
650 armature=bl.object.getData(armatureObj)
655 for g in bl.object.boneGroups(armatureObj):
656 self.bone_groups.append((g.name, []))
661 for b in armature.bones.values():
665 bl.bone.getHeadLocal(b),
666 bl.bone.getTailLocal(b),
669 self.__getBone(bone, b)
671 for b in armature.bones.values():
673 self.__checkConnection(b, None)
678 pose = bl.object.getPose(armatureObj)
679 for b in pose.bones.values():
683 self.__assignBoneGroup(b, b.bone_group)
684 for c in b.constraints:
685 if bl.constraint.isIKSolver(c):
689 target=self.__boneByName(bl.constraint.ikTarget(c))
696 link=self.__boneByName(b.name)
701 chainLength=bl.constraint.ikChainLen(c)
702 for i in range(chainLength):
704 chainBone=self.__boneByName(e.name)
706 chainBone.ik_index=target.index
709 IKSolver(target, link, chainLength,
710 int(bl.constraint.ikItration(c) * 0.1),
711 bl.constraint.ikRotationWeight(c)
721 for i, v in enumerate(englishmap.boneMap):
722 if v[0]==ik.target.name:
724 return len(englishmap.boneMap)
725 self.ik_list.sort(key=getIndex)
727 def __assignBoneGroup(self, poseBone, boneGroup):
729 for g in self.bone_groups:
730 if g[0]==boneGroup.name:
731 g[1].append(poseBone.name)
733 def __checkConnection(self, b, p):
734 if bl.bone.isConnected(b):
735 parent=self.__boneByName(p.name)
736 parent.isConnect=True
739 self.__checkConnection(c, b)
745 boneMap=englishmap.boneMap
746 original=self.bones[:]
748 for i, k_v in enumerate(boneMap):
749 if k_v[0]==bone.name:
754 self.bones.sort(key=getIndex)
757 for i, b in enumerate(self.bones):
758 src=original.index(b)
761 b.index=sortMap[b.index]
763 b.parent_index=sortMap[b.parent_index]
765 b.tail_index=sortMap[b.tail_index]
767 b.ik_index=sortMap[b.ik_index]
775 if b.parent_index==None:
776 b.parent_index=0xFFFF
778 if b.type==6 or b.type==7:
780 parent=self.bones[b.parent_index]
781 #print('parnet', parent.name)
782 parent.tail_index=b.index
785 if b.tail_index==None:
790 def getIndex(self, bone):
791 for i, b in enumerate(self.bones):
796 def indexByName(self, name):
800 return self.getIndex(self.__boneByName(name))
802 def __boneByName(self, name):
803 return self.boneMap[name]
805 def __getBone(self, parent, b):
806 if len(b.children)==0:
810 for i, c in enumerate(b.children):
812 bl.bone.getHeadLocal(c),
813 bl.bone.getTailLocal(c),
814 bl.bone.isConnected(c))
817 bone.parent_index=parent.index
819 if bone.isConnect or (not parent.tail_index and parent.tail==bone.pos):
820 parent.tail_index=bone.index
821 self.__getBone(bone, c)
823 def __addBone(self, bone):
824 bone.index=len(self.bones)
825 self.bones.append(bone)
826 self.boneMap[bone.name]=bone
829 class PmdExporter(object):
841 self.armatureObj=None
845 for o in bl.object.each():
846 object_node_map[o]=Node(o)
847 for o in bl.object.each():
848 node=object_node_map[o]
850 object_node_map[node.o.parent].children.append(node)
853 root=object_node_map[bl.object.getActive()]
855 self.englishName=o.name
856 self.englishComment=o[MMD_COMMENT] if MMD_COMMENT in o else 'blender export\n'
857 self.name=o[MMD_MB_NAME] if MMD_MB_NAME in o else 'Blenderエクスポート'
858 self.comment=o[MMD_MB_COMMENT] if MMD_MB_COMMENT in o else 'Blnderエクスポート\n'
861 self.oneSkinMesh=OneSkinMesh()
862 self.__createOneSkinMesh(root)
863 bl.message(self.oneSkinMesh)
864 if len(self.oneSkinMesh.morphList)==0:
866 self.oneSkinMesh.createEmptyBasicSkin()
869 self.skeleton=BoneBuilder()
870 self.skeleton.build(self.armatureObj)
872 def __createOneSkinMesh(self, node):
873 ############################################################
874 # search armature modifier
875 ############################################################
876 for m in node.o.modifiers:
877 if bl.modifier.isType(m, 'ARMATURE'):
878 armatureObj=bl.modifier.getArmatureObject(m)
879 if not self.armatureObj:
880 self.armatureObj=armatureObj
881 elif self.armatureObj!=armatureObj:
882 print("warning! found multiple armature. ignored.",
885 if node.o.type.upper()=='MESH':
886 self.oneSkinMesh.addMesh(node.o)
888 for child in node.children:
889 self.__createOneSkinMesh(child)
891 def write(self, path):
894 io.comment=self.comment
898 for pos, attribute, b0, b1, weight in self.oneSkinMesh.vertexArray.zip():
899 # convert right-handed z-up to left-handed y-up
904 v.normal.x=attribute.nx
905 v.normal.y=attribute.ny
906 v.normal.z=attribute.nz
908 v.uv.y=1.0-attribute.v # reverse vertical
909 v.bone0=self.skeleton.indexByName(b0)
910 v.bone1=self.skeleton.indexByName(b1)
911 v.weight0=int(100*weight)
912 v.edge_flag=0 # edge flag, 0: enable edge, 1: not edge
915 vertexCount=self.oneSkinMesh.getVertexCount()
916 for material_name, indices in self.oneSkinMesh.vertexArray.each():
917 #print('material:', material_name)
918 m=bl.material.get(material_name)
920 material=io.addMaterial()
921 setMaterialParams(material, m)
923 material.vertex_count=len(indices)
924 material.toon_index=0
925 textures=[os.path.basename(path)
926 for path in bl.material.eachEnalbeTexturePath(m)]
928 material.texture='*'.join(textures)
933 assert(i<vertexCount)
934 for i in xrange(0, len(indices), 3):
936 io.indices.append(indices[i])
937 io.indices.append(indices[i+1])
938 io.indices.append(indices[i+2])
942 for i, b in enumerate(self.skeleton.bones):
946 boneNameMap[b.name]=i
947 v=englishmap.getUnicodeBoneName(b.name)
954 bone_english_name=toCP932(b.name)
955 assert(len(bone_english_name)<20)
956 bone.english_name=bone_english_name
961 b.ik_index=self.skeleton.indexByName('eyes')
966 bone.parent_index=b.parent_index
967 bone.tail_index=b.tail_index
968 bone.ik_index=b.ik_index
970 # convert right-handed z-up to left-handed y-up
971 bone.pos.x=b.pos[0] if not near(b.pos[0], 0) else 0
972 bone.pos.y=b.pos[2] if not near(b.pos[2], 0) else 0
973 bone.pos.z=b.pos[1] if not near(b.pos[1], 0) else 0
976 for ik in self.skeleton.ik_list:
978 solver.index=self.skeleton.getIndex(ik.target)
979 solver.target=self.skeleton.getIndex(ik.effector)
980 solver.length=ik.length
981 b=self.skeleton.bones[ik.effector.parent_index]
982 for i in xrange(solver.length):
983 solver.children.append(self.skeleton.getIndex(b))
984 b=self.skeleton.bones[b.parent_index]
985 solver.iterations=ik.iterations
986 solver.weight=ik.weight
989 for i, m in enumerate(self.oneSkinMesh.morphList):
993 v=englishmap.getUnicodeSkinName(m.name)
995 v=[m.name, m.name, 0]
998 morph.english_name=m.name
1001 for index, offset in m.offsets:
1002 # convert right-handed z-up to left-handed y-up
1003 morph.append(index, offset[0], offset[2], offset[1])
1004 morph.vertex_count=len(m.offsets)
1008 for i, m in enumerate(self.oneSkinMesh.morphList):
1010 io.face_list.append(i)
1011 for i, m in enumerate(self.oneSkinMesh.morphList):
1013 io.face_list.append(i)
1014 for i, m in enumerate(self.oneSkinMesh.morphList):
1016 io.face_list.append(i)
1017 for i, m in enumerate(self.oneSkinMesh.morphList):
1019 io.face_list.append(i)
1022 for g in self.skeleton.bone_groups:
1023 boneDisplayName=io.addBoneGroup()
1025 name=englishmap.getUnicodeBoneGroupName(g[0])
1028 boneDisplayName.name=name+'\n'
1031 boneDisplayName.english_name=englishName+'\n'
1034 for i, b in enumerate(self.skeleton.bones):
1037 if b.type in [6, 7]:
1039 io.addBoneDisplay(i, self.skeleton.getBoneGroup(b))
1041 #assert(len(io.bones)==len(io.bone_display_list)+1)
1044 io.english_name=self.englishName
1045 io.english_comment=self.englishComment
1049 for o in bl.object.each():
1051 if o.name.startswith(TOON_TEXTURE_OBJECT):
1057 toonMesh=bl.object.getData(toonMeshObject)
1058 toonMaterial=bl.mesh.getMaterial(toonMesh, 0)
1060 t=bl.material.getTexture(toonMaterial, i)
1062 io.toon_textures[i]="%s" % t.name
1064 io.toon_textures[i]="toon%02d.bmp" % (i+1)
1067 io.toon_textures[i]="toon%02d.bmp" % (i+1)
1071 for i, obj in enumerate(self.oneSkinMesh.rigidbodies):
1072 name=obj[RIGID_NAME] if RIGID_NAME in obj else obj.name
1074 rigidBody=pmd.RigidBody(name)
1075 rigidNameMap[name]=i
1076 boneIndex=boneNameMap[obj[RIGID_BONE_NAME]]
1079 bone=self.skeleton.bones[0]
1081 bone=self.skeleton.bones[boneIndex]
1082 rigidBody.boneIndex=boneIndex
1083 #rigidBody.position.x=obj[RIGID_LOCATION][0]
1084 #rigidBody.position.y=obj[RIGID_LOCATION][1]
1085 #rigidBody.position.z=obj[RIGID_LOCATION][2]
1086 rigidBody.position.x=obj.location.x-bone.pos[0]
1087 rigidBody.position.y=obj.location.z-bone.pos[2]
1088 rigidBody.position.z=obj.location.y-bone.pos[1]
1089 rigidBody.rotation.x=-obj.rotation_euler[0]
1090 rigidBody.rotation.y=-obj.rotation_euler[2]
1091 rigidBody.rotation.z=-obj.rotation_euler[1]
1092 rigidBody.processType=obj[RIGID_PROCESS_TYPE]
1093 rigidBody.group=obj[RIGID_GROUP]
1094 rigidBody.target=obj[RIGID_INTERSECTION_GROUP]
1095 rigidBody.weight=obj[RIGID_WEIGHT]
1096 rigidBody.linearDamping=obj[RIGID_LINEAR_DAMPING]
1097 rigidBody.angularDamping=obj[RIGID_ANGULAR_DAMPING]
1098 rigidBody.restitution=obj[RIGID_RESTITUTION]
1099 rigidBody.friction=obj[RIGID_FRICTION]
1100 if obj[RIGID_SHAPE_TYPE]==0:
1101 rigidBody.shapeType=pmd.SHAPE_SPHERE
1102 rigidBody.w=obj.scale[0]
1105 elif obj[RIGID_SHAPE_TYPE]==1:
1106 rigidBody.shapeType=pmd.SHAPE_BOX
1107 rigidBody.w=obj.scale[0]
1108 rigidBody.d=obj.scale[1]
1109 rigidBody.h=obj.scale[2]
1110 elif obj[RIGID_SHAPE_TYPE]==2:
1111 rigidBody.shapeType=pmd.SHAPE_CAPSULE
1112 rigidBody.w=obj.scale[0]
1113 rigidBody.h=obj.scale[2]
1115 io.rigidbodies.append(rigidBody)
1118 for obj in self.oneSkinMesh.constraints:
1119 constraint=pmd.Constraint(obj[CONSTRAINT_NAME])
1120 constraint.rigidA=rigidNameMap[obj[CONSTRAINT_A]]
1121 constraint.rigidB=rigidNameMap[obj[CONSTRAINT_B]]
1122 constraint.pos.x=obj.location[0]
1123 constraint.pos.y=obj.location[2]
1124 constraint.pos.z=obj.location[1]
1125 constraint.rot.x=-obj.rotation_euler[0]
1126 constraint.rot.y=-obj.rotation_euler[2]
1127 constraint.rot.z=-obj.rotation_euler[1]
1128 constraint.constraintPosMin.x=obj[CONSTRAINT_POS_MIN][0]
1129 constraint.constraintPosMin.y=obj[CONSTRAINT_POS_MIN][1]
1130 constraint.constraintPosMin.z=obj[CONSTRAINT_POS_MIN][2]
1131 constraint.constraintPosMax.x=obj[CONSTRAINT_POS_MAX][0]
1132 constraint.constraintPosMax.y=obj[CONSTRAINT_POS_MAX][1]
1133 constraint.constraintPosMax.z=obj[CONSTRAINT_POS_MAX][2]
1134 constraint.constraintRotMin.x=obj[CONSTRAINT_ROT_MIN][0]
1135 constraint.constraintRotMin.y=obj[CONSTRAINT_ROT_MIN][1]
1136 constraint.constraintRotMin.z=obj[CONSTRAINT_ROT_MIN][2]
1137 constraint.constraintRotMax.x=obj[CONSTRAINT_ROT_MAX][0]
1138 constraint.constraintRotMax.y=obj[CONSTRAINT_ROT_MAX][1]
1139 constraint.constraintRotMax.z=obj[CONSTRAINT_ROT_MAX][2]
1140 constraint.springPos.x=obj[CONSTRAINT_SPRING_POS][0]
1141 constraint.springPos.y=obj[CONSTRAINT_SPRING_POS][1]
1142 constraint.springPos.z=obj[CONSTRAINT_SPRING_POS][2]
1143 constraint.springRot.x=obj[CONSTRAINT_SPRING_ROT][0]
1144 constraint.springRot.y=obj[CONSTRAINT_SPRING_ROT][1]
1145 constraint.springRot.z=obj[CONSTRAINT_SPRING_ROT][2]
1146 io.constraints.append(constraint)
1149 bl.message('write: %s' % path)
1150 return io.write(path)
1153 def _execute(filepath=''):
1154 active=bl.object.getActive()
1156 print("abort. no active object.")
1158 exporter=PmdExporter()
1161 exporter.write(filepath)
1162 bl.object.activate(active)