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

Leave a Reply

Your email address will not be published. Required fields are marked *