title: Maxscript access to full Object Offset Transformation (non uniform scale)
body: <p>I'm currently trying to transform some object's pivot by some arbitrary matrix.</p>
  
  <p>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.</p>
  
  <p>I have the impression that, for this to work, this would need to access the <code>scalerotationpart</code> of the <em>objectOffset</em>, which seems to be unavalaible in maxscript.</p>
  
  <p>This would be doable using the C++ SDK as the <em>objectOffset</em> scale is expressed as a <code>ScaleValue</code> (which contains a scale vector and a quaternion, see <a href="http://docs.autodesk.com/3DSMAX/16/ENU/3ds-Max-SDK-Programmer-Guide/index.html?url=files/GUID-4C8E39BA-219D-4EC6-B60E-A677656C865A.htm,topicNumber=d30e22149" rel="nofollow">SDK docs</a>).</p>
  
  <p>Am I missing something obvious ? or do I have to craft some maxscript plugin extension for this to work ?</p>
  
  <p>Thanks,</p>
  
  <p>My starting point was the following function :</p>
  
  <pre><code>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 )
  )
  </code></pre>