From f4804ad98eba965d2e8ad857777f63d315fd9563 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Sat, 10 Jul 2010 10:33:45 +0900 Subject: [PATCH] update bone group export. --- swig/blender/bl24.py | 4 ++ swig/blender/bl25.py | 4 ++ swig/blender/pmd_export.py | 167 ++++++++++++++++++++------------------------- 3 files changed, 82 insertions(+), 93 deletions(-) diff --git a/swig/blender/bl24.py b/swig/blender/bl24.py index 3732a73..d2f374c 100755 --- a/swig/blender/bl24.py +++ b/swig/blender/bl24.py @@ -272,6 +272,10 @@ class object: g.name=name g.color_set=color_set + @staticmethod + def boneGroups(o): + return object.getPose(o).bone_groups + class modifier: @staticmethod diff --git a/swig/blender/bl25.py b/swig/blender/bl25.py index 43fe6d8..71f6636 100755 --- a/swig/blender/bl25.py +++ b/swig/blender/bl25.py @@ -239,6 +239,10 @@ class object: g.name=name g.color_set=color_set + @staticmethod + def boneGroups(o): + return object.getPose(o).bone_groups + class modifier: @staticmethod diff --git a/swig/blender/pmd_export.py b/swig/blender/pmd_export.py index 43fc7f9..1cc9add 100644 --- a/swig/blender/pmd_export.py +++ b/swig/blender/pmd_export.py @@ -477,7 +477,7 @@ class OneSkinMesh(object): if CONSTRAINT_A in obj: return - print("export", obj.name) + bl.message("export: %s" % obj.name) # メッシュのコピーを生成してオブジェクトの行列を適用する copyMesh, copyObj=bl.object.duplicate(obj) @@ -494,7 +494,12 @@ class OneSkinMesh(object): for m in [m for m in copyObj.modifiers]: if m.type=='SOLIDFY': continue - bpy.ops.object.modifier_apply(modifier=m.name) + elif m.type=='ARMATURE': + continue + elif m.type=='MIRROR': + bpy.ops.object.modifier_apply(modifier=m.name) + else: + print(m.tpye) weightMap, secondWeightMap=self.__getWeightMap(copyObj, copyMesh) self.__processFaces(copyMesh, weightMap, secondWeightMap) @@ -627,18 +632,37 @@ class Bone(object): return "" % (self.name, self.type) class BoneBuilder(object): - __slots__=['bones', 'boneMap', 'ik_list', ] + __slots__=['bones', 'boneMap', 'ik_list', 'bone_groups',] def __init__(self): self.bones=[] self.boneMap={} self.ik_list=[] + self.bone_groups=[] + + def getBoneGroup(self, bone): + for i, g in enumerate(self.bone_groups): + for b in g[1]: + if b==bone.name: + return i + print('no gorup', bone) + return 0 def build(self, armatureObj): if not armatureObj: return - print("gather bones") + bl.message("build skeleton") armature=bl.object.getData(armatureObj) + + #################### + # bone group + #################### + for g in bl.object.boneGroups(armatureObj): + self.bone_groups.append((g.name, [])) + + #################### + # get bones + #################### for b in armature.bones.values(): if b.name=='center': # root bone @@ -657,14 +681,19 @@ class BoneBuilder(object): self.__addBone(bone) self.__getBone(bone, b) - print("check connection") for b in armature.bones.values(): if not b.parent: self.__checkConnection(b, None) - print("gather ik") + #################### + # get IK + #################### pose = bl.object.getPose(armatureObj) for b in pose.bones.values(): + #################### + # assing bone group + #################### + self.__assignBoneGroup(b, b.bone_group) for c in b.constraints: if bl.constraint.isIKSolver(c): #################### @@ -695,6 +724,8 @@ class BoneBuilder(object): bl.constraint.ikRotationWeight(c) )) + #################### + # boneのsort self._sortBy() self._fix() @@ -709,6 +740,12 @@ class BoneBuilder(object): else: self.ik_list.sort(key=getIndex) + def __assignBoneGroup(self, poseBone, boneGroup): + if boneGroup: + for g in self.bone_groups: + if g[0]==boneGroup.name: + g[1].append(poseBone.name) + def __checkConnection(self, b, p): if bl.bone.isConnected(b): parent=self.__boneByName(p.name) @@ -827,7 +864,7 @@ class PmdExporter(object): # ワンスキンメッシュを作る self.oneSkinMesh=OneSkinMesh(scene) self.__createOneSkinMesh(root) - print(self.oneSkinMesh) + bl.message(self.oneSkinMesh) if len(self.oneSkinMesh.morphList)==0: # create emtpy skin self.oneSkinMesh.createEmptyBasicSkin() @@ -835,8 +872,8 @@ class PmdExporter(object): self.name=root.o.name # skeleton - self.builder=BoneBuilder() - self.builder.build(self.armatureObj) + self.skeleton=BoneBuilder() + self.skeleton.build(self.armatureObj) def __createOneSkinMesh(self, node): ############################################################ @@ -875,15 +912,15 @@ class PmdExporter(object): v.normal.z=normal[1] v.uv.x=uv[0] v.uv.y=1.0-uv[1] # reverse vertical - v.bone0=self.builder.indexByName(b0) - v.bone1=self.builder.indexByName(b1) + v.bone0=self.skeleton.indexByName(b0) + v.bone1=self.skeleton.indexByName(b1) v.weight0=int(100*weight) v.edge_flag=0 # edge flag, 0: enable edge, 1: not edge # 面とマテリアル vertexCount=self.oneSkinMesh.getVertexCount() for material_name, indices in self.oneSkinMesh.vertexArray.each(): - print('material:', material_name) + #print('material:', material_name) m=bl.material.get(material_name) # マテリアル material=io.addMaterial() @@ -908,7 +945,7 @@ class PmdExporter(object): # bones boneNameMap={} - for i, b in enumerate(self.builder.bones): + for i, b in enumerate(self.skeleton.bones): bone=io.addBone() # name @@ -929,7 +966,7 @@ class PmdExporter(object): if len(v)>=3: # has type if v[2]==5: - b.ik_index=self.builder.indexByName('eyes') + b.ik_index=self.skeleton.indexByName('eyes') bone.type=v[2] else: bone.type=b.type @@ -944,15 +981,15 @@ class PmdExporter(object): bone.pos.z=b.pos[1] if not near(b.pos[1], 0) else 0 # IK - for ik in self.builder.ik_list: + for ik in self.skeleton.ik_list: solver=io.addIK() - solver.index=self.builder.getIndex(ik.target) - solver.target=self.builder.getIndex(ik.effector) + solver.index=self.skeleton.getIndex(ik.target) + solver.target=self.skeleton.getIndex(ik.effector) solver.length=ik.length - b=self.builder.bones[ik.effector.parent_index] + b=self.skeleton.bones[ik.effector.parent_index] for i in xrange(solver.length): - solver.children.append(self.builder.getIndex(b)) - b=self.builder.bones[b.parent_index] + solver.children.append(self.skeleton.getIndex(b)) + b=self.skeleton.bones[b.parent_index] solver.iterations=ik.iterations solver.weight=ik.weight @@ -990,28 +1027,25 @@ class PmdExporter(object): if m.type==4: io.face_list.append(i) - # ボーン表示枠 - def createBoneDisplayName(name, english): + # ボーングループ + for g in self.skeleton.bone_groups: boneDisplayName=io.addBoneGroup() - if isBlender24(): - boneDisplayName.name=name.decode('utf-8').encode('cp932') - boneDisplayName.english_name=english - else: - boneDisplayName.setName(name.encode('cp932')) - boneDisplayName.setEnglishName(english.encode('cp932')) - 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): + # name + name=englishmap.getUnicodeBoneGroupName(g[0]) + if not name: + name=g[0] + boneDisplayName.setName(toCP932(name+'\n')) + # english + englishName=g[0] + boneDisplayName.setEnglishName(toCP932(englishName+'\n')) + + # ボーングループメンバー + for i, b in enumerate(self.skeleton.bones): if i==0: continue if b.type in [6, 7]: continue - io.addBoneDisplay(i, getBoneDisplayGroup(b)) + io.addBoneDisplay(i, self.skeleton.getBoneGroup(b)) # English io.english_name="blender export" @@ -1030,9 +1064,9 @@ class PmdExporter(object): boneIndex=boneNameMap[obj[RIGID_BONE_NAME]] if boneIndex==0: boneIndex=0xFFFF - bone=self.builder.bones[0] + bone=self.skeleton.bones[0] else: - bone=self.builder.bones[boneIndex] + bone=self.skeleton.bones[boneIndex] rigidBody.boneIndex=boneIndex #rigidBody.position.x=obj[RIGID_LOCATION][0] #rigidBody.position.y=obj[RIGID_LOCATION][1] @@ -1096,63 +1130,10 @@ class PmdExporter(object): constraint.springRot.z=obj[CONSTRAINT_SPRING_ROT][2] # 書き込み - print('write', path) + bl.message('write %s' % path) 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 0 - - def __execute(filename, scene): if not scene.objects.active: print("abort. no active object.") -- 2.11.0