Need Serious Help - Python in Maya

Need Serious Help - Python in Maya

snt170130
Community Visitor Community Visitor
471 Views
1 Reply
Message 1 of 2

Need Serious Help - Python in Maya

snt170130
Community Visitor
Community Visitor
Hello,
I'm a senior and this is my last semester at college. I'm in an Animation pathway but I could never get coding down very well, even after 3 classes it's still so foreign to me. I need help with my first assignment, if I don't pass this class I won't be able to graduate. I'm so tired of college I just want it to be done, and I'm so afraid of failing this class. If anyone can lend any help in how to write this code out and have it run in Maya, I'd be much appreciated.
We are supposed to write the code in Py-Charm and then put it in Maya 2022.
 
 
All details are in the attached document.
0 Likes
472 Views
1 Reply
Reply (1)
Message 2 of 2

Kahylan
Advisor
Advisor

Hi!

 

Normally this forum is meant to ask specific questions and not to get other people to do your homework.
But I get it, college can be tuff, so I'll help you out this time. Also because I was bored today tbh.

I haven't used py-charm in a while, so you'll have to figure out how to get the directorys right yourself. But basically this is the code that you want in your stacker Module:

import maya.cmds as mc


def stack_objs(o1 = '', o2 = '', o3 = ''):
	"""
	stacks objects on top of each other. Takes three arguments (string), o1, o2, o3.
	Names of the objects to be stacked
	"""

	#check if correct arguments given
	va = verify_args(o1,o2,o3)
	if va == None:
		mc.warning("one or more of the given arguments are not transforms")
		return None

	#stacking o2 on o1
	tc = get_center_point(obj = o1, cl = -2)
	bc = get_center_point(obj = o2, cl = 1)
	create_stack(o2, bc = bc, tc = tc)
	#stacking o3 on o2
	tc = get_center_point(obj = o2, cl = -2)
	bc = get_center_point(obj = o3, cl = 1)
	create_stack(o3, bc = bc, tc = tc)
	return True


def create_stack(obj = '', bc = [], tc = []):
	"""
	stacks top objects on top of each other. Takes three arguments
	obj (string) for name of the object to be stacked,
	bc (list) for the bottom center point of the object (needs to contain three numeric values),
	tc (list) for the top center point of the object to be stacked on (needs to contain three numeric values),
	"""

	#moving object to origin
	mc.move((-1)*bc[0],(-1)*bc[1],(-1)*bc[2],obj,r = True, ws = True)
	#moving object to tc
	mc.move(tc[0],tc[1],tc[2],obj,r = True, ws = True)
	

def get_center_point(obj = None, cl =1):
	"""
	Gets bounding box information of given object (obj), returing top or bottom center point, 
	accornding to centerlevel (cl) bottom = 1, top = (-2)
	"""
	#getting bounding box information
	cp = mc.xform(obj, q= True, bb = True, ws = True)
	#calculation mid x and mid z
	cx = cp[0] + ((((cp[3]-cp[0])**2)**0.5)/2)
	cz = cp[2] + ((((cp[5]-cp[2])**2)**0.5)/2)

	return (cx,cp[cl],cz)

def verify_args(o1= '', o2 ='',o3= ''):
	"""
	Verifies if given arguments contain information and are transforms
	"""
	check = None
	#run checks on all givem arguments
	for arg in [o1,o2,o3]:
		if arg != '':
			if mc.objExists(arg)==True:
				if mc.objectType(arg, isType = 'transform'):
					check = True
				else:
					mc.warning('{0} is not a transform Node'.format(arg))
					check = None
					break
			else:
				mc.warning('{0} does not exist'.format(arg))
				check = None
				break
		else:
			mc.warning("empty argument given")
			check = None
			break

	return check

 

I should mention that I didn't follow the instructions as religiously as your instructor seems to want you to in two instances.
1) The "get_center_point "function. Your instructor specified that he wants two flags, one for top, one for bottom. But that didn't really make sense in the logic for me, since those flags would be mutally exclusive because the return value needs to be a tuple with three values for the rest of the script to work. So I decided to have one flag instead with two different values. One for top and one for bottom.

2) The "verify_args" function. Your instructor advised you to only check if all the given arguments have a value. Which would look like this:

def verify_args(o1= '', o2 ='',o3= ''):

	if '' in [o1,o2,o3]:
		return None

	return True

 But this doesn't check if the objects given exist or if they are transforms, which is vital for the rest of the code to work, so I added an objExists and an isType check into it as well. And I gave each a specific warning if they are False.

Also, sice I was bored I decided to modify the script so it doesn't only take three objects and so it takes the selected objects as arguments if no object names are specified.

import maya.cmds as mc


def stack_objs(*args):
	"""
	stacks objects on top of each other. Takes three arguments (string), o1, o2, o3.
	Names of the objects to be stacked
	"""
	#storing given arguments in objects
	objects = []
	for a in args:
		objects.append(a)

	#switch to selection if not enough arguments given
	if len(objects) < 2:
		objects = mc.ls(selection = True, et = "transform") or []
		if len(objects) < 2:
			mc.warning('must specify or select atleats 2 objects')
			return None

	#check if correct arguments given
	va = verify_args(*objects)
	if va == None:
		mc.warning("one or more of the given arguments are not transforms")
		return None

	for a in range(0, len(objects)-1):
		#stacking objects
		tc = get_center_point(obj = objects[a], cl = -2)
		bc = get_center_point(obj = objects[a+1], cl = 1)
		create_stack(objects[a+1], bc = bc, tc = tc)

	return True


def create_stack(obj = '', bc = [], tc = []):
	"""
	stacks top objects on top of each other. Takes three arguments
	obj (string) for name of the object to be stacked,
	bc (list) for the bottom center point of the object (needs to contain three numeric values),
	tc (list) for the top center point of the object to be stacked on (needs to contain three numeric values),
	"""

	#moving object to origin
	mc.move((-1)*bc[0],(-1)*bc[1],(-1)*bc[2],obj,r = True, ws = True)
	#moving object to tc
	mc.move(tc[0],tc[1],tc[2],obj,r = True, ws = True)
	

def get_center_point(obj = None, cl =1):
	"""
	Gets bounding box information of given object (obj), returing top or bottom center point, 
	accornding to centerlevel (cl) bottom = 1, top = (-2)
	"""
	#getting bounding box information
	cp = mc.xform(obj, q= True, bb = True, ws = True)
	#calculation mid x and mid z
	cx = cp[0] + ((((cp[3]-cp[0])**2)**0.5)/2)
	cz = cp[2] + ((((cp[5]-cp[2])**2)**0.5)/2)

	return (cx,cp[cl],cz)

def verify_args(*args):
	"""
	Verifies if given arguments contain information and are transforms
	"""
	check = None
	#run checks on all givem arguments
	for arg in args:
		if arg != '':
			if mc.objExists(arg)==True:
				if mc.objectType(arg, isType = 'transform'):
					check = True
				else:
					mc.warning('{0} is not a transform Node'.format(arg))
					check = None
					break
			else:
				mc.warning('{0} does not exist'.format(arg))
				check = None
				break
		else:
			mc.warning("empty argument given")
			check = None
			break

	return check

 

However, now that you can see how the code looks like. I would hope that you take your time to really read it trough and try to understand it. Otherwise you'll just end up stuck on next exercise... Also understanding how to code simple scripts like this can really make your day to day work with Maya easier.

Feel free to ask questions if you don't understand something.

I hope this helps!

0 Likes