def constrainTo(self, constrainers, constraintType="Pose", maintainOffset=False, name=None): """Adds an constraint to this object. Args: constrainers (Object or Object list): Constraint object to add to this object or objects. constraintType (str): String name of the constraint type. maintainOffset (bool): Sets the constraint to maintain offset when creating the constraint. name (str): Name of the constraint. If set to None, a name is automatically generated. Returns: string: Constraint object """ if name is None: constraintName = "" if hasattr(constrainers, '__iter__'): constraintName = '_'.join([self.getName(), 'To', constrainers[0].getName(), constraintType + 'Constraint']) else: constraintName = '_'.join([self.getName(), 'To', constrainers.getName(), constraintType + 'Constraint']) else: constraintName = name constraint = None if constraintType == "Orientation": constraint = OrientationConstraint(constraintName) elif constraintType == "Pose": constraint = PoseConstraint(constraintName) elif constraintType == "Position": constraint = PositionConstraint(constraintName) elif constraintType == "Scale": constraint = ScaleConstraint(constraintName) else: raise ValueError("'" + constraintType + "' is not a valid constraint type. Valid types are Orientation, Pose, Position, or Scale") # Accept a single object or a list of objects if hasattr(constrainers, '__iter__'): pass else: constrainers = [constrainers] for constrainer in constrainers: constraint.addConstrainer(constrainer) constraint.setMaintainOffset(maintainOffset) self.addConstraint(constraint) return constraint
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()
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()
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()
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()
def construct(self, jsonData): """Returns a constructed scene item based on the provided json data. Args: jsondata (dict): the JSON data to use to decode into a Math value. Returns: object: The constructed scene item. """ if '__typeHierarchy__' not in jsonData or 'name' not in jsonData: raise Exception("Invalid JSON data for constructing scene item:" + str(jsonData)) # ========= # Controls # ========= if "Control" in jsonData['__typeHierarchy__']: item = Control(jsonData['name']) # =========== # Attributes # =========== elif "AttributeGroup" in jsonData['__typeHierarchy__']: item = AttributeGroup(jsonData['name']) elif "BoolAttribute" in jsonData['__typeHierarchy__']: item = BoolAttribute(jsonData['name']) elif "ScalarAttribute" in jsonData['__typeHierarchy__']: item = ScalarAttribute(jsonData['name']) elif "IntegerAttribute" in jsonData['__typeHierarchy__']: item = IntegerAttribute(jsonData['name']) elif "StringAttribute" in jsonData['__typeHierarchy__']: item = StringAttribute(jsonData['name']) elif "ComponentInput" in jsonData['__typeHierarchy__']: item = ComponentInput(jsonData['name']) elif "ComponentOutput" in jsonData['__typeHierarchy__']: item = ComponentOutput(jsonData['name']) # ============ # Constraints # ============ elif "OrientationConstraint" in jsonData['__typeHierarchy__']: item = OrientationConstraint(jsonData['name']) elif "PoseConstraint" in jsonData['__typeHierarchy__']: item = PoseConstraint(jsonData['name']) elif "PositionConstraint" in jsonData['__typeHierarchy__']: item = PositionConstraint(jsonData['name']) elif "ScaleConstraint" in jsonData['__typeHierarchy__']: item = ScaleConstraint(jsonData['name']) elif "BaseConstraint" in jsonData['__typeHierarchy__']: item = BaseConstraint(jsonData['name']) # ========== # Operators # ========== elif "KLOperator" in jsonData['__typeHierarchy__']: item = KLOperator(jsonData['name']) # ============ # Scene Items # ============ elif "Group" in jsonData['__typeHierarchy__']: item = Group(jsonData['name']) elif "Null" in jsonData['__typeHierarchy__']: item = Null(jsonData['name']) elif "Chain" in jsonData['__typeHierarchy__']: item = Chain(jsonData['name']) elif "Joint" in jsonData['__typeHierarchy__']: item = Joint(jsonData['name']) elif "Container" in jsonData['__typeHierarchy__']: item = Container(jsonData['name']) elif "Curve" in jsonData['__typeHierarchy__']: item = Curve(jsonData['name']) elif "HierarchyGroup" in jsonData['__typeHierarchy__']: item = HierarchyGroup(jsonData['name']) elif "Joint" in jsonData['__typeHierarchy__']: item = Joint(jsonData['name']) elif "Layer" in jsonData['__typeHierarchy__']: item = Layer(jsonData['name']) elif "Locator" in jsonData['__typeHierarchy__']: item = Locator(jsonData['name']) elif "SceneItem" in jsonData['__typeHierarchy__']: item = SceneItem(jsonData['name']) else: raise Exception("KrakenLoader does not support the given type:" + jsonData['__typeHierarchy__']) # Before registering or decoding, set the parent so that the full name contains the entire path. if len(self.parentItems) > 0: item.setParent(self.parentItems[-1]) self.registerItem(item) # Store the item as the parent item before decoding the object # which in turn decodes the children items. self.parentItems.append(item) item.jsonDecode(self, jsonData) # Pop the parent item stack, which reverts the current parent item # to the previous value. self.parentItems.pop() return item
def constrainTo(self, constrainers, constraintType="Pose", maintainOffset=False, name=None): """Adds an constraint to this object. Args: constrainers (Object or Object list): Constraint object to add to this object or objects. constraintType (str): String name of the constraint type. maintainOffset (bool): Sets the constraint to maintain offset when creating the constraint. name (str): Name of the constraint. If set to None, a name is automatically generated. Returns: string: Constraint object """ if name is None: constraintName = "" if hasattr(constrainers, '__iter__'): constraintName = '_'.join([ self.getName(), 'To', constrainers[0].getName(), constraintType + 'Constraint' ]) else: constraintName = '_'.join([ self.getName(), 'To', constrainers.getName(), constraintType + 'Constraint' ]) else: constraintName = name constraint = None if constraintType == "Orientation": constraint = OrientationConstraint(constraintName) elif constraintType == "Pose": constraint = PoseConstraint(constraintName) elif constraintType == "Position": constraint = PositionConstraint(constraintName) elif constraintType == "Scale": constraint = ScaleConstraint(constraintName) else: raise ValueError( "'" + constraintType + "' is not a valid constraint type. Valid types are Orientation, Pose, Position, or Scale" ) # Accept a single object or a list of objects if hasattr(constrainers, '__iter__'): pass else: constrainers = [constrainers] for constrainer in constrainers: constraint.addConstrainer(constrainer) constraint.setMaintainOffset(maintainOffset) self.addConstraint(constraint) return constraint