;

Pymel Blog Rss Feed

Sunday, May 27, 2012


Prior to starting any of these lessons be sure to import the PyMEL library. See here for why and how.



Now let's write a slightly more complex script. This one will create a sphere, a cone, or a cylinder. We'll also be able to choose between poly or nurbs versions of the objects. Lastly, we'll be able tell Maya where to place the newly created object. If you want to see a simpler UI tutorial check this out.

 

Here's the code:

def MakePolyObj(primativeType):
	if(primativeType == 1):
		newObj = polySphere()[0]
	elif(primativeType == 2):
		newObj = polyCube()[0]
	else:
		newObj = polyCone()[0]
	return newObj
	
def MakeNurbsObj(primativeType):
	if(primativeType == 1):
		newObj = sphere()[0]
	elif(primativeType == 2):
		newObj = nurbsCube()[0]
	else:
		newObj = cone()[0]
	return newObj

def MakeObj(typeRBG, floatFieldX, floatFieldY, floatFieldZ, primativeType):
	objType = typeRBG.getSelect()
	xVal = floatFieldX.getValue()
	yVal = floatFieldY.getValue()
	zVal = floatFieldZ.getValue()
	if(objType == 1):
		createdObj = MakePolyObj(primativeType)
	else:
		createdObj = MakeNurbsObj(primativeType)
	createdObj.translate.set(xVal, yVal, zVal)

def WindowUI():
	myWindow = window(t="Test Window", rtf=True)
	mainLayout = columnLayout(w=400, rs=7)
	with mainLayout:
		radioLayout = columnLayout(w=400, co=["left", 115])
		with radioLayout:
			typeRBG = radioButtonGrp(l="Object Type:  ", nrb=2, la2 = ["Poly","Nurbs"], sl=1, cw=[[1,70], [2,50], [3,50]])
		floatFieldMainLayout = columnLayout(w=400, co=["left", 32.5])
		with floatFieldMainLayout:
			floatFieldRowLayout = rowLayout(w=400, nc=7, cw=[[1,80],[2,15],[3,70],[4,15],[5,70],[6,15],[7,70]])
			with floatFieldRowLayout:
				text(l="Enter Position")
				text(l="X: ")
				floatFieldX = floatField()
				text(l="Y: ")
				floatFieldY = floatField()
				text(l="Z: ")
				floatFieldZ = floatField()
		buttonLayout = rowColumnLayout(nc = 3, w=400, cs=[[1,40], [2,5], [3,5]])
		with buttonLayout:
			sphereButton = button(l="Sphere", w=100)
			cubeButton = button(l="Cube", w=100)
			coneButton = button(l="Cone", w=100)
	sphereButton.setCommand(Callback(MakeObj, typeRBG, floatFieldX, floatFieldY, floatFieldZ, 1))
	cubeButton.setCommand(Callback(MakeObj, typeRBG, floatFieldX, floatFieldY, floatFieldZ, 2))
	coneButton.setCommand(Callback(MakeObj, typeRBG, floatFieldX, floatFieldY, floatFieldZ, 3))
	myWindow.show()
WindowUI()

If you understand the basic UI tutorial, then this script should should look approachable. Granted, the UI is a little more complex. I've taken care to specify column widths. For example, on line 39, I create a rowLayout. And I use the 'nc' flag to set the number of columns to 7. Then, using the 'cw' flag, I pass 7 [columnNumber, columnSpacing] pairs to get the labels and floatFields to look how I want them to look.

 

You can also see on lines 53-55, I attach all the buttons to the MakeObj function. The only differences are the numbers we are passing to each callback object.

 

In the MakeObj function, we grab the values off of the floatFields and the radio buttons. Then depending on the value of the radio button, we either create a nurbs or poly object. Finally, depending on the primativeType value, we create either a sphere, a cube, and a cone.

 

Be sure to check out the other tutorials if you need a refresher on built in Maya commands, indentation, or PyMEL classes.

 

Let me know if you have any questions!

My comment system is powered by Disqus. And they require you to put in a name and an email. But I'd love to hear what you think. So if you want to comment anonymously, just put any name and test@test.com for your email.

blog comments powered by Disqus