def __gather_trans_rot_scale(blender_object, export_settings): trans, rot, sca = gltf2_blender_extract.decompose_transition( blender_object.matrix_local, 'NODE', export_settings) if bpy.app.version < (2, 80, 0): if blender_object.dupli_type == 'GROUP' and blender_object.dupli_group: trans = -gltf2_blender_extract.convert_swizzle_location( blender_object.dupli_group.dupli_offset, export_settings) else: if blender_object.instance_type == 'COLLECTION' and blender_object.instance_collection: trans = -gltf2_blender_extract.convert_swizzle_location( blender_object.instance_collection.instance_offset, export_settings) translation, rotation, scale = (None, None, None) trans[0], trans[1], trans[2] = gltf2_blender_math.round_if_near(trans[0], 0.0), gltf2_blender_math.round_if_near(trans[1], 0.0), \ gltf2_blender_math.round_if_near(trans[2], 0.0) rot[0], rot[1], rot[2], rot[3] = gltf2_blender_math.round_if_near(rot[0], 0.0), gltf2_blender_math.round_if_near(rot[1], 0.0), \ gltf2_blender_math.round_if_near(rot[2], 0.0), gltf2_blender_math.round_if_near(rot[3], 1.0) sca[0], sca[1], sca[2] = gltf2_blender_math.round_if_near(sca[0], 1.0), gltf2_blender_math.round_if_near(sca[1], 1.0), \ gltf2_blender_math.round_if_near(sca[2], 1.0) if trans[0] != 0.0 or trans[1] != 0.0 or trans[2] != 0.0: translation = [trans[0], trans[1], trans[2]] if rot[0] != 0.0 or rot[1] != 0.0 or rot[2] != 0.0 or rot[3] != 1.0: rotation = [rot[0], rot[1], rot[2], rot[3]] if sca[0] != 1.0 or sca[1] != 1.0 or sca[2] != 1.0: scale = [sca[0], sca[1], sca[2]] return translation, rotation, scale
def gather_joint(blender_bone, export_settings): """ Generate a glTF2 node from a blender bone, as joints in glTF2 are simply nodes :param blender_bone: a blender PoseBone :param export_settings: the settings for this export :return: a glTF2 node (acting as a joint) """ axis_basis_change = mathutils.Matrix.Identity(4) if export_settings['gltf_yup']: axis_basis_change = mathutils.Matrix( ((1.0, 0.0, 0.0, 0.0), (0.0, 0.0, 1.0, 0.0), (0.0, -1.0, 0.0, 0.0), (0.0, 0.0, 0.0, 1.0))) # extract bone transform if blender_bone.parent is None: correction_matrix_local = gltf2_blender_math.multiply( axis_basis_change, blender_bone.bone.matrix_local) else: correction_matrix_local = gltf2_blender_math.multiply( blender_bone.parent.bone.matrix_local.inverted(), blender_bone.bone.matrix_local) matrix_basis = blender_bone.matrix_basis if export_settings['gltf_bake_skins']: gltf2_io_debug.print_console("WARNING", "glTF bake skins not supported") # matrix_basis = blender_object.convert_space(blender_bone, blender_bone.matrix, from_space='POSE', # to_space='LOCAL') trans, rot, sca = gltf2_blender_extract.decompose_transition( gltf2_blender_math.multiply(correction_matrix_local, matrix_basis), 'JOINT', export_settings) translation, rotation, scale = (None, None, None) if trans[0] != 0.0 or trans[1] != 0.0 or trans[2] != 0.0: translation = [trans[0], trans[1], trans[2]] if rot[0] != 0.0 or rot[1] != 0.0 or rot[2] != 0.0 or rot[3] != 1.0: rotation = [rot[0], rot[1], rot[2], rot[3]] if sca[0] != 1.0 or sca[1] != 1.0 or sca[2] != 1.0: scale = [sca[0], sca[1], sca[2]] # traverse into children children = [] for bone in blender_bone.children: children.append(gather_joint(bone, export_settings)) # finally add to the joints array containing all the joints in the hierarchy return gltf2_io.Node(camera=None, children=children, extensions=None, extras=None, matrix=None, mesh=None, name=blender_bone.name, rotation=rotation, scale=scale, skin=None, translation=translation, weights=None)
def gather_joint(blender_object, blender_bone, export_settings): """ Generate a glTF2 node from a blender bone, as joints in glTF2 are simply nodes. :param blender_bone: a blender PoseBone :param export_settings: the settings for this export :return: a glTF2 node (acting as a joint) """ axis_basis_change = mathutils.Matrix.Identity(4) if export_settings[gltf2_blender_export_keys.YUP]: axis_basis_change = mathutils.Matrix( ((1.0, 0.0, 0.0, 0.0), (0.0, 0.0, 1.0, 0.0), (0.0, -1.0, 0.0, 0.0), (0.0, 0.0, 0.0, 1.0))) # extract bone transform if blender_bone.parent is None: correction_matrix_local = gltf2_blender_math.multiply(axis_basis_change, blender_bone.bone.matrix_local) else: correction_matrix_local = gltf2_blender_math.multiply( blender_bone.parent.bone.matrix_local.inverted(), blender_bone.bone.matrix_local) matrix_basis = blender_bone.matrix_basis trans, rot, sca = gltf2_blender_extract.decompose_transition( gltf2_blender_math.multiply(correction_matrix_local, matrix_basis), export_settings) translation, rotation, scale = (None, None, None) if trans[0] != 0.0 or trans[1] != 0.0 or trans[2] != 0.0: translation = [trans[0], trans[1], trans[2]] if rot[0] != 1.0 or rot[1] != 0.0 or rot[2] != 0.0 or rot[3] != 0.0: rotation = [rot[1], rot[2], rot[3], rot[0]] if sca[0] != 1.0 or sca[1] != 1.0 or sca[2] != 1.0: scale = [sca[0], sca[1], sca[2]] # traverse into children children = [] if export_settings["gltf_def_bones"] is False: for bone in blender_bone.children: children.append(gather_joint(blender_object, bone, export_settings)) else: _, children_, _ = gltf2_blender_gather_skins.get_bone_tree(None, blender_bone.id_data) if blender_bone.name in children_.keys(): for bone in children_[blender_bone.name]: children.append(gather_joint(blender_object, blender_bone.id_data.pose.bones[bone], export_settings)) # finally add to the joints array containing all the joints in the hierarchy return gltf2_io.Node( camera=None, children=children, extensions=None, extras=__gather_extras(blender_bone, export_settings), matrix=None, mesh=None, name=blender_bone.name, rotation=rotation, scale=scale, skin=None, translation=translation, weights=None )
def __gather_trans_rot_scale(blender_object, export_settings): if blender_object.matrix_parent_inverse == Matrix.Identity(4): trans = blender_object.location if blender_object.rotation_mode in ['QUATERNION', 'AXIS_ANGLE']: rot = blender_object.rotation_quaternion else: rot = blender_object.rotation_euler.to_quaternion() sca = blender_object.scale else: # matrix_local = matrix_parent_inverse*location*rotation*scale # Decomposing matrix_local gives less accuracy, but is needed if matrix_parent_inverse is not the identity. if blender_object.matrix_local[3][3] != 0.0: trans, rot, sca = gltf2_blender_extract.decompose_transition( blender_object.matrix_local, export_settings) else: # Some really weird cases, scale is null (if parent is null when evaluation is done) print_console( 'WARNING', 'Some nodes are 0 scaled during evaluation. Result can be wrong' ) trans = blender_object.location if blender_object.rotation_mode in ['QUATERNION', 'AXIS_ANGLE']: rot = blender_object.rotation_quaternion else: rot = blender_object.rotation_euler.to_quaternion() sca = blender_object.scale # make sure the rotation is normalized rot.normalize() trans = gltf2_blender_extract.convert_swizzle_location( trans, None, None, export_settings) rot = gltf2_blender_extract.convert_swizzle_rotation(rot, export_settings) sca = gltf2_blender_extract.convert_swizzle_scale(sca, export_settings) if blender_object.instance_type == 'COLLECTION' and blender_object.instance_collection: trans -= gltf2_blender_extract.convert_swizzle_location( blender_object.instance_collection.instance_offset, None, None, export_settings) translation, rotation, scale = (None, None, None) trans[0], trans[1], trans[2] = gltf2_blender_math.round_if_near(trans[0], 0.0), gltf2_blender_math.round_if_near(trans[1], 0.0), \ gltf2_blender_math.round_if_near(trans[2], 0.0) rot[0], rot[1], rot[2], rot[3] = gltf2_blender_math.round_if_near(rot[0], 1.0), gltf2_blender_math.round_if_near(rot[1], 0.0), \ gltf2_blender_math.round_if_near(rot[2], 0.0), gltf2_blender_math.round_if_near(rot[3], 0.0) sca[0], sca[1], sca[2] = gltf2_blender_math.round_if_near(sca[0], 1.0), gltf2_blender_math.round_if_near(sca[1], 1.0), \ gltf2_blender_math.round_if_near(sca[2], 1.0) if trans[0] != 0.0 or trans[1] != 0.0 or trans[2] != 0.0: translation = [trans[0], trans[1], trans[2]] if rot[0] != 1.0 or rot[1] != 0.0 or rot[2] != 0.0 or rot[3] != 0.0: rotation = [rot[1], rot[2], rot[3], rot[0]] if sca[0] != 1.0 or sca[1] != 1.0 or sca[2] != 1.0: scale = [sca[0], sca[1], sca[2]] return translation, rotation, scale
def gather_joint(blender_bone, export_settings): """ Generate a glTF2 node from a blender bone, as joints in glTF2 are simply nodes. :param blender_bone: a blender PoseBone :param export_settings: the settings for this export :return: a glTF2 node (acting as a joint) """ axis_basis_change = mathutils.Matrix.Identity(4) if export_settings[gltf2_blender_export_keys.YUP]: axis_basis_change = mathutils.Matrix( ((1.0, 0.0, 0.0, 0.0), (0.0, 0.0, 1.0, 0.0), (0.0, -1.0, 0.0, 0.0), (0.0, 0.0, 0.0, 1.0))) # extract bone transform if blender_bone.parent is None: correction_matrix_local = gltf2_blender_math.multiply(axis_basis_change, blender_bone.bone.matrix_local) else: correction_matrix_local = gltf2_blender_math.multiply( blender_bone.parent.bone.matrix_local.inverted(), blender_bone.bone.matrix_local) matrix_basis = blender_bone.matrix_basis if export_settings[gltf2_blender_export_keys.BAKE_SKINS]: gltf2_io_debug.print_console("WARNING", "glTF bake skins not supported") # matrix_basis = blender_object.convert_space(blender_bone, blender_bone.matrix, from_space='POSE', # to_space='LOCAL') trans, rot, sca = gltf2_blender_extract.decompose_transition( gltf2_blender_math.multiply(correction_matrix_local, matrix_basis), export_settings) translation, rotation, scale = (None, None, None) if trans[0] != 0.0 or trans[1] != 0.0 or trans[2] != 0.0: translation = [trans[0], trans[1], trans[2]] if rot[0] != 0.0 or rot[1] != 0.0 or rot[2] != 0.0 or rot[3] != 1.0: rotation = [rot[0], rot[1], rot[2], rot[3]] if sca[0] != 1.0 or sca[1] != 1.0 or sca[2] != 1.0: scale = [sca[0], sca[1], sca[2]] # traverse into children children = [] for bone in blender_bone.children: children.append(gather_joint(bone, export_settings)) # finally add to the joints array containing all the joints in the hierarchy return gltf2_io.Node( camera=None, children=children, extensions=None, extras=None, matrix=None, mesh=None, name=blender_bone.name, rotation=rotation, scale=scale, skin=None, translation=translation, weights=None )
def __gather_trans_rot_scale(blender_object, export_settings): trans, rot, sca = gltf2_blender_extract.decompose_transition(blender_object.matrix_local, 'NODE', export_settings) if blender_object.dupli_type == 'GROUP' and blender_object.dupli_group: trans = -gltf2_blender_extract.convert_swizzle_location(blender_object.dupli_group.dupli_offset, export_settings) translation, rotation, scale = (None, None, None) if trans[0] != 0.0 or trans[1] != 0.0 or trans[2] != 0.0: translation = [trans[0], trans[1], trans[2]] if rot[0] != 0.0 or rot[1] != 0.0 or rot[2] != 0.0 or rot[3] != 1.0: rotation = [rot[0], rot[1], rot[2], rot[3]] if sca[0] != 1.0 or sca[1] != 1.0 or sca[2] != 1.0: scale = [sca[0], sca[1], sca[2]] return translation, rotation, scale
def __gather_trans_rot_scale(blender_object, export_settings): if blender_object.matrix_parent_inverse == Matrix.Identity(4): trans = blender_object.location if blender_object.rotation_mode in ['QUATERNION', 'AXIS_ANGLE']: rot = blender_object.rotation_quaternion else: rot = blender_object.rotation_euler.to_quaternion() sca = blender_object.scale else: # matrix_local = matrix_parent_inverse*location*rotation*scale # Decomposing matrix_local gives less accuracy, but is needed if matrix_parent_inverse is not the identity. trans, rot, sca = gltf2_blender_extract.decompose_transition( blender_object.matrix_local, export_settings) trans = gltf2_blender_extract.convert_swizzle_location( trans, None, None, export_settings) rot = gltf2_blender_extract.convert_swizzle_rotation(rot, export_settings) sca = gltf2_blender_extract.convert_swizzle_scale(sca, export_settings) if bpy.app.version < (2, 80, 0): if blender_object.dupli_type == 'GROUP' and blender_object.dupli_group: trans = -gltf2_blender_extract.convert_swizzle_location( blender_object.dupli_group.dupli_offset, None, None, export_settings) else: if blender_object.instance_type == 'COLLECTION' and blender_object.instance_collection: trans = -gltf2_blender_extract.convert_swizzle_location( blender_object.instance_collection.instance_offset, None, None, export_settings) translation, rotation, scale = (None, None, None) trans[0], trans[1], trans[2] = gltf2_blender_math.round_if_near(trans[0], 0.0), gltf2_blender_math.round_if_near(trans[1], 0.0), \ gltf2_blender_math.round_if_near(trans[2], 0.0) rot[0], rot[1], rot[2], rot[3] = gltf2_blender_math.round_if_near(rot[0], 1.0), gltf2_blender_math.round_if_near(rot[1], 0.0), \ gltf2_blender_math.round_if_near(rot[2], 0.0), gltf2_blender_math.round_if_near(rot[3], 0.0) sca[0], sca[1], sca[2] = gltf2_blender_math.round_if_near(sca[0], 1.0), gltf2_blender_math.round_if_near(sca[1], 1.0), \ gltf2_blender_math.round_if_near(sca[2], 1.0) if trans[0] != 0.0 or trans[1] != 0.0 or trans[2] != 0.0: translation = [trans[0], trans[1], trans[2]] if rot[0] != 1.0 or rot[1] != 0.0 or rot[2] != 0.0 or rot[3] != 0.0: rotation = [rot[1], rot[2], rot[3], rot[0]] if sca[0] != 1.0 or sca[1] != 1.0 or sca[2] != 1.0: scale = [sca[0], sca[1], sca[2]] return translation, rotation, scale