Search ProofOfProgress Blog

Monday, March 28, 2011

Solution: Align Pivot To World Without Affecting Geometry in Maxscript


This script moves the pivot of an object to the world and aligns it without
affecting the original location of the object geometry.

Some things I learned about max's internals from doing this:
1. theObject.objectOffsetPos is the LOCAL coordinates of the objects
"Invisible" axis. Plug those Local coordinates into the objects
transform matrix to get the WORLD coordinates of the objects
"Invisible" axis.

Example:
LocalCoords = theObject.objectOffsetPos;
TheMatrix = theObject.Transform;
WorldSpaceCoords = LocalCoords * TheMatrix;

2. The Objects VISIBLE pivot is a Matrix.
The Objects INVISIBLE pivot is a quaternion and point3 position.

This is self-Evident when you mess around.


Function alignPivotToWorld
&theObject
--Aligns the pivot to the world WITHOUT affecting the geometry.
--
--*****************************************************
--More Scripts at: www.ProofOfProgress.BlogSpot.com **
--*****************************************************
--
=(
--VLM = Visible Local Matrix.
--The matrix/pivot you see when selecting object and "Local" axis is selected as viewable.
VLM = theObject.Transform;
IP_LocalRot = theObject.objectOffsetRot; --Rotation to be used later.
IP_LOCAL = theObject.objectOffsetPos; --Invisible Pivot Local coordinates
--In relation to VLM matrix.
IP_WORLD = IP_LOCAL * VLM; --World Coordinates of Invisible Pivot. [Local To World Transform]
VLM_0 = matrix3 1; --Reset Visible Local matrix coordinates.

NEW_IP_LOCAL = IP_WORLD * inverse(VLM_0); --[World To local Transform]

theObject.Transform = VLM_0;
theObject.objectOffsetPos = NEW_IP_LOCAL;

--Now Handle Rotation:
--Since rotation of visible local matrix has been zeroed out,
--You must add that loss to the invisible pivot rotation.
GeomWorldRot = VLM.RotationPart + IP_LocalRot;
theObject.objectOffsetRot = GeomWorldRot;

)--[FN:alignPivotToWorld]

--Test Code;
OBJ = $;
thePos = [0,0,0];
alignPivotToWorld &OBJ;


I don't have the time, but if anyone would like to edit this
So that the pivot can be adjusted to be put ANYWHERE rather than aligned to the
World, let me know. I will re-post it and credit will be given.

No comments:

Post a Comment