From 033057f76b149bb57376ab37749d874faa06104c Mon Sep 17 00:00:00 2001 From: ousttrue Date: Mon, 3 Oct 2011 02:17:42 +0900 Subject: [PATCH] fix export_pmd --- blender25-meshio/export_mqo.py | 20 +-- blender25-meshio/export_pmd.py | 310 ++++++++++++++++++++--------------------- blender25-meshio/import_pmd.py | 4 +- pymeshio/pmd/__init__.py | 21 ++- pymeshio/pmd/reader.py | 7 +- pymeshio/pmd/writer.py | 6 +- 6 files changed, 180 insertions(+), 188 deletions(-) diff --git a/blender25-meshio/export_mqo.py b/blender25-meshio/export_mqo.py index 7c1f4b7..fbe3a77 100644 --- a/blender25-meshio/export_mqo.py +++ b/blender25-meshio/export_mqo.py @@ -17,16 +17,16 @@ Usage: Run this script from "File->Export" menu. -0.1 20080128: -0.2 20100518: refactoring. -0.3 20100606: integrate 2.4 and 2.5. -0.4 20100626: refactoring. -0.5 20100710: add [apply_modifier] option(2.5 only). -0.6 20100714: remove shape_key when apply_modifier. fix material. -2.0 20100724: update for Blender2.53. -2.1 20101005: update for Blender2.54. -2.2 20101228: update for Blender2.55. -2.4 20110429: update for Blender2.57b. +20080128: +20100518: refactoring. +20100606: integrate 2.4 and 2.5. +20100626: refactoring. +20100710: add [apply_modifier] option(2.5 only). +20100714: remove shape_key when apply_modifier. fix material. +20100724: update for Blender2.53. +20101005: update for Blender2.54. +20101228: update for Blender2.55. +20110429: update for Blender2.57b. """ bl_addon_info = { diff --git a/blender25-meshio/export_pmd.py b/blender25-meshio/export_pmd.py index 7a40cdd..f3ad899 100644 --- a/blender25-meshio/export_pmd.py +++ b/blender25-meshio/export_pmd.py @@ -14,22 +14,23 @@ pmd Importer This script exports a pmd model. -0.1 20100318: first implementation. -0.2 20100519: refactoring. use C extension. -1.0 20100530: implement basic features. -1.1 20100612: integrate 2.4 and 2.5. -1.2 20100616: implement rigid body. -1.3 20100619: fix rigid body, bone weight. -1.4 20100626: refactoring. -1.5 20100629: sphere map. -1.6 20100710: toon texture & bone group. -1.7 20100711: separate vertex with normal or uv. -2.0 20100724: update for Blender2.53. -2.1 20100731: add full python module. -2.2 20101005: update for Blender2.54. -2.3 20101228: update for Blender2.55. -2.4 20110429: update for Blender2.57b. -2.5 20110522: implement RigidBody and Constraint. +20100318: first implementation. +20100519: refactoring. use C extension. +20100530: implement basic features. +20100612: integrate 2.4 and 2.5. +20100616: implement rigid body. +20100619: fix rigid body, bone weight. +20100626: refactoring. +20100629: sphere map. +20100710: toon texture & bone group. +20100711: separate vertex with normal or uv. +20100724: update for Blender2.53. +20100731: add full python module. +20101005: update for Blender2.54. +20101228: update for Blender2.55. +20110429: update for Blender2.57b. +20110522: implement RigidBody and Constraint. +20111002: update for pymeshio-2.1.0 """ bl_addon_info = { @@ -51,15 +52,13 @@ bl_addon_info = { ############################################################################### import os import sys +import io -try: - # C extension - from .meshio import pmd, englishmap - print('use meshio C module') -except ImportError: - # full python - from .pymeshio import englishmap - from .pymeshio import pmd + +from .pymeshio import englishmap +from .pymeshio import common +from .pymeshio import pmd +from .pymeshio.pmd import writer # for 2.5 @@ -71,26 +70,6 @@ from . import bl25 as bl xrange=range -def setMaterialParams(material, m): - # diffuse - material.diffuse.r=m.diffuse_color[0] - material.diffuse.g=m.diffuse_color[1] - material.diffuse.b=m.diffuse_color[2] - material.diffuse.a=m.alpha - # specular - material.shinness=0 if m.specular_toon_size<1e-5 else m.specular_hardness*10 - material.specular.r=m.specular_color[0] - material.specular.g=m.specular_color[1] - material.specular.b=m.specular_color[2] - # ambient - material.ambient.r=m.mirror_color[0] - material.ambient.g=m.mirror_color[1] - material.ambient.b=m.mirror_color[2] - # flag - material.flag=1 if m.subsurface_scattering.use else 0 - # toon - material.toon_index=0 - def toCP932(s): return s.encode('cp932') @@ -895,28 +874,25 @@ class PmdExporter(object): self.__createOneSkinMesh(child) def write(self, path): - io=pmd.IO() - io.name=self.name - io.comment=self.comment - io.version=1.0 + model=pmd.Model(1.0) + model.name=self.name.encode('cp932') + model.comment=self.comment.encode('cp932') # 頂点 - for pos, attribute, b0, b1, weight in self.oneSkinMesh.vertexArray.zip(): + model.vertices=[pmd.Vertex( # convert right-handed z-up to left-handed y-up - v=io.addVertex() - v.pos.x=pos[0] - v.pos.y=pos[2] - v.pos.z=pos[1] + common.Vector3(pos[0], pos[2], pos[1]), # convert right-handed z-up to left-handed y-up - v.normal.x=attribute.nx - v.normal.y=attribute.nz - v.normal.z=attribute.ny - v.uv.x=attribute.u - v.uv.y=1.0-attribute.v # reverse vertical - 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 + common.Vector3(attribute.nx, attribute.nz, attribute.ny), + # reverse vertical + common.Vector2(attribute.u, 1.0-attribute.v), + self.skeleton.indexByName(b0), + self.skeleton.indexByName(b1), + int(100*weight), + # edge flag, 0: enable edge, 1: not edge + 0 + ) + for pos, attribute, b0, b1, weight in self.oneSkinMesh.vertexArray.zip()] # 面とマテリアル vertexCount=self.oneSkinMesh.getVertexCount() @@ -926,11 +902,6 @@ class PmdExporter(object): m=bl.material.get(material_name) except KeyError as e: m=DefaultMatrial() - # マテリアル - material=io.addMaterial() - setMaterialParams(material, m) - - material.vertex_count=len(indices) def get_texture_name(texture): pos=texture.replace("\\", "/").rfind("/") if pos==-1: @@ -940,23 +911,38 @@ class PmdExporter(object): textures=[get_texture_name(path) for path in bl.material.eachEnalbeTexturePath(m)] print(textures) - if len(textures)>0: - material.texture='*'.join(textures) - else: - material.texture="" + # マテリアル + 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' % ( @@ -347,8 +347,10 @@ class Morph(object): class BoneGroup(object): - __slots__=['_name', '_english_name'] - def __init__(self, name='group'): self._name=name; self._english_name='center' + __slots__=['name', 'english_name'] + def __init__(self, name=b'group', english_name=b'center'): + self.name=name + self.english_name=english_name SHAPE_SPHERE=0 @@ -380,16 +382,16 @@ class RigidBody(object): bone_index, collision_group, no_collision_group, - shape_type, - shape_size, - shape_position, - shape_rotation, mass, linear_damping, angular_damping, restitution, friction, - mode + mode, + shape_type=0, + shape_size=common.Vector3(), + shape_position=common.Vector3(), + shape_rotation=common.Vector3() ): self.name=name self.bone_index=bone_index @@ -484,7 +486,6 @@ class Model(object): 'vertices', 'indices', 'materials', 'bones', 'ik_list', 'morphs', 'morph_indices', 'bone_group_list', 'bone_display_list', - 'bone_group_english_list', 'toon_textures', 'rigidbodies', 'joints', @@ -506,7 +507,6 @@ class Model(object): self.bone_group_list=[] self.bone_display_list=[] # extend - self.bone_group_english_list=[] self.toon_textures=[b'']*10 self.rigidbodies=[] self.joints=[] @@ -536,7 +536,6 @@ class Model(object): and self.morph_indices==rhs.morph_indices and self.bone_group_list==rhs.bone_group_list and self.bone_display_list==rhs.bone_display_list - and self.bone_group_english_list==rhs.bone_group_english_list and self.toon_textures==rhs.toon_textures and self.rigidbodies==rhs.rigidbodies and self.joints==rhs.joints diff --git a/pymeshio/pmd/reader.py b/pymeshio/pmd/reader.py index f8defda..6b8c5da 100644 --- a/pymeshio/pmd/reader.py +++ b/pymeshio/pmd/reader.py @@ -127,7 +127,7 @@ def __read(reader, model): for _ in range(reader.read_uint(2))] model.morph_indices=[reader.read_uint(2) for _ in range(reader.read_uint(1))] - model.bone_group_list=[reader.read_text(50) + model.bone_group_list=[pmd.BoneGroup(reader.read_text(50)) for _ in range(reader.read_uint(1))] model.bone_display_list=[(reader.read_uint(2), reader.read_uint(1)) for _i in range(reader.read_uint(4))] @@ -150,8 +150,9 @@ def __read(reader, model): if morph.name==b'base': continue morph.english_name=reader.read_text(20) - model.bone_group_english_list=[reader.read_text(50) - for _ in model.bone_group_list] + for g in model.bone_group_list: + g.english_name=reader.read_text(50) + ############################################################ # extend2: toon_textures diff --git a/pymeshio/pmd/writer.py b/pymeshio/pmd/writer.py index 66e5fb6..187f59d 100644 --- a/pymeshio/pmd/writer.py +++ b/pymeshio/pmd/writer.py @@ -73,7 +73,7 @@ class Writer(common.BinaryWriter): def write_bone_group_list(self, bone_group_list): self.write_uint(len(bone_group_list), 1) for g in bone_group_list: - self.write_text(g, 50) + self.write_text(g.name, 50) def write_bone_display_list(self, bone_display_list): self.write_uint(len(bone_display_list), 4) @@ -142,8 +142,8 @@ def write(ios, model): if skin.name==b'base': continue writer.write_text(skin.english_name, 20) - for english in model.bone_group_english_list: - writer.write_text(english, 50) + for g in model.bone_group_list: + writer.write_text(g.english_name, 50) for toon_texture in model.toon_textures: writer.write_text(toon_texture, 100) writer.write_rigidbodies(model.rigidbodies) -- 2.11.0