Announcements

Between mid-October and November, the content on AREA will be relocated to the Autodesk Community M&E Hub and the Autodesk Community Gallery. Learn more HERE.

Maya Python beginner Syntax issue?

Maya Python beginner Syntax issue?

Anonymous
Not applicable
1,556 Views
3 Replies
Message 1 of 4

Maya Python beginner Syntax issue?

Anonymous
Not applicable

Hi all,

 

I've recently started learning python and MEL in Maya and came across a vague syntax error that I'm confused about.

I'm practicing with if and for statements. I get an error saying # Error: invalid syntax.

The error doesn't tell me which line has the error or which part of the line has the error. I would really appreciate the help!

 

Below is my code. Pleas elet me know your thoughts:

 

if len (selection) == 0:
selection = cmds.ls( dag = True, long = True)
selection.sort(key = len(, reverse = True)
print selection


for obj in selection:
print obj

 

0 Likes
Accepted solutions (1)
1,557 Views
3 Replies
Replies (3)
Message 2 of 4

stuzzz
Collaborator
Collaborator

I believe that the problem is not just about syntax but about logic as well... is that correct?

if len (selection) == 0: # the variable named selection is used before initialisation, is there a line missing above? If you restart maya and launch this code, maya would tell you that this variable is not defined.

selection = cmds.ls( dag = True, long = True) #by doing so you are not querying the current selection but all the entire dag node scene fullpath name

selection.sort(key = len(, reverse = True) # the key falg is not set properly and reverse also

print selection

 

I would rather do this to have a correct syntax but still not sure of what you want to achieve:

 

selection = cmds.ls( sl=True, long = True) # query first the selected node
if (selection): # no need to get the length of the selection, the pythonic syntax works this way. If you have no items in selection, there is not point to process something.
	selection.sort(reverse = True) #if you need to reverse the list order 
	print selection # then print it

 

0 Likes
Message 3 of 4

Anonymous
Not applicable

Thanks for the reply!

 

I'm actually following a Udemy video series about writing my first if statement in Python. Basically, the if statement checks if something is selected and if nothing is selected, get a new selection. When I select an object and run the script, it's supposed to list out everything associated with that selection minus the internal files (hence the dag). The "long" parameter is supposed to provide access to the full path of the object. So, for example, if the object I selected, was parented to another object, then the path would include the parent and child object.

 

The sort function sorts the list of the selected object in whatever order I tell it to. In this case, objects are organized with the longest path name object first, shortest path name as last (hence the reverse).

 

 

As for the missing line. There is a "from Maya import cmds" line that I forgot to paste here in my post. This gives me access to using Maya commands in Python.

 

I did restart Maya and run the code again, but it still gives me the syntax error without any explanation.

As far as keys go, I'm confused about what it does. From my understanding, key is an optional parameter that lets another function be used inside the sort function. Getting the length is something that only applies if there's something selected.

 

Is it possible that the "len" key is just redundant here without really needing to use it?

As for the reverse value, I'm not sure how it's being used wrong?

 

Sorry for the very lengthy message, I hope this clarifies a few things!

Thanks again!

 

0 Likes
Message 4 of 4

stuzzz
Collaborator
Collaborator
Accepted solution

ok then, this would help:

 

from maya.cmds import cmds
selection = cmds.ls( sl=True, dag=True, long = True) 
if (selection): 
	selection.sort(key=len, reverse = True) 
	print selection 

 

0 Likes