I'm currently trying to transform some object's pivot by some arbitrary matrix.
So far I've been able to do it for objects without any non-uniform scales applied, but it breaks when the target object has some non-uniform scales.
I have the impression that, for this to work, this would need to access the scalerotationpart
of the objectOffset, which seems to be unavalaible in maxscript.
This would be doable using the C++ SDK as the objectOffset scale is expressed as a ScaleValue
(which contains a scale vector and a quaternion, see SDK docs).
Am I missing something obvious ? or do I have to craft some maxscript plugin extension for this to work ?
Thanks,
My starting point was the following function :
fn AlignPivotTo Obj Trgt =
(
-- http://forums.cgsociety.org/archive/index.php/t-636495.html
--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:
-- 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
local deltaTransform = obj.transform * inverse Trgt
TmOffset *= deltaTransform
-- Apply matrix
Obj.transform = Trgt
-- Restore offsets
Obj.objectOffsetPos = TmOffset.translationPart * inverse TmOffset.scalerotationpart
Obj.objectOffsetRot = TmOffset.rotationPart * invers eTmOffset.scalerotationpart
Obj.objectOffsetScale = TmOffset.scalePart
-- Restore child transforms
for i = 1 to Obj.children.count do ( Obj.children[i].transform = ChldTms[i] * inverse Trgt * Obj.transform )
)