Example #1
0
class HandComponentRig(HandComponent):
    """Hand Component"""

    def __init__(self, name='Hand', parent=None):

        Profiler.getInstance().push("Construct Hand Rig Component:" + name)
        super(HandComponentRig, self).__init__(name, parent)


        # =========
        # Controls
        # =========
        # Hand
        self.handCtrlSpace = CtrlSpace('hand', parent=self.ctrlCmpGrp)
        self.handCtrl = Control('hand', parent=self.handCtrlSpace, shape="square")
        self.handCtrl.rotatePoints(0, 0, 90.0)
        self.handCtrl.lockScale(True, True, True)
        self.handCtrl.lockTranslation(True, True, True)


        # ==========
        # Deformers
        # ==========
        self.deformersLayer = self.getOrCreateLayer('deformers')
        self.defCmpGrp = ComponentGroup(self.getName(), self, parent=self.deformersLayer)
        self.addItem('defCmpGrp', self.defCmpGrp)

        self.handDef = Joint('hand', parent=self.defCmpGrp)
        self.handDef.setComponent(self)


        # ==============
        # Constrain I/O
        # ==============
        # Constraint inputs
        self.armEndInputConstraint = PoseConstraint('_'.join([self.handCtrlSpace.getName(), 'To', self.armEndInputTgt.getName()]))
        self.armEndInputConstraint.setMaintainOffset(True)
        self.armEndInputConstraint.addConstrainer(self.armEndInputTgt)
        self.handCtrlSpace.addConstraint(self.armEndInputConstraint)

        # Constraint outputs
        self.handOutputConstraint = PoseConstraint('_'.join([self.handOutputTgt.getName(), 'To', self.handCtrl.getName()]))
        self.handOutputConstraint.addConstrainer(self.handCtrl)
        self.handOutputTgt.addConstraint(self.handOutputConstraint)

        # Constraint deformers
        self.handDefConstraint = PoseConstraint('_'.join([self.handDef.getName(), 'To', self.handCtrl.getName()]))
        self.handDefConstraint.addConstrainer(self.handCtrl)
        self.handDef.addConstraint(self.handDefConstraint)

        Profiler.getInstance().pop()


    def addFinger(self, name, data):

        fingerCtrls = []
        fingerJoints = []

        parentCtrl = self.handCtrl
        for i, joint in enumerate(data):
            if i == 0:
                jointName = name + 'Meta'
            else:
                jointName = name + str(i).zfill(2)

            jointXfo = joint.get('xfo', Xfo())
            jointCrvData = joint.get('curveData')

            # Create Controls
            newJointCtrlSpace = CtrlSpace(jointName, parent=parentCtrl)
            newJointCtrl = Control(jointName, parent=newJointCtrlSpace, shape='square')
            newJointCtrl.lockScale(True, True, True)
            newJointCtrl.lockTranslation(True, True, True)

            if jointCrvData is not None:
                newJointCtrl.setCurveData(jointCrvData)

            fingerCtrls.append(newJointCtrl)

            # Create Deformers
            jointDef = Joint(jointName, parent=self.defCmpGrp)
            fingerJoints.append(jointDef)

            # Create Constraints

            # Set Xfos
            newJointCtrlSpace.xfo = jointXfo
            newJointCtrl.xfo = jointXfo

            parentCtrl = newJointCtrl


        # =================
        # Create Operators
        # =================
        # Add Deformer KL Op
        deformersToCtrlsKLOp = KLOperator(name + 'DefConstraint', 'MultiPoseConstraintSolver', 'Kraken')
        self.addOperator(deformersToCtrlsKLOp)

        # Add Att Inputs
        deformersToCtrlsKLOp.setInput('drawDebug', self.drawDebugInputAttr)
        deformersToCtrlsKLOp.setInput('rigScale', self.rigScaleInputAttr)

        # Add Xfo Inputs
        deformersToCtrlsKLOp.setInput('constrainers', fingerCtrls)

        # Add Xfo Outputs
        deformersToCtrlsKLOp.setOutput('constrainees', fingerJoints)

        return deformersToCtrlsKLOp


    def loadData(self, data=None):
        """Load a saved guide representation from persisted data.

        Arguments:
        data -- object, The JSON data object.

        Return:
        True if successful.

        """

        super(HandComponentRig, self).loadData(data)

        # Data
        fingerData = data.get('fingerData')
        handXfo = data.get('handXfo', Xfo())

        self.handCtrlSpace.xfo = handXfo
        self.handCtrl.xfo = handXfo

        fingerOps = []
        for finger in fingerData.keys():
            fingerOp = self.addFinger(finger, fingerData[finger])
            fingerOps.append(fingerOp)

        # ============
        # Set IO Xfos
        # ============
        self.armEndInputTgt.xfo = handXfo
        self.handOutputTgt.xfo = handXfo

        # Eval Constraints
        self.armEndInputConstraint.evaluate()
        self.handOutputConstraint.evaluate()
        self.handDefConstraint.evaluate()

        # Eval Operators
        for op in fingerOps:
            op.evaluate()
class SimpleControlComponentRig(SimpleControlComponent):
    """Simple Control Component Rig"""
    def __init__(self, name='SimpleControl', parent=None):

        Profiler.getInstance().push("Construct Simple Control Rig Component:" +
                                    name)
        super(SimpleControlComponentRig, self).__init__(name, parent)

        # =========
        # Controls
        # =========
        # Add Controls
        self.mainCtrl = Control('main', shape='square', parent=self.ctrlCmpGrp)
        self.mainCtrlSpace = self.mainCtrl.insertCtrlSpace()
        self.mainCtrl.lockScale(x=True, y=True, z=True)

        # Add Component Params to Main control
        mainSrtSettingsAttrGrp = AttributeGroup('DisplayInfo_MainSrtSettings',
                                                parent=self.mainCtrl)
        self.rigScaleAttr = ScalarAttribute('rigScale',
                                            value=1.0,
                                            parent=mainSrtSettingsAttrGrp,
                                            minValue=0.1,
                                            maxValue=100.0)

        self.rigScaleOutputAttr.connect(self.rigScaleAttr)

        # ==========
        # Deformers
        # ==========
        deformersLayer = self.getOrCreateLayer('deformers')
        self.defCmpGrp = ComponentGroup(self.getName(),
                                        self,
                                        parent=deformersLayer)
        self.addItem('defCmpGrp', self.defCmpGrp)
        self.mainDef = Joint('main', parent=self.defCmpGrp)
        self.mainDef.setComponent(self)

        # ==============
        # Constrain I/O
        # ==============
        # Constrain inputs
        self.mainInputConstraint = PoseConstraint('_'.join(
            [self.mainCtrlSpace.getName(), 'To',
             self.mainInputTgt.getName()]))
        self.mainInputConstraint.setMaintainOffset(True)
        self.mainInputConstraint.addConstrainer(self.mainInputTgt)
        self.mainCtrlSpace.addConstraint(self.mainInputConstraint)

        # Constrain outputs
        self.mainOutputConstraint = PoseConstraint('_'.join(
            [self.outputTgt.getName(), 'To',
             self.mainCtrl.getName()]))
        self.mainOutputConstraint.addConstrainer(self.mainCtrl)
        self.outputTgt.addConstraint(self.mainOutputConstraint)

        # Constrain deformers
        self.mainDefConstraint = PoseConstraint('_'.join(
            [self.mainDef.getName(), 'To',
             self.mainCtrl.getName()]))
        self.mainDefConstraint.addConstrainer(self.mainCtrl)
        self.mainDef.addConstraint(self.mainDefConstraint)

        # ===============
        # Add Canvas Ops
        # ===============

        Profiler.getInstance().pop()

    def loadData(self, data=None):
        """Load a saved guide representation from persisted data.

        Arguments:
        data -- object, The JSON data object.

        Return:
        True if successful.

        """

        super(SimpleControlComponentRig, self).loadData(data)

        ctrlSize = data.get('ctrlSize', 1.0)
        ctrlXfo = data.get('ctrlXfo', Xfo())

        # ================
        # Resize Controls
        # ================
        self.mainCtrl.setShape('square')
        self.mainCtrl.rotatePoints(90, 0, 0)
        self.mainCtrl.scalePoints(Vec3(ctrlSize, ctrlSize, ctrlSize))

        # =======================
        # Set Control Transforms
        # =======================
        self.mainCtrlSpace.xfo = ctrlXfo
        self.mainCtrl.xfo = ctrlXfo

        # ============
        # Set IO Xfos
        # ============
        self.mainInputTgt.xfo = ctrlXfo
        self.mainDef.xfo = ctrlXfo
        self.outputTgt.xfo = ctrlXfo

        # ====================
        # Evaluate Constraints
        # ====================
        self.mainInputConstraint.evaluate()
        self.mainOutputConstraint.evaluate()
        self.mainDefConstraint.evaluate()
Example #3
0
class FabriceSpineRig(FabriceSpine):
    """Fabrice Spine Component"""
    def __init__(self, name="spine", parent=None):

        Profiler.getInstance().push("Construct Spine Rig Component:" + name)
        super(FabriceSpineRig, self).__init__(name, parent)

        # =========
        # Controls
        # =========
        # COG
        self.cogCtrlSpace = CtrlSpace('cog', parent=self.ctrlCmpGrp)
        self.cogCtrl = Control('cog', parent=self.cogCtrlSpace, shape="circle")
        self.cogCtrl.rotatePoints(90, 0, 0)
        self.cogCtrl.scalePoints(Vec3(3.0, 3.0, 3.0))
        self.cogCtrl.translatePoints(Vec3(0.0, 0.0, 0.2))
        self.cogCtrl.lockScale(x=True, y=True, z=True)
        self.cogCtrl.setColor("orange")

        # Spine Base
        self.spineBaseCtrlSpace = CtrlSpace('spineBase', parent=self.cogCtrl)
        self.spineBaseCtrl = Control('spineBase',
                                     parent=self.spineBaseCtrlSpace,
                                     shape="pin")
        self.spineBaseCtrl.rotatePoints(90, 0, 0)
        self.spineBaseCtrl.translatePoints(Vec3(0, 1.0, 0))
        self.spineBaseCtrl.lockScale(x=True, y=True, z=True)

        # Spine Base Handle
        self.spineBaseHandleCtrlSpace = CtrlSpace('spineBaseHandle',
                                                  parent=self.spineBaseCtrl)
        self.spineBaseHandleCtrl = Control(
            'spineBaseHandle',
            parent=self.spineBaseHandleCtrlSpace,
            shape="pin")
        self.spineBaseHandleCtrl.rotatePoints(90, 0, 0)
        self.spineBaseHandleCtrl.translatePoints(Vec3(0, 1.0, 0))
        self.spineBaseHandleCtrl.lockScale(x=True, y=True, z=True)
        self.spineBaseHandleCtrl.setColor("orange")

        # Spine End
        self.spineEndCtrlSpace = CtrlSpace('spineEnd', parent=self.cogCtrl)
        self.spineEndCtrl = Control('spineEnd',
                                    parent=self.spineEndCtrlSpace,
                                    shape="pin")
        self.spineEndCtrl.rotatePoints(90, 0, 0)
        self.spineEndCtrl.lockScale(x=True, y=True, z=True)
        self.spineEndCtrl.translatePoints(Vec3(0, 1.0, 0))

        # Spine End Handle
        self.spineEndHandleCtrlSpace = CtrlSpace('spineEndHandle',
                                                 parent=self.spineEndCtrl)
        self.spineEndHandleCtrl = Control('spineEndHandle',
                                          parent=self.spineEndHandleCtrlSpace,
                                          shape="pin")
        self.spineEndHandleCtrl.rotatePoints(90, 0, 0)
        self.spineEndHandleCtrl.translatePoints(Vec3(0, 1.0, 0))
        self.spineEndHandleCtrl.lockScale(x=True, y=True, z=True)
        self.spineEndHandleCtrl.setColor("orange")

        # ==========
        # Deformers
        # ==========
        deformersLayer = self.getOrCreateLayer('deformers')
        self.defCmpGrp = ComponentGroup(self.getName(),
                                        self,
                                        parent=deformersLayer)
        self.chestDef = Joint('chest', parent=self.defCmpGrp)
        self.chestDef.setComponent(self)

        self.deformerJoints = []
        self.spineOutputs = []
        self.setNumDeformers(1)

        # =====================
        # Create Component I/O
        # =====================
        # Setup component Xfo I/O's
        self.spineVertebraeOutput.setTarget(self.spineOutputs)

        # =====================
        # Constraint Deformers
        # =====================
        self.chestDefConstraint = PoseConstraint('_'.join(
            [self.chestDef.getName(), 'To',
             self.spineBaseOutputTgt.getName()]))
        self.chestDefConstraint.addConstrainer(self.spineBaseOutputTgt)
        self.chestDef.addConstraint(self.chestDefConstraint)

        # ==============
        # Constrain I/O
        # ==============

        # Constraint inputs
        self.spineSrtInputConstraint = PoseConstraint('_'.join([
            self.cogCtrlSpace.getName(), 'To',
            self.spineMainSrtInputTgt.getName()
        ]))
        self.spineSrtInputConstraint.addConstrainer(self.spineMainSrtInputTgt)
        self.spineSrtInputConstraint.setMaintainOffset(True)
        self.cogCtrlSpace.addConstraint(self.spineSrtInputConstraint)

        # Constraint outputs
        self.spineCogOutputConstraint = PoseConstraint('_'.join(
            [self.spineCogOutputTgt.getName(), 'To',
             self.cogCtrl.getName()]))
        self.spineCogOutputConstraint.addConstrainer(self.cogCtrl)
        self.spineCogOutputTgt.addConstraint(self.spineCogOutputConstraint)

        # Spine Base
        self.spineBaseOutputPosConstraint = PositionConstraint('_'.join([
            self.spineBaseOutputTgt.getName(), 'PosTo',
            self.spineOutputs[0].getName()
        ]))
        self.spineBaseOutputPosConstraint.addConstrainer(self.spineOutputs[0])
        self.spineBaseOutputTgt.addConstraint(
            self.spineBaseOutputPosConstraint)

        self.spineBaseOutputOriConstraint = OrientationConstraint('_'.join([
            self.spineBaseOutputTgt.getName(), 'PosTo',
            self.cogCtrl.getName()
        ]))
        self.spineBaseOutputOriConstraint.addConstrainer(self.cogCtrl)
        self.spineBaseOutputTgt.addConstraint(
            self.spineBaseOutputOriConstraint)

        # Spine End
        self.spineEndOutputConstraint = PoseConstraint('_'.join(
            [self.spineEndOutputTgt.getName(), 'To', 'spineEnd']))
        self.spineEndOutputConstraint.addConstrainer(self.spineOutputs[0])
        self.spineEndOutputTgt.addConstraint(self.spineEndOutputConstraint)

        self.spineEndCtrlOutputConstraint = PoseConstraint('_'.join([
            self.spineEndCtrlOutputTgt.getName(), 'To',
            self.spineEndCtrl.getName()
        ]))
        self.spineEndCtrlOutputConstraint.addConstrainer(self.spineEndCtrl)
        self.spineEndCtrlOutputTgt.addConstraint(
            self.spineEndCtrlOutputConstraint)

        # ===============
        # Add Splice Ops
        # ===============
        # Add Spine Splice Op
        self.bezierSpineSpliceOp = SpliceOperator('spineSpliceOp',
                                                  'BezierSpineSolver',
                                                  'Kraken')
        self.addOperator(self.bezierSpineSpliceOp)

        # Add Att Inputs
        self.bezierSpineSpliceOp.setInput('drawDebug', self.drawDebugInputAttr)
        self.bezierSpineSpliceOp.setInput('rigScale', self.rigScaleInputAttr)
        self.bezierSpineSpliceOp.setInput('length', self.lengthInputAttr)

        # Add Xfo Inputs
        self.bezierSpineSpliceOp.setInput('base', self.spineBaseCtrl)
        self.bezierSpineSpliceOp.setInput('baseHandle',
                                          self.spineBaseHandleCtrl)
        self.bezierSpineSpliceOp.setInput('tipHandle', self.spineEndHandleCtrl)
        self.bezierSpineSpliceOp.setInput('tip', self.spineEndCtrl)

        # Add Xfo Outputs
        self.bezierSpineSpliceOp.setOutput('outputs', self.spineOutputs)

        # Add Deformer Splice Op
        self.deformersToOutputsSpliceOp = SpliceOperator(
            'spineDeformerSpliceOp',
            'MultiPoseConstraintSolver',
            'Kraken',
            alwaysEval=True)
        self.addOperator(self.deformersToOutputsSpliceOp)

        # Add Att Inputs
        self.deformersToOutputsSpliceOp.setInput('drawDebug',
                                                 self.drawDebugInputAttr)
        self.deformersToOutputsSpliceOp.setInput('rigScale',
                                                 self.rigScaleInputAttr)

        # Add Xfo Outputs
        self.deformersToOutputsSpliceOp.setInput('constrainers',
                                                 self.spineOutputs)

        # Add Xfo Outputs
        self.deformersToOutputsSpliceOp.setOutput('constrainees',
                                                  self.deformerJoints)

        Profiler.getInstance().pop()

    def setNumDeformers(self, numDeformers):

        # Add new deformers and outputs
        for i in xrange(len(self.spineOutputs), numDeformers):
            name = 'spine' + str(i + 1).zfill(2)
            spineOutput = ComponentOutput(name, parent=self.outputHrcGrp)
            self.spineOutputs.append(spineOutput)

        for i in xrange(len(self.deformerJoints), numDeformers):
            name = 'spine' + str(i + 1).zfill(2)
            spineDef = Joint(name, parent=self.defCmpGrp)
            spineDef.setComponent(self)
            self.deformerJoints.append(spineDef)

        return True

    def loadData(self, data=None):
        """Load a saved guide representation from persisted data.

        Arguments:
        data -- object, The JSON data object.

        Return:
        True if successful.

        """

        super(FabriceSpineRig, self).loadData(data)

        # Get Data
        cogPos = data['cogPos']
        cogCtrlCrvData = data['cogCtrlCrvData']

        spineBasePos = data['spineBasePos']
        spineBaseCtrlCrvData = data['spineBaseCtrlCrvData']

        spineBaseHandlePos = data['spineBaseHandlePos']
        spineBaseHandleCtrlCrvData = data['spineBaseHandleCtrlCrvData']

        spineEndHandlePos = data['spineEndHandlePos']
        spineEndHandleCtrlCrvData = data['spineEndHandleCtrlCrvData']

        spineEndPos = data['spineEndPos']
        spineEndCtrlCrvData = data['spineEndCtrlCrvData']

        numDeformers = data['numDeformers']

        # Set Xfos
        self.cogCtrlSpace.xfo.tr = cogPos
        self.cogCtrl.xfo.tr = cogPos
        self.cogCtrl.setCurveData(cogCtrlCrvData)

        self.spineBaseCtrlSpace.xfo.tr = spineBasePos
        self.spineBaseCtrl.xfo.tr = spineBasePos
        self.spineBaseCtrl.setCurveData(spineBaseCtrlCrvData)

        self.spineBaseHandleCtrlSpace.xfo.tr = spineBaseHandlePos
        self.spineBaseHandleCtrl.xfo.tr = spineBaseHandlePos
        self.spineBaseHandleCtrl.setCurveData(spineBaseHandleCtrlCrvData)

        self.spineEndHandleCtrlSpace.xfo.tr = spineEndHandlePos
        self.spineEndHandleCtrl.xfo.tr = spineEndHandlePos
        self.spineEndHandleCtrl.setCurveData(spineEndHandleCtrlCrvData)

        self.spineEndCtrlSpace.xfo.tr = spineEndPos
        self.spineEndCtrl.xfo.tr = spineEndPos
        self.spineEndCtrl.setCurveData(spineEndCtrlCrvData)

        length = spineBasePos.distanceTo(
            spineBaseHandlePos) + spineBaseHandlePos.distanceTo(
                spineEndHandlePos) + spineEndHandlePos.distanceTo(spineEndPos)
        self.lengthInputAttr.setMax(length * 3.0)
        self.lengthInputAttr.setValue(length)

        # Update number of deformers and outputs
        self.setNumDeformers(numDeformers)

        # Updating constraint to use the updated last output.
        self.spineEndOutputConstraint.setConstrainer(self.spineOutputs[-1],
                                                     index=0)

        # ============
        # Set IO Xfos
        # ============

        # ====================
        # Evaluate Splice Ops
        # ====================
        # evaluate the spine op so that all the output transforms are updated.
        self.bezierSpineSpliceOp.evaluate()

        # evaluate the constraint op so that all the joint transforms are updated.
        self.deformersToOutputsSpliceOp.evaluate()

        # evaluate the constraints to ensure the outputs are now in the correct location.
        self.spineSrtInputConstraint.evaluate()
        self.spineCogOutputConstraint.evaluate()
        self.spineBaseOutputPosConstraint.evaluate()
        self.spineBaseOutputOriConstraint.evaluate()
        self.spineEndOutputConstraint.evaluate()
        self.spineEndCtrlOutputConstraint.evaluate()
Example #4
0
class FabriceSpineRig(FabriceSpine):
    """Fabrice Spine Component"""

    def __init__(self, name="spine", parent=None):

        Profiler.getInstance().push("Construct Spine Rig Component:" + name)
        super(FabriceSpineRig, self).__init__(name, parent)


        # =========
        # Controls
        # =========
        # COG
        self.cogCtrlSpace = CtrlSpace('cog', parent=self.ctrlCmpGrp)
        self.cogCtrl = Control('cog', parent=self.cogCtrlSpace, shape="circle")
        self.cogCtrl.rotatePoints(90, 0, 0)
        self.cogCtrl.scalePoints(Vec3(3.0, 3.0, 3.0))
        self.cogCtrl.translatePoints(Vec3(0.0, 0.0, 0.2))
        self.cogCtrl.lockScale(x=True, y=True, z=True)
        self.cogCtrl.setColor("orange")

        # Spine Base
        self.spineBaseCtrlSpace = CtrlSpace('spineBase', parent=self.cogCtrl)
        self.spineBaseCtrl = Control('spineBase', parent=self.spineBaseCtrlSpace, shape="pin")
        self.spineBaseCtrl.rotatePoints(90, 0, 0)
        self.spineBaseCtrl.translatePoints(Vec3(0, 1.0, 0))
        self.spineBaseCtrl.lockScale(x=True, y=True, z=True)

        # Spine Base Handle
        self.spineBaseHandleCtrlSpace = CtrlSpace('spineBaseHandle', parent=self.spineBaseCtrl)
        self.spineBaseHandleCtrl = Control('spineBaseHandle', parent=self.spineBaseHandleCtrlSpace, shape="pin")
        self.spineBaseHandleCtrl.rotatePoints(90, 0, 0)
        self.spineBaseHandleCtrl.translatePoints(Vec3(0, 1.0, 0))
        self.spineBaseHandleCtrl.lockScale(x=True, y=True, z=True)
        self.spineBaseHandleCtrl.setColor("orange")

        # Spine End
        self.spineEndCtrlSpace = CtrlSpace('spineEnd', parent=self.cogCtrl)
        self.spineEndCtrl = Control('spineEnd', parent=self.spineEndCtrlSpace, shape="pin")
        self.spineEndCtrl.rotatePoints(90, 0, 0)
        self.spineEndCtrl.lockScale(x=True, y=True, z=True)
        self.spineEndCtrl.translatePoints(Vec3(0, 1.0, 0))

        # Spine End Handle
        self.spineEndHandleCtrlSpace = CtrlSpace('spineEndHandle', parent=self.spineEndCtrl)
        self.spineEndHandleCtrl = Control('spineEndHandle', parent=self.spineEndHandleCtrlSpace, shape="pin")
        self.spineEndHandleCtrl.rotatePoints(90, 0, 0)
        self.spineEndHandleCtrl.translatePoints(Vec3(0, 1.0, 0))
        self.spineEndHandleCtrl.lockScale(x=True, y=True, z=True)
        self.spineEndHandleCtrl.setColor("orange")


        # ==========
        # Deformers
        # ==========
        deformersLayer = self.getOrCreateLayer('deformers')
        self.defCmpGrp = ComponentGroup(self.getName(), self, parent=deformersLayer)
        self.chestDef = Joint('chest', parent=self.defCmpGrp)
        self.chestDef.setComponent(self)

        self.deformerJoints = []
        self.spineOutputs = []
        self.setNumDeformers(1)


        # =====================
        # Create Component I/O
        # =====================
        # Setup component Xfo I/O's
        self.spineVertebraeOutput.setTarget(self.spineOutputs)

        # =====================
        # Constraint Deformers
        # =====================
        self.chestDefConstraint = PoseConstraint('_'.join([self.chestDef.getName(), 'To', self.spineBaseOutputTgt.getName()]))
        self.chestDefConstraint.addConstrainer(self.spineBaseOutputTgt)
        self.chestDef.addConstraint(self.chestDefConstraint)

        # ==============
        # Constrain I/O
        # ==============

        # Constraint inputs
        self.spineSrtInputConstraint = PoseConstraint('_'.join([self.cogCtrlSpace.getName(), 'To', self.spineMainSrtInputTgt.getName()]))
        self.spineSrtInputConstraint.addConstrainer(self.spineMainSrtInputTgt)
        self.spineSrtInputConstraint.setMaintainOffset(True)
        self.cogCtrlSpace.addConstraint(self.spineSrtInputConstraint)

        # Constraint outputs
        self.spineCogOutputConstraint = PoseConstraint('_'.join([self.spineCogOutputTgt.getName(), 'To', self.cogCtrl.getName()]))
        self.spineCogOutputConstraint.addConstrainer(self.cogCtrl)
        self.spineCogOutputTgt.addConstraint(self.spineCogOutputConstraint)

        # Spine Base
        self.spineBaseOutputPosConstraint = PositionConstraint('_'.join([self.spineBaseOutputTgt.getName(), 'PosTo', self.spineOutputs[0].getName()]))
        self.spineBaseOutputPosConstraint.addConstrainer(self.spineOutputs[0])
        self.spineBaseOutputTgt.addConstraint(self.spineBaseOutputPosConstraint)

        self.spineBaseOutputOriConstraint = OrientationConstraint('_'.join([self.spineBaseOutputTgt.getName(), 'PosTo', self.cogCtrl.getName()]))
        self.spineBaseOutputOriConstraint.addConstrainer(self.cogCtrl)
        self.spineBaseOutputTgt.addConstraint(self.spineBaseOutputOriConstraint)

        # Spine End
        self.spineEndOutputConstraint = PoseConstraint('_'.join([self.spineEndOutputTgt.getName(), 'To', 'spineEnd']))
        self.spineEndOutputConstraint.addConstrainer(self.spineOutputs[0])
        self.spineEndOutputTgt.addConstraint(self.spineEndOutputConstraint)

        self.spineEndCtrlOutputConstraint = PoseConstraint('_'.join([self.spineEndCtrlOutputTgt.getName(), 'To', self.spineEndCtrl.getName()]))
        self.spineEndCtrlOutputConstraint.addConstrainer(self.spineEndCtrl)
        self.spineEndCtrlOutputTgt.addConstraint(self.spineEndCtrlOutputConstraint)


        # ===============
        # Add Splice Ops
        # ===============
        # Add Spine Splice Op
        self.bezierSpineSpliceOp = SpliceOperator('spineSpliceOp', 'BezierSpineSolver', 'Kraken')
        self.addOperator(self.bezierSpineSpliceOp)

        # Add Att Inputs
        self.bezierSpineSpliceOp.setInput('drawDebug', self.drawDebugInputAttr)
        self.bezierSpineSpliceOp.setInput('rigScale', self.rigScaleInputAttr)
        self.bezierSpineSpliceOp.setInput('length', self.lengthInputAttr)

        # Add Xfo Inputs
        self.bezierSpineSpliceOp.setInput('base', self.spineBaseCtrl)
        self.bezierSpineSpliceOp.setInput('baseHandle', self.spineBaseHandleCtrl)
        self.bezierSpineSpliceOp.setInput('tipHandle', self.spineEndHandleCtrl)
        self.bezierSpineSpliceOp.setInput('tip', self.spineEndCtrl)

        # Add Xfo Outputs
        self.bezierSpineSpliceOp.setOutput('outputs', self.spineOutputs)

        # Add Deformer Splice Op
        self.deformersToOutputsSpliceOp = SpliceOperator('spineDeformerSpliceOp', 'MultiPoseConstraintSolver', 'Kraken', alwaysEval=True)
        self.addOperator(self.deformersToOutputsSpliceOp)

        # Add Att Inputs
        self.deformersToOutputsSpliceOp.setInput('drawDebug', self.drawDebugInputAttr)
        self.deformersToOutputsSpliceOp.setInput('rigScale', self.rigScaleInputAttr)

        # Add Xfo Outputs
        self.deformersToOutputsSpliceOp.setInput('constrainers', self.spineOutputs)

        # Add Xfo Outputs
        self.deformersToOutputsSpliceOp.setOutput('constrainees', self.deformerJoints)

        Profiler.getInstance().pop()


    def setNumDeformers(self, numDeformers):

        # Add new deformers and outputs
        for i in xrange(len(self.spineOutputs), numDeformers):
            name = 'spine' + str(i + 1).zfill(2)
            spineOutput = ComponentOutput(name, parent=self.outputHrcGrp)
            self.spineOutputs.append(spineOutput)

        for i in xrange(len(self.deformerJoints), numDeformers):
            name = 'spine' + str(i + 1).zfill(2)
            spineDef = Joint(name, parent=self.defCmpGrp)
            spineDef.setComponent(self)
            self.deformerJoints.append(spineDef)

        return True


    def loadData(self, data=None):
        """Load a saved guide representation from persisted data.

        Arguments:
        data -- object, The JSON data object.

        Return:
        True if successful.

        """

        super(FabriceSpineRig, self).loadData( data )

        # Get Data
        cogPos = data['cogPos']
        cogCtrlCrvData = data['cogCtrlCrvData']

        spineBasePos = data['spineBasePos']
        spineBaseCtrlCrvData = data['spineBaseCtrlCrvData']

        spineBaseHandlePos = data['spineBaseHandlePos']
        spineBaseHandleCtrlCrvData = data['spineBaseHandleCtrlCrvData']

        spineEndHandlePos = data['spineEndHandlePos']
        spineEndHandleCtrlCrvData = data['spineEndHandleCtrlCrvData']

        spineEndPos = data['spineEndPos']
        spineEndCtrlCrvData = data['spineEndCtrlCrvData']

        numDeformers = data['numDeformers']

        # Set Xfos
        self.cogCtrlSpace.xfo.tr = cogPos
        self.cogCtrl.xfo.tr = cogPos
        self.cogCtrl.setCurveData(cogCtrlCrvData)

        self.spineBaseCtrlSpace.xfo.tr = spineBasePos
        self.spineBaseCtrl.xfo.tr = spineBasePos
        self.spineBaseCtrl.setCurveData(spineBaseCtrlCrvData)

        self.spineBaseHandleCtrlSpace.xfo.tr = spineBaseHandlePos
        self.spineBaseHandleCtrl.xfo.tr = spineBaseHandlePos
        self.spineBaseHandleCtrl.setCurveData(spineBaseHandleCtrlCrvData)

        self.spineEndHandleCtrlSpace.xfo.tr = spineEndHandlePos
        self.spineEndHandleCtrl.xfo.tr = spineEndHandlePos
        self.spineEndHandleCtrl.setCurveData(spineEndHandleCtrlCrvData)

        self.spineEndCtrlSpace.xfo.tr = spineEndPos
        self.spineEndCtrl.xfo.tr = spineEndPos
        self.spineEndCtrl.setCurveData(spineEndCtrlCrvData)

        length = spineBasePos.distanceTo(spineBaseHandlePos) + spineBaseHandlePos.distanceTo(spineEndHandlePos) + spineEndHandlePos.distanceTo(spineEndPos)
        self.lengthInputAttr.setMax(length * 3.0)
        self.lengthInputAttr.setValue(length)

        # Update number of deformers and outputs
        self.setNumDeformers(numDeformers)

        # Updating constraint to use the updated last output.
        self.spineEndOutputConstraint.setConstrainer(self.spineOutputs[-1], index=0)

        # ============
        # Set IO Xfos
        # ============

        # ====================
        # Evaluate Splice Ops
        # ====================
        # evaluate the spine op so that all the output transforms are updated.
        self.bezierSpineSpliceOp.evaluate()

        # evaluate the constraint op so that all the joint transforms are updated.
        self.deformersToOutputsSpliceOp.evaluate()

        # evaluate the constraints to ensure the outputs are now in the correct location.
        self.spineSrtInputConstraint.evaluate()
        self.spineCogOutputConstraint.evaluate()
        self.spineBaseOutputPosConstraint.evaluate()
        self.spineBaseOutputOriConstraint.evaluate()
        self.spineEndOutputConstraint.evaluate()
        self.spineEndCtrlOutputConstraint.evaluate()
Example #5
0
class HandComponentRig(HandComponent):
    """Hand Component"""

    def __init__(self, name='Hand', parent=None):

        Profiler.getInstance().push("Construct Hand Rig Component:" + name)
        super(HandComponentRig, self).__init__(name, parent)


        # =========
        # Controls
        # =========
        # Hand
        self.handCtrlSpace = CtrlSpace('hand', parent=self.ctrlCmpGrp)
        self.handCtrl = Control('hand', parent=self.handCtrlSpace, shape="square")
        self.handCtrl.rotatePoints(0, 0, 90.0)
        self.handCtrl.lockScale(True, True, True)
        self.handCtrl.lockTranslation(True, True, True)


        # ==========
        # Deformers
        # ==========
        self.deformersLayer = self.getOrCreateLayer('deformers')
        self.defCmpGrp = ComponentGroup(self.getName(), self, parent=self.deformersLayer)
        self.addItem('defCmpGrp', self.defCmpGrp)

        self.handDef = Joint('hand', parent=self.defCmpGrp)
        self.handDef.setComponent(self)


        # ==============
        # Constrain I/O
        # ==============
        # Constraint inputs
        self.armEndInputConstraint = PoseConstraint('_'.join([self.handCtrlSpace.getName(), 'To', self.armEndInputTgt.getName()]))
        self.armEndInputConstraint.setMaintainOffset(True)
        self.armEndInputConstraint.addConstrainer(self.armEndInputTgt)
        self.handCtrlSpace.addConstraint(self.armEndInputConstraint)

        # Constraint outputs
        self.handOutputConstraint = PoseConstraint('_'.join([self.handOutputTgt.getName(), 'To', self.handCtrl.getName()]))
        self.handOutputConstraint.addConstrainer(self.handCtrl)
        self.handOutputTgt.addConstraint(self.handOutputConstraint)

        # Constraint deformers
        self.handDefConstraint = PoseConstraint('_'.join([self.handDef.getName(), 'To', self.handCtrl.getName()]))
        self.handDefConstraint.addConstrainer(self.handCtrl)
        self.handDef.addConstraint(self.handDefConstraint)

        Profiler.getInstance().pop()


    def addFinger(self, name, data):

        fingerCtrls = []
        fingerJoints = []

        parentCtrl = self.handCtrl
        for i, joint in enumerate(data):
            if i == 0:
                jointName = name + 'Meta'
            else:
                jointName = name + str(i).zfill(2)

            jointXfo = joint.get('xfo', Xfo())
            jointCrvData = joint.get('curveData')

            # Create Controls
            newJointCtrlSpace = CtrlSpace(jointName, parent=parentCtrl)
            newJointCtrl = Control(jointName, parent=newJointCtrlSpace, shape='square')
            newJointCtrl.lockScale(True, True, True)
            newJointCtrl.lockTranslation(True, True, True)

            if jointCrvData is not None:
                newJointCtrl.setCurveData(jointCrvData)

            fingerCtrls.append(newJointCtrl)

            # Create Deformers
            jointDef = Joint(jointName, parent=self.defCmpGrp)
            fingerJoints.append(jointDef)

            # Create Constraints

            # Set Xfos
            newJointCtrlSpace.xfo = jointXfo
            newJointCtrl.xfo = jointXfo

            parentCtrl = newJointCtrl


        # =================
        # Create Operators
        # =================
        # Add Deformer KL Op
        deformersToCtrlsKLOp = KLOperator(name + 'DeformerKLOp', 'MultiPoseConstraintSolver', 'Kraken')
        self.addOperator(deformersToCtrlsKLOp)

        # Add Att Inputs
        deformersToCtrlsKLOp.setInput('drawDebug', self.drawDebugInputAttr)
        deformersToCtrlsKLOp.setInput('rigScale', self.rigScaleInputAttr)

        # Add Xfo Inputs
        deformersToCtrlsKLOp.setInput('constrainers', fingerCtrls)

        # Add Xfo Outputs
        deformersToCtrlsKLOp.setOutput('constrainees', fingerJoints)

        return deformersToCtrlsKLOp


    def loadData(self, data=None):
        """Load a saved guide representation from persisted data.

        Arguments:
        data -- object, The JSON data object.

        Return:
        True if successful.

        """

        super(HandComponentRig, self).loadData(data)

        # Data
        fingerData = data.get('fingerData')
        handXfo = data.get('handXfo', Xfo())

        self.handCtrlSpace.xfo = handXfo
        self.handCtrl.xfo = handXfo

        fingerOps = []
        for finger in fingerData.keys():
            fingerOp = self.addFinger(finger, fingerData[finger])
            fingerOps.append(fingerOp)

        # ============
        # Set IO Xfos
        # ============
        self.armEndInputTgt.xfo = handXfo
        self.handOutputTgt.xfo = handXfo

        # Eval Constraints
        self.armEndInputConstraint.evaluate()
        self.handOutputConstraint.evaluate()
        self.handDefConstraint.evaluate()

        # Eval Operators
        for op in fingerOps:
            op.evaluate()