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 dialogLooks perfectly reasonable to me!
Things I like:
show from outside; you might consider it obvious, in which case that’s good, but I have both seen and been guilty of it myself of calling show from within __init__ which limits the use of the GUI and complicates reasoning when it comes time to hide it and potentially show or otherwise manage it’s liveliness later on.Things I don’t like:
super, e.g. super(HierarchyDialog, self).__init__(parent) but it’s a detail.Overall I’d say you’ve got it right so far!
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 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 python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/afdd4e64-f331-467a-b46d-c5f46b0a9a66%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
class HierarchyDialog(QtGui.QDialog):
"""
Keywords:
state:
Denotes the selection case - 'root node selection' or 'user based selection'
STATE_ROOT_SELS :
States the case if the selection is Root Node
STATE_USER_SELS :
States the case if the selection is User-based
"""
# 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()
# Do I need to include in a bogus state if any of the above 2 conditions are not met
else:
cmds.warning("Please have a valid selection")I re-post my message...@MarcusThanks for the information! Glad to know that I am on track, to be honest, I was not able to find much info online, perhaps my searching skill was not that great in this aspect :p@JustinThanks for the code. I have decided to use your format for my code.Even so, I have a few questions.1. I tried writing a doc string, again I would like to have some insights if possible? Instead of writing it after the `init` function, I wrote it after the class statement, accordingly to what I have read online
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?
class HierarchyDialog(QtGui.QDialog):
"""
Keywords:
state:
Denotes the selection case - 'root node selection' or 'user based selection'
STATE_ROOT_SELS :
States the case if the selection is Root Node
STATE_USER_SELS :
States the case if the selection is User-based
"""
# 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()
# Do I need to include in a bogus state if any of the above 2 conditions are not met
else:
cmds.warning("Please have a valid selection")
--
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 python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/f4a75603-ff76-4ff1-8161-0cfb5a088ebb%40googlegroups.com.
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?
--
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 python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/2cd81c76-93f9-4837-81b8-be04c5d90bb4%40googlegroups.com.
# Documenting statesSTATE_ROOT_SELS = "root_sels"STATE_USER_SELS = "user_sels"
( STATE_ROOT_SELS, STATE_USER_SELS, ) = range(2)
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))))
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 dialogOn Thu, Oct 27, 2016 at 2:16 PM likage <dissid...@gmail.com> 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 python_inside_maya+unsub...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/2cd81c76-93f9-4837-81b8-be04c5d90bb4%40googlegroups.com.
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 python_inside_maya+unsub...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CAPGFgA1EGuYDhMYN4LDGb%2BR3uX8D%3D3Aiom_3KUz9uv85jDBK9Q%40mail.gmail.com.
On docstrings, here are some considerations to make.
On 2, if you write your docstring in a particular way, then there are parsers out there (primarily Sphinx) capable of interpreting this and produce HTML documentation, similar to Python’s own documentation.
Personally, I’ve found that I rarely generate documentation, but still follow the syntax. That way, I don’t have to come up with my own syntax, and can benefit from the experience from others, linters and other goodies you get from sticking to standards. If one day the requirement came about that I do need documentation, well hey, that’s a neat bonus too.
On docstrings conventions, these are the most common that I know of.
I personally stick with Google’s approach because I found it a good balance between readability in the generated HTML pages, and the code itself. But I see a lot of Sphinx out there as well, and have no trouble understanding that either.
Hope it helps!
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/2cd81c76-93f9-4837-81b8-be04c5d90bb4%40googlegroups.com.
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 python_inside_maya+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CAPGFgA1EGuYDhMYN4LDGb%2BR3uX8D%3D3Aiom_3KUz9uv85jDBK9Q%40mail.gmail.com.
----
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 python_inside_maya+unsub...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CAPaTLMR43Z3VwmknqEN91HTuNM7bt1Kb_BM%2BsSrQdPrNmp6j-A%40mail.gmail.com.
super(HierarchyDialog, self).__init__(parent=parent)
I am not very familiar with using the underscores, in this case, public and private variables, as a matter of fact. If you read my code, pretty much everything is not of private variables.
Was not aware that I can add a number of states but in your code, can I suppose that both ALLOWED_STATES and SELECTION_STATES are considered as global variables then?
--
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 python_inside_maya+unsub...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/ba9fb2bd-c5c3-4770-9269-f4c9d8b34a78%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
There's a fundamental problem with this in my view, the dounder init method of a class gets executed _after_ the instance (aka self) get created, doing checks there means you basically create an object that can potentially be invalid by passing the wrong thing and that's a problem.
You could use a classmethod instead or even a dounder new, but I would go the other way around by using a simple function instead! Classes are a very powerful tool but it's a bit overkill on this case (imho anyway).
Cheers!
There's a fundamental problem with this in my view, the dounder init method of a class gets executed _after_ the instance (aka self) get created, doing checks there means you basically create an object that can potentially be invalid by passing the wrong thing and that's a problem.
You could use a classmethod instead or even a dounder new, but I would go the other way around by using a simple function instead! Classes are a very powerful tool but it's a bit overkill on this case (imho anyway).
Cheers!
--
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 python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CAPamJi9JY90vPHxJxaNJpU2C3D6zPix1TUv%2BcfXmMTN1gTW-5g%40mail.gmail.com.
What's the "dounder init method"? Did I miss some code somewhere?
On Sat, Oct 29, 2016 at 1:41 PM Cesar Saez <ces...@gmail.com> wrote:There's a fundamental problem with this in my view, the dounder init method of a class gets executed _after_ the instance (aka self) get created, doing checks there means you basically create an object that can potentially be invalid by passing the wrong thing and that's a problem.
You could use a classmethod instead or even a dounder new, but I would go the other way around by using a simple function instead! Classes are a very powerful tool but it's a bit overkill on this case (imho anyway).
What's the "dounder init method"? Did I miss some code somewhere?
Cheers!
--
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 python_inside_maya+unsub...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CAPamJi9JY90vPHxJxaNJpU2C3D6zPix1TUv%2BcfXmMTN1gTW-5g%40mail.gmail.com.
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 python_inside_maya+unsub...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CAPGFgA2OEPUi6hr2M%2BZwug672GKyjiC9hJUHoi75G3yHwSUqhw%40mail.gmail.com.
What's the "dounder init method"? Did I miss some code somewhere?I think Cesar is referring to the 'double-underscore' aka 'dunder ' aka 'magic methods' aka 'special methods'- def __init__()
On Sat, Oct 29, 2016 at 9:30 AM, Justin Israel <justin...@gmail.com> wrote:
On Sat, Oct 29, 2016 at 1:41 PM Cesar Saez <ces...@gmail.com> wrote:There's a fundamental problem with this in my view, the dounder init method of a class gets executed _after_ the instance (aka self) get created, doing checks there means you basically create an object that can potentially be invalid by passing the wrong thing and that's a problem.
You could use a classmethod instead or even a dounder new, but I would go the other way around by using a simple function instead! Classes are a very powerful tool but it's a bit overkill on this case (imho anyway).
What's the "dounder init method"? Did I miss some code somewhere?
Cheers!
--
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 python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CAPamJi9JY90vPHxJxaNJpU2C3D6zPix1TUv%2BcfXmMTN1gTW-5g%40mail.gmail.com.
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 python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CAPGFgA2OEPUi6hr2M%2BZwug672GKyjiC9hJUHoi75G3yHwSUqhw%40mail.gmail.com.--
--
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 python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CAPaTLMTWREaRPUXwBn%3DnY_0dqe5AaMEy_PJOO5PFstdWRCtFTw%40mail.gmail.com.
There's a fundamental problem with this in my view, the dounder init method of a class gets executed _after_ the instance (aka self) get created, doing checks there means you basically create an object that can potentially be invalid by passing the wrong thing and that's a problem.
class A(object): def __init__(self): raise ValueError('Something bad happened!') def __del__(self): print "Delete Called" def main(): try: a = A() except ValueError, e: print e if __name__ == '__main__': main()
Sure you can (although it forces the user to enclose any instantiation on a try except, but it works). I guess my point is that validation is usually done in the constructor and not initialisation, creating an instance just to destroy it later on without doing any work (contributing to solve the problem the programmer is trying to solve) is a bit wasteful and I would not recommend it as good practice to someone trying to learn.
--
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 python_inside_maya+unsub...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CAPaTLMS434aqjf8AMrLVZTvE%3DCJxXXN7-ChHN%3DMd%2B8wG-6v1%2Bg%40mail.gmail.com.
It forces the user to enclose any instantiation on a try except
class A(object): def __init__(self): raise ValueError('Something bad happened!') def __del__(self): print "Delete Called" def main():
a = A()if __name__ == '__main__': main()
Traceback (most recent call last): File "/Users/alok/Desktop/python.py", line 13, in <module> main() File "/Users/alok/Desktop/python.py", line 10, in main A() File "/Users/alok/Desktop/python.py", line 3, in __init__
raise ValueError('Something bad happened!')
ValueError: Something bad happened! Delete Called
I guess my point is that validation is usually done in the constructor and not initialisation
Validating in __new__ will allow you to catch problems sooner, and give you the option of returning some sort of sentinel instead of just raising an exception, though that is probably not generally good practice.
Sure you can (although it forces the user to enclose any instantiation on a try except, but it works). I guess my point is that validation is usually done in the constructor and not initialisation, creating an instance just to destroy it later on without doing any work (contributing to solve the problem the programmer is trying to solve) is a bit wasteful and I would not recommend it as good practice to someone trying to learn.
On 29 Oct 2016 1:21 PM, "Alok Gandhi" <alok.ga...@gmail.com> wrote:
There's a fundamental problem with this in my view, the dounder init method of a class gets executed _after_ the instance (aka self) get created, doing checks there means you basically create an object that can potentially be invalid by passing the wrong thing and that's a problem.I don't see this as a problem. The __init__() method is for initialization of the instance. If something goes wrong during the initialization, you can always raise from within __init__(). I mean, whatever sanity checks you want to put in for 'filling in' the variables should happen in __init__() because that is the primary role of this method. If anything bad happens and the object becomes 'invalid' python will call __del__() to clean it after itself. So it is perfectly safe to raise from __init__().Here's a snippet exhibiting the call to __del__() by python's memory management when __init__() fail:class A(object): def __init__(self): raise ValueError('Something bad happened!') def __del__(self): print "Delete Called" def main(): try: a = A() except ValueError, e: print e if __name__ == '__main__': main()Would result in:>>>Something bad happened!>>>Delete Called
--
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 python_inside_maya+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CAPaTLMS434aqjf8AMrLVZTvE%3DCJxXXN7-ChHN%3DMd%2B8wG-6v1%2Bg%40mail.gmail.com.
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 python_inside_maya+unsub...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CAPamJi-KnYYb3f1OOWEcMJMmjHeKmy1jzUSvp0hHmZuc%3DsmT9g%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CAPamJi-KnYYb3f1OOWEcMJMmjHeKmy1jzUSvp0hHmZuc%3DsmT9g%40mail.gmail.com.
--
--
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 python_inside_maya+unsub...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CAPaTLMQ1BTMhmveb79Mjpr1E1SGmXqC3BVqYA5Qi32K%2BgftAUw%40mail.gmail.com.
if you are totally closed to the idea of doing something different: well... there's no much benefit coming out of a discussion on the subject.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CAPaTLMQ1BTMhmveb79Mjpr1E1SGmXqC3BVqYA5Qi32K%2BgftAUw%40mail.gmail.com.
--
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 python_inside_maya+unsub...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CAPamJi-b_GUD92GkbmddmxDbeFHQMxAS573Nx_JZc%2BqbBQ1yLQ%40mail.gmail.com.