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$
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$
  • $\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 Jun 18 '15 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 Nov 7 '15 at 18:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.