I can offer a couple points of feedback so far, but I didn't get too deep
into the too since it has some compatibility issues with python2.6 (Maya
2013).
If you use the str.format() approach for formatting text, python2.6 doesn't
support the anonymous fields. That was introduced in python2.7

So in python2.7, this is valid:     "My string says {} {}!".format("Hello",
"World")
But in python2.6 you have to name the fields:   "My string says {0}
{1}!".format("Hello", "World")

Also, there are a couple button callbacks where you use "mm.eval()", but if
someone doesn't have maya.mel imported as mm in their global scope (either
having done it manually in the script editor, or in their userSetup.py)
then it will produce an error. That could be another candidate for partial,
doing something like partial(mm.eval, "my mel command"). Otherwise you have
to do the more verbose string approach that has to first import maya.mel:
"import maya.mel as mm; mm.eval('my mel command')". I kind of prefer the
former, if you have to call mel as a python callback. Or it can call a
python method you have set up already that will cleanly call the mel
command, like:   self.__prefixHierarchy()

Those two blocking issues aside, a couple of other notes:

* "New-style" python classes always at least subclass object:   class
MyClass(object):

* When testing if a list is empty, instead of allocating a new empty list
object each time, just to test if it is equal to the other list, you can
simply check if your list is empty using:     if not myList

* It is usually best not to have your class constructor have side effects,
such as showing the window. It should have a separate method like show().
That way someone can import and create an instance of the window and show
it when they want to. Constructors are for setting up the state of the
class.

* The approach where you are using the tool_launcher() method with a string
tool name might be cleaner if you made those tools formal functions or
methods. There are a couple of places where you test for the literal string
names in order to do different logic, which could be bug prone because
changing the name in one spot means you have to find and replace all of the
literal string references. Maybe you might want to define those once in
your class as class constants:

class MyClass(object):
    ACTION_NAME_1 = "action1"
    ACTION_NAME_2 = "action2"

Or if you want to make the association between an action and a displayable
name, you can register a dictionary in your constructor that maps them:

self._actions = {
    "Action 1": self.__action1,
    "Action 2": self.__action2,
}

Then you won't have to do the 'if actionName == "literal name"' test in
multiple places to figure out the action to perform. You can just look up
the associated action in the dict:  self._actions[actionName]

* rsplit() is a method of a string
so this:    str.rsplit(str(temp_name), split_char, 1)
can be written as:    temp_name.rsplit(split_char, 1)








On Sat, Jul 19, 2014 at 3:39 AM, Gabriele Bartoli <[email protected]>
wrote:

> Here I am!
>
> I adjusted my script, and I'd like for you to give it a go. It is a set of
> tools for node renaming purposes. It has a Help section which should cover
> all the information needed to use it. The short version of it is that it
> works on the assumption of having a parent_child naming convention and aims
> to rename all the nodes in the hierarchy using such rule.
>
> If you have the time, I'd like for you to test it out and give me some
> feedback regarding the UI, the tools and the overall usefulness. It would
> be really appreciated! It is kind of long, so don't waste too much time
> on the coding part.
>
> You'll find it here: http://pastebin.ubuntu.com/7814931/
>
> Cheers!
>
>
>
>
>
> On Tuesday, 15 July 2014 17:54:27 UTC+2, Gabriele Bartoli 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/d857e3a5-c027-4a79-b67d-b15ad2ac1a9b%40googlegroups.com
> <https://groups.google.com/d/msgid/python_inside_maya/d857e3a5-c027-4a79-b67d-b15ad2ac1a9b%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>
> For more options, visit https://groups.google.com/d/optout.
>

-- 
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/CAPGFgA0yjKzbbxqOQBvp%3Dm%2B6nK4hmOD6QRGai2CwC5zV-1P0uQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to