3
$\begingroup$

How can I rotate a plane to make it have the same orientation of another plane. Until now I have:

import bpy
# The object to rotate
ob = bpy.context.active_object
# The object world normal vector
ob_local_normal = ob.data.polygons[0].normal
ob_world_normal = ob.matrix_world * ob_local_normal
print(ob_world_normal)
# The plane to be aligned/parallel with
pl = bpy.data.objects['Plane']
# The plane world normal vector
pl_local_normal = pl.data.polygons[0].normal
pl_world_normal = ob.matrix_world * pl_local_normal
print(pl_world_normal)
# Now I need to rotate the object...

In Python rotate a polygon to face something was also helpful but I need the 2 planes beeing parallel each other.

$\endgroup$

1 Answer 1

4
$\begingroup$
import bpy
from mathutils import Matrix, Vector

def scale_from_vector(v):
    mat = Matrix.Identity(4)
    for i in range(3):
        mat[i][i] = v[i]
    return mat    

o1 = bpy.data.objects['Object.001']
o2 = bpy.data.objects['Object.002']

loc_src, rot_src, scale_src = o1.matrix_world.decompose()
loc_dst, rot_dst, scale_dst = o2.matrix_world.decompose()

Apply the rotation of object o1 to object o2

o2.matrix_world = (
    Matrix.Translation(loc_dst) * 
    rot_src.to_matrix().to_4x4() * 
    scale_from_vector(scale_dst)
)

If you only want to align an axis use the following code instead:

#only align z-axis
axis = Vector((0.0, 0.0, 1.0))
z1 = rot_src * axis
z2 = rot_dst * axis
q = z2.rotation_difference(z1)

o2.matrix_world = (
    Matrix.Translation(loc_dst) *
    (q*rot_dst).to_matrix().to_4x4() *
    scale_from_vector(scale_dst)
)
$\endgroup$
2
  • $\begingroup$ Thanks, now the normal is the same... but the local orientation is not the same and it looks like I should rotate the destination plan around the normal vector to make it aligned with the source plan. Is there an easy way to do it? $\endgroup$
    – isar
    Commented Jun 18, 2015 at 20:21
  • $\begingroup$ There probably is, but I'm having difficulty understanding exactly what you want done. Perhaps if you could paste together some screenshots that illustrate the result that disappoints you next to what you wish it looked like I could fine tune the procedure. $\endgroup$
    – Mutant Bob
    Commented Nov 7, 2015 at 18:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .