Home Technical Talk

Maya Scripting: Storing Rigging Information in Notes

polycounter lvl 4
Offline / Send Message
Recursive Sweatpants polycounter lvl 4
I am working on a script to generate modular rig segments such as IK/FK chains and other such controls to drive a result skeleton. After creating a rig segment, the user can then take those controls and add space-switching parents to them to make that part of the rig constrained to whatever parents the user wants. The script also has a derigging feature to remove the rig from the result joints cleanly; it can easily be reapplied, but currently the user has to set up all of the space-switching parents every time.

I have an idea for the derigging function to note which space-switching parents the rig segment has (if any) and stores that information in the result joints, so that when the script reapplies the rig, it can automatically reapply the space-switching parents as well.

Right now, I'm considering storing this information in the result joint's Notes attribute, but I would like to make sure that this isn't a bad idea before going through with it. Could I use a joint's Notes attribute to store rigging information for a rigging script, or should I store it in a custom attribute, if at all?

Replies

  • ProperSquid
    Options
    Offline / Send Message
    ProperSquid polycounter lvl 12
    I recommend storing it in a custom attribute. Notes may be used for other things, so you generally don't have to worry about anybody touching your attribute. If you're writing the attributes in Python (I highly recommend doing this), then you can use a format such as json to store your data in the attributes.

    Example:
    <div># Do your imports
    import json
    
    from maya import cmds
    
    # Globals to make your life easier
    RIG_ATTRIBUTE = "recursiveRigData"
    
    # Write the attribute to the node
    def write_rig_data(node, data):
        # Quick note, I don't have Maya on this machine, so everything should work,
        # but no promises.
        if not cmds.attributeQuery(RIG_ATTRIBUTE, node=node, exists=True):
            cmds.addAttr(node, longName=RIG_ATTRIBUTE, dataType="string")
    
        # Unlock the attribute, set the data, then lock it again.
        attr = "{}.{}".format(node, RIG_ATTRIBUTE)
        cmds.setAttr(attr, lock=False)
        cmds.setAttr(attr, json.dumps(data), type="string")
        cmds.setAttr(attr, lock=True)
    
    
    def read_rig_data(node):
        # I'm assuming that the data you pass into the attribute is always a
        # dictionary. I recommend this because you can store a lot of information in
        # the attribute this way.
        if not cmds.attributeQuery(RIG_ATTRIBUTE, node=node, exists=True):
            return {}
    
        attr = "{}.{}".format(node, RIG_ATTRIBUTE)
        return json.loads(cmds.getAttr(attr))<br></div>
  • Recursive Sweatpants
    Options
    Offline / Send Message
    Recursive Sweatpants polycounter lvl 4
    I see! I'll store it in a custom attribute, then. I appreciate the Python example, but I don't think the data I'm writing is enough to warrant writing it into its own file. Right now it's just a simple string containing the names of the parent objects and what type of constraint the space-switching system is bound to them with, which constitutes little more than two to nine or so items in general.

    Also, thank you for letting me know that dictionaries are a thing! I've never really used Python before switching to it from MEL, so I know very little about it outside of what I've needed for this script. Having actual key-value pairs will definitely simplify a few of my functions, which currently try to do the same thing with nested lists.
  • ProperSquid
    Options
    Offline / Send Message
    ProperSquid polycounter lvl 12
    I think json is still great for this. You can do something like:

    data = {"constraint_relationships": {"node1": "parent",
                                         "node2": "orient",
                                         "node3": "scale"}}
    
    # JSON string in the attribute
    '{"constraint_relationships": {"node1": "parent", "node3": "scale", "node2": "orient"}}'&nbsp;


    Having the data this way gives you a lot of flexibility later. For example, you can say what length the joint is, or what side it is.
Sign In or Register to comment.