Permalink
Browse files

implementing import_pmx...

  • Loading branch information...
1 parent dd75f61 commit a7744035815a6ad1c446abc5fcc94c74fc8484e0 @ousttrue committed Oct 12, 2011
Showing with 120 additions and 36 deletions.
  1. +16 −0 ChangeLog
  2. +1 −1 blender26-meshio/__init__.py
  3. +86 −10 blender26-meshio/import_pmx.py
  4. +10 −7 pymeshio/pmx/__init__.py
  5. +7 −18 setup.py
View
@@ -1,3 +1,19 @@
+2.2.3 (2011-10-12)
+~~~~~~~~~~~~~~~~~~
+* fix for pmxmca
+
+2.2.2 (2011-10-10)
+~~~~~~~~~~~~~~~~~~
+* add pmd2pmx script
+
+2.2.1 (2011-10-07)
+~~~~~~~~~~~~~~~~~~
+* implement pmd to pmx converter
+
+2.2.0 (2011-10-03)
+~~~~~~~~~~~~~~~~~~
+* implement pmx writer
+
2.1.0 (2011-10-02)
~~~~~~~~~~~~~~~~~~
* refactoring api
@@ -7,7 +7,7 @@
'blender': (2, 6, 0),
'location': 'File > Import-Export',
'description': 'Import-Export PMD/PMX/MQO meshes',
- 'warning': '', # used for warning icon and text in addons panel
+ 'warning': 'pmx importer/exporter is under development',
'wiki_url': 'http://meshio.sourceforge.jp/',
'support': 'COMMUNITY',
}
@@ -8,7 +8,24 @@
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 get_name(name, fmt, *args):
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 get_name(name, fmt, *args):
# 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'}
View
@@ -215,25 +215,28 @@ def diff(self, rhs):
self._diff(rhs, 'ik')
def getConnectionFlag(self):
- return self.flag & 0x0001
+ return (self.flag & 0x0001)!=0
+
+ def getVisibleFlag(self):
+ return (self.flag & 0x0008)!=0
def getIkFlag(self):
- return (self.flag & 0x0020) >> 5
+ return (self.flag & 0x0020)!=0
def getRotationFlag(self):
- return (self.flag & 0x0100) >> 8
+ return (self.flag & 0x0100)!=0
def getTranslationFlag(self):
- return (self.flag & 0x0200) >> 9
+ return (self.flag & 0x0200)!=0
def getFixedAxisFlag(self):
- return (self.flag & 0x0400) >> 10
+ return (self.flag & 0x0400)!=0
def getLocalCoordinateFlag(self):
- return (self.flag & 0x0800) >> 11
+ return (self.flag & 0x0800)!=0
def getExternalParentDeformFlag(self):
- return (self.flag & 0x2000) >> 13
+ return (self.flag & 0x2000)!=0
class Material(Diff):
View
@@ -7,7 +7,7 @@
import shutil
name='pymeshio'
-version='2.2.3'
+version='2.3.0'
short_description='3d model io library for mqo, pmd, pmx, vmd and vpd'
long_description='''\
`pymeshio` is a package for 3d model io.
@@ -26,6 +26,7 @@
* read MikuMikuDance vmd format
* read MikuMikuDance vpd format
* convert MikuMikuDance pmd format to MikuMikuDance pmx format
+* blender-2.6 import/export plugin
Install
-------
@@ -58,28 +59,16 @@
ToDo
--------
-* update blender25 plugin to blender26
-* blender26 importer for pmx
-* blender26 exporter for pmx
+* importer for pmx
+* exporter for pmx
New
-------
-2.2.3 (2011-10-12)
+2.3.0 (2011-10-13)
~~~~~~~~~~~~~~~~~~
-* fix for pmxmca
-
-2.2.2 (2011-10-10)
-~~~~~~~~~~~~~~~~~~
-* add pmd2pmx script
-
-2.2.1 (2011-10-07)
-~~~~~~~~~~~~~~~~~~
-* implement pmd to pmx converter
-
-2.2.0 (2011-10-03)
-~~~~~~~~~~~~~~~~~~
-* implement pmx writer
+* fix blender-2.5 plugin for blender-2.6
+* add pmx importer
'''

0 comments on commit a774403

Please sign in to comment.