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.
34 'category': 'Import/Export',
35 'name': 'Export: MikuMikuDance Model Format (.pmd)',
39 'location': 'File > Export',
40 'description': 'Export to the MikuMikuDance Model Format (.pmd)',
41 'warning': '', # used for warning icon and text in addons panel
42 'wiki_url': 'http://sourceforge.jp/projects/meshio/wiki/FrontPage',
43 'tracker_url': 'http://sourceforge.jp/ticket/newticket.php?group_id=5081',
46 MMD_SHAPE_GROUP_NAME='_MMD_SHAPE'
48 MMD_MB_COMMENT='mb_comment'
50 BASE_SHAPE_NAME='Basis'
51 RIGID_NAME='rigid_name'
52 RIGID_SHAPE_TYPE='rigid_shape_type'
53 RIGID_PROCESS_TYPE='rigid_process_type'
54 RIGID_BONE_NAME='rigid_bone_name'
55 #RIGID_LOCATION='rigid_loation'
56 RIGID_GROUP='ribid_group'
57 RIGID_INTERSECTION_GROUP='rigid_intersection_group'
58 RIGID_WEIGHT='rigid_weight'
59 RIGID_LINEAR_DAMPING='rigid_linear_damping'
60 RIGID_ANGULAR_DAMPING='rigid_angular_damping'
61 RIGID_RESTITUTION='rigid_restitution'
62 RIGID_FRICTION='rigid_friction'
63 CONSTRAINT_NAME='constraint_name'
64 CONSTRAINT_A='const_a'
65 CONSTRAINT_B='const_b'
66 CONSTRAINT_POS_MIN='const_pos_min'
67 CONSTRAINT_POS_MAX='const_pos_max'
68 CONSTRAINT_ROT_MIN='const_rot_min'
69 CONSTRAINT_ROT_MAX='const_rot_max'
70 CONSTRAINT_SPRING_POS='const_spring_pos'
71 CONSTRAINT_SPRING_ROT='const_spring_rot'
72 TOON_TEXTURE_OBJECT='ToonTextures'
75 ###############################################################################
77 ###############################################################################
83 from meshio import pmd, englishmap
84 print('use meshio C module')
87 from pymeshio import englishmap
88 from pymeshio import mmd as pmd
92 return sys.version_info[0]<3
97 from Blender import Mathutils
103 def setMaterialParams(material, m):
105 material.diffuse.r=m.R
106 material.diffuse.g=m.G
107 material.diffuse.b=m.B
108 material.diffuse.a=m.alpha
110 material.shinness=0 if m.spec<1e-5 else m.spec*10
111 material.specular.r=m.specR
112 material.specular.g=m.specG
113 material.specular.b=m.specB
115 material.ambient.r=m.mirR
116 material.ambient.g=m.mirG
117 material.ambient.b=m.mirB
119 material.flag=1 if m.enableSSS else 0
135 def setMaterialParams(material, m):
137 material.diffuse.r=m.diffuse_color[0]
138 material.diffuse.g=m.diffuse_color[1]
139 material.diffuse.b=m.diffuse_color[2]
140 material.diffuse.a=m.alpha
142 material.shinness=0 if m.specular_toon_size<1e-5 else m.specular_hardness*10
143 material.specular.r=m.specular_color[0]
144 material.specular.g=m.specular_color[1]
145 material.specular.b=m.specular_color[2]
147 material.ambient.r=m.mirror_color[0]
148 material.ambient.g=m.mirror_color[1]
149 material.ambient.b=m.mirror_color[2]
151 material.flag=1 if m.subsurface_scattering.enabled else 0
153 material.toon_index=7
156 return s.encode('cp932')
160 __slots__=['o', 'children']
161 def __init__(self, o):
166 ###############################################################################
167 # Blenderのメッシュをワンスキンメッシュ化する
168 ###############################################################################
169 def near(x, y, EPSILON=1e-5):
171 return d>=-EPSILON and d<=EPSILON
174 class VertexAttribute(object):
176 'nx', 'ny', 'nz', # normal
179 def __init__(self, nx, ny, nz, u, v):
187 return "<vkey: %f, %f, %f, %f, %f>" % (
188 self.nx, self.ny, self.nz, self.u, self.v)
191 return self.nx + self.ny + self.nz + self.u + self.v
193 def __eq__(self, rhs):
194 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
197 class VertexKey(object):
199 'obj_index', 'index',
202 def __init__(self, obj_index, index):
203 self.obj_index=obj_index
207 return "<vkey: %d, %d>" % (self.obj_index, self.index)
210 return self.index*100+self.obj_index
212 def __eq__(self, rhs):
213 return self.obj_index==rhs.obj_index and self.index==rhs.index
216 class VertexArray(object):
223 'attributes', # normal and uv
224 'b0', 'b1', 'weight',
229 # indexArrays split with each material
242 return "<VertexArray %d positions, %d indexArrays>" % (
243 len(self.positions), len(self.indexArrays))
247 self.positions, self.attributes,
248 self.b0, self.b1, self.weight)
251 keys=[key for key in self.indexArrays.keys()]
254 yield(key, self.indexArrays[key])
256 def __addOrGetIndex(self, obj_index, base_index, pos, normal, uv, b0, b1, weight0):
257 key=VertexKey(obj_index, base_index)
258 attribute=VertexAttribute(
259 normal[0], normal[1], normal[2],
261 if key in self.vertexMap:
262 if attribute in self.vertexMap[key]:
263 return self.vertexMap[key][attribute]
265 return self.__addVertex(self.vertexMap[key],
266 pos, attribute, b0, b1, weight0)
269 self.vertexMap[key]=vertexMapKey
270 return self.__addVertex(vertexMapKey,
271 pos, attribute, b0, b1, weight0)
273 def __addVertex(self, vertexMapKey, pos, attribute, b0, b1, weight0):
274 index=len(self.positions)
275 vertexMapKey[attribute]=index
277 self.positions.append((pos.x, pos.y, pos.z))
279 self.attributes.append(attribute)
283 self.weight.append(weight0)
287 def getMappedIndex(self, obj_name, base_index):
288 return self.vertexMap[VertexKey(self.objectMap[obj_name], base_index)]
290 def addTriangle(self,
291 object_name, material,
292 base_index0, base_index1, base_index2,
298 weight0, weight1, weight2
300 if object_name in self.objectMap:
301 obj_index=self.objectMap[object_name]
303 obj_index=len(self.objectMap)
304 self.objectMap[object_name]=obj_index
305 index0=self.__addOrGetIndex(obj_index, base_index0, pos0, n0, uv0, b0_0, b1_0, weight0)
306 index1=self.__addOrGetIndex(obj_index, base_index1, pos1, n1, uv1, b0_1, b1_1, weight1)
307 index2=self.__addOrGetIndex(obj_index, base_index2, pos2, n2, uv2, b0_2, b1_2, weight2)
309 if not material in self.indexArrays:
310 self.indexArrays[material]=[]
311 self.indexArrays[material]+=[index0, index1, index2]
315 __slots__=['name', 'type', 'offsets']
316 def __init__(self, name, type):
321 def add(self, index, offset):
322 self.offsets.append((index, offset))
326 self.offsets.sort(lambda l, r: l[0]-r[0])
328 self.offsets.sort(key=lambda e: e[0])
331 return "<Morph %s>" % self.name
333 class IKSolver(object):
334 __slots__=['target', 'effector', 'length', 'iterations', 'weight']
335 def __init__(self, target, effector, length, iterations, weight):
337 self.effector=effector
339 self.iterations=iterations
343 class OneSkinMesh(object):
344 __slots__=['vertexArray', 'morphList', 'rigidbodies', 'constraints', ]
346 self.vertexArray=VertexArray()
352 return "<OneSkinMesh %s, morph:%d>" % (
356 def addMesh(self, obj):
357 if not bl.object.isVisible(obj):
361 self.__rigidbody(obj)
362 self.__constraint(obj)
364 def __getWeightMap(self, obj, mesh):
368 def setWeight(i, name, w):
371 if i in secondWeightMap:
373 if w<secondWeightMap[i][1]:
375 elif w<weightMap[i][1]:
377 secondWeightMap[i]=(name, w)
380 weightMap[i]=(name, w)
382 if w>weightMap[i][1]:
384 secondWeightMap[i]=weightMap[i]
385 weightMap[i]=(name, w)
387 secondWeightMap[i]=(name, w)
389 weightMap[i]=(name, w)
391 # ToDo bone weightと関係ないvertex groupを除外する
393 for name in bl.object.getVertexGroupNames(obj):
394 for i, w in mesh.getVertsFromGroup(name, 1):
395 setWeight(i, name, w)
397 for i, v in enumerate(mesh.verts):
400 setWeight(i, obj.vertex_groups[g.group].name, g.weight)
402 setWeight(i, obj.vertex_groups[0].name, 1)
405 for i in xrange(len(mesh.verts)):
406 if i in secondWeightMap:
407 secondWeightMap[i]=(secondWeightMap[i][0], 1.0-weightMap[i][1])
409 weightMap[i]=(weightMap[i][0], 1.0)
410 secondWeightMap[i]=("", 0)
412 print("no weight vertex")
414 secondWeightMap[i]=("", 0)
416 return weightMap, secondWeightMap
418 def __processFaces(self, obj_name, mesh, weightMap, secondWeightMap):
420 for i, face in enumerate(mesh.faces):
421 faceVertexCount=bl.face.getVertexCount(face)
422 material=mesh.materials[bl.face.getMaterialIndex(face)]
423 v=[mesh.verts[index] for index in bl.face.getVertices(face)]
424 uv=bl.mesh.getFaceUV(
425 mesh, i, face, bl.face.getVertexCount(face))
427 if faceVertexCount==3:
429 self.vertexArray.addTriangle(
430 obj_name, material.name,
437 bl.vertex.getNormal(v[2]),
438 bl.vertex.getNormal(v[1]),
439 bl.vertex.getNormal(v[0]),
443 weightMap[v[2].index][0],
444 weightMap[v[1].index][0],
445 weightMap[v[0].index][0],
446 secondWeightMap[v[2].index][0],
447 secondWeightMap[v[1].index][0],
448 secondWeightMap[v[0].index][0],
449 weightMap[v[2].index][1],
450 weightMap[v[1].index][1],
451 weightMap[v[0].index][1]
453 elif faceVertexCount==4:
455 self.vertexArray.addTriangle(
456 obj_name, material.name,
463 bl.vertex.getNormal(v[2]),
464 bl.vertex.getNormal(v[1]),
465 bl.vertex.getNormal(v[0]),
469 weightMap[v[2].index][0],
470 weightMap[v[1].index][0],
471 weightMap[v[0].index][0],
472 secondWeightMap[v[2].index][0],
473 secondWeightMap[v[1].index][0],
474 secondWeightMap[v[0].index][0],
475 weightMap[v[2].index][1],
476 weightMap[v[1].index][1],
477 weightMap[v[0].index][1]
479 self.vertexArray.addTriangle(
480 obj_name, material.name,
487 bl.vertex.getNormal(v[0]),
488 bl.vertex.getNormal(v[3]),
489 bl.vertex.getNormal(v[2]),
493 weightMap[v[0].index][0],
494 weightMap[v[3].index][0],
495 weightMap[v[2].index][0],
496 secondWeightMap[v[0].index][0],
497 secondWeightMap[v[3].index][0],
498 secondWeightMap[v[2].index][0],
499 weightMap[v[0].index][1],
500 weightMap[v[3].index][1],
501 weightMap[v[2].index][1]
504 def __mesh(self, obj):
508 if RIGID_SHAPE_TYPE in obj:
510 if CONSTRAINT_A in obj:
513 #if not bl.modifier.hasType(obj, 'ARMATURE'):
516 bl.message("export: %s" % obj.name)
518 # メッシュのコピーを生成してオブジェクトの行列を適用する
519 copyMesh, copyObj=bl.object.duplicate(obj)
520 if len(copyMesh.verts)>0:
522 copyObj.scale=obj.scale
523 bpy.ops.object.scale_apply()
524 copyObj.rotation_euler=obj.rotation_euler
525 bpy.ops.object.rotation_apply()
526 copyObj.location=obj.location
527 bpy.ops.object.location_apply()
529 for m in [m for m in copyObj.modifiers]:
530 if m.type=='SOLIDFY':
532 elif m.type=='ARMATURE':
534 elif m.type=='MIRROR':
535 bpy.ops.object.modifier_apply(modifier=m.name)
539 weightMap, secondWeightMap=self.__getWeightMap(copyObj, copyMesh)
540 self.__processFaces(obj.name, copyMesh, weightMap, secondWeightMap)
541 bl.object.delete(copyObj)
543 def createEmptyBasicSkin(self):
544 self.__getOrCreateMorph('base', 0)
546 def __skin(self, obj):
547 if not bl.object.hasShapeKey(obj):
551 blenderMesh=bl.object.getData(obj)
555 vg=bl.object.getVertexGroup(obj, MMD_SHAPE_GROUP_NAME)
559 for b in bl.object.getShapeKeys(obj):
560 if b.name==BASE_SHAPE_NAME:
561 baseMorph=self.__getOrCreateMorph('base', 0)
566 v=bl.shapekey.getByIndex(b, index)
567 pos=[v[0], v[1], v[2]]
569 indices=self.vertexArray.getMappedIndex(obj.name, index)
570 for attribute, i in indices.items():
575 baseMorph.add(i, pos)
576 indexRelativeMap[i]=relativeIndex
581 print(basis.name, len(baseMorph.offsets))
583 if len(baseMorph.offsets)==0:
587 for b in bl.object.getShapeKeys(obj):
588 if b.name==BASE_SHAPE_NAME:
592 morph=self.__getOrCreateMorph(b.name, 4)
594 for index, src, dst in zip(
595 xrange(len(blenderMesh.verts)),
596 bl.shapekey.get(basis),
598 offset=[dst[0]-src[0], dst[1]-src[1], dst[2]-src[2]]
599 if offset[0]==0 and offset[1]==0 and offset[2]==0:
602 indices=self.vertexArray.getMappedIndex(obj.name, index)
603 for attribute, i in indices.items():
607 morph.add(indexRelativeMap[i], offset)
608 assert(len(morph.offsets)<len(baseMorph.offsets))
611 original=self.morphList[:]
613 for i, v in enumerate(englishmap.skinMap):
617 return len(englishmap.skinMap)
619 self.morphList.sort(lambda l, r: getIndex(l)-getIndex(r))
621 self.morphList.sort(key=getIndex)
623 def __rigidbody(self, obj):
626 if not RIGID_SHAPE_TYPE in obj:
628 self.rigidbodies.append(obj)
630 def __constraint(self, obj):
633 if not CONSTRAINT_A in obj:
635 self.constraints.append(obj)
637 def __getOrCreateMorph(self, name, type):
638 for m in self.morphList:
642 self.morphList.append(m)
645 def getVertexCount(self):
646 return len(self.vertexArray.positions)
650 __slots__=['index', 'name', 'ik_index',
651 'pos', 'tail', 'parent_index', 'tail_index', 'type', 'isConnect']
652 def __init__(self, name, pos, tail, isConnect):
657 self.parent_index=None
660 self.isConnect=isConnect
663 def __eq__(self, rhs):
664 return self.index==rhs.index
667 return "<Bone %s %d>" % (self.name, self.type)
669 class BoneBuilder(object):
670 __slots__=['bones', 'boneMap', 'ik_list', 'bone_groups',]
677 def getBoneGroup(self, bone):
678 for i, g in enumerate(self.bone_groups):
682 print('no gorup', bone)
685 def build(self, armatureObj):
689 bl.message("build skeleton")
690 armature=bl.object.getData(armatureObj)
695 for g in bl.object.boneGroups(armatureObj):
696 self.bone_groups.append((g.name, []))
701 for b in armature.bones.values():
705 bl.bone.getHeadLocal(b),
706 bl.bone.getTailLocal(b),
709 self.__getBone(bone, b)
711 for b in armature.bones.values():
713 self.__checkConnection(b, None)
718 pose = bl.object.getPose(armatureObj)
719 for b in pose.bones.values():
723 self.__assignBoneGroup(b, b.bone_group)
724 for c in b.constraints:
725 if bl.constraint.isIKSolver(c):
729 target=self.__boneByName(bl.constraint.ikTarget(c))
736 link=self.__boneByName(b.name)
741 chainLength=bl.constraint.ikChainLen(c)
742 for i in range(chainLength):
744 chainBone=self.__boneByName(e.name)
746 chainBone.ik_index=target.index
749 IKSolver(target, link, chainLength,
750 int(bl.constraint.ikItration(c) * 0.1),
751 bl.constraint.ikRotationWeight(c)
761 for i, v in enumerate(englishmap.boneMap):
762 if v[0]==ik.target.name:
764 return len(englishmap.boneMap)
766 self.ik_list.sort(lambda l, r: getIndex(l)-getIndex(r))
768 self.ik_list.sort(key=getIndex)
770 def __assignBoneGroup(self, poseBone, boneGroup):
772 for g in self.bone_groups:
773 if g[0]==boneGroup.name:
774 g[1].append(poseBone.name)
776 def __checkConnection(self, b, p):
777 if bl.bone.isConnected(b):
778 parent=self.__boneByName(p.name)
779 parent.isConnect=True
782 self.__checkConnection(c, b)
788 boneMap=englishmap.boneMap
789 original=self.bones[:]
791 for i, k_v in enumerate(boneMap):
792 if k_v[0]==bone.name:
798 self.bones.sort(lambda l, r: getIndex(l)-getIndex(r))
800 self.bones.sort(key=getIndex)
803 for i, b in enumerate(self.bones):
804 src=original.index(b)
807 b.index=sortMap[b.index]
809 b.parent_index=sortMap[b.parent_index]
811 b.tail_index=sortMap[b.tail_index]
813 b.ik_index=sortMap[b.ik_index]
821 if b.parent_index==None:
822 b.parent_index=0xFFFF
824 if b.type==6 or b.type==7:
826 parent=self.bones[b.parent_index]
827 #print('parnet', parent.name)
828 parent.tail_index=b.index
831 if b.tail_index==None:
836 def getIndex(self, bone):
837 for i, b in enumerate(self.bones):
842 def indexByName(self, name):
846 return self.getIndex(self.__boneByName(name))
848 def __boneByName(self, name):
849 return self.boneMap[name]
851 def __getBone(self, parent, b):
852 if len(b.children)==0:
856 for i, c in enumerate(b.children):
858 bl.bone.getHeadLocal(c),
859 bl.bone.getTailLocal(c),
860 bl.bone.isConnected(c))
863 bone.parent_index=parent.index
865 if bone.isConnect or (not parent.tail_index and parent.tail==bone.pos):
866 parent.tail_index=bone.index
867 self.__getBone(bone, c)
869 def __addBone(self, bone):
870 bone.index=len(self.bones)
871 self.bones.append(bone)
872 self.boneMap[bone.name]=bone
875 class PmdExporter(object):
887 self.armatureObj=None
891 for o in bl.object.each():
892 object_node_map[o]=Node(o)
893 for o in bl.object.each():
894 node=object_node_map[o]
896 object_node_map[node.o.parent].children.append(node)
899 root=object_node_map[bl.object.getActive()]
901 self.englishName=o.name
902 self.englishComment=o[MMD_COMMENT] if MMD_COMMENT in o else 'blender export\n'
903 self.name=o[MMD_MB_NAME] if MMD_MB_NAME in o else 'Blenderエクスポート'
904 self.comment=o[MMD_MB_COMMENT] if MMD_MB_COMMENT in o else 'Blnderエクスポート\n'
907 self.oneSkinMesh=OneSkinMesh()
908 self.__createOneSkinMesh(root)
909 bl.message(self.oneSkinMesh)
910 if len(self.oneSkinMesh.morphList)==0:
912 self.oneSkinMesh.createEmptyBasicSkin()
915 self.skeleton=BoneBuilder()
916 self.skeleton.build(self.armatureObj)
918 def __createOneSkinMesh(self, node):
919 ############################################################
920 # search armature modifier
921 ############################################################
922 for m in node.o.modifiers:
923 if bl.modifier.isType(m, 'ARMATURE'):
924 armatureObj=bl.modifier.getArmatureObject(m)
925 if not self.armatureObj:
926 self.armatureObj=armatureObj
927 elif self.armatureObj!=armatureObj:
928 print("warning! found multiple armature. ignored.",
931 if node.o.type.upper()=='MESH':
932 self.oneSkinMesh.addMesh(node.o)
934 for child in node.children:
935 self.__createOneSkinMesh(child)
937 def write(self, path):
939 io.setName(toCP932(self.name))
940 io.setComment(toCP932(self.comment))
944 for pos, attribute, b0, b1, weight in self.oneSkinMesh.vertexArray.zip():
945 # convert right-handed z-up to left-handed y-up
950 v.normal.x=attribute.nx
951 v.normal.y=attribute.ny
952 v.normal.z=attribute.nz
954 v.uv.y=1.0-attribute.v # reverse vertical
955 v.bone0=self.skeleton.indexByName(b0)
956 v.bone1=self.skeleton.indexByName(b1)
957 v.weight0=int(100*weight)
958 v.edge_flag=0 # edge flag, 0: enable edge, 1: not edge
961 vertexCount=self.oneSkinMesh.getVertexCount()
962 for material_name, indices in self.oneSkinMesh.vertexArray.each():
963 #print('material:', material_name)
964 m=bl.material.get(material_name)
966 material=io.addMaterial()
967 setMaterialParams(material, m)
969 material.vertex_count=len(indices)
970 material.toon_index=0
971 textures=[os.path.basename(path)
972 for path in bl.material.eachEnalbeTexturePath(m)]
974 material.setTexture(toCP932('*'.join(textures)))
976 material.setTexture(toCP932(""))
979 assert(i<vertexCount)
980 for i in xrange(0, len(indices), 3):
982 io.indices.append(indices[i])
983 io.indices.append(indices[i+1])
984 io.indices.append(indices[i+2])
988 for i, b in enumerate(self.skeleton.bones):
992 boneNameMap[b.name]=i
993 v=englishmap.getUnicodeBoneName(b.name)
997 cp932=v[1].encode('cp932')
998 assert(len(cp932)<20)
1002 bone_english_name=toCP932(b.name)
1003 assert(len(bone_english_name)<20)
1004 bone.setEnglishName(bone_english_name)
1009 b.ik_index=self.skeleton.indexByName('eyes')
1014 bone.parent_index=b.parent_index
1015 bone.tail_index=b.tail_index
1016 bone.ik_index=b.ik_index
1018 # convert right-handed z-up to left-handed y-up
1019 bone.pos.x=b.pos[0] if not near(b.pos[0], 0) else 0
1020 bone.pos.y=b.pos[2] if not near(b.pos[2], 0) else 0
1021 bone.pos.z=b.pos[1] if not near(b.pos[1], 0) else 0
1024 for ik in self.skeleton.ik_list:
1026 solver.index=self.skeleton.getIndex(ik.target)
1027 solver.target=self.skeleton.getIndex(ik.effector)
1028 solver.length=ik.length
1029 b=self.skeleton.bones[ik.effector.parent_index]
1030 for i in xrange(solver.length):
1031 solver.children.append(self.skeleton.getIndex(b))
1032 b=self.skeleton.bones[b.parent_index]
1033 solver.iterations=ik.iterations
1034 solver.weight=ik.weight
1037 for i, m in enumerate(self.oneSkinMesh.morphList):
1041 v=englishmap.getUnicodeSkinName(m.name)
1043 v=[m.name, m.name, 0]
1045 cp932=v[1].encode('cp932')
1046 morph.setName(cp932)
1047 morph.setEnglishName(m.name.encode('cp932'))
1050 for index, offset in m.offsets:
1051 # convert right-handed z-up to left-handed y-up
1052 morph.append(index, offset[0], offset[2], offset[1])
1053 morph.vertex_count=len(m.offsets)
1057 for i, m in enumerate(self.oneSkinMesh.morphList):
1059 io.face_list.append(i)
1060 for i, m in enumerate(self.oneSkinMesh.morphList):
1062 io.face_list.append(i)
1063 for i, m in enumerate(self.oneSkinMesh.morphList):
1065 io.face_list.append(i)
1066 for i, m in enumerate(self.oneSkinMesh.morphList):
1068 io.face_list.append(i)
1071 for g in self.skeleton.bone_groups:
1072 boneDisplayName=io.addBoneGroup()
1074 name=englishmap.getUnicodeBoneGroupName(g[0])
1077 boneDisplayName.setName(toCP932(name+'\n'))
1080 boneDisplayName.setEnglishName(toCP932(englishName+'\n'))
1083 for i, b in enumerate(self.skeleton.bones):
1086 if b.type in [6, 7]:
1088 io.addBoneDisplay(i, self.skeleton.getBoneGroup(b))
1090 #assert(len(io.bones)==len(io.bone_display_list)+1)
1093 io.setEnglishName(toCP932(self.englishName))
1094 io.setEnglishComment(toCP932(self.englishComment))
1098 for o in bl.object.each():
1100 if o.name.startswith(TOON_TEXTURE_OBJECT):
1106 toonMesh=bl.object.getData(toonMeshObject)
1107 toonMaterial=bl.mesh.getMaterial(toonMesh, 0)
1109 t=bl.material.getTexture(toonMaterial, i)
1111 io.getToonTexture(i).setName(toCP932(t.name))
1113 io.getToonTexture(i).setName(toCP932("toon%02d.bmp\n" % i))
1116 io.getToonTexture(i).setName(toCP932("toon%02d.bmp\n" % i))
1120 for i, obj in enumerate(self.oneSkinMesh.rigidbodies):
1121 name=obj[RIGID_NAME] if RIGID_NAME in obj else obj.name
1123 rigidBody=io.addRigidBody()
1124 rigidBody.setName(name.encode('cp932'))
1125 rigidNameMap[name]=i
1126 boneIndex=boneNameMap[obj[RIGID_BONE_NAME]]
1129 bone=self.skeleton.bones[0]
1131 bone=self.skeleton.bones[boneIndex]
1132 rigidBody.boneIndex=boneIndex
1133 #rigidBody.position.x=obj[RIGID_LOCATION][0]
1134 #rigidBody.position.y=obj[RIGID_LOCATION][1]
1135 #rigidBody.position.z=obj[RIGID_LOCATION][2]
1136 rigidBody.position.x=obj.location.x-bone.pos[0]
1137 rigidBody.position.y=obj.location.z-bone.pos[2]
1138 rigidBody.position.z=obj.location.y-bone.pos[1]
1139 rigidBody.rotation.x=-obj.rotation_euler[0]
1140 rigidBody.rotation.y=-obj.rotation_euler[2]
1141 rigidBody.rotation.z=-obj.rotation_euler[1]
1142 rigidBody.processType=obj[RIGID_PROCESS_TYPE]
1143 rigidBody.group=obj[RIGID_GROUP]
1144 rigidBody.target=obj[RIGID_INTERSECTION_GROUP]
1145 rigidBody.weight=obj[RIGID_WEIGHT]
1146 rigidBody.linearDamping=obj[RIGID_LINEAR_DAMPING]
1147 rigidBody.angularDamping=obj[RIGID_ANGULAR_DAMPING]
1148 rigidBody.restitution=obj[RIGID_RESTITUTION]
1149 rigidBody.friction=obj[RIGID_FRICTION]
1150 if obj[RIGID_SHAPE_TYPE]==0:
1151 rigidBody.shapeType=pmd.SHAPE_SPHERE
1152 rigidBody.w=obj.scale[0]
1153 elif obj[RIGID_SHAPE_TYPE]==1:
1154 rigidBody.shapeType=pmd.SHAPE_BOX
1155 rigidBody.w=obj.scale[0]
1156 rigidBody.d=obj.scale[1]
1157 rigidBody.h=obj.scale[2]
1158 elif obj[RIGID_SHAPE_TYPE]==2:
1159 rigidBody.shapeType=pmd.SHAPE_CAPSULE
1160 rigidBody.w=obj.scale[0]
1161 rigidBody.h=obj.scale[2]
1164 for obj in self.oneSkinMesh.constraints:
1165 constraint=io.addConstraint()
1166 constraint.setName(obj[CONSTRAINT_NAME].encode('cp932'))
1167 constraint.rigidA=rigidNameMap[obj[CONSTRAINT_A]]
1168 constraint.rigidB=rigidNameMap[obj[CONSTRAINT_B]]
1169 constraint.pos.x=obj.location[0]
1170 constraint.pos.y=obj.location[2]
1171 constraint.pos.z=obj.location[1]
1172 constraint.rot.x=-obj.rotation_euler[0]
1173 constraint.rot.y=-obj.rotation_euler[2]
1174 constraint.rot.z=-obj.rotation_euler[1]
1175 constraint.constraintPosMin.x=obj[CONSTRAINT_POS_MIN][0]
1176 constraint.constraintPosMin.y=obj[CONSTRAINT_POS_MIN][1]
1177 constraint.constraintPosMin.z=obj[CONSTRAINT_POS_MIN][2]
1178 constraint.constraintPosMax.x=obj[CONSTRAINT_POS_MAX][0]
1179 constraint.constraintPosMax.y=obj[CONSTRAINT_POS_MAX][1]
1180 constraint.constraintPosMax.z=obj[CONSTRAINT_POS_MAX][2]
1181 constraint.constraintRotMin.x=obj[CONSTRAINT_ROT_MIN][0]
1182 constraint.constraintRotMin.y=obj[CONSTRAINT_ROT_MIN][1]
1183 constraint.constraintRotMin.z=obj[CONSTRAINT_ROT_MIN][2]
1184 constraint.constraintRotMax.x=obj[CONSTRAINT_ROT_MAX][0]
1185 constraint.constraintRotMax.y=obj[CONSTRAINT_ROT_MAX][1]
1186 constraint.constraintRotMax.z=obj[CONSTRAINT_ROT_MAX][2]
1187 constraint.springPos.x=obj[CONSTRAINT_SPRING_POS][0]
1188 constraint.springPos.y=obj[CONSTRAINT_SPRING_POS][1]
1189 constraint.springPos.z=obj[CONSTRAINT_SPRING_POS][2]
1190 constraint.springRot.x=obj[CONSTRAINT_SPRING_ROT][0]
1191 constraint.springRot.y=obj[CONSTRAINT_SPRING_ROT][1]
1192 constraint.springRot.z=obj[CONSTRAINT_SPRING_ROT][2]
1195 bl.message('write: %s' % path)
1196 return io.write(path)
1199 def _execute(filename):
1200 bl.initialize('pmd_export', scene)
1201 active=bl.object.getActive()
1203 print("abort. no active object.")
1205 exporter=PmdExporter()
1208 exporter.write(filename)
1209 bl.object.activate(active)