Align pivot to a rotated and xformed poly to its main axis (Maxscript) 
3joez 
In maxscript, is there a way to align the pivot to an object that has been xformed and rotated? Could that be even possible, anyway?
read 1056 times 7/22/2014 11:07:16 AM (last edit: 7/22/2014 11:07:16 AM)

br0t 
Spontaneously no. Max does not know about the orientation of the objects if the transforms are fucked up, our brain has no problem detecting that but you have to explicitly tell Max. You could do some sort of analysis of the mesh to figure out the orientation of the average edges or something, and create a transform from that.
Edit: Ah wait maybe I misunderstood. I am not sure how the XForm modifier works but it probably stores the transform or the offset to the original transform somehow so you might be able to restore it.
never get low & slow & out of ideas
read 1039 times 7/22/2014 7:02:57 PM (last edit: 7/22/2014 7:06:21 PM)

3joez 
Yes, br0t, I was thinking about an average direction of the segments using dot product or something. Pretty complex stuff. I don't think xform stores transform, especially when it has been converted in various format and then reimported in max.
read 1032 times 7/22/2014 7:24:42 PM (last edit: 7/22/2014 7:24:42 PM)

br0t 
Well after the Reset the XForm modifier seems to store the rotation and scale offset of the object to the world. But of course that is lost when collapsed or exported :/
never get low & slow & out of ideas
read 1022 times 7/23/2014 9:04:59 AM (last edit: 7/23/2014 9:04:59 AM)

Dub. 
it would be pretty easy to write a script that aligns the Y axis to a selected edge..
something like this: (don't try running this it most likely won't work  I am not near a Max)
o = selection[1] edgeSel = ((polyop.getEdgeSelection o) as array) theVerts = polyop.getVertsFromEdge o edgeSel[1] firstVert = polyop.getVert o theVerts[1] secVert = polyop.getVert o theVerts[2] y = normalize(secVert  firstVert) x = cross [0,0,1] y z = cross x y newMatrix = (matrix3 x y z (o.center)) for i=1 to (numverts o) do ( v = polyop.getvert o i v = v * inverse newMatrix polyop.setVert o i v ) o.transform = newMatrix
I think something like that should work
read 1018 times 7/23/2014 10:49:26 AM (last edit: 7/23/2014 10:49:26 AM)

3joez 
Really close, I've tweaked the syntax and now it works, but
o = selection[1] edgesel = ((polyop.getedgeselection o) as array) verts = ((polyop.getvertsusingedge o edgesel[1]) as array) v1 = polyop.getVert o verts[1] v2 = polyop.getVert o verts[2] y = normalize (v2v1) x = cross [0,0,1] y z = cross x y newMatrix = (matrix3 x y z (o.center)) for i = 1 to (o.numverts) do ( v = polyop.getvert o i v = v * inverse newMatrix polyop.setVert o i v ) o.transform = newMatrix
...the object rotates and aligns to world. Is it possible to move the pivot and not the object (so the pivot is aligned with the edge)?
read 1013 times 7/23/2014 11:55:00 AM (last edit: 7/23/2014 11:55:20 AM)

br0t 
Have a look at Martin's function at the end of this page:http://forums.cgsociety.org/showthread.php?f=98&t=636495&page=2&pp=15
never get low & slow & out of ideas
read 1002 times 7/23/2014 3:28:28 PM (last edit: 7/23/2014 3:28:28 PM)

3joez 
Thx br0t, will take a look.
read 1000 times 7/23/2014 4:27:54 PM (last edit: 7/23/2014 4:27:54 PM)

Dub. 
Sorry, Had an error or two in there!'
Try this:
o = selection[1] edgesel = ((polyop.getedgeselection o) as array) verts = ((polyop.getvertsusingedge o edgesel[1]) as array) v1 = polyop.getVert o verts[1] v2 = polyop.getVert o verts[2] y = normalize (v2v1) x = cross y [0,0,1] z = cross x y newMatrix = (matrix3 x y z (o.center)) origPos = #() for i = 1 to (o.numverts) do ( v = polyop.getvert o i append origPos v ) o.transform = newMatrix for i = 1 to (o.numverts) do ( polyop.setVert o i origPos[i] )
If you have an editable poly selected with an edge selected, it will align the transforms with the edge you have selected.
read 970 times 7/26/2014 5:52:09 AM (last edit: 7/26/2014 5:53:41 AM)

3joez 
Dub, that's incredible. It's perfect.
Can you help me to understand these lines?
y = normalize (v2v1) x = cross y [0,0,1] z = cross x y newMatrix = (matrix3 x y z (o.center))
read 941 times 7/28/2014 10:27:26 AM (last edit: 7/28/2014 10:27:26 AM)

Dub. 
Well a transform matrix is basically 4 vectors. X axis, Y axis, Z axis and the position vector.
If you imagine the move transform gizmo, that is basically a visual representation of the transform matrix.
next you need to know what normalizing a vector does. Basically if you have a vector with any length, say 5.43 a normalize operation will scale the vector so that it still points in exactly the same direction, but is exactly 1.0 in length.
The other thing you need to know is how a cross product works. The simple explaination, is that given two normalized vectors, it returns a new vector that is at right angles to both vectors.
this picture shows an example of two blue vectors and the new red vector:
So line by line, here is what is going on in those lines of code:
y = normalize (v2v1)
this takes the vector between the two verts (one each end of the selected edge) and normalizes it. This gives us a vector 1.0 in length, pointing parallel to the selected edge.
x = cross y [0,0,1]
this gives us a new vector that is exactly right angles to the edge.
z = cross x y
this gives us our final vector that is right angles to the edge AND the x vector from the last step. Now we have 3 vectors all orthogonal to each other  perfect for turning into a transform matrix!
newMatrix = (matrix3 x y z (o.center))
This final line constructs the matrix, feeding in the x y and z values, and for the last slot, the center of the objects bounding box.
I hope that sorta made sense!
R
read 898 times 8/1/2014 3:09:39 AM (last edit: 8/1/2014 3:09:39 AM)

3joez 
I'm really interested in this kind of informations. Thanks for taking the time to explain.
read 888 times 8/1/2014 9:14:01 AM (last edit: 8/1/2014 9:14:01 AM)
