Category Archives: PYTHON

Add notes to a node in maya with python

So sometimes you want to add some notes to your objects in maya, the quick python script below will add a string attribute to the node you input

from maya import cmds

def add_notes(node = None, default_text = "Input note here"):
    
    if not node:
        sel = cmds.ls(sl=1)
        if sel:
            node = sel[0]
        else:
            cmds.warning("Please select a node to apply the notes field too")

    if not cmds.attributeQuery("notes", n = node, ex = True):
        cmds.addAttr(node, ln = "notes", sn="nts", dt="string")
        cmds.setAttr("%s.notes"%node, default_text, type="string")
    else:
        cmds.warning("%s already has a notes attribute"%node)

add_notes("my_node_name", "Remember to rewind to 0 when opening this file")

notes_val = cmds.getAttr("%s.notes"%node)

Cheers,
Gary

Convert Python list in to MEL String Array – [Maya]

Hi there,

So this is a handy little script if you want to pass a python list into a mel command in your code, say you have a list of objects that you now wat to pass in eg:

[u'pCube1', u'pCube2', u'pCube3', u'pCube4', u'pCube5', u'pCube6', u'pCube7', u'pCube8', u'pCube9']

, you’re going to need to convert it to a type that MEL understands, which looks like this:

{"pCube1", "pCube2", "pCube3", "pCube4", "pCube5", "pCube6", "pCube7", "pCube8", "pCube9", "pCube10"}

and the command below will do all this for you, it turns the whole thing in to a string, and then replaces the relevant characters with the ones that MEL understands.

def convertListToMelStringArr(pyList):
    pyList = str([str(x) for x in pyList])
    pyList = pyList.replace("'", "\"")
    pyList = pyList.replace("[", "{")
    pyList = pyList.replace("]", "}")
    return pyList

Enjoy

Using setKeyframe on only the transform and not the shape [Maya]

So I ended up with a few issues when attempting to set the keyframe on an object using the layout specified in the python command, I was trying to do something like:

cmds.setKeyframe("pCube1", value=True, attribute="visibility")

However as I was using instancing, this would also keyframe the visibility on the pCubeShape1 which I didn’t want, even if I explicitly passed in only the name of the transform, to avoid this you need to pass in the transform name and the attribute in the same go, so something along the lines of:

cmds.setKeyframe("pCube1.visibility", value=True)

and this should only change it on the transform.

Good Luck!

Gary