How to use a list to feed the flags of a maya command using list comprehension ?

84 views
Skip to first unread message

Sam555

unread,
Oct 16, 2014, 4:12:56 AM10/16/14
to python_in...@googlegroups.com
Hello guys,

I'm learning python and stumble upon some simple things.
is there a way to use a list to feed the flags of a maya command ?

i have a very long list of attributes that i want to send in a list comprehension, something like this: (i've shortened the list for demonstration)

attrs=["constructionHistory",  "gravity", "gravityY", "gravityZ", "nodeState", "offset", "pivot"]

for attr in attrs :        
        attributesList=[pm.polyExtrudeFacet(extrusion, attr=1", q=1) for attr in attrs]

This code doesn't work as expected because "attr" is not recognize as a variable in the flags.
How would you do that ?

Thank you.

Marcus Ottosson

unread,
Oct 16, 2014, 4:31:58 AM10/16/14
to python_in...@googlegroups.com
attrs=["constructionHistory",
       "gravity",
       "gravityY",
       "gravityZ",
       "nodeState",
       "offset",
       "pivot"
]

pm.polyExtrudeFacet(*attrs)

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/c846f255-057e-4c39-ad35-f60f23c899ca%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Marcus Ottosson
konstr...@gmail.com

Justin Israel

unread,
Oct 16, 2014, 5:49:51 AM10/16/14
to python_in...@googlegroups.com

You want to pass a value of 1 for each attr in the list as a key? You can convert it to a dict and then use unpacking

kw = dict((a, 1) for a in attrs)
pm.polyExtrudeFacet(extrusion, q=1, **kw)

--

Sam555

unread,
Oct 16, 2014, 9:38:12 AM10/16/14
to python_in...@googlegroups.com
Thank you to both of you,

i need to fully grasp the use of dict, they seems so useful in so many case.

Thanks again.

Solved.

Marcus Ottosson

unread,
Oct 16, 2014, 10:26:42 AM10/16/14
to python_in...@googlegroups.com

After a second, and Justin’s read it occurred to me that though my example answers your question on how to pass flags via a list, it doesn’t work. You would have to either do it the way Justin said, or use a dictionary and the double-star.

attrs = {"constructionHistory": True,
         "gravity": True,
         "gravityY": True,
         "gravityZ": True
} 

pm.polyExtrudeFacet(**attrs)

A single star will “expand” a list into individual arguments, whereas the double-star will expand a dictionary into keyword-arguments.

Expanded, it would look like this:

pm.polyExtrudeFacet(constructionHistory=True, gravity=True, ...)

Whereas the expanded list above looks like this:

pm.polyExtrudeFacet('constructionHistory', 'gravity', 'gravityY', ...)

Which is why it wouldn’t work. :)

Best,
Marcus


--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Marcus Ottosson
konstr...@gmail.com

Sam555

unread,
Oct 18, 2014, 2:29:06 PM10/18/14
to python_in...@googlegroups.com
Hello guys,

i'am still stuck with this one, i can't get a for loop working with the dict unpacking, i t is not iterating on the key:value pair....as i surely not using the good way to iterate with dict:

attrs = {"offset": True,
         "smoothingAngle": True,
         "localTranslateZ": True,
         "localScaleX": True}


for attr in attrs:
    print(pm.polyExtrudeFacet(extrusion, q=1, **attrs))

it's giving me the same value each time.
What am i doing wrong ?

Thank you for your help !

Justin Israel

unread,
Oct 18, 2014, 2:41:00 PM10/18/14
to python_in...@googlegroups.com


On 19/10/2014 7:29 AM, "Sam555" <samit...@gmail.com> wrote:
>
> Hello guys,
>
> i'am still stuck with this one, i can't get a for loop working with the dict unpacking, i t is not iterating on the key:value pair....as i surely not using the good way to iterate with dict:
>
> attrs = {"offset": True,
>          "smoothingAngle": True,
>          "localTranslateZ": True,
>          "localScaleX": True}
>
>
> for attr in attrs:
>     print(pm.polyExtrudeFacet(extrusion, q=1, **attrs))
>

It doesn't make any sense to do this. What you are doing is looping over each key in the dict, not using it, and calling the same command each time, with the ordinal key/value pairs from the attrs dict. Seems like you want to ditch the loop altogether and call this once.

> it's giving me the same value each time.
> What am i doing wrong ?
>
> Thank you for your help !
>

> --
> You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.

> To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/8a8b11a7-ab7d-4d1a-b626-785b8280dc68%40googlegroups.com.

Marcus Ottosson

unread,
Oct 18, 2014, 2:58:32 PM10/18/14
to python_in...@googlegroups.com
for attr in attrs:
     print(pm.polyExtrudeFacet(extrusion, q=1, **attrs))

Yes, that is quite the nonsensical command. :)

You use it like this:

attrs = {"query": True,
         
"offset": True,
         "smoothingAngle": True,
         "localTranslateZ": True,
         "localScaleX": True
}

pm.polyExtrudeFacet(**attrs)

Which is the same thing as doing this:

pm.polyExtruceFacet(query=True,
                    offset=True,
                    smoothingAngle=True,
                    localTranslateZ=True,
                    localScaleX=True)

Though I don’t know where you’re getting extrusion from.



For more options, visit https://groups.google.com/d/optout.



--
Marcus Ottosson
konstr...@gmail.com

Sam555

unread,
Oct 18, 2014, 3:02:26 PM10/18/14
to python_in...@googlegroups.com
Yes i see my error calling over the ordinal key/value from the dict but calling only "pm.polyExtrudeFacet(extrusion, q=1, **attrs" returns only one value and not every attributes value i want to query.

My steps :

# get the mesh
sel=pm.selected()[0]

# get the shape
shapeNode = sel.getShapes()[0]

# get the extrudes nodes
 extNodes = sorted([node for connection in shapeNode.connections(c=1) for node in connection if pm.nodeType(node)=='polyExtrudeFace'])

# node attributes (shortened for demonstration)

attrs = {"offset": True,
         "smoothingAngle": True,
         "localTranslateZ": True,
         "localScaleX": True}

#  list all the attributes value for say the first extrude node :
extAttrValues = pm.polyExtrudeFacet(extNodes [0], q=1, **attrs)
it only returns one value in the extAttrValues  list.

Thank for your help.


Marcus Ottosson

unread,
Oct 18, 2014, 4:34:54 PM10/18/14
to python_in...@googlegroups.com

Aah, now I see what you’re trying to achieve. How about this.

for attr in ["offset",
             "smoothingAngle",
             "localTranslateZ",
             "localScaleX"]:
    kwargs = {attr: True, "query": True}
    print pm.polyExtrudeFacet(extNodes[0], **kwargs)

This will run a query for each string in the list.


--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Marcus Ottosson
konstr...@gmail.com

Justin Israel

unread,
Oct 18, 2014, 4:37:08 PM10/18/14
to python_in...@googlegroups.com

Oh right. Now it makes sense why you wee doing that loop. Marcus seems to have solved the riddle.

Sam555

unread,
Oct 18, 2014, 5:02:25 PM10/18/14
to python_in...@googlegroups.com
Thank you so much Marcus and Justin,

i'll need to digest this few line and get the grasp of dict.
Loving python and this place, thanks for your help, time, patience and sharing your knowlegde, that's very appreciated.

Regards.

ursprod...@gmail.com

unread,
Oct 19, 2014, 6:54:06 PM10/19/14
to python_in...@googlegroups.com
Hey Marcus,
Where do get the extNodes list from?

Marcus Ottosson

unread,
Oct 20, 2014, 3:22:26 AM10/20/14
to python_in...@googlegroups.com

Where do get the extNodes list from?

That was from the last example that Sam posted.

extNodes = sorted([node for connection in shapeNode.conn...

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Marcus Ottosson
konstr...@gmail.com

Reply all
Reply to author
Forward
0 new messages