[Closed] Aligning only the pivot of an object using MAXScript
hey, I’ve been battling with this too… seems so complicated in maxscript.
I want to set an objects pivot (rotation and position) to that of another. i use a bit of a dirty method smoetimes where I take the object with the required pivot, delelte all the faces and attach the other object. This has it’s own problems and is a bit heavy too, but often does the trick.
This question seems to have come up before, but no decent solution… surely it has to be doable?
Hey, Martin, I tried your code and it works fine… the first time you execute it. Just tried it with a box an a point, and aligning the pivot of the box to the one in the point:
1st time – it aligns ONLY the pivot of the box to the point.
2nd time – it allows also the geometry of the box to the point.
If you move the point around and run the function, the pivot of the box will align to that point… but the position of the box (i mean, the offset position) seems to be equal to the old position of the point (i refresh the variable that holds the transform of the point). And weird things happen if I move the box around and run the script again.
Am I doing something wrong???
Yes, you are right, and no, I don’t think you are doing something wrong. I have now added a Reset Pivot operation in the beginning, so the object/pivot always has a well defined start. This seems to work better.
So just insert a
ResetPivot obj
right after
with redraw off (
in the old script above.
Does this help?
– MartinB
martin:
I’m not sure if this is any help to you, but here’s what i’ve done sometimes; I have two objects visually on top of each other, and then i want to have pivot of first object aligned in position and rotation to other object’s pivot. I think this could be more like what Rorschach is doing…
obj = $Teapot01 -- xyz aligned pivot
obj2 = $Teapot02 -- rotated and moved pivot
-- Store parent info
obj_par = $teapot01.parent
obj2_par = $teapot02.parent
-- Store transformation info
obj_tra = obj.transform
-- unlink
obj.parent = ()
obj2.parent = ()
--rotate and move pivot
obj.objectoffsetrot = obj2.rotation
obj.rotation = obj2.objectoffsetRot
obj.pos = obj_tra.pos
obj.pivot = obj2.pivot
-- reparent
obj.parent = obj_par
obj2.parent = obj2_par
Rorschach:
It would be nice to have some similar command for object pivots like working pivot has:
<void>WorkingPivot.setTM <matrix3>tm
But basically I want to transform the pivot into fixed orientation that is given in a matrix3 value, without moving the geometry.
Hi, Martin. Maybe this is not exactly You want but, anyway, just decided to put my 2 cents.
fn fnSetPivot nod TM = (
If canConvertTo nod Editable_Poly do (
If classof nod != Editable_Poly do convertToPoly nod
vertsPos = For i = 1 to (polyOp.getNumverts nod) collect (polyOP.getvert nod i)
nod.transform = TM
for i = 1 to vertsPos.count do polyOP.setvert nod i vertsPos[i]
)
)
nod = teapot position:[50,70,90] dir:[50,70,90]
TM = (matrix3 [1,0,0] [0,1,0] [0,0,1] [0,0,0])
fnSetPivot nod TM
So, this function just stores position for each vertex, then sets object transform and then moves each vertex to it`s original position. Nothing special, but works fine for me.
Deficiency is that it works only with geometry objects and it converts them into
Editable poly.
This fails miserably for any objects having a negative or non-uniform scale, but it seems to work well in any other case I have tested:
(
fn AlignPivotTo Obj Trgt =
(
-- Get matrix from object
if classOf Trgt != matrix3 then Trgt = Trgt.transform
-- Store child transforms
local ChldTms = in coordSys Trgt ( for Chld in Obj.children collect Chld.transform )
-- Current offset transform matrix
local TmScale = scaleMatrix Obj.objectOffsetScale
local TmRot = Obj.objectOffsetRot as matrix3
local TmPos = transMatrix Obj.objectOffsetPos
local TmOffset = TmScale * TmRot * TmPos
-- New offset transform matrix
TmOffset *= obj.transform * inverse Trgt
-- Apply matrix
Obj.transform = Trgt
-- Restore offsets
Obj.objectOffsetPos = TmOffset.translation
Obj.objectOffsetRot = TmOffset.rotation
Obj.objectOffsetScale = TmOffset.scale
-- Restore child transforms
for i = 1 to Obj.children.count do Obj.children[i].transform = ChldTms[i] * inverse Trgt * Obj.transform
)
-- Examples
AlignPivotTo $Box01 $Point01
AlignPivotTo $Box02 (matrix3 1)
)
Cheers,
Martijn
[QUOTE=magicm]This fails miserably for any objects having a negative or non-uniform scale, but it seems to work well in any other case I have tested:
(
fn AlignPivotTo Obj Trgt =
( ... )
Thanks a lot! That looks quite good! Any idea why non-uniform scale is not working?
But NU Scale is evil anyway… Try manually editing the pivot on a NU-scaled object… >:)
– MartinB
I just wanted to express my thanks for that code, since it works absolutely perfect for what I was trying to accomplish, and probably saved me many hours of work.
Thanks!
Sorry for resurrecting an old post but credit where credit’s is due! I echo the above post’s sentiment