Aligning the object axis to an edge [SOLVED]

On 21/08/2014 at 16:46, xxxxxxxx wrote:

User Information:
Cinema 4D Version:   14 
Platform:      Mac OSX  ; 
Language(s) :       PYTHON  ;

---------
I have been trying to create a script that would align the object axis (a polygonal object, of course), to a selected axis but keeping all the points in their same absolute positions.
I already have a way to find the points that define the edge.
So, lets say that I have the coordinates of vertex #1 of the edge in variable A and the coordinates of vertex #2 of the edge in variable B.
If I multiply both coordinates by the global matrix of the object, I get their absolute positions.
I can easily move the object to the absolute coordinate A and then move all the points to the difference between their old position and the new position. This way, I can effectively place the axis at the location of point A without changing how the object looks.
But I also want the axis oriented to the direction of the vertex #2.
In a nutshell, I want the object axis to be placed at the location of the vertex #1 and pointing (could be axis Z) to vertex #2. Besides this, I want all the points of the object adjusted so that it seems that only the axis moved and reoriented.
Can anyone help me out?

p.s. This is in python, not in C++

On 21/08/2014 at 17:55, xxxxxxxx wrote:

this should be so simple, your object has a matrix (position rotation scale 4x4 matrix), you need to change position and rotation,here is the algorithm:

1- store all object points global position "absolute coordinate as you stated" in a list
2- transform your object to point A "this will move it to point A"
3- get the Vector (call it D) where: D = point B - point A
4- get the perpendicular vector between your object z axis and the D vector
E = cross(Z, D) #pseudo code
5- get angle between Z and D
A = acos(Z,D) #pseudo code
6- rotate your object matrix by the quaternion (this means a vector and an angle, which are E and A
Quat = Quaternion(E,A) #pseudo code
7- new matrix = old matrix * Quat
8- set all vertices with your stored list

On 22/08/2014 at 02:31, xxxxxxxx wrote:

Thank you for the explanation, Mohamed.
However, I find nothing about Quaternion in the python SDK.
What is the equivalent?

On 22/08/2014 at 02:59, xxxxxxxx wrote:

may be a look at function, or an euler rotation

On 22/08/2014 at 04:51, xxxxxxxx wrote:

Thank you again.
I made it work. It may not be the most elegant code, but it is working :-)