Classes beginner

77 views
Skip to first unread message

Felix

unread,
Apr 16, 2016, 5:45:11 PM4/16/16
to Python Programming for Autodesk Maya
Hi to everyone! I am starting with Class a few days, and there are many examples on the internet, but not practical examples (Maya), so this is my first class and of course I don't know what is wrong. I hope someone can give me a hand. Greetings!

import maya.cmds as cmds
import maya.mel as mel

class Control(object):
def __init__(self, name, shape, size):
self.name = name
self.shape = shape
self.size = size

def create_me(self):
if self.shape == 'box':
mel.eval( 'curve -d 1 -p -0.5 0.5 0.5 -p 0.5 0.5 0.5;' )
if self.shape == 'circle':
cmds.circle( radius=1 )
if self.shape == 'square':
mel.eval( 'curve -d 1 -p -0.5 0 -0.5 -p 0.5 0 -0.5 -p 0.5 0 0.5 -p -0.5 0 0.5 -p -0.5 0 -0.5' )

cmds.scale( self.size, self.size, self.size )
cmds.makeIdentity( apply=True, t=0, r=0, s=1 )
cmds.rename( self.name )
cmds.select( clear=True )


legControl = Control.create_me('L_leg1_control', 'box', 2)

Justin Israel

unread,
Apr 16, 2016, 8:06:16 PM4/16/16
to Python Programming for Autodesk Maya

Hi

Everything looks fine except for how you call your class

legControl = Control.create_me('L_leg1_control', 'box', 2)

create_me() is called a "method". It is bound to an instance of your class, which means you need to first make an instance of your Control.

legControl = Control('L_leg1_control', 'box', 2)

Now you have constructed an instance, with your params that were passed to your __init__()

You can call create_me() to perform that extra set up.

legControl.create_me()

Note that your create_me method does not return anything. It only performs actions on your instance.

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/b6eddbbc-8d18-415e-85c7-b3d15c423c14%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Felix

unread,
Apr 17, 2016, 2:30:38 AM4/17/16
to Python Programming for Autodesk Maya
Thanks Justin! for the reply, that help me alot. Saludos!
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsub...@googlegroups.com.

haggi...@gmail.com

unread,
Apr 17, 2016, 6:38:18 AM4/17/16
to Python Programming for Autodesk Maya
I'd say you can put everything into the __init__() method. If I see it correctly, then you do not return anything from create_me() and you do not give create_me() additional arguments. The class is (at the moment) useless without the create_me(). So you can call create_me() from the __init__() method what would create your geometry automatically if you call:

legControl = Control('L_leg1_control', 'box', 2)

Without the need to call another method.
Reply all
Reply to author
Forward
0 new messages