Most of what I wanted to comment is already covered by Justin and Marcus.
Here are my very very minor comments (consider them as extras):
1. self._state instead self.state (~ by convention, self.state should be
used only if `state` is public. From your code, and I presume, `state` is
not public. I usually start with ALL attributes - (methods, properties) as
private (starting with an '_') and then make them public, further down the
development as the need arises.
2.
> # Documenting states
> STATE_ROOT_SELS = "root_sels"
> STATE_USER_SELS = "user_sels"
>
are just enums and just can be alternatively written as, which are more
convenient to use
(
STATE_ROOT_SELS,
STATE_USER_SELS,) = range(2)
This way you can add huge number of states, (though not required in your
case), simply by adding another line, without having to worry about the
corresponding strings. The bonus is more protection from potential bugs
when you accidentally assign "same string val" to different attrs.
The above code can be made more procedural (although a bit convoluted), and
can be used at module level -
from collections import namedtuple
ALLOWED_STATES = [
'ROOT_SELS',
'USER_SELS',
# Feel free to add more states here
]
SELECTION_STATES = (namedtuple('SELECTION_STATES', ALLOWED_STATES)
(*range(len(ALLOWED_STATES))))
Enums became a part of python standard library as of 3.4, here is the PEP
<https://www.python.org/dev/peps/pep-0435/>,
In the light of all of the above, the code becomes:
from collections import namedtuple
ALLOWED_STATES = [
'ROOT_SELS',
'USER_SELS',
# Feel free to add more state here
]
SELECTION_STATES = (
namedtuple('SELECTION_STATES', ALLOWED_STATES)(*range(len(ALLOWED_STATES))))
class HierarchyDialog(QtGui.QDialog):
def __init__(self, state, parent=None):
super(HierarchyDialog, self).__init(parent=parent)
self.setWindowTitle('Hierarchy Dialog')
self.setModal(False)
self._state = state
if self._state == SELECTION_STATE.ROOT_SELS:
# Run function_A()
pass
elif self._state == SELECTION_STATES.USER_SELS:
# Run function_B()
pass
else:
cmds.warning("Please check your selections."
" Either select some nodes or a root node")
def run_dialog():
selection = cmds.ls(selection=True)
if not selection:
cmds.warning("Please select a Stack or some nodes")
return
state = None
if len(selection) == 1 and cmds.nodeType(selection) == 'nurbsSurface':
state = SELECTION_STATES.ROOT_SELS
elif len(selection) >= 1:
for items in selection:
if not cmds.nodeType(selection) == 'nurbsSurface' in items:
state = SELECTION_STATES.USER_SELS
dialog = HierarchyDialog(state)
dialog.show()
return dialog
On Thu, Oct 27, 2016 at 9:44 AM, Justin Israel <[email protected]>
wrote:
>
>
> On Thu, Oct 27, 2016 at 2:16 PM likage <[email protected]> wrote:
>
>>
>> 2. Is there any chance that I will need to add in an `else` statement
>> should the self.state not conform to either `self.STATE_ROOT_SELS` or
>> `self.STATE_USER_SELS`? Otherwise, I suppose I can stopped such from
>> happening in the run_dialog function, right?
>>
>>
>> Can you raise a ValueError if they don't pass a valid constant?
>>
>>
>> Hey Justin, was wondering if you could give me an example scenario as to
>> when this will occurs?
>> I asked because I tried selecting objects of other node types etc, I am
>> getting errors (need to rewrite my logic here) but only within the
>> `run_dialog` and it does not seems to pass in this `else` statement..
>>
>
> In your run_dialog() function, it looks like there is the potential for
> "state" to be an empty string, and passed to your dialog constructor. If it
> is not acceptable for your dialog to operate without a valid state value,
> then your dialog should raise a ValueError. If it can operate with an
> invalid state value, then don't raise an exception.
>
> So in your run_dialog(), what does it mean for your dialog to receive an
> empty string for state? Can the dialog still be useful?
>
>> --
>> 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/2cd81c76-93f9-4837-81b8-
>> be04c5d90bb4%40googlegroups.com
>> <https://groups.google.com/d/msgid/python_inside_maya/2cd81c76-93f9-4837-81b8-be04c5d90bb4%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/CAPGFgA1EGuYDhMYN4LDGb%2BR3uX8D%3D3Aiom_
> 3KUz9uv85jDBK9Q%40mail.gmail.com
> <https://groups.google.com/d/msgid/python_inside_maya/CAPGFgA1EGuYDhMYN4LDGb%2BR3uX8D%3D3Aiom_3KUz9uv85jDBK9Q%40mail.gmail.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/CAPaTLMR43Z3VwmknqEN91HTuNM7bt1Kb_BM%2BsSrQdPrNmp6j-A%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.