Saturday, July 22, 2017

python modeling script align vertices to selected in axis

Hi,

Today I was working on fixing the topology of a personal dog character I was modeling and I wanted the edge flow to be aligned in an axis.

This was a little helpful python script i wrote to align all the vertices selected to the last selected object in the axis chosen. (should work in front view by changing parameter to appropriate axis)

There may be bugs but hope you find it helpful.

first select all vertices you want to align then last select the object you want to align to in given axis.
translate x should be (axis=0) ty (axis=1) and tz (axis=2)
def alignToAxis(axis = 1):
    #axis = 1 #ty
    print 'align first selected vertices to last thing'
    sel = cmds.ls(selection=True)
    if not sel:
        return
        
    pos_obj = sel[-1]
    vertices = cmds.filterExpand(sm=31)
    pos = cmds.xform( pos_obj, translation=True, ws = True, query=True)
    for v in vertices:
        pos_v = cmds.xform( v, translation=True, ws = True, query=True)
        pos_v[ axis ] = pos[ axis ]
        cmds.xform( v, translation = pos_v, ws = True)
    
alignToAxis(axis = 1)

and

this one is helpful for making locator at selected vertex. you can use this locator or move it to where you want vertices to align to.
def locatorAtSelected():
    print 'put locator at selected thing'
    sel = cmds.ls(selection=True)
    if not sel:
        return
    pos = cmds.xform( sel[0], translation=True, ws = True, query=True)
    loc = cmds.spaceLocator()
    cmds.xform( loc[0], translation = pos, ws = True)
locatorAtSelected()

Happy Sketching!
Nate