PDA

View Full Version : Python in Mel Expressions?


robert89
09-25-2010, 11:40 AM
Hi Everybody,

I am pretty new to scripting, so I hope I can explain everything so it makes sence.

I've been working on a python plugin that requires for a mel expression to be created. The only problem is that I don't know how to add python lists to the expression editor.

Basic Idea of the problem:
selectedObjects = cmds.ls(selection=True, transforms=True)
cmds.expression( name=controlObject+"_Exp", string="$List[] = {selectedObjects};" )

This gives an error.
This is what it should look like after the script is ran.

Lets say the selectedObjects = ["Head", "Root", "World"]
The end result in the expression editor should be:
$List[] = {"Head", "Root", "World"};

How do I do something like that?
Robert

ginodauri
09-25-2010, 01:54 PM
I think first problem is that you don't add objects , you add string selectedObjects.
It should look something like this.
cmds.expression( name=controlObject+"_Exp", string="$List[] = {"+selectedObjects[0]+"};")
Or if it is " dinamic" selection you made for loop:
for i in selectedObjects:
list+=(selectedObjects[i]+",")


Second problem you need to declare variable List(this is mel btw :) ):

string $List[] = {"Head", "Root", "World"};

robert89
09-25-2010, 11:08 PM
Thank you for your reply, but it didn;t solve the problem.

The problem remains that because the complete expression is between "", the python lists are being processed as text and not as a python list.

Does anybody have an idea how to fix that?
Robert

ginodauri
09-27-2010, 12:10 AM
I hope this solve your problem:


import maya.cmds as mc
def test1():
list=mc.ls(sl=1)
melList=""
for i in list:
melList+='"'+i+'",'
exp="string $list[]={"+melList[0:-1] + "};"
mc.expression(s=exp)

robert89
09-27-2010, 10:28 AM
That worked. Thank you very much.

CGTalk Moderation
09-27-2010, 10:28 AM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.