1
$\begingroup$

Custom Orientation

I have got a mesh that's converted from curve. It has only edges. I need to successively orient one of the objects in the scene to the direction pointed by the blue arrow in the illustration above (perpendicular to the edge and in the direction of the normal of the hypothetical face, formed by the planar vertices). How do I calculate the orientation using python?

$\endgroup$
  • $\begingroup$ Could you please clarify what you mean by ''successively orient one of the objects in the scene"? What is the context of your situation? How and why are you trying to atchieve this? This might help forming a better solution to your problem. $\endgroup$ – Martynas Žiemys Nov 10 '18 at 15:02
  • $\begingroup$ @MartinZ Thanks for your interest. It's an animation. The object (or character, if you will) needs to follow path but with the custom orientation. In the first few frames it will be at e0, then e1 and so on. $\endgroup$ – Blender Dadaist Nov 10 '18 at 15:37
  • $\begingroup$ You could make an empty follow the curve as a path and then copy it's rotation with a constraint to whatever other object if it does not need to move allong the curve. Would you want a more detailed explanation how to do that? $\endgroup$ – Martynas Žiemys Nov 10 '18 at 15:56
  • $\begingroup$ Actually, follow path constraint does not orient the empty to the edge (or 'segment' in case of the curve) perpendicular. As far as I could see, it applies the rotation value from matrix_world of the curve object to the constrained object. Secondly, follow constraint needs a curve as path. However, I can't use curve, I need a mesh (converted from curve). $\endgroup$ – Blender Dadaist Nov 10 '18 at 16:08
  • $\begingroup$ It can, if you have enough vertices in the curve and align the objects orientation to the first segment. See this I would offer a solution in more detail in an answer but I still cannot understand the wider context and be sure it would not conflict with something I do not know about your situation. For example I do not know what the edge is for so I don't know why it must be mesh object only. It's hard to guess the whole situation, without more detailed explanation. $\endgroup$ – Martynas Žiemys Nov 10 '18 at 16:29
1
$\begingroup$

a simple cross product solves your problem:

\begin{equation} \hat{z}: the \space unit \space vector \space along \space z \space axis\\ \vec{e_\perp}:the \space vector \space perpendicular \space to \space edge \space and \space the \space z \space axis\\ \vec{p_1}:position \space of \space first \space vertex \space in \space world \space coordinates\\ \vec{p_2}:position \space of \space second \space vertex \space in \space world \space coordinates\\ \vec{e_1}:vector \space along \space edge\\ \vec{p_2} - \vec{p_1} = \vec{e_1}\\ \hat{z} \times \vec{e_1} = \vec{e_\perp}\\ \vec{e_1} \times \vec{e_\perp} = \vec{desiredDirection} \end{equation}

you can use mathutils's Vector library to do this.

| improve this answer | |
$\endgroup$
  • $\begingroup$ Thanks for the answer. I did not say it needs to be perpendicular to z axis. So the cross product with z hat is not necessary. I need the direction perpendicular to the face formed by the planar vertices. I can take the cross product of the vector difference of the two of the three vertices at a time. The problem arises when all three verts are linear. In such case I need to go on till there is a non co-linear vertex. Anyway, I am creating a temporary bmesh face for some other purpose. So just a normal_update of the face followed by face normal gives me the desired direction. $\endgroup$ – Blender Dadaist Nov 11 '18 at 8:10
  • $\begingroup$ This is not perpendicular to z!. You need a vector perpendicular to edge and in the plane of 'z and edge'. This calculation gives you such vector $\endgroup$ – MohammadHossein Jamshidi Nov 11 '18 at 12:03
  • $\begingroup$ Yes, I got your point. But there is no mention of z axis in my question. My requirement is not as you say "a vector perpendicular to edge and in the plane of 'z and edge'". The direction is not dependent on any of the axes x, y or z. I have mentioned clearly that it needs to be aligned to the (not existent or hypothetical) face, which would be formed by the vertices. I reiterate what I said in the comment above: the problem with the cross product of the vector difference of the coordinates of the adjacent points is the co-linear vertices. $\endgroup$ – Blender Dadaist Nov 11 '18 at 12:17
  • $\begingroup$ So you should take the normal vector of the face instead of z direction and it would be done! $\endgroup$ – MohammadHossein Jamshidi Nov 11 '18 at 18:37

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.