X-Git-Url: http://git.osdn.jp/view?a=blobdiff_plain;f=swig%2Fblender24%2Fpmd_export.py;h=c407b4e0927b6119cec1e62f2ea1b75df794e25e;hb=bfebd28b39c1a986abc0ad47aa431a3d924e49ff;hp=b6de92cbb0d4d7e3999898f94dee4cd1de4e21a8;hpb=4be99cad4871609f1892332f90ad349c01ac8b11;p=meshio%2Fmeshio.git diff --git a/swig/blender24/pmd_export.py b/swig/blender24/pmd_export.py index b6de92c..c407b4e 100644 --- a/swig/blender24/pmd_export.py +++ b/swig/blender24/pmd_export.py @@ -7,11 +7,12 @@ Tooltip: 'Export PMD file for MikuMikuDance.' """ __author__= ["ousttrue"] -__version__= "0.2" +__version__= "1.0" __url__=() __bpydoc__=""" 0.1 20100318 first implementation. 0.2 20100519 refactoring. use C extension. +1.0 20100530 implement, basic features. """ import Blender import os @@ -177,6 +178,8 @@ class Morph(object): def sort(self): self.offsets.sort(lambda l, r: l[0]-r[0]) + def __str__(self): + return "" % self.name class IKSolver(object): __slots__=['target', 'effector', 'length', 'iterations', 'weight'] @@ -350,7 +353,7 @@ class OneSkinMesh(object): indices=self.vertexArray.getMappedIndices(index) for i in indices: baseMorph.add(i, pos) - indexRelativeMap[i]=relativeIndex + indexRelativeMap[relativeIndex]=i relativeIndex+=1 break print(len(baseMorph.offsets)) @@ -377,6 +380,17 @@ class OneSkinMesh(object): morph.add(indexRelativeMap[i], offset) assert(len(morph.offsets)==len(baseMorph.offsets)) + # sort skinmap + if len(self.morphList)>1: + original=self.morphList[:] + def getIndex(morph): + for i, v in enumerate(englishmap.skinMap): + if v[0]==morph.name: + return i + #print(morph) + return 0 + self.morphList.sort(lambda l, r: getIndex(l)-getIndex(r)) + def __getOrCreateMorph(self, name, type): for m in self.morphList: if m.name==name: @@ -477,7 +491,9 @@ class BoneBuilder(object): e=e.parent self.ik_list.append( IKSolver(target, link, chainLength, - c[cSetting.ITERATIONS], c.influence)) + int(c[cSetting.ITERATIONS] * 0.1), + c[cSetting.ROTWEIGHT] + )) def __checkConnection(self, b, p): if Blender.Armature.CONNECTED in b.options: @@ -572,6 +588,12 @@ class PmdExporter(object): self.builder=BoneBuilder() self.builder.build(self.armatureObj) self.builder.sortBy(englishmap.boneMap) + def getIndex(ik): + for i, v in enumerate(englishmap.boneMap): + if v[0]==ik.target.name: + return i + return len(englishmap.boneMap) + self.builder.ik_list.sort(lambda l, r: getIndex(l)-getIndex(r)) def __createOneSkinMesh(self, node): ############################################################ @@ -592,12 +614,14 @@ class PmdExporter(object): self.__createOneSkinMesh(child) def write(self, path): + print('write') io=pmd.IO() io.name=self.name io.comment="blender export" io.version=1.0 # 頂点 + print('vertices') for pos, normal, uv, b0, b1, weight in self.oneSkinMesh.vertexArray.zip(): # convert right-handed z-up to left-handed y-up v=io.addVertex() @@ -615,6 +639,7 @@ class PmdExporter(object): v.edge_flag=0 # edge flag, 0: enable edge, 1: not edge # 面とマテリアル + print('faces and materials') vertexCount=self.oneSkinMesh.getVertexCount() for m, indices in self.oneSkinMesh.vertexArray.indexArrays.items(): m=Blender.Material.Get(m) @@ -650,6 +675,7 @@ class PmdExporter(object): #io.indices.append(indices[i+2]) # bones + print('bones') for b in self.builder.bones: if b.name.endswith("_t"): if b.name.startswith("arm twist1_") or b.name.startswith("arm twist2_"): @@ -698,6 +724,7 @@ class PmdExporter(object): bone.pos.z=b.pos[1] if not near(b.pos[1], 0) else 0 # IK + print('ik') for ik in self.builder.ik_list: solver=io.addIK() solver.index=self.builder.getIndex(ik.target) @@ -711,38 +738,62 @@ class PmdExporter(object): solver.weight=ik.weight # 表情 + print('shape keys') for i, m in enumerate(self.oneSkinMesh.morphList): # morph morph=io.addMorph() - unicode=englishmap.getUnicodeSkinName(m.name) - if unicode: - cp932=unicode.encode('cp932') - else: - cp932=m.name + v=englishmap.getUnicodeSkinName(m.name) + assert(v) + cp932=v[1].encode('cp932') morph.name="%s\n" % cp932 morph.english_name="%s\n" % m.name - morph.type=m.type + m.type=v[2] + morph.type=v[2] for index, offset in m.offsets: # convert right-handed z-up to left-handed y-up morph.append(index, offset[0], offset[2], offset[1]) morph.vertex_count=len(m.offsets) - # 表情枠 - if i>0: + # 表情枠 + print('display list') + # type==0はbase + for i, m in enumerate(self.oneSkinMesh.morphList): + if m.type==3: + io.face_list.append(i) + for i, m in enumerate(self.oneSkinMesh.morphList): + if m.type==2: + io.face_list.append(i) + for i, m in enumerate(self.oneSkinMesh.morphList): + if m.type==1: + io.face_list.append(i) + for i, m in enumerate(self.oneSkinMesh.morphList): + if m.type==4: io.face_list.append(i) # ボーン表示枠 - boneDisplayName=io.addBoneDisplayName() - boneDisplayName.name="bones\n" - boneDisplayName.english_name="bones\n" - displayIndex=1 + print('bone display list') + def createBoneDisplayName(name, english): + boneDisplayName=io.addBoneDisplayName() + boneDisplayName.name=name.decode('utf-8').encode('cp932') + boneDisplayName.english_name=english + boneDisplayName=createBoneDisplayName("IK\n", "IK\n") + boneDisplayName=createBoneDisplayName("体(上)\n", "Body[u]\n") + boneDisplayName=createBoneDisplayName("髪\n", "Hair\n") + boneDisplayName=createBoneDisplayName("腕\n", "Arms\n") + boneDisplayName=createBoneDisplayName("指\n", "Fingers\n") + boneDisplayName=createBoneDisplayName("体(下)\n", "Body[l]\n") + boneDisplayName=createBoneDisplayName("足\n", "Legs\n") for i, b in enumerate(self.builder.bones): + if i==0: + continue if b.type in [6, 7]: - io.addBoneDisplay(i, displayIndex) + continue + io.addBoneDisplay(i, getBoneDisplayGroup(b)) # English + print('english') io.english_name="blender export" io.english_coment="blender export" @@ -750,8 +801,60 @@ class PmdExporter(object): io.getToonTexture(i).name="toon%02d.bmp\n" % i # 書き込み - return io.write(path.encode(FS_ENCODING)) - + return io.write(path) + + +def getBoneDisplayGroup(bone): + boneGroups=[ + [ # IK + "necktie IK", "hair IK_L", "hair IK_R", "leg IK_L", "leg IK_R", + "toe IK_L", "toe IK_R", + ], + [ # 体(上) + "upper body", "neck", "head", "eye_L", "eye_R", + "necktie1", "necktie2", "necktie3", "eyes", + "eyelight_L", "eyelight_R", + ], + [ # 髪 + "front hair1", "front hair2", "front hair3", + "hair1_L", "hair2_L", "hair3_L", + "hair4_L", "hair5_L", "hair6_L", + "hair1_R", "hair2_R", "hair3_R", + "hair4_R", "hair5_R", "hair6_R", + ], + [ # 腕 + "shoulder_L", "arm_L", "arm twist_L", "elbow_L", + "wrist twist_L", "wrist_L", "sleeve_L", + "shoulder_R", "arm_R", "arm twist_R", "elbow_R", + "wrist twist_R", "wrist_R", "sleeve_R", + ], + [ # 指 + "thumb1_L", "thumb2_L", "fore1_L", "fore2_L", "fore3_L", + "middle1_L", "middle2_L", "middle3_L", + "third1_L", "third2_L", "third3_L", + "little1_L", "little2_L", "little3_L", + "thumb1_R", "thumb2_R", "fore1_R", "fore2_R", "fore3_R", + "middle1_R", "middle2_R", "middle3_R", + "third1_R", "third2_R", "third3_R", + "little1_R", "little2_R", "little3_R", + ], + [ # 体(下) + "lower body", "waist accessory", + "front skirt_L", "back skirt_L", + "front skirt_R", "back skirt_R", + ], + [ # 足 + "leg_L", "knee_L", "ankle_L", + "leg_R", "knee_R", "ankle_R", + ], + ] + index=1 + for g in boneGroups: + if bone.name in g: + return index + index+=1 + print(bone) + return -1 def export_pmd(filename): filename=filename.decode(INTERNAL_ENCODING)