def _getAdditionalTransformBone(prop): arm = prop.id_data bone_id = prop.get('additional_transform_bone_id', -1) if bone_id < 0: return '' fnBone = FnBone.from_bone_id(arm, bone_id) if not fnBone: return '' return fnBone.pose_bone.name
def _get_bone(prop): bone_id = prop.get('bone_id', -1) if bone_id < 0: return '' root = prop.id_data fnModel = FnModel(root) arm = fnModel.armature() fnBone = FnBone.from_bone_id(arm, bone_id) if not fnBone: return '' return fnBone.pose_bone.name
def __exportBones(self): """ Export bones. Returns: A dictionary to map Blender bone names to bone indices of the pmx.model instance. """ arm = self.__armature boneMap = {} pmx_bones = [] pose_bones = arm.pose.bones world_mat = arm.matrix_world r = {} # sort by a depth of bones. t = [] for i in pose_bones: t.append((i, self.__countBoneDepth(i))) sorted_bones = sorted(pose_bones, key=self.__countBoneDepth) with bpyutils.edit_object(arm) as data: for p_bone in sorted_bones: bone = data.edit_bones[p_bone.name] if p_bone.is_mmd_shadow_bone: continue pmx_bone = pmx.Bone() if p_bone.mmd_bone.name_j != '': pmx_bone.name = p_bone.mmd_bone.name_j else: pmx_bone.name = bone.name mmd_bone = p_bone.mmd_bone if mmd_bone.additional_transform_bone_id != -1: fnBone = FnBone.from_bone_id( arm, mmd_bone.additional_transform_bone_id) pmx_bone.additionalTransform = ( fnBone.pose_bone, mmd_bone.additional_transform_influence) pmx_bone.hasAdditionalRotate = mmd_bone.has_additional_rotation pmx_bone.hasAdditionalLocation = mmd_bone.has_additional_location pmx_bone.name_e = p_bone.mmd_bone.name_e or '' pmx_bone.location = world_mat @ mathutils.Vector( bone.head) * self.__scale @ self.TO_PMX_MATRIX pmx_bone.parent = bone.parent pmx_bone.visible = mmd_bone.is_visible pmx_bone.isMovable = not all(p_bone.lock_location) pmx_bone.isRotatable = not all(p_bone.lock_rotation) pmx_bones.append(pmx_bone) self.__bone_name_table.append(p_bone.name) boneMap[bone] = pmx_bone r[bone.name] = len(pmx_bones) - 1 if p_bone.mmd_bone.is_tip: pmx_bone.displayConnection = -1 elif p_bone.mmd_bone.use_tail_location: tail_loc = world_mat @ mathutils.Vector( bone.tail) * self.__scale @ self.TO_PMX_MATRIX pmx_bone.displayConnection = tail_loc - pmx_bone.location else: for child in bone.children: if child.use_connect: pmx_bone.displayConnection = child break #if not pmx_bone.displayConnection: #I think this wasn't working properly #pmx_bone.displayConnection = bone.tail - bone.head #add fixed and local axes if mmd_bone.enabled_fixed_axis: pmx_bone.axis = mmd_bone.fixed_axis if mmd_bone.enabled_local_axes: pmx_bone.localCoordinate = pmx.Coordinate( mmd_bone.local_axis_x, mmd_bone.local_axis_z) for idx, i in enumerate(pmx_bones): if i.parent is not None: i.parent = pmx_bones.index(boneMap[i.parent]) logging.debug('the parent of %s:%s: %s', idx, i.name, i.parent) if isinstance(i.displayConnection, pmx.Bone): i.displayConnection = pmx_bones.index(i.displayConnection) elif isinstance(i.displayConnection, bpy.types.EditBone): i.displayConnection = pmx_bones.index( boneMap[i.displayConnection]) if i.additionalTransform is not None: b, influ = i.additionalTransform i.additionalTransform = (r[b.name], influ) self.__model.bones = pmx_bones return r
def __exportBones(self): """ Export bones. Returns: A dictionary to map Blender bone names to bone indices of the pmx.model instance. """ arm = self.__armature boneMap = {} pmx_bones = [] pose_bones = arm.pose.bones world_mat = arm.matrix_world r = {} # sort by a depth of bones. t = [] for i in pose_bones: t.append((i, self.__countBoneDepth(i))) sorted_bones = sorted(pose_bones, key=self.__countBoneDepth) with bpyutils.edit_object(arm) as data: for p_bone in sorted_bones: bone = data.edit_bones[p_bone.name] if p_bone.is_mmd_shadow_bone: continue pmx_bone = pmx.Bone() if p_bone.mmd_bone.name_j != '': pmx_bone.name = p_bone.mmd_bone.name_j else: pmx_bone.name = bone.name mmd_bone = p_bone.mmd_bone if mmd_bone.additional_transform_bone_id != -1: fnBone = FnBone.from_bone_id(arm, mmd_bone.additional_transform_bone_id) pmx_bone.additionalTransform = (fnBone.pose_bone, mmd_bone.additional_transform_influence) pmx_bone.hasAdditionalRotate = mmd_bone.has_additional_rotation pmx_bone.hasAdditionalLocation = mmd_bone.has_additional_location pmx_bone.name_e = p_bone.mmd_bone.name_e or '' pmx_bone.location = world_mat * mathutils.Vector(bone.head) * self.__scale * self.TO_PMX_MATRIX pmx_bone.parent = bone.parent pmx_bone.visible = mmd_bone.is_visible pmx_bone.isMovable = not all(p_bone.lock_location) pmx_bone.isRotatable = not all(p_bone.lock_rotation) pmx_bones.append(pmx_bone) self.__bone_name_table.append(p_bone.name) boneMap[bone] = pmx_bone r[bone.name] = len(pmx_bones) - 1 if p_bone.mmd_bone.is_tip: pmx_bone.displayConnection = -1 elif p_bone.mmd_bone.use_tail_location: tail_loc = world_mat * mathutils.Vector(bone.tail) * self.__scale * self.TO_PMX_MATRIX pmx_bone.displayConnection = tail_loc - pmx_bone.location else: for child in bone.children: if child.use_connect: pmx_bone.displayConnection = child break #if not pmx_bone.displayConnection: #I think this wasn't working properly #pmx_bone.displayConnection = bone.tail - bone.head #add fixed and local axes if mmd_bone.enabled_fixed_axis: pmx_bone.axis = mmd_bone.fixed_axis if mmd_bone.enabled_local_axes: pmx_bone.localCoordinate = pmx.Coordinate( mmd_bone.local_axis_x, mmd_bone.local_axis_z) for idx, i in enumerate(pmx_bones): if i.parent is not None: i.parent = pmx_bones.index(boneMap[i.parent]) logging.debug('the parent of %s:%s: %s', idx, i.name, i.parent) if isinstance(i.displayConnection, pmx.Bone): i.displayConnection = pmx_bones.index(i.displayConnection) elif isinstance(i.displayConnection, bpy.types.EditBone): i.displayConnection = pmx_bones.index(boneMap[i.displayConnection]) if i.additionalTransform is not None: b, influ = i.additionalTransform i.additionalTransform = (r[b.name], influ) self.__model.bones = pmx_bones return r