X-Git-Url: http://git.osdn.jp/view?a=blobdiff_plain;f=blender26-meshio%2Fexport_pmx.py;h=7493bd0808eee602fbf7f5a54718ec5c7fad4ddd;hb=607dea923e3f6baa83031ae37e22bc2075ee71ff;hp=12c86a377ce0e511c76dadffc59a300ae120b166;hpb=56e8906d87aa792e34cd819396e4970f4aac7372;p=meshio%2Fpymeshio.git diff --git a/blender26-meshio/export_pmx.py b/blender26-meshio/export_pmx.py old mode 100755 new mode 100644 index 12c86a3..7493bd0 --- a/blender26-meshio/export_pmx.py +++ b/blender26-meshio/export_pmx.py @@ -1,5 +1,314 @@ # coding: utf-8 -def write(self, path): - pass +import io +from . import bl +from . import oneskinmesh +from .pymeshio import pmx +from .pymeshio import common +from .pymeshio.pmx import writer + + +def create_pmx(ex): + model=pmx.Model() + model.name=ex.name + model.comment=ex.comment + + def get_deform(b0, b1, weight): + print(b0, b1, weight) + if b0==-1: + return pmx.Bdef1(b1, weight) + elif b1==-1: + return pmx.Bdef1(b0, weight) + else: + return pmx.Bdef2(b0, b1, weight) + + model.vertices=[pmx.Vertex( + # convert right-handed z-up to left-handed y-up + common.Vector3(pos[0], pos[2], pos[1]), + # convert right-handed z-up to left-handed y-up + common.Vector3(attribute.nx, attribute.nz, attribute.ny), + # reverse vertical + common.Vector2(attribute.u, 1.0-attribute.v), + get_deform(ex.skeleton.indexByName(b0), ex.skeleton.indexByName(b1), weight), + # edge flag, 0: enable edge, 1: not edge + 1.0 + ) + for pos, attribute, b0, b1, weight in ex.oneSkinMesh.vertexArray.zip()] + + # bones + boneNameMap={} + for i, b in enumerate(self.skeleton.bones): + + # name + boneNameMap[b.name]=i + v=englishmap.getUnicodeBoneName(b.name) + if not v: + v=[b.name, b.name] + assert(v) + bone=pmx.Bone(v[1]) + bone.english_name=b.name + + if len(v)>=3: + # has type + if v[2]==5: + b.ik_index=self.skeleton.indexByName('eyes') + bone.type=v[2] + else: + bone.type=b.type + + bone.parent_index=b.parent_index + bone.tail_index=b.tail_index + bone.ik_index=b.ik_index + + # convert right-handed z-up to left-handed y-up + bone.pos.x=b.pos[0] if not near(b.pos[0], 0) else 0 + bone.pos.y=b.pos[2] if not near(b.pos[2], 0) else 0 + bone.pos.z=b.pos[1] if not near(b.pos[1], 0) else 0 + + model.bones.append(bone) + return model + + # IK + for ik in self.skeleton.ik_list: + solver=pmd.IK() + solver.index=self.skeleton.getIndex(ik.target) + solver.target=self.skeleton.getIndex(ik.effector) + solver.length=ik.length + b=self.skeleton.bones[ik.effector.parent_index] + for i in range(solver.length): + solver.children.append(self.skeleton.getIndex(b)) + b=self.skeleton.bones[b.parent_index] + solver.iterations=ik.iterations + solver.weight=ik.weight + model.ik_list.append(solver) + + # 面とマテリアル + vertexCount=self.oneSkinMesh.getVertexCount() + for material_name, indices in self.oneSkinMesh.vertexArray.each(): + #print('material:', material_name) + try: + m=bl.material.get(material_name) + except KeyError as e: + m=DefaultMatrial() + def get_texture_name(texture): + pos=texture.replace("\\", "/").rfind("/") + if pos==-1: + return texture + else: + return texture[pos+1:] + textures=[get_texture_name(path) + for path in bl.material.eachEnalbeTexturePath(m)] + print(textures) + # マテリアル + model.materials.append(pmd.Material( + # diffuse_color + common.RGB(m.diffuse_color[0], m.diffuse_color[1], m.diffuse_color[2]), + m.alpha, + # specular_factor + 0 if m.specular_toon_size<1e-5 else m.specular_hardness*10, + # specular_color + common.RGB(m.specular_color[0], m.specular_color[1], m.specular_color[2]), + # ambient_color + common.RGB(m.mirror_color[0], m.mirror_color[1], m.mirror_color[2]), + # flag + 1 if m.subsurface_scattering.use else 0, + # toon + 0, + # vertex_count + len(indices), + # texture + ('*'.join(textures) if len(textures)>0 else "").encode('cp932') + )) + # 面 + for i in indices: + assert(i