Recently I've dipped into some 3D Studio Max work. In the process of learning 3D Studio's modelling tools, I found the align objects tool highly useful.
It's premise is simple: take in an array of points and then align them to the grid by finding the "average" in the requested axis. Here is a screenshot to give an idea of what the tool does:
Unfortunately in Maya there wasn't a button like this in the toolbar shelf. While there is the average vertices function, I found this method of control more finite and quicker . Since the premise is simple, I decided to write a set of scripts to do the same thing. All are written in Python with the simple xform.
If you haven't already, it's wise to run this script to allow Maya's Python commands object to be imported and utilized in the local namespace.
import maya.cmds as cmds
Additionally, you can use any word in place "cmds" but since all my functions reference the maya.cmds object, you'll need to change the reference to "cmds" in each script. I recommend just using "cmds", it's easy enough to copy and paste.
X Align:
# Average X position of verts selectedVerts = cmds.ls(sl=1,fl=1) #grabs the selected vertices (this could be anything, really). The fl=1 argument setting is to make sure Maya selects all the vertices that belong to the mesh object. sumVertX = 0 # the sum of all the X translations count = len(selectedVerts) # the number of objects selected for x in selectedVerts: #for each selected object sumVertX += (cmds.xform(x,q=1,t=1))[0] #get and add all the first values translation [0] from the xform function's list that it returns avgVertX = sumVertX / count #average the translations for x in selectedVerts: #apply the result to each selected objects #following: xform requires a list of translations to be passed to it when manipulating the values [x,y,z] format, you will give it the avgVertX value first, then for y,z we will reference the current value by querying the current values. cmds.xform(x,t=[avgVertX,(cmds.xform(x,q=1,t=1))[1],(cmds.xform(x,q=1,t=1))[2]])
Y Align:
# Average Y position of verts selectedVerts = cmds.ls(sl=1,fl=1) sumVertY = 0 count = len(selectedVerts) for x in selectedVerts: sumVertY += (cmds.xform(x,q=1,t=1))[1] avgVertY = sumVertY / count for x in selectedVerts: cmds.xform(x,t=[(cmds.xform(x,q=1,t=1))[0],avgVertY,(cmds.xform(x,q=1,t=1))[2]])
Z Align:
# Average Z position of verts selectedVerts = cmds.ls(sl=1,fl=1) sumVertZ = 0 count = len(selectedVerts) for x in selectedVerts: sumVertZ += (cmds.xform(x,q=1,t=1))[2] avgVertZ = sumVertZ / count for x in selectedVerts: cmds.xform(x,t=[(cmds.xform(x,q=1,t=1))[0],(cmds.xform(x,q=1,t=1))[1],avgVertZ])
Additionally, you can combine these into a single script with a few conditionals, I present them here as three separate scripts for simplicity of learning.
Icons
Hope this is helpful!