Search ProofOfProgress Blog

Monday, March 28, 2011

Solution: Align Pivot To Another Object Without Affecting Geometry in MaxScript

This code can be used to Align a pivot of an object to another object or
some other arbitrary matrix without affecting the position of the original geometry.

Potential Usage: programmatically moving pivots before export so your model
will look right in your game engine.

Function ReAssignPivot
inputTransform --New Transform Matrix To Be Pivot
&theObject --The Object to Act on.
--Function Description:
--Gives the object a new pivot, defined by a transform matrix.
--Does so WITHOUT affecting the world position of the original geometry.
-- More Free Scripts: ***
--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 = inputTransform; --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.

RotationLoss = VLM.RotationPart - VLM_0.RotationPart;
GeomWorldRot = RotationLoss + IP_LocalRot;
theObject.objectOffsetRot = GeomWorldRot;


--Test Code;
--This test code will align SomeCircle's pivot to PivotObject's Pivot.
inputTransform = $PivotObject.Transform;
affectedObject = $SomeCircle;
ReAssignPivot inputTransform &OBJ;

No comments:

Post a Comment