Anchor Line



'''
@author: ksaville
@summary: draw an unselectable line between two selected objs 
'''

import maya.cmds as mc


def anchor_button ():
    '''
    If there is two objs selected run anchor_line with the two selected objs as the targets.
    Otherwise, return a warning
    '''
    selected = mc.ls (sl=1, fl=1)
    print len(selected)
    if len(selected) == 2:
        obj1 = selected[0]
        obj2 = selected[1]
        anchor_line(obj1, obj2)
    else:
        mc.warning("Select two objects")

def anchor_line (obj1, obj2, name=None, selectable=False):
    '''
    Create a line from obj1 to obj2.
    Cluster and constrain the line's cv's to obj1 and obj2
    ...
    Return the names of the line and clusters. 
    '''
    subj1 = obj1
    subj2 = obj2
    
    if name is None:
        name = (subj1)+"_Anchor_Line" 
    crv = mc.curve(name=name, p=[(0, 0, 1), (0, 0, -1)], d=1)
    cluster1 = mc.cluster(crv + ".cv[0]")[1]
    cluster2 = mc.cluster(crv + ".cv[1]")[1]
    cluster1 = mc.rename(cluster1, subj1+"_Clu")
    cluster2 = mc.rename(cluster2, subj2+"_Clu")
    mc.pointConstraint(obj1, cluster1)
    mc.pointConstraint(obj2, cluster2)
    clu1vis = str(cluster1) + ".visibility"
    clu2vis = str(cluster2) + ".visibility"
    mc.setAttr(clu1vis, 0)
    mc.setAttr(clu2vis, 0)
    
    mc.group(cluster1, cluster2, crv, n=crv+"_Grp")
    
    template= (1-selectable)
    mc.setAttr(name + ".overrideEnabled", 1)
    mc.setAttr(name + ".template", template)
    
    return [crv, cluster1, cluster2]
Advertisements