import sys
import os
import errno
import json
import re

import maya.cmds as cmd
import maya.mel as mel

def pTack(name='',makeLoc=False):
    sels = cmd.ls(sl=True)
    if sels:
        if '.vtx[' in sels[0]:
            tack(name=name,makeLoc=makeLoc)
        else:
            for i in range(0,(len(sels)-1)):
                cmd.select(cl=True)
                pos = cmd.xform(sels[i], q=True, t=True, os=True)
                overridePos = {}
                overridePos['x'] = pos[0]
                overridePos['y'] = pos[1]
                overridePos['z'] = pos[2]
                cmd.select(sels[-1], r=True)
                tack(name=name,makeLoc=makeLoc,overridePos=overridePos)
                cmd.select(cl=True)

def tack(name='',makeLoc=False,overridePos={'x':0,'y':0,'z':0}):
    '''
    Tack. A follicle based node that tracks and accuratly follows a point on a given geo.
    Simply select a vertex on a polygon object and run this fumction.
    This function returns a dictionary : [ follicle, follicleShape, locatorShape ]
    '''
    item = {}
    if not name:
        name = 'tack'
    fault = 0
    sels = cmd.ls(sl=True)
    if sels:
        for sel in sels:
            # - Get Object Name
            pointPos = []
            if '.vtx[' in sel:
                object = sel.split('.')[0]
                vert = sel.split('.')[1]
                pointPos = cmd.xform(sel, q=True, t=True, os=True)
            elif '.' in sel:
                object = sel.split('.')[0]
            else:
                object = sel
            
            if 'x' in overridePos.keys() and 'y' in overridePos.keys() and 'z' in overridePos.keys():
                if overridePos['x'] or overridePos['y'] or overridePos['z']:
                    pointPos = [overridePos['x'],overridePos['y'],overridePos['z']]
            else:
                cmd.error('\"overridePos\" flag is incorrect! Do \"overridePos={\"x\":0,\"y\":0,\"z\":0}\"')
            
            if pointPos:
                # - Create Nodes
                pointPosUV = cmd.createNode('closestPointOnMesh', n='{0}_CPOM'.format(object))
                follicle = cmd.createNode('transform', n=name)
                follicleShape = cmd.createNode('follicle', n='{0}Shape'.format(name), p=follicle)
                cmd.setAttr('{0}.visibility'.format(follicleShape), 0)
                cmd.setAttr('{0}.hiddenInOutliner'.format(follicleShape), 1)
                
                # - Connect Nodes
                cmd.setAttr('{0}.inPositionX'.format(pointPosUV), pointPos[0])
                cmd.setAttr('{0}.inPositionY'.format(pointPosUV), pointPos[1])
                cmd.setAttr('{0}.inPositionZ'.format(pointPosUV), pointPos[2])
                cmd.connectAttr('{0}.outMesh'.format(object), '{0}.inMesh'.format(pointPosUV), f=True)
                u = cmd.getAttr('{0}.parameterU'.format(pointPosUV))
                v = cmd.getAttr('{0}.parameterV'.format(pointPosUV))
                cmd.connectAttr('{0}.outMesh'.format(object), '{0}.inputMesh'.format(follicleShape), f=True)
                cmd.connectAttr('{0}.worldMatrix'.format(object), '{0}.inputWorldMatrix'.format(follicleShape), f=True)
                cmd.setAttr('{0}.parameterU'.format(follicleShape), u)
                cmd.setAttr('{0}.parameterV'.format(follicleShape), v)
                cmd.connectAttr('{0}.outTranslate'.format(follicleShape), '{0}.translate'.format(follicle), f=True)
                cmd.connectAttr('{0}.outRotate'.format(follicleShape), '{0}.rotate'.format(follicle), f=True)
                
                cmd.delete(pointPosUV)
                
                item['follicle'] = follicle
                item['follicleShape'] = follicleShape
                # - Add Locator
                if makeLoc:
                    locatorShape = cmd.createNode('locator', n='{0}_locator'.format(follicle), p=follicle)
                    item['locatorShape'] = locatorShape
            else:
                fault = 1
    else:
        cmd.headsUpMessage('Select one or more vertices on a polygon object.')
    
    if fault:
        cmd.headsUpMessage('One or more of the selection(s) is not a supported type. Select one or more DAG objects (Transforms or Polygon Vertex).')
    return item

Tags:

Comments are closed