- 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
-
-