# I want to scale an Object along its local Axis and the return it to its initial scale using the global Axis

import bpy
from mathutils import Matrix, Vector

bpy.ops.transform.resize(value=(1, 2, 1), orient_type='LOCAL')
obj = bpy.context.active_object
mat = obj.matrix_world
print(mat)
bpy.ops.transform.resize(value=(1, .5, 1), orient_type='GLOBAL', orient_matrix=mat)


I have the following code. I have an group of parented bones. I am trying to length each individually along their own local axis. But because of the parent child relationship the children always deform along the local axis of the parent. My plan is to lengthen each bone along its local axis and then shrink each of its children along it's parents local axis. Repeating this process down the line. However I am a little confused on how to reverse the Local applied scale to the children's Global scale.

The code I have here is attempting to resize one object using local resize and then return it to its original size using a global resize. I just need help with understanding how the matrices work.

import bpy

bpy.ops.transform.resize(value=(1, 2, 1), orient_type='LOCAL')
obj = bpy.context.selected_pose_bones
mat = obj.matrix.to_3x3()
print(mat)
bpy.ops.transform.resize(value=(1, .5, 1), orient_type='GLOBAL', orient_matrix=mat)


This works and I am going to try and explain this. I am sort of newb here so bear with me, and by all means correct me in the comments if I am wrong.

Each translated, scaled or rotated object has a matrix that describes its relation to its original or "initial" coordinates in 3d space.

The way matrices work is they take each vertex's 'initial" coordinates and multiply them by a matrix, giving the vertices new coordinates. Before the any movement of the object happens the object simply has its original or "initial" coordinates located around the origin, Like how when you add a new object it always appears straight up and down and is usually in the center. If you transform rotate or scale the object, then that objects vertices will now be calculated using matrices, until of course, you apply (ctrl A) the scale rotation or position, which will change the "initial" coordinates of the object. Before that the matrices which are being used to describe the objects scale, location and rotation are stored in the object and are available to through the python API.

So what is happening here, is I am getting the bones rotational matrix and applying it to the scale command. So I am scaling in the direction specified by the rotation matrix, which describes the objects rotation from its "initial" coordinates.

But because of the way the matrices are stored, meaning that they include not only the rotational information but also the translation information, I have to use the .to_3x3() to isolate the the rotational part of the transformation.