Creating a generic method to handle Creating Primitives?

32 views
Skip to first unread message

I73

unread,
Nov 22, 2016, 2:59:24 PM11/22/16
to Python Programming for Autodesk Maya
Hey I am trying to create a method to handle creating new objects, with a dictionary

class GenerationProperties():
    primitives
= {
       
'sphere' : cmds.polySphere,
       
'geosphere' : cmds.polyPrimitive,
       
'cube' : cmds.polyCube,
       
'cylinder' : cmds.polyCylinder,
       
'cone' : cmds.polyCone,
       
'plane' : cmds.polyPlane,
       
'torus' : cmds.polyTorus,
       
'prism' : cmds.polyPrism,
       
'pyramid' : cmds.polyPyramid,
       
'pipe' : cmds.polyPipe,
   
}


   
def CreatePrimitive(self, primitiveType, subdivisionsX = 1, subdivisionsY = 1):
        prim
= self.primitives[primitiveType]()
       
#how can I refrence prim to change it's subdivisions after I created?
       
# cmds.'Type?'(prim, subdivisionsX, subdivisionsY)



The problem that I am having is I don't know how I can use cmds.poly'Type'() as a generic (cast) to change the subdivisions or edit after I create the primitive. 


Any help would be appreciated! 

Justin Israel

unread,
Nov 22, 2016, 3:45:10 PM11/22/16
to python_in...@googlegroups.com
I could see two different ways. Since you already have the proper command mapped, you can just use it in edit mode:

fn = self.primitives[primitiveType]
xform, shape = fn()
fn(shape, e=True, sx=30)

or

prim = self.primitives[primitiveType]
cmds.setAttr("%s.subdivisionHeight" % prim[1], 30)

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 python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/331d4f46-5bb2-45dd-9b9d-bde6b7caff91%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Wesley Keeling

unread,
Nov 22, 2016, 4:21:57 PM11/22/16
to python_in...@googlegroups.com
Justin to the rescue again! Thanks so much man! 

To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsub...@googlegroups.com.

--
You received this message because you are subscribed to a topic in the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/python_inside_maya/xkXvEPPRrzg/unsubscribe.
To unsubscribe from this group and all its topics, 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/CAPGFgA0-txhFXCABd6-W2whij-cffCu5uTBGk5zST%3DsBHXQk5w%40mail.gmail.com.

Justin Israel

unread,
Nov 22, 2016, 4:54:49 PM11/22/16
to python_in...@googlegroups.com
On Wed, Nov 23, 2016 at 10:21 AM Wesley Keeling <wesley....@iugome.com> wrote:
Justin to the rescue again! Thanks so much man! 

No worries!
 
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.

--
You received this message because you are subscribed to a topic in the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/python_inside_maya/xkXvEPPRrzg/unsubscribe.
To unsubscribe from this group and all its topics, send an email to python_inside_m...@googlegroups.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/CAAD4CW6%3DiavvS89wGMCVV%3DGt0Fof02epvXcnBsKGOKc7M_Dixg%40mail.gmail.com.

Wesley Keeling

unread,
Nov 22, 2016, 5:18:28 PM11/22/16
to python_in...@googlegroups.com
I do have one last question since each polly command takes in multiple commands to settAttr [subdivisionsAxis, subdivisionsHeight, subdivisionsWidth, etc] It won't really help me to settAttr in a generic way. 
 
The lambda (if I understand it correctly):
fn = self.primitives[primitiveType] xform, shape = fn() fn(shape, e=True, sx=30)

Fits perfectly for what I need, just one last question. How can I set the scale property from fn()? I cannot use scale as a flag, it's tossing a error 'No flag name 'scale'.

If it's also not too much to ask can I confirm the logic of those lines?
I am assigning fn to the dictonary and then calling it as a function with the properties of Xform on the 'shape'?

Thanks again man!


To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsub...@googlegroups.com.

--
You received this message because you are subscribed to a topic in the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/python_inside_maya/xkXvEPPRrzg/unsubscribe.
To unsubscribe from this group and all its topics, send an email to python_inside_maya+unsub...@googlegroups.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.

--
You received this message because you are subscribed to a topic in the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/python_inside_maya/xkXvEPPRrzg/unsubscribe.

Justin Israel

unread,
Nov 22, 2016, 6:18:52 PM11/22/16
to python_in...@googlegroups.com
On Wed, Nov 23, 2016 at 11:18 AM Wesley Keeling <wesley....@iugome.com> wrote:
I do have one last question since each polly command takes in multiple commands to settAttr [subdivisionsAxis, subdivisionsHeight, subdivisionsWidth, etc] It won't really help me to settAttr in a generic way. 
 
The lambda (if I understand it correctly):

It isnt a lambda. A lambda looks like this:

fn = lambda x: x+1

It is just a reference to a function (callable) object
 
fn = self.primitives[primitiveType] xform, shape = fn() fn(shape, e=True, sx=30)

Fits perfectly for what I need, just one last question. How can I set the scale property from fn()? I cannot use scale as a flag, it's tossing a error 'No flag name 'scale'.

If not all of those commands support a "scale" argument, then you won't be able to do it that way. You would have to have another way to switch on the type to return a function that can apply a scale.

Do you just mean scaling via cmds.xform() ? That should work with any of the objects.
 

If it's also not too much to ask can I confirm the logic of those lines?
I am assigning fn to the dictonary and then calling it as a function with the properties of Xform on the 'shape'?

You are assigning fn to be a reference to a function, which was stored as a value in your dictionary. You looked up that value by a primitive type.
You then call that callable, which is stored in the variable "fn", and split the two item tuple that is returned into two variables. "xform" contains the transform of the new object, and "shape" contains the shape.
Then you call that callable stored in "fn" again, passing it the shape and some other arguments.
 
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.

--
You received this message because you are subscribed to a topic in the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/python_inside_maya/xkXvEPPRrzg/unsubscribe.
To unsubscribe from this group and all its topics, send an email to python_inside_m...@googlegroups.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.

--
You received this message because you are subscribed to a topic in the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/python_inside_maya/xkXvEPPRrzg/unsubscribe.
To unsubscribe from this group and all its topics, send an email to python_inside_m...@googlegroups.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/CAAD4CW6%3DoLAD1C8paThXvq1M8PyhFnX_ccpooBLqMPNB3e9Viw%40mail.gmail.com.

Wesley Keeling

unread,
Nov 22, 2016, 6:54:35 PM11/22/16
to python_in...@googlegroups.com
Do you just mean scaling via cmds.xform() ? That should work with any of the objects.

Yeah I would think so too: 

fn(shape, e=True, sx=_sx, sy=_sy, scale=_scale)

# Error: TypeError: file Generation.py line 95: Invalid flag 'scale' # 


To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsub...@googlegroups.com.

--
You received this message because you are subscribed to a topic in the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/python_inside_maya/xkXvEPPRrzg/unsubscribe.
To unsubscribe from this group and all its topics, send an email to python_inside_maya+unsub...@googlegroups.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.

--
You received this message because you are subscribed to a topic in the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/python_inside_maya/xkXvEPPRrzg/unsubscribe.
To unsubscribe from this group and all its topics, send an email to python_inside_maya+unsub...@googlegroups.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.

--
You received this message because you are subscribed to a topic in the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/python_inside_maya/xkXvEPPRrzg/unsubscribe.
To unsubscribe from this group and all its topics, 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/CAPGFgA3F-JsXtbcwJQwARnYz7i_sZ36G%2BPGD6UwW_gFy0SwpGw%40mail.gmail.com.

Wesley Keeling

unread,
Nov 22, 2016, 7:05:13 PM11/22/16
to python_in...@googlegroups.com
It seems that it's not calling the xForm command:

 Error: TypeError: file Generation.py line 95: Invalid flag 'deletePriorHistory'

and that's clearly a Xform flag:

To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsubscribe@googlegroups.com.

--
You received this message because you are subscribed to a topic in the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/python_inside_maya/xkXvEPPRrzg/unsubscribe.
To unsubscribe from this group and all its topics, send an email to python_inside_maya+unsubscribe@googlegroups.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.

--
You received this message because you are subscribed to a topic in the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/python_inside_maya/xkXvEPPRrzg/unsubscribe.
To unsubscribe from this group and all its topics, send an email to python_inside_maya+unsubscribe@googlegroups.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.

--
You received this message because you are subscribed to a topic in the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/python_inside_maya/xkXvEPPRrzg/unsubscribe.
To unsubscribe from this group and all its topics, send an email to python_inside_maya+unsubscribe@googlegroups.com.

Wesley Keeling

unread,
Nov 22, 2016, 7:09:35 PM11/22/16
to python_in...@googlegroups.com
I got it working, thank you for all your help!!!!!
Reply all
Reply to author
Forward
0 new messages