 # Rotate an object using another object orientation

Hi , i would like to
rotate an object_or_vertex using another object local-orientation ?
In the image, i want to rotate the “greycube”, along the X of the “greenbar” .
im trying to, rotate the vector-distance between them
to get the location, with bad result.
Also i have no idea of how rotate the “rotation”.

Right now i understand that coordinates of vertex are “local” (they depend on the transform)
so to obtain the realworld 3Dlocation
i have to multiply them for the matrix of the object. ( Little test with bluecube )

Can i use the matrix to obtain the rotation too?

I’m not 100% sure I understand what it is you are trying to do, but it may be a lot easier to just change the pivot point of the object you are trying to rotate:

``````def set_origin(ob, global_origin=Vector()):
mw = ob.matrix_world
o = mw.inverted() @ Vector(global_origin)
ob.data.transform(Matrix.Translation(-o))
mw.translation = global_origin

# call the function:
set_origin(bpy.context.object)

``````

then use the rotate code to rotate it, but this way, the center of rotation will be at the origin, and it will remain constant, I am not sure if that is what you want. If that is not, there is a math function for it, normally they will use some form of sin and cosine. Trigonometric stuff. Alternatively I suppose you could also change the pivot point location as it rotates.

and thank you very much for this piece of code,
its pretty useful get an example of inverted_matrix used ! this is the kind things i was searching about!

Sorry for the incoming mess
Basically i would just do ,
what happen when you parent an object .
I can parent A to B and now i can use the local transfromation of B to move A.
I dont think i can use parent since i would do this to object, and faces, and vertex,… (am i wrong?)

I followed some tutorials about matrix but i get lost in understanding some parts.

When i get to the Inverted matrix the definition is “a matrix that invert- cancel the transformations”,
for me its not really clear and i made some experiment to understand,

I just duplicated some cube to show tha path, but the inverted matrix what is exactly?
,i am very interested but also confused XD.

Watching this i clearly understand that the transformations occur around the world origin, but maybe i can do it with any “point-origin”

Basically is just what happen when you parent an object .
I can parent A to B and now i can use the local transfromation of B to move A.

I do not use linear algebra much, as I just do not understand it that much, and, most of the functions that you need in Blender Python are built in, so you do not need linear algebra. To drive the rotation of one object from the rotation of another would normally be accomplished via drivers if the ratio was not one-to-one, but it can just as easily be done using Python rotation functions. By the way, I found that function from the last post by searching the internet, which is one of the best ways to find the code that you need, but you have to use the right keywords and have a good idea what you are looking for. I am working on creating tutorials on my website, but have not covered drivers yet.

Yep , watching tutorials and googling on euler-matrix-quaternion here !
but keywords and general behaviour are not universal for everybody.
Like the pivot XD, coming from maya i usually think to it like a free identity,
not the result of the matrix stuff.

Coming over from Maya . . . . I did that about 10 years ago, and the one major thing that you have to remember is that Blender actually requires that you be in the right mode before it will allow you to run code . . . then it is not well known by former Maya users how to know what mode they need. Usually, it is EDIT or OBJECT if you are building a model. I have tutorials up now that could probably give you are real shoe-in for understanding with Blender.

or on my website:
https://www.archeuslore.com/blender/blender.html

This is how i solved
i get the local direction of a object “Tool”,
use that direction to orient a rotation of another object “Cube”,

Use that quaternion-rotation to :
Rotate the rotation of the cube with tool local rotation (Rotation)
Rotate the cube around the “tool” location. (Location)

So running the script will rotate about 15 a cube, around the tool ( like its parented.)

``````import bpy
import math
import mathutils
from mathutils import Matrix, Euler, Quaternion , Vector

TOOL = bpy.data.objects["ROTATOR"]       # The tool
QQQ= bpy.data.objects["Cube"]            # the cube to rotate around the tool
#  WAY  1 ------------------------------------
mat= TOOL.matrix_world                                 # TOOL  get the matrix
localZ = Vector( (mat, mat, mat ) )  # TOOL  get the local Z direction

OTHER_rotation  = mathutils.Quaternion(  (localZ) , math.radians(15.0) )
QQQ.rotation_euler.rotate( OTHER_rotation )     # Make the cube rotate along the localZ of Tool of 15

newlocation = mathutils.Vector(  (QQQ.location-TOOL.location)  ) # Get the distance between Tool and Cube
newlocation.rotate(OTHER_rotation)                               # Rotate this vector as before
QQQ.location= newlocation + TOOL.location                        # apply location of cube
``````

Another way of getting the localZ is create a vector, and rotate the vector using the object rotation.

``````#(translation, rotation, scale) = THIS.matrix_world.decompose()    # Get the matrix of obj
#abstract_y = mathutils.Vector((0.0, 1.0, 0.0))             # make a vector  abstract for Z
#True_Y = rotation @ abstract_y                             # Rotate the vector as the object
``````

So this is a value that used correctly will “clear” or “nullify” the matrix,
so if you have the cube in loc 5,5,5 rotated about 11,22,33
the inverted matrix correctly appliedd, will set the cube back to 0,0,0 0,0,0.
**Otherwise if instead of adding, you just set the inverted value as the cube matrix, **
this will lead your cube to pop from ,5,5,5 to -5,-5,-5 and rotation inverted too…
being these values based on the origin of the world , this will look like a creazy behaviour,
jumping from a location , to the negative of that location , and back , every time you run the script.

2 Likes

That looks pretty impressive, I did not quite understand what TOOL is, as I put the script in Blender to see how it worked.

This guy puts out some pretty good tutorials on matrices and linear algebra, there are some others like
ProfRobBob who puts out some outstanding tutorials too.