[Python] Import file from optionMenu

Become a member of the CGSociety

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

THREAD CLOSED
 
Thread Tools Search this Thread Display Modes
Old 02 February 2013   #1
[Python] Import file from optionMenu

That might be a vast over simplification of the issue I'm having, but I never know what to title these threads.

I might be over thinking this, but I've run across an issue that I can't figure out.

1. I created a UI with a text field, optionMenuGrp and in import button. The text field has a directory in it, the option menu group has a name.
2. rather than have david_clothRig.mb in the option menu, I did the little snippit below to just have "david" appear in the menu.


#get the cloth rig directory that's in the text field
self.clothRigDir = mc.textFieldButtonGrp("dirField", query = True, text = True)
#returns C:/Users/Me/Desktop/clothRigDir

#get items in the directory
cDir = os.listdir(self.clothRigDir)
#returns [u'davidson_clothRigFile.mb', u'david_clothRigFile.mb', u'mindy_clothRigFile.mb']		

#for the files in the cloth directory...
for f in cDir:
	#if they are maya files...
	if f.endswith(".mb") or f.endswith(".ma"):

		#get the full name, including the extension...
		fullname = os.path.splitext(f)[0]
		
                #seperate the name on the underscore...
		name = fullname.split("_")[0]
		
                #and finally, put the simple name in the cloth rig drop down menu
		mc.menuItem(label = name)


3. (my problem) I have an Import button that calls a importRig function. I want to select david from my menu then click my import button to import the david_clothRig.mb file. If I had "david_clothRig.mb" in the optionMenu then I could just do "dir + menuItemName" or something and it would work, but I don't.

Is there a way to associate/get the file associated with the name in the optionMenu?

Thanks.
 
Old 02 February 2013   #2
Hmm. To me, this sounds like a very specific tool for a very specific scenario:

It is designed to optimize the UI for files starting in <somename> followed by an underscore and some other text, e.g. somename_sometext.mb. If that's the case, is it not safe to assume that you also know what the part after the underscore is? E.g. in your example 'clothRig'?

If not, then there is no way for a machine to know which file you meant. If there are similar files in that directory, e.g.:

david_clothRig.mb
david_fbikRig.mb
david_clothRig.ma

Then a) you'll have a bunch of 'david' items in your menu, the user won't be able to distinguish between then, and b) you can't reconstruct a specific file just based on the prefix 'david'.

So, due to the issue above and provided I didn't completely misunderstand your situation, you have two options:
  1. You place the the whole filename, minus extension (this can probably be guessed and reconstructed later) in the optionMenu. I find that to be the most intuitive solution in terms of UI design and what a user might expect to see
  2. You create a second optionMenu (-managed 0) that you populate with the full filename + extension and keep in synch with the visible, shortname optionMenu. That way, the import looks for the item selected in the shortname optionMenu and uses the same itemIndex in the invisible optionMenu to retrieve the actual filename to work with. THis is the most failsafe method, if you diligently track all operations to your optionMenu and keep them in synch.
 
Old 02 February 2013   #3
Thank you so much for the response. I wasn't too worried about getting multiple Davids or messing up a name in the menu because this was a personal project to help me maintain/work with my cloth rigs and I have a very strict naming structure for myself. If I ever decide to share this tool, I'd probably change all of this.

I ended up using your 1st solution and it worked perfectly, but since I'm stubborn I REALLY wanted to see if it was possible to figure out my problem without putting a longer name in a dropdown menu.

I managed this:

1. I created an empty list called clothRigList.
2. I put all of the file names into the clothRigList.
3. I queried the position of the short name in the option menu.
cRigMenuPos = mc.optionMenuGrp("clothRigMenu", query = True, select = True, value = True)
So if I selected the first name, I'd get 1, which is Davidson.

4. I used the position of the item in the option menu to import it's file.
Davidson is 0 in the clothRigList, so I did something like this:

rigFile = self.clothRigDir + "/" + self.clothRigList[(cRigMenuPos - 1)]
which gives me:
c:Users/Me/Desktop/clothRigDir/davidson_clothRigFile.mb
(In retrospect, I could have just put all of that in the list)

It realize it's not the most efficient solution, but it works too *shrug*

Thanks again for your help! It was greatly appreciated.
 
Old 02 February 2013   #4
we should have a FAQ of some sort, as this is one of the top topics when it comes to basics of command handlers. Use command handler objects! This is the easiest and most efficient way to assign customized handlers for UI objects:


import maya.cmds as cmds

class FileCommandHandler:
    def __init__(self,filePath):
        self.filePath = filePath
        
    def __call__(self,*args):
        """
        called when object is "executed"
        """
        
        print "Opening file '%s' here" % self.filePath
        

w = cmds.window( menuBar=True, width=200 )
cmds.menu( label='Files', allowOptionBoxes=True )

cmds.menuItem( label='Blue', command=FileCommandHandler("../blue.mb"))
cmds.menuItem( label='Something else', command=FileCommandHandler("c:/poo/poo/poo.ma"))
cmds.menuItem( label='More stuff', command=FileCommandHandler("stuff.txt"))

cmds.showWindow(w)
__________________
Victor
ngskintools.com - skinning plugin for Autodesk Maya
 
Old 02 February 2013   #5
Thanks for this. I'm teaching myself Python and a lot of things I don't know about because I haven't needed to use them. I hadn't even heard of the __call__ method until now! I'll have to study it to fully understand what it does, but thanks for your help. This definitely looks like it'll be worth using.
 
Old 02 February 2013   #6
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
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 01:42 AM.


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