2
$\begingroup$

Two Planes

We see two identical Plane objects: A(blue) and B(red) They are oriented arbitrarily in the 3D space. Notice that the origin of A is located in the center and the origin of B is located in the lower left corner.

The problem: What is the python code in Blender that will match the shapes of A to B perfectly.

I tried to match the matrices:

A.matrix_world = B.matrix_world

This matches location, rotation and scale of the objects but I still need to offset A by half width and half height in order to fit them perfectly.

Is it possible to do that with some of the available Matrix functions in Blender?

$\endgroup$
  • $\begingroup$ Are these both single face planes? $\endgroup$ – batFINGER Feb 8 '18 at 15:02
  • $\begingroup$ Yes they are single face Plane objects. Maybe I should upload the blend file for more clarity? $\endgroup$ – Rumen Belev Feb 8 '18 at 15:55
2
$\begingroup$

Face centre as "Pseudo Origin"

Given we have 2 single face planes, will make the face centre the "pseudo origin" and match that.

Test script, moving "Plane.001" to "Plane". The difference in face centre coordinates vector is used to create a local transformation matrix. Premultiplying this by the matrix world will translate the "face centres" to match.

import bpy
from mathutils import Matrix
context = bpy.context
scene = context.scene
# going to match transform of p2 to p1
p1 = scene.objects["Plane"]
mw = p1.matrix_world
p2 = scene.objects["Plane.001"]

# local offsets
o1 = p1.data.polygons[0].center
o2 = p2.data.polygons[0].center
# match the face centers global coords
# local translation matrix
LTM = Matrix.Translation(o1 - o2)
p2.matrix_world = mw * LTM
| improve this answer | |
$\endgroup$
  • $\begingroup$ Your answer really helped me! I will post slightly modified version that takes origins instead of face centers, but it doesn't deviate much from this answer. Thank you! $\endgroup$ – Rumen Belev Feb 8 '18 at 21:18
2
$\begingroup$

Here is my slightly modified version of batFINGER's solution:

import bpy
from mathutils import Matrix
from mathutils import Vector

A = bpy.data.objects["A"]
B = bpy.data.objects["B"]

# local offsets
# The origin of A is in the center (positive dimensions divided by 2)
# The origin ob B is at lower left corner (zeroes)

# local translation matrix
LTM = Matrix.Translation(A.dimensions/2 - Vector())

# match Planes - A to B

A.matrix_world = B.matrix_world * LTM

This will work if one of the origins is in the center of the Plane and the other origin is in the lower left corner, and both planes A and B don't have applied rotation to them

| improve this answer | |
$\endgroup$

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.