Beispiel #1
0
def logMatrix4AsSRT(m, msg="matrix4"):

    t = XSIMath.CreateTransform()
    t.SetMatrix4(m)

    vScl = t.Scaling
    vRot = t.Rotation.XYZAngles
    vRot.Set(XSIMath.RadiansToDegrees(vRot.X),
             XSIMath.RadiansToDegrees(vRot.Y),
             XSIMath.RadiansToDegrees(vRot.Z))
    vPos = t.Translation

    logVector(vScl, msg + " S")
    logVector(vRot, msg + " R")
    logVector(vPos, msg + " T")
Beispiel #2
0
    def switchToIk(self):

        # Get the distance between Root and effector and compare it to bones total length to define if the chain is bend or not
        vRootEff = XSIMath.CreateVector3()
        vRootEff.Sub(self.jnt0.kinematics.Global.Transform.Translation,
                     self.ik.kinematics.Global.Transform.Translation)

        # We get an approximation of the length otherwise it's sometimes tricky to compare distance
        r = 100000
        dRootEffLength = round((vRootEff.Length() * r)) / r
        dBonesLength = round(((self.length0 + self.length1) * r)) / r

        # xsi.MatchTransform(self.ik, self.eff, c.siRT, None)
        # tra.matchGlobalTransform(self.eff, self.ik, True, True, False)
        tra.matchGlobalTransform(self.ik, self.fk2, True, True, True)
        self.blend.Value = 1
        # tra.matchGlobalTransform(self.fk2, self.ik, False, True, False)

        # Compute Roll
        if dRootEffLength < dBonesLength:
            # Get Vectors to built the plane
            vFKChain0 = XSIMath.CreateVector3()
            vIKChain0 = XSIMath.CreateVector3()
            vRootEff = XSIMath.CreateVector3()

            vFKChain0.Sub(self.fk0.kinematics.Global.Transform.Translation,
                          self.fk1.kinematics.Global.Transform.Translation)
            vIKChain0.Sub(self.jnt0.kinematics.Global.Transform.Translation,
                          self.jnt1.kinematics.Global.Transform.Translation)
            vRootEff.Sub(self.jnt0.kinematics.Global.Transform.Translation,
                         self.ik.kinematics.Global.Transform.Translation)

            # vFKChain0.NormalizeInPlace()
            # vIKChain0.NormalizeInPlace()
            # vRootEff.NormalizeInPlace()

            # Get Plane's Normal
            vNormFKPlane = XSIMath.CreateVector3()
            vNormIKPlane = XSIMath.CreateVector3()

            vNormFKPlane.Cross(vFKChain0, vRootEff)
            vNormIKPlane.Cross(vIKChain0, vRootEff)

            # vNormFKPlane.NormalizeInPlace()
            # vNormIKPlane.NormalizeInPlace()

            # Get the Angle between the two planess
            dAngle = XSIMath.RadiansToDegrees(vNormFKPlane.Angle(vNormIKPlane))

            # Check if we have to remove or add the angle
            vCrossPlane = XSIMath.CreateVector3()
            vCrossPlane.Cross(vNormFKPlane, vNormIKPlane)
            # vCrossPlane.NormalizeInPlace()

            dDirectionDot = vCrossPlane.Dot(vRootEff)

            # Set the new roll Value
            a = self.roll.Value + (dDirectionDot / abs(dDirectionDot)) * dAngle

            # Set the angle in a -180 / 180 range
            if abs(a) > 180:
                a = a % ((-a / abs(a)) * 360)

            # [OLD WAY ]Set the new roll Value --

            # Check if the bone is negated or not
            ## dNegate = self.jnt1.kinematics.Local.posx.Value + self.jnt1.kinematics.Local.nposx.Value
            #dNegate = 1
            # oldAngle = self.roll.Value

            # if (dDirectionDot * dNegate) <  0:
            # newAngle = oldAngle - dAngle
            # else:
            # newAngle = oldAngle + dAngle

            # if newAngle > 180:
            # newAngle -= 360
            # elif newAngle < -180:
            # newAngle += 360
            # [END OLD WAY ] -------------------

            self.roll.Value = a

        # Match Scale Value
        # We remove Soft distances
        self.softik.Value = 0

        if dRootEffLength > dBonesLength:
            if self.scale.Value > self.maxstretch.Value:
                self.maxstretch.Value = self.scale.Value
                self.scale.Value = 1

        if self.isleg:
            z = XSIMath.CreateVector3(1, 0, 0)
            y = XSIMath.CreateVector3(0, 1, 0)

            if self.negate:
                z.NegateInPlace()
                y.NegateInPlace()

            z.MulByRotationInPlace(
                self.fk2.Kinematics.Global.Transform.Rotation)
            y.MulByRotationInPlace(
                self.fk2.Kinematics.Global.Transform.Rotation)

            t = self.fk2.Kinematics.Global.Transform
            t.SetRotation(tra.getRotationFromAxis(z, y, "zy"))

            self.ik.Kinematics.Global.Transform = t
        else:
            tra.matchGlobalTransform(self.ik, self.fk2, False, True, True)