X-Git-Url: http://git.osdn.net/view?a=blobdiff_plain;f=blender26-meshio%2Fimport_pmx.py;h=c3173152c121799e80eb03b88f15f7246ba06f36;hb=a7744035815a6ad1c446abc5fcc94c74fc8484e0;hp=453355bb2b92ccae79a4696a029858ca593ad3b5;hpb=dd75f61d18ee761ce84e52939132fd5c9d06920d;p=meshio%2Fpymeshio.git diff --git a/blender26-meshio/import_pmx.py b/blender26-meshio/import_pmx.py index 453355b..c317315 100644 --- a/blender26-meshio/import_pmx.py +++ b/blender26-meshio/import_pmx.py @@ -8,7 +8,24 @@ import os from . import bl +def convert_coord(pos): + """ + Left handed y-up to Right handed z-up + """ + return (pos.x, pos.z, pos.y) + def __create_a_material(m, name, textures_and_images): + """ + materialを作成する + + :Params: + m + pymeshio.pmx.Material + name + material name + textures_and_images + list of (texture, image) + """ material = bl.material.create(name) # diffuse material.diffuse_shader='FRESNEL' @@ -31,7 +48,54 @@ def __create_a_material(m, name, textures_and_images): texture_index=bl.material.addTexture(material, textures_and_images[m.texture_index][0]) return material +def __create_armature(bones): + """ + armatureを作成する + + :Params: + bones + list of pymeshio.pmx.Bone + """ + armature, armature_object=bl.armature.create() + + bl.armature.makeEditable(armature_object) + # create bones + def create_bone(b): + bone=bl.armature.createBone(armature, b.name) + # bone position + bone.head=bl.createVector(*convert_coord(b.position)) + if not b.getConnectionFlag(): + bone.tail=bl.createVector(*convert_coord(b.position)) + elif not b.getVisibleFlag(): + bone.tail=bone.head+bl.createVector(0, 1, 0) + + return bone + bl_bones=[create_bone(b) for b in bones] + + # build skeleton + for b, bone in zip(bones, bl_bones): + if b.parent_index!=-1: + print("%s -> %s" % (bones[b.parent_index].name, b.name)) + parent_bone=bl_bones[b.parent_index] + bone.parent=parent_bone + if b.getConnectionFlag() and b.tail_index!=-1: + assert(b.tail_index!=0) + tail_bone=bl_bones[b.tail_index] + bone.tail=tail_bone.head + bl.bone.setConnected(tail_bone) + else: + print("no parent %s" % b.name) + + # fix + bl.armature.update(armature) + bl.enterObjectMode() + + return armature_object + def _execute(filepath): + """ + importerr 本体 + """ bl.progress_set('load %s' % filepath, 0.0) print(filepath) @@ -47,14 +111,19 @@ def _execute(filepath): model_name=model.english_name if len(model_name)==0: model_name=os.path.basename(filepath) - root=bl.object.createEmpty(model_name) - root[bl.MMD_MB_NAME]=model.name - root[bl.MMD_MB_COMMENT]=model.comment - root[bl.MMD_COMMENT]=model.english_comment + root_object=bl.object.createEmpty(model_name) + root_object[bl.MMD_MB_NAME]=model.name + root_object[bl.MMD_MB_COMMENT]=model.comment + root_object[bl.MMD_COMMENT]=model.english_comment + + # armatureを作る + armature_object=__create_armature(model.bones) + if armature_object: + bl.object.makeParent(root_object, armature_object) # テクスチャを作る texture_dir=os.path.dirname(filepath) - textures_and_images=[bl.texture.create(os.path.join(texture_dir, t)) + textures_and_images=[bl.texture.create(os.path.join(texture_dir, t)) for t in model.textures] print(textures_and_images) @@ -65,7 +134,7 @@ def _execute(filepath): return fmt.format(*args) index_generator=(i for i in model.indices) # 頂点配列。(Left handed y-up) to (Right handed z-up) - vertices=[(pos.x, pos.z, pos.y) + vertices=[convert_coord(pos) for pos in (v.position for v in model.vertices)] for i, m in enumerate(model.materials): # マテリアル毎にメッシュを作成する @@ -77,12 +146,19 @@ def _execute(filepath): # activate object bl.object.deselectAll() bl.object.activate(mesh_object) - bl.object.makeParent(root, mesh_object) - indices=[next(index_generator) + bl.object.makeParent(root_object, mesh_object) + # vertices & faces + indices=[next(index_generator) for _ in range(m.vertex_count)] - bl.mesh.addGeometry(mesh, vertices, - [(indices[i], indices[i+1], indices[i+2]) + bl.mesh.addGeometry(mesh, vertices, + [(indices[i], indices[i+1], indices[i+2]) for i in range(0, len(indices), 3)]) + if armature_object: + # armature modifirer + bl.modifier.addArmature(mesh_object, armature_object) + + # shape + return {'FINISHED'}