On Thu, Oct 27, 2016 at 5:42 AM likage <[email protected]> wrote:

> I would like some opinions on the way I am using instance variables in my
> class as I am not so sure if I am doing it right.. To be honest, this is my
> first time using parameters in my class __init__..
>
> class HierarchyDialog(QtGui.QDialog):
>     def __init__(self, state, parent=None):
>         QtGui.QDialog.__init__(self, parent)
>         self.setWindowTitle('Hierarchy Dialog')
>         self.setModal(False)
>
>         self.state = state
>
>         if self.state == "root_sels":
>             # Run function_A()
>         elif self.state == "user_sels":
>             # Run function_B()
>         else:
>             cmds.warning("Please check your selections. Either select
> some nodes or a root node")
>
>         ...
>         ...
>
>
> def run_dialog():
>     state = ""
>     selection = cmds.ls(selection=True)
>     if not selection:
>         cmds.warning("Please select a Stack or some nodes")
>         return
>
>     if len(selection) == 1 and cmds.nodeType(selection) == 'nurbsSurface':
>         state = "root_sels"
>     elif len(selection) >= 1:
>         for items in selection:
>             if not cmds.nodeType(selection) == 'nurbsSurface' in items:
>                 state = "user_sels"
>
>     dialog = HierarchyDialog(state)
>     dialog.show()
>     return dialog
>
> In my  `run_dialog`, I am checking for selections in the scene.
> There are only 2 types of selections - Selecting a nurbs surface node -
> suppose that is the top node of the hierarchy, or any of its children
> (suppose they are all meshs) within that nurbs suface node
>
> Is checking against a string as defined in my `__init__` function the
> right way to do so?
>

The only suggestion I would make here is that you move your literal strings
representing your available states into constants. This has a couple of
benefits:
It prevents typos when you constantly retype those literal strings in
different places. Your code editor has the opportunity to warn you when you
mistype a non-existing variable name, but not if you mistype a string
literal.
It lets you enumerate the available options as code symbols, to pick an
option  (via code completion, or introspection)
It makes it easier to documents them.

They can still be strings if you want the constants to be printable in a
meaningful way, or they can be ints:

class HierarchyDialog(QtGui.QDialog):

    # Documenting states
    STATE_ROOT_SELS = "root_sels"
    STATE_USER_SELS = "user_sels"

    def __init__(self, state, parent=None):
        QtGui.QDialog.__init__(self, parent)
        ...

        self.state = state

        if self.state == self.STATE_ROOT_SELS:
            # Run function_A()
        elif self.state == self.STATE_USER_SELS:
            # Run function_B()
        ...
def run_dialog():
    state = ""
    ...
    if len(selection) == 1 and cmds.nodeType(selection) == 'nurbsSurface':
        state = HierarchyDialog.STATE_ROOT_SELS
    elif len(selection) >= 1:
        for items in selection:
            if not cmds.nodeType(selection) == 'nurbsSurface' in items:
                state = HierarchyDialog.STATE_USER_SELS

    dialog = HierarchyDialog(state)
    ...

​

You can choose the naming you want for the constants.
I was a little confused though on the expected behaviour of state not being
either of the available options. Is that really a warning, or is it an
error for your dialog? Can your dialog operate without the state set
correctly? Anyways, you can expand on your state constants as needed.

Justin



> --
> 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/afdd4e64-f331-467a-b46d-c5f46b0a9a66%40googlegroups.com
> <https://groups.google.com/d/msgid/python_inside_maya/afdd4e64-f331-467a-b46d-c5f46b0a9a66%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/CAPGFgA3AP1v0TWegDjqQOjpbZ6_G-PfNzvkyTNAnqEHF-_m04w%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to