3
$\begingroup$

I have 2 Edges intersecting at 90 degree. How can I add an Empty aligned to them. I suppose this can be done using mathutils. I need the matrix and not just the Translation component which is not aligned.

Thanks, this will be very useful!

Empty

$\endgroup$
  • $\begingroup$ Do the answers to question 18565 provide any help? Except for the Python aspect, this appears to be identical to that question. $\endgroup$ – brasshat Jul 6 '15 at 8:28
  • $\begingroup$ No, the answers to question 18565 don't help but the answer below does work like a charm! Thanks anyway. $\endgroup$ – isar Jul 7 '15 at 0:00
3
$\begingroup$

Getting the location is indeed trivial, even more with mathutils.geometry.intersect_line_line().

Getting rotation here is not so hard either. Let's call your two edges e1 and e2 (note that this is theoretical code, assuming you have your vertices array and empty object already at hand):

# We get 'average' intersection point - in case the two edges do not actually exactly intersect.
inter = mathutils.geometry.intersect_line_line(vertices[e1.v1].co, vertices[e1.v2].co, vertices[e2.v1].co, vertices[e2.v2].co)
vi = (inter[0] + inter[1]) / 2

# Then, we get our first axis.
ax = (vertices[e1.v1].co - vi).normalized()

# We get our third axis by cross product of first axis and second edge.
az = (ax.cross(vertices[e2.v1].co - vi)).normalized()

# Then second axis is crossproduct of the first and third ones.
# Note that if you know for sure your two edges are orthogonal,
# You can skip that and rather create ay exactly as we did with ax.
ay = (az.cross(ax)).normalized()

# Those three axes allow us to create a rotation matrix directly.
# However, we need to put them as columns, not rows. Since matrix
# creation in Blender uses rows, we'll have to transpose it.
mrot = mathutils.Matrix((ax, ay, az))
mrot.transpose()

# And now, we just have to make this a 4d matrix and add translation component.
mat = mrot.to_4x4()
mat.translation = vi

# And set this a base matrix of our empty:
empty.matrix_basis = mat
$\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.