On Thu, Mar 16, 2017 at 1:57 PM yann19 <[email protected]> wrote:
Suppose if the push buttons are 'Cube' and 'Duplicate', and I checked
scale_x withthe min value being 1.0
while max value is 2.0, how should I write in `create_node()`?
Should I store such variables as a dictionary or some sort so that it will
be easier for me to call and use
the values?
I am not sure what is the best way for me to call such values..
FYI, I have quite some widgets in my ui in which I need them for my new
node creations
First, I would recommend repeating string literals as little as possible.
It is prone to error if you make a typo somewhere or change the naming.
TYPE_CUBE = "Cube"
ACTION_DUP = "Duplicate"
def which_geo(self):
if self.geo_btn.text() == TYPE_CUBE:
...
def which _cmd(self):
if self.cmd_btn.text() == ACTION_DUP:
...
In order to handle these types and actions more dynamically, as you
request, you could accomplish as simply as with a dictionary, or you could
organize things into classes.
TYPE_SPHERE = "Sphere"
TYPE_CUBE = "Cube"
ACTION_DUP = "Duplicate"
ACTION_INST = "Instance"
def sphere_duplicate(*args, **kwargs):
#...
action_map = {
TYPE_SPHERE: {
ACTION_DUP: sphere_duplicate,
ACTION_INST: sphere_instance,
},
TYPE_CUBE: {
ACTION_DUP: cube_duplicate,
ACTION_INST: cube_instance,
},
}
This creates a mapping of object types to actions and functions.
If you wanted to be more organized than this, you could do it with classes.
class ObjectActioner(object):
NAME=""
def callByName(self, name, *args, **kwargs):
name = name.lower()
fn = getattr(self, name)
return fn(*args, **kwargs)
def duplicate(self, *args, **kwargs):
raise NotImplementedError()
def instance(self, *args, **kwargs):
raise NotImplementedError()
class Sphere(ObjectActioner):
NAME = "Sphere"
def duplicate(self, *args, **kwargs):
#...
def instance(self, *args, **kwargs):
#...
class Cube(ObjectActioner):
NAME = "Cube"
#...
object_map = {
Sphere.NAME: Sphere,
Cube.NAME: Cube,
}
Once you have some kind of mapping, then it is just a matter of looking up
the correct object or function and passing it the arguments in your
create_node() function. This would be instead of having to do complicated
"if then" logic in create_node()
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/cf106490-d8d7-41c2-82fd-a8312e36cbb8%40googlegroups.com
<https://groups.google.com/d/msgid/python_inside_maya/cf106490-d8d7-41c2-82fd-a8312e36cbb8%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/CAPGFgA0-ihYdkAS1jYpyeQb0cGjJ4zEiqs6yuA2PVzkF9FRmMQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.