CGTalk > Software > Autodesk Maya > Maya Programming
Login register
Thread Closed share thread « Previous Thread | Next Thread »  
 
Thread Tools Search this Thread Display Modes
Old 03-04-2013, 01:09 AM   #1
Geuse
Expert
 
Geuse's Avatar
portfolio
Dag
Sweden
 
Join Date: Apr 2006
Posts: 521
Send a message via MSN to Geuse
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.
 
Old 03-04-2013, 03:28 AM   #2
Panupat
Expert
 
Panupat's Avatar
portfolio
Panupat Chong
Thailand
 
Join Date: Feb 2004
Posts: 1,942
Easiest way to match string is to use "in"
Code:
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.
Code:
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)
 
Old 03-04-2013, 10:36 AM   #3
Geuse
Expert
 
Geuse's Avatar
portfolio
Dag
Sweden
 
Join Date: Apr 2006
Posts: 521
Send a message via MSN to Geuse
Oh, thank you!
Continuing =).......

EDIT:
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-04-2013 at 10:58 AM.
 
Old 03-04-2013, 01:44 PM   #4
Panupat
Expert
 
Panupat's Avatar
portfolio
Panupat Chong
Thailand
 
Join Date: Feb 2004
Posts: 1,942
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.

Code:
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["Polygons"]
listDict["General"]
listDict["Subdivs"]
listDict["Deformation"]

listDict["Deformation"] will give you an empty list, but it's there.
 
Old 03-04-2013, 01:54 PM   #5
Geuse
Expert
 
Geuse's Avatar
portfolio
Dag
Sweden
 
Join Date: Apr 2006
Posts: 521
Send a message via MSN to Geuse
Gahh...
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...


Code:
import maya.cmds as mc import maya.mel as mel shlfPath = mel.eval("internalVar -userShelfDir") shlfPath = shlfPath.split(";") #print shlfPath fileString=[] for each in shlfPath: fileString.append(sorted(os.listdir(each))) shelfFiles=[] 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 delMayaNtv=[] 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?
 
Old 03-04-2013, 01:57 PM   #6
Geuse
Expert
 
Geuse's Avatar
portfolio
Dag
Sweden
 
Join Date: Apr 2006
Posts: 521
Send a message via MSN to Geuse
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

Code:
list=[] 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: list.append(var) print list

Last edited by Geuse : 03-04-2013 at 02:20 PM.
 
Old 03-04-2013, 02:39 PM   #7
Panupat
Expert
 
Panupat's Avatar
portfolio
Panupat Chong
Thailand
 
Join Date: Feb 2004
Posts: 1,942
There are quite a lot of ways to loop through dictionary. 1 way is to pass the listB as key
Code:
for b in listB: print listDict[b]

Or loop through the dictionary itself
Code:
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
Code:
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-04-2013 at 03:03 PM.
 
Old 03-04-2013, 03:38 PM   #8
Geuse
Expert
 
Geuse's Avatar
portfolio
Dag
Sweden
 
Join Date: Apr 2006
Posts: 521
Send a message via MSN to Geuse
Awesome. Thanks!
Well I feel confident enough to continue. Let's see how it goes =)
 
Old 03-04-2013, 04:10 PM   #9
Panupat
Expert
 
Panupat's Avatar
portfolio
Panupat Chong
Thailand
 
Join Date: Feb 2004
Posts: 1,942
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.
Code:
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.

Code:
polylist, generallist, subdlist, deformlist = [[a for a in listA if b in a] for b in listB]
 
Old 03-08-2013, 02:50 AM   #10
Geuse
Expert
 
Geuse's Avatar
portfolio
Dag
Sweden
 
Join Date: Apr 2006
Posts: 521
Send a message via MSN to Geuse
Thanks again!
Awesome! really appreciate it!!
 
Old 03-08-2013, 02:50 AM   #11
CGTalk Moderation
Lord of the posts
CGTalk Forum Leader
 
Join Date: Sep 2003
Posts: 1,066,480
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


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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
CGSociety
Society of Digital Artists
www.cgsociety.org

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

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


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