Hi Gabriele, All you need to do is import your script in the shelf button, it will take care of the “loading” for you.
*testScript.py* def test_function(): print "Test complete" Your shelf button can then consist of something like this: import testScript testScript.test_function() As a side note, you shouldn’t have anything outside of functions or classes that executes anything; so imports and attribute definitions are okay, but showing and deleting windows are not. Reload is useful if you change your script on disk; so really only for dev, not during production, you can do something like what you did: import testScript reload(testScript) testScript.test_function() Also, have a look at PEP08 for naming conventions; mainly how to stick with snake_case: *test_script.py* def test_function(): print "Test complete" Best, Marcus On 15 July 2014 16:54, Gabriele Bartoli <[email protected]> wrote: > Hello there. > > This is what I'd like to achieve: let's say I have a script that I saved > as testScript.py, and it is located in the scripts folder, so that Maya can > find it. I want to add an icon to my custom shelf which, instead of running > the code of script, "loads" the script file and runs it. This would allow > me to edit the script and avoid substituting the code inside the custom > shelf icon. > Here is a code example: > # Load maya commands module > import maya.cmds as mc > > # This prevents multiple windows to pop up > if mc.window("myWindow", ex=True): > mc.deleteUI("myWindow", window=True) > > # Window set-up > mc.window("myWindow",title="Test Window", s=False, wh=(300,100)) > mc.columnLayout(adj=True) > toggle_test = mc.checkBox(label = 'Toggle me!', value = False) > mc.button(l="Press to print the value of the checkbox below", w=300, h=100 > , > command = 'printIt()' > ) > mc.showWindow("myWindow") > > > # Print the value > def printIt(): > > # Query toggle status > var = mc.checkBox(toggle_test, query = True, value = True ) > > # Print the status > print "Checkbox's status is:", var > > The code runs fine if selected and executed in the script editor. I > created a shelf button with the following code: > import testScript > > reload (testScript) > > After restarting maya, or deleting the global instance of the printIt > function, if I click on the shelf button the script runs and the UI is > shown. If I then click on the UI button, I get the following error: > > # Error: NameError: file <maya console> line 1: name 'printIt' is not > defined # > > Ok, I get what is happening. The button press is running the code > 'printIt()', but the function has not been imported in global space (I > might be throwing wrong terms around a bit, please correct me if that > happens) and it can't be found. > > I am willing to edit the script, but I want it to work both by importing > it or running it from the script editor. First option is to edit the shelf > button code to: > from testScript_01 import * > > import testScript > > reload (testScript) > > This seems to work fine, but as I understand is not the suggested way of > solving the problem. > > I could also edit > command = 'printIt()' > to > command = 'testScript.printIt()' > > but this would have to side effects: first, if the script file is renamed > or imported as something else, the function would not work anymore. Second, > running the code inside the script editor would not work anymore. > > So, any idea on how I could fix this issue? Is there a way I could feed > the proper filename to the code? > > Something like: > > import testScript > > reload (testScript) > # Add a line of code that feeds the string 'testScript' to the imported > module > that then could be used inside the script as: > # Declare var for fed string > > input_string = #fed string > > ... > > command = input_string + '.printIt()' > > Hopefully I managed to explain the issue properly :P > > Thanks in advance! > > -- > 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 [email protected]. > To view this discussion on the web visit > https://groups.google.com/d/msgid/python_inside_maya/be3c13c1-48f0-41d3-8184-e36fda61073e%40googlegroups.com > <https://groups.google.com/d/msgid/python_inside_maya/be3c13c1-48f0-41d3-8184-e36fda61073e%40googlegroups.com?utm_medium=email&utm_source=footer> > . > For more options, visit https://groups.google.com/d/optout. > -- *Marcus Ottosson* [email protected] -- 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 [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CAFRtmOC%2BOQH_8vk6Tc6moskVFoyTYXi9%2BJhev0Ubn0t%2BVv5nBw%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.
