Geometry nodes - Align Instance object to face orientation, rotate normal to longest edge

In the question posted here...:

Geometry nodes - Align Instance object to face orientation

...a very usefull solution was provided to the problem of aligning instances in Geometry Nodes to the normal, while rotating the normal to have one axis of the normal face the closest edge of the face.

In my use case I would need to have one particular axis of the normal (say, the X-axis) to face the longest edge of the face.

So, is there a way to align the Z axis to point in the direction of the normal while the X-axis always points to the longest edge on the face?

In the above screenshot, the Z-Axis is aligned to the normal, using the solution in the above mentioned link. Also in some cases the X-Axis is pointing towards the longest edge (solution aims at aligning to closest edge). However, in some of the cases the X or Y-axis are not aligned to any edge. My aim is to have the X-axis always point to the longest edge, wich is always the edge of the face that is pointing outwards of the model.

Please note that the instances (gizmo's) in this screenshot are only on selected faces, which are the faces that face each other, and not the faces that are pointing in or out of the model. Any possible answer does not need to take this feature into account (is a problem already solved).

Thank you!!

On each face in a mesh, this GN group stores the vector of its longest edge.

The group makes two passes over face-corners. One to weight each corner by the distance between it and the previous corner in the face-loop. Another to return the face-corner with the longest preceding edge, and store that edge's vector on the face.

(I haven't found a way of avoiding going over the loop twice.. maybe someone will be able to suggest a better way)

Once you have the direction of the longest edge (or the first tied-longest edge) per face, you can align the Z of your instances to their face-normals, and then spin them around their (good) Z's to align their X's to the longest edges of their faces:

(Anti-clockwise, face-forward) Like so:

Blender 3.41 stable

Not sure if this looks like exactly what you want! But it could easily be elaborated to, say, the mean of the 2 longest edges per face, right-angle to the shortest edge. or whatever is appropriate, by taking advantage of the 'Sort Index' in Corners of Face

• Thank you, Robin! This works like a charm. You used some nodes I didn't even knew existed. I will have to study this to understand how exactly this works. Consider me impressed! Commented Feb 26, 2023 at 20:47
• np, @Aloys :) Feel free to come back with comments if you need more explanation. Commented Feb 26, 2023 at 20:53
• Could I please trouble you to elaborate a bit on the second pass of the loop? My guess is that the second pass subtracts a corner index and its neighbor after the "Corners of Face" node has sorted them by "negative distance" weight, which then gets the longest edge in a way I can't quite figure out. Commented Feb 19 at 20:29
• Hi, @bobhasajetpack ! In the 'first pass', we store, on each face-corner, the length of its previous edge. That just gives us a way of sorting them in Corners of Face , in the 'second pass'. If we multiply the stored lengths by -1, then the 0th corner, (sorted ascending) will be the corner after the longest edge. Then we subtract to calculate the actual vector to the 0th corner from the one before it, and store that vector on each face. (If we didn't multiply by -1, then the 0th corner would be the one after the shortest edge.) Commented Feb 20 at 17:42
• @RobinBetts Thank you so much! The penny finally dropped. Your problem solving prowess is even more impressive when I can start understanding the logic. Commented Feb 23 at 0:50