Python issues

Become a member of the CGSociety

Connect, Share, and Learn with our Large Growing CG Art Community. It's Free!

Thread Tools Display Modes
  03 March 2013
Python issues

Hi all!
I'm converting to Python and thought of scripting something that I originally started in MEL.
The idea is to create a "shelf load/unload UI" as I have 32 shelves including mayas standard ones.

Anyway. basically what I'm looking for is an equivalent gmatch command with filtering.

gmatch "shelf_Polygons.mel" "*Poly*" => True

I've fiddled with the list comprehension which I love, but can't seem to run filter on it as I only want to match part of the name.
I'm looping through my shelf paths and appending them to a list.
I'm also looping through my shelves in maya UI and adding them to another list. Then I want to see which lists are loaded, which are not, which are deleted etc..

Thanks in advance.

PS. I also posted this in the python in maya google group, but it never showed up =/
So I don't mean to crosspost if that should happen.
  03 March 2013
Easiest way to match string is to use "in"
samples = "shelf_Polygon"
if "_Poly" in samples: print "This is a Polygon Shelf!"
You can either do that directly in list comprehension or inside the for loop.
sample = ["shelf_this", "shelf_that", "shelf_those"]
thislist = [s for s in sample if "_this" in s]

thislist = []
thatlist = []
thoselist = []
for s in sample:
    if "_this" in s: thislist.append(s)
    if "_that" in s: thatlist.append(s)
    if "_those" in s: thoselist.append(s)
  03 March 2013
Oh, thank you!
Continuing =).......

On second thought, that didn't work as I hoped. Is there a way to match 2 list to each other in this way:
listA = ["shelf_Polygons.mel", "shelf_General.mel", "shelf_Subdivs.mel"]
listB = ["Polygons", "General", "Subdivs", "Deformation"]

newList = [x for x in listB if x in listA]

The string "Polygon" is clearly in listA so how can I make this work?
I could resort to using a bunch of loops, but I rather learn a cleaner approach since I'm doing this mainly for learning.

PS. I didn't mean "filter", but "wildcard" *

Last edited by Geuse : 03 March 2013 at 10:58 AM.
  03 March 2013
Your end results only have 1 list? Don't you want 4 lists, 1 for each members of listB?

Anyway, that's a good question. I'm not sure how to cut down loops, but here is 1 way to shorten the code that I can think of. You'll need an empty dictionary to start with.

listDict = {}

listA = ["shelf_Polygons.mel", "shelf_General.mel", "shelf_Subdivs.mel"]
listB = ["Polygons", "General", "Subdivs", "Deformation"]

for b in listB:
    listDict[b] = [a for a in listA if b in a]

That will give you 4 members in the dictionary.


listDict["Deformation"] will give you an empty list, but it's there.
  03 March 2013
I'm practically doing this ----->

I can't find how I ought to write the different statements! and how list the data in a practical way.

What I would like is to have 5 lists at the end

mayaShlfNtv = list of Maya native shelves
mayaShlfCstm= list of custom shelves
delMayaNtv = list of deleted Maya native shelves
delMayaCstm = list of deleted custom shelves
mayaShlfUI = list of shelves in UI for error checking.

I'll post the base of code here. Haven't gotten that far though...

import maya.cmds as mc
import maya.mel as mel
shlfPath = mel.eval("internalVar -userShelfDir")
shlfPath = shlfPath.split(";")
#print shlfPath
for each in shlfPath:
for each in fileString:
    shelfFiles += each

#So here I have all my shelf files
#print shelfFiles

gShelfTopLevel = mel.eval("global string $gShelfTopLevel; $temp = $gShelfTopLevel;")
mayaShlfUI = mc.shelfTabLayout(gShelfTopLevel, query=True, childArray=True)
#And now I have the shelves names in Maya.

#I add a list to contain which shelves are Maya Native
mayaShelves=['General','Curves','Surfaces','Polygons','Subdivs'  ,'Deformation','Animation','Dynamics','Rendering',  'PaintEffects','Toon','Muscle','Fluids','Fur','Hai  r','nCloth','Custom']

#Then I obtain any deleted shelves by this
delShlf = [x for x in shelfFiles if ".deleted" in x]
print delShlf
#Now I want to split this list up in 2 separate lists as delMayaNtv(native) and delMayaCstm
#to place these in a UI in separate layouts. So I don't mix my custom with maya natives

#Now here's where I encounter problems
for each in mayaShelves:
    delMayaNtv.append([x for x in delShlf if each in x])

I obviously only want existing items in this list, but for each loop it appends an empty item to my list =/. How to fix this?
  03 March 2013
Thank You Panupat!
However, I'm not sure if a dictionary is what I'd want there. I'd like to be able to loop through it as a list.

So something more along this I think

listA = ["shelf_Polygons.mel", "shelf_General.mel", "shelf_Subdivs.mel"]
listB = ["Polygons", "General", "Subdivs", "Deformation"]

for b in listB:
    var=[a for a in listA if b in a]
    print var
#This is what I can come up with, but it has to be a cleaner
#way than checking the length?
    if len(var)>0:
print list

Last edited by Geuse : 03 March 2013 at 02:20 PM.
  03 March 2013
There are quite a lot of ways to loop through dictionary. 1 way is to pass the listB as key
for b in listB:
    print listDict[b]

Or loop through the dictionary itself
for key, value in listDict.iteritems():
    print key, value

You could actually replace listB entirely with the dictionary since my first example. Keep in mind tho that dictionary will not retain the order you create it with (meaning 'Polygon' could show up on the second loop instead of the first even though you declare it first.)

Here's how you iterate through keys only
for key in listDict:
    print key

2 additional notes...

1. For list, you don't need to do len(list) > 0. A list with 0 length will check as False on it's own. So

if len(var)>0:
would be the same as
if var:

2. There is an any() method you can do to check if your list comprehension returns anything.

if any(d for d in some_list): do stuff

Last edited by Panupat : 03 March 2013 at 03:03 PM.
  03 March 2013
Awesome. Thanks!
Well I feel confident enough to continue. Let's see how it goes =)
  03 March 2013
Your welcome And I have good news. I got a tip from stackoverflow, and here's the final line that should do exactly what you want.
listA = ["shelf_Polygons.mel", "shelf_General.mel", "shelf_Subdivs.mel"]
listB = ["Polygons", "General", "Subdivs", "Deformation"]

[[a for a in listA if b in a] for b in listB]
# Result: [['shelf_Polygons.mel'], ['shelf_General.mel'], ['shelf_Subdivs.mel'], []] # 

you can add that directly to your variable.

polylist, generallist, subdlist, deformlist = [[a for a in listA if b in a] for b in listB]
  03 March 2013
Thanks again!
Awesome! really appreciate it!!
  03 March 2013
Thread automatically closed

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.
CGTalk Policy/Legalities
Note that as CGTalk Members, you agree to the terms and conditions of using this website.
Thread Closed share thread

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Society of Digital Artists

Powered by vBulletin
Copyright 2000 - 2006,
Jelsoft Enterprises Ltd.
Minimize Ads
Forum Jump

All times are GMT. The time now is 05:39 AM.

Powered by vBulletin
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.