OSDN Git Service

refactoring.
authorousttrue <ousttrue@gmail.com>
Thu, 10 Jun 2010 20:46:48 +0000 (05:46 +0900)
committerousttrue <ousttrue@gmail.com>
Thu, 10 Jun 2010 20:46:48 +0000 (05:46 +0900)
swig/blender/bl24.py
swig/blender/bl25.py
swig/blender/pmd_import.py

index d85d2b3..5ad411a 100644 (file)
@@ -686,12 +686,12 @@ def vertexSetUv(mvert, uv):
     mvert.uvco=uv
 
 def meshAssignVertexGroup(meshObject, name, index, weight):
-    mesh.getData(mesh=True).assignVertsToGroup(name, 
+    meshObject.getData(mesh=True).assignVertsToGroup(name, 
             [index], weight, Blender.Mesh.AssignModes.ADD)
 
 def meshCreateVerteicesAndFaces(mesh, vertices, faces):
     mesh.verts.extend(vertices)
-    mesh.faces.extend(mesh_face_indices, ignoreDups=True)
+    mesh.faces.extend(faces, ignoreDups=True)
 
 def meshAddUV(mesh):
     mesh.addUVLayer('NewUV')
@@ -699,3 +699,58 @@ def meshAddUV(mesh):
 def meshVertsDelete(mesh, remove_vertices):
     mesh.verts.delete(remove_vertices)
 
+def createArmature(scene):
+    armature = Blender.Armature.New()
+    armature_object = scene.objects.new(armature)
+
+    # set XRAY
+    armature_object.drawMode = (
+            armature_object.drawMode | Blender.Object.DrawModes.XRAY)
+    # armature settings
+    armature.drawType = Blender.Armature.OCTAHEDRON
+    armature.drawNames=True
+    armature.envelopes = False
+    armature.vertexGroups = True
+    armature.mirrorEdit = True
+
+    return armature, armature_object
+
+def armatureMakeEditable(scene, armature_object):
+    # create armature
+    armature_object.getData().makeEditable()
+
+def createIkConstraint(armature_object, p_bone, effector_name, ik):
+    cSetting = Blender.Constraint.Settings
+    # IK solver
+    constraint = p_bone.constraints.append(Blender.Constraint.Type.IKSOLVER)
+    constraint[cSetting.CHAINLEN]=len(ik.children)
+    constraint[cSetting.TARGET]=armature_object
+    constraint[cSetting.USETIP]=False
+    constraint[cSetting.BONE]=effector_name
+    #ik_solver.influence=ik.weight
+    # not used. place folder when export.
+    constraint[cSetting.ROTWEIGHT]=ik.weight
+    constraint[cSetting.ITERATIONS]=ik.iterations * 10
+    return constraint
+
+def createArmatureBone(armature, name):
+    bone=Blender.Armature.Editbone()
+    bone.name=name
+    armature.bones[name]=bone
+    return bone
+
+def boneSetConnected(bone):
+    bone.options+=[Blender.Armature.CONNECTED]
+
+def createVector(x, y, z):
+    return Mathutils.Vector(x, y, z)
+
+def armatureUpdate(armature):
+    armature.update()
+
+def boneLayerMask(bone, mask):
+    mask=0
+    for i, enable in enumerate(mask):
+        mask+=(1<<i)
+    bone.layerMask=mask
+
index 5cba50b..e8b182c 100644 (file)
@@ -329,3 +329,47 @@ def meshVertsDelete(mesh, remove_vertices):
     bpy.ops.mesh.delete(type='VERT')
     exitEditMode()
 
+def createArmature(scene):
+    armature = bpy.data.armatures.new('Armature')
+    armature_object=bpy.data.objects.new('Armature', armature)
+    scene.objects.link(armature_object)
+
+    armature_object.x_ray=True
+    armature.draw_names=True
+    armature.drawtype='OCTAHEDRAL'
+    armature.deform_envelope=False
+    armature.deform_vertexgroups=True
+    armature.x_axis_mirror=True
+
+    return armature, armature_object
+
+def armatureMakeEditable(scene, armature_object):
+    # select only armature object and set edit mode
+    scene.objects.active=armature_object
+    bpy.ops.object.mode_set(mode='OBJECT', toggle=False)
+    bpy.ops.object.mode_set(mode='EDIT', toggle=False)
+
+def createIkConstraint(armature_object, p_bone, effector_name, ik):
+    constraint = p_bone.constraints.new('IK')
+    constraint.chain_length=len(ik.children)
+    constraint.target=armature_object
+    constraint.subtarget=effector_name
+    constraint.use_tail=False
+    return constraint
+
+def createArmatureBone(armature, name):
+    return armature.edit_bones.new(name)
+
+def boneSetConnected(bone):
+    bone.connected=True
+
+def createVector(x, y, z):
+    return mathutils.Vector([x, y, z])
+
+def armatureUpdate(armature):
+    pass
+
+def boneLayerMask(bone, mask):
+    for i, enable in enumerate(mask):
+        bone.layer[i]=True if enable else False 
+
index aefdc4b..f04d5d3 100644 (file)
@@ -82,142 +82,27 @@ def progress_set(message, progress):
     progressBar.set(message, progress)
 
 
-if isBlender24():
-    # functions
-    def convert_coord(pos):
-        """
-        Left handed y-up to Right handed z-up
-        """
-        return (pos.x, pos.z, pos.y)
+###############################################################################
+def convert_coord(pos):
+    """
+    Left handed y-up to Right handed z-up
+    """
+    return (pos.x, pos.z, pos.y)
 
+def convert_uv(uv):
+    return (uv.x, 1.0 - uv.y)
 
-    def convert_uv(uv):
-        return (uv.x, 1.0 - uv.y)
+def to_radian(degree):
+    return math.pi * degree / 180
 
 
+if isBlender24():
+    # functions
     def get_bone_name(l, index):
         name=englishmap.getEnglishBoneName(l.bones[index].getName())
         return name if name else l.bones[index].getName().encode(bl.INTERNAL_ENCODING)
 
 
-    class Builder(object):
-        def __init__(self):
-            self.boneMap={}
-
-        def build(self, armature, bones):
-            for b in bones:
-                if not b.parent:
-                    self.__build(armature, b, None, None)
-            armature.update()
-
-        def __build(self, armature, b, p, parent):
-            name=englishmap.getEnglishBoneName(b.getName())
-            if not name:
-                name=b.getName().encode(bl.INTERNAL_ENCODING)
-            self.boneMap[name]=b
-
-            bone=Blender.Armature.Editbone()
-            bone.name=name
-            armature.bones[name]=bone
-
-            if b.tail_index==0:
-                # 先端
-                assert(b.type==6 or b.type==7)
-                bone.head = Mathutils.Vector(*convert_coord(b.pos))
-                bone.tail=bone.head+Mathutils.Vector(0, 1, 0)
-                assert(parent)
-                bone.parent=parent
-                if bone.name=="center_t":
-                    # センターボーンは(0, 1, 0)の方向を向いていないと具合が悪い
-                    parent.tail=parent.head+Mathutils.Vector(0, 1, 0)
-                    bone.head=parent.tail
-                    bone.tail=bone.head+Mathutils.Vector(0, 1, 0)
-                else:
-                    assert(parent.tail==bone.head)
-                bone.options=[Blender.Armature.CONNECTED]
-                # armature layer 2
-                bone.layerMask = (1<<1)
-            else:
-                bone.head = Mathutils.Vector(*convert_coord(b.pos))
-                bone.tail = Mathutils.Vector(*convert_coord(b.tail))
-                if parent:
-                    bone.parent=parent
-                    if parent.tail==bone.head:
-                        bone.options=[Blender.Armature.CONNECTED]
-
-            if bone.head==bone.tail:
-                bone.tail=bone.head+Mathutils.Vector(0, 1, 0)
-
-            for c in b.children:
-                self.__build(armature, c, b, bone)
-
-
-    def importArmature(scene, l):
-        # create armature
-        armature = Blender.Armature.New()
-        # link to object
-        armature_object = scene.objects.new(armature)
-        # create action
-        act = Blender.Armature.NLA.NewAction()
-        act.setActive(armature_object)
-        # set XRAY
-        armature_object.drawMode = (
-                armature_object.drawMode | Blender.Object.DrawModes.XRAY)
-        # armature settings
-        armature.drawType = Blender.Armature.OCTAHEDRON
-        armature.drawNames=True
-        armature.envelopes = False
-        armature.vertexGroups = True
-        armature.mirrorEdit = True
-
-        # create armature
-        armature.makeEditable()
-
-        ############################################################
-        # build bone
-        ############################################################
-        builder=Builder()
-        builder.build(armature, l.bones)
-
-        ############################################################
-        # IK
-        ############################################################
-        pose = armature_object.getPose()
-        cSetting = Blender.Constraint.Settings
-        for ik in l.ik_list:
-            # IKtarget->parent(=IK).name
-            target=l.bones[ik.target]
-            name = englishmap.getEnglishBoneName(target.getName())
-            p_bone = pose.bones[name]
-            if not p_bone:
-                print('not found', name)
-                continue
-            if len(ik.children) >= 16:
-                print('over MAX_CHAINLEN', ik, len(ik.children))
-                continue
-            # IK solver
-            ik_solver = p_bone.constraints.append(Blender.Constraint.Type.IKSOLVER)
-            ik_solver[cSetting.CHAINLEN]=len(ik.children)
-            ik_solver[cSetting.TARGET]=armature_object
-            ik_solver[cSetting.USETIP]=False
-
-            effector_name=englishmap.getEnglishBoneName(
-                    l.bones[ik.index].getName())
-            if not effector_name:
-                effector_name=l.bones[ik.index].getName()
-
-            ik_solver[cSetting.BONE]=effector_name
-            #ik_solver.influence=ik.weight
-            # not used. place folder when export.
-            ik_solver[cSetting.ROTWEIGHT]=ik.weight
-            ik_solver[cSetting.ITERATIONS]=ik.iterations * 10
-
-        armature.makeEditable()
-        armature.update()
-
-        return armature_object
-        
-
     def importShape(obj, l, vertex_map):
         if len(l.morph_list)==0:
             return
@@ -306,26 +191,6 @@ if isBlender24():
 
 else:
     # for 2.5
-    def to_radian(degree):
-        return math.pi * degree / 180
-
-
-    def convert_coord(pos):
-        """
-        Left handed y-up to Right handed z-up
-        """
-        return (pos.x, pos.z, pos.y)
-
-
-    def convert_uv(uv):
-        return (uv.x, 1.0 - uv.y)
-
-
-    def getBoneName(bone):
-        name = englishmap.getEnglishBoneName(bone.getName())
-        return name if name else bone.getName()
-
-
     def get_bone_name(l, index):
         name=englishmap.getEnglishBoneName(l.bones[index].getName())
         return name if name else l.bones[index].getName()
@@ -345,97 +210,6 @@ else:
         return texture
 
 
-    def build_bone(armature, b, parent=None):
-        if b.tail_index==0:
-            return
-
-        name=getBoneName(b)
-        bone = armature.edit_bones.new(name if name else b.getName())
-        if parent:
-            bone.head = mathutils.Vector(convert_coord(b.pos))
-            bone.parent=parent
-            bone.connected=True if parent.tail==bone.head else False
-            bone.tail = mathutils.Vector(convert_coord(b.tail))
-            if bone.head==bone.tail:
-                bone.tail=bone.head-mathutils.Vector((0, 1, 0))
-        elif b.__class__ is pmd.BONE_IK:
-            bone.head = mathutils.Vector(convert_coord(b.pos))
-            bone.tail = mathutils.Vector(convert_coord(b.tail))
-        else:
-            # center
-            tail=mathutils.Vector(convert_coord(b.pos))
-            bone.tail = tail
-            bone.head = tail-mathutils.Vector((0, 1, 0))
-
-        for child in b.children:
-            build_bone(armature, child, bone)
-
-
-    def importArmature(scene, l):
-        # create armature
-        armature = bpy.data.armatures.new('Armature')
-        # link to object
-        armature_object=bpy.data.objects.new('Armature', armature)
-        scene.objects.link(armature_object)
-        armature_object.x_ray=True
-        armature.draw_names=True
-
-        # armature settings
-        armature.drawtype='OCTAHEDRAL'
-        armature.deform_envelope=False
-        armature.deform_vertexgroups=True
-        armature.x_axis_mirror=True
-
-        # create action
-        #act = Blender.Armature.NLA.NewAction()
-        #act.setActive(armature_object)
-
-        # select only armature object and set edit mode
-        scene.objects.active=armature_object
-        bpy.ops.object.mode_set(mode='OBJECT', toggle=False)
-        bpy.ops.object.mode_set(mode='EDIT', toggle=False)
-
-        # create armature
-        for b in l.bones:
-            if not b.parent:
-                build_bone(armature, b)
-
-        bpy.ops.object.mode_set(mode='OBJECT', toggle=False)
-        bpy.ops.object.select_all(action='DESELECT')
-
-        ############################################################
-        # IK
-        ############################################################
-        pose = armature_object.pose
-        for ik in l.ik_list:
-            effector=l.bones[ik.target]
-            parent=l.bones[effector.parent_index]
-            name=getBoneName(parent)
-            p_bone = pose.bones[name]
-            if not p_bone:
-                print('not found', name)
-                continue
-            if len(ik.children) >= 16:
-                print('over MAX_CHAINLEN', ik, len(ik.children))
-                continue
-            # IK
-            ik_const = p_bone.constraints.new('IK')
-            ik_const.chain_length=len(ik.children)
-            ik_const.target=armature_object
-            ik_const.subtarget=getBoneName(l.bones[ik.index])
-            # ROT
-            rot_const = p_bone.constraints.new('LIMIT_ROTATION')
-            rot_const.influence = ik.weight
-            rot_const.owner_space = 'LOCAL'
-            rot_const.use_limit_x=True
-            rot_const.use_limit_z=True
-            rot_const.minimum_x=to_radian(ik.iterations)
-            rot_const.maximum_x=to_radian(180)
-            rot_const.minimum_z=to_radian(180 - ik.iterations)
-            rot_const.maximum_z=to_radian(0)
-
-        return armature_object
-        
 
     def importShape(meshObject, l, vertex_map):
         if len(l.morph_list)==0:
@@ -495,7 +269,81 @@ else:
             #icu.interpolation = Blender.IpoCurve.InterpTypes.LINEAR
             #icu.append( (0.0, 0.0) )
 
-
+def __build(armature, b, p, parent):
+    name=englishmap.getEnglishBoneName(b.getName())
+    if not name:
+        name=b.getName().encode(bl.INTERNAL_ENCODING)
+
+    bone=bl.createArmatureBone(armature, name)
+
+    if b.tail_index==0:
+        # 先端
+        assert(b.type==6 or b.type==7)
+        bone.head = bl.createVector(*convert_coord(b.pos))
+        bone.tail=bone.head+bl.createVector(0, 1, 0)
+        assert(parent)
+        bone.parent=parent
+        if bone.name=="center_t":
+            # センターボーンは(0, 1, 0)の方向を向いていないと具合が悪い
+            parent.tail=parent.head+bl.createVector(0, 1, 0)
+            bone.head=parent.tail
+            bone.tail=bone.head+bl.createVector(0, 1, 0)
+        else:
+            assert(parent.tail==bone.head)
+        bl.boneSetConnected(bone)
+        # armature layer 2
+        bl.boneLayerMask(bone, [0, 1])
+    else:
+        bone.head = bl.createVector(*convert_coord(b.pos))
+        bone.tail = bl.createVector(*convert_coord(b.tail))
+        if parent:
+            bone.parent=parent
+            if parent.tail==bone.head:
+                bl.boneSetConnected(bone)
+
+    if bone.head==bone.tail:
+        bone.tail=bone.head+bl.createVector(0, 1, 0)
+
+    for c in b.children:
+        __build(armature, c, b, bone)
+
+
+def __importArmature(scene, l):
+    # build bone
+    armature, armature_object=bl.createArmature(scene)
+    bl.armatureMakeEditable(scene, armature_object)
+    for b in l.bones:
+        if not b.parent:
+            __build(armature, b, None, None)
+    bl.armatureUpdate(armature)
+    bl.exitEditMode()
+
+    # IK constraint
+    pose = bl.objectGetPose(armature_object)
+    for ik in l.ik_list:
+        target=l.bones[ik.target]
+        name = englishmap.getEnglishBoneName(target.getName())
+        p_bone = pose.bones[name]
+        if not p_bone:
+            print('not found', name)
+            continue
+        if len(ik.children) >= 16:
+            print('over MAX_CHAINLEN', ik, len(ik.children))
+            continue
+        effector_name=englishmap.getEnglishBoneName(
+                l.bones[ik.index].getName())
+        if not effector_name:
+            effector_name=l.bones[ik.index].getName()
+
+        constraint=bl.createIkConstraint(armature_object, 
+                p_bone, effector_name, ik)
+
+    bl.armatureMakeEditable(scene, armature_object)
+    bl.armatureUpdate(armature)
+    bl.exitEditMode()
+
+    return armature_object
+        
 def __import16MaerialAndMesh(meshObject, l, 
         material_order, face_map, tex_dir):
 
@@ -800,7 +648,7 @@ def __execute(filename, scene):
         bl.objectMakeParent(root, o)
 
     # import armature
-    armature_object=importArmature(scene, io)
+    armature_object=__importArmature(scene, io)
     if armature_object:
         bl.objectMakeParent(root, armature_object)
         armature = bl.objectGetData(armature_object)