def MatchRotation(Obj, Target): T = Target.Kinematics.Global.Transform O = Obj.Kinematics.Global.Transform R = XSIMath.CreateRotation() T.GetRotation(R) O.SetRotation(R) Obj.Kinematics.Global.Transform = O
def GetOrientationFromPickPosition(pos): if isinstance(pos, list) and len(pos) > 1: rot = [] vm = XSI.Desktop.ActiveLayout.Views("vm") focus = vm.GetAttributeValue("focusedviewport") if focus: cam = vm.GetAttributeValue("activecamera:" + focus) else: cam = "Right" y = XSIMath.CreateVector3(0, 1, 0) if cam == "Right": y.Set(1, 0, 0) elif cam == "Top": y.Set(0, 1, 0) elif cam == "Front": y.Set(0, 0, 1) else: x1 = XSIMath.CreateVector3() x2 = XSIMath.CreateVector3() x1.Sub(pos[1], pos[0]) x2.Sub(pos[len(pos) - 1], pos[len(pos) - 2]) y.Cross(x1, x2) y.NormalizeInPlace() for idx in range(len(pos) - 1): start = pos[idx] end = pos[idx + 1] delta = XSIMath.CreateVector3() delta.Sub(end, start) ''' n = AddNull(xsi.ActiveSceneRoot,4,1,"Start") t = n.Kinematics.Global.Transform t.SetTranslation(start) n.Kinematics.Global.Transform = t n = AddNull(xsi.ActiveSceneRoot,4,1,"End") t = n.Kinematics.Global.Transform t.SetTranslation(end) n.Kinematics.Global.Transform = t ''' r = GetRotationFromTwoVectors(delta, y) rot.append(r) # duplicate the last array item rot.append(rot[len(rot) - 1]) return rot else: return XSIMath.CreateRotation()
def GetRotationFromTwoVectors(direction, up=XSIMath.CreateVector3(0, 1, 0)): cross = XSIMath.CreateVector3() side = XSIMath.CreateVector3() direction.NormalizeInPlace() up.NormalizeInPlace() cross.Cross(direction, up) cross.NormalizeInPlace() side.Cross(cross, direction) side.NormalizeInPlace() m = XSIMath.CreateMatrix3() m.Set(cross.X, cross.Y, cross.Z, direction.X, direction.Y, direction.Z, side.X, side.Y, side.Z) r = XSIMath.CreateRotation() r.SetFromMatrix3(m) return r