Skip to content

Instantly share code, notes, and snippets.

@Onefabis
Last active January 11, 2019 22:36
Show Gist options
  • Save Onefabis/2fa4a3517d0e319bfba5a7fa0c0e59eb to your computer and use it in GitHub Desktop.
Save Onefabis/2fa4a3517d0e319bfba5a7fa0c0e59eb to your computer and use it in GitHub Desktop.
angle measure construction script that show angle in real time between two vectors
'''
Select three components and run the script.
Remember that selection order matters, so the second one will display the angle between 1-2 vector and 2-3 vector.
'''
import maya.cmds as mc
# make sure that tracking of selection order is turned on
if not mc.selectPref( q=1, trackSelectionOrder=1 ):
mc.selectPref( trackSelectionOrder=1 )
# get ordered component selection
objs = mc.ls( os=1, fl=1 )
# script will work if there are only three components selected
if len( objs ) == 3:
# create locators from selection objects
grps = []
decs = []
for o in objs:
# create empty group
grp = mc.group( em=1, n= o + '_grp' )
# edit group visibility by turning on its handle
mc.setAttr( grp + '.displayHandle', 1 )
# replace the selection with component and appropriate component
mc.select( o, r=1 )
mc.select( grp, add=1 )
# create pointOnPolyConstraint so each empty group will follow to appropriate component
pCon = mc.pointOnPolyConstraint()
# create decompose matrix and connect it to the worl space matrix of the empty group
dec = mc.createNode( 'decomposeMatrix' )
mc.connectAttr( grp + '.worldMatrix', dec + '.inputMatrix' )
# add decompose matrix name for the further manipulations
decs.append( dec )
# store the second group name so it will drive angle annotation position
if o == objs[1]:
grps = grp
# store two pairs of decompose matrices
pairs = [ (decs[0],decs[1]), (decs[2],decs[1]) ]
# create angle node that will output angle between two vectors: vector1( selection1-selection2 ) and vector2( selection3-selection2 )
angle = mc.createNode( 'angleBetween' )
# since we know that we have only two pairs we just iterate through both of them
for k in xrange(2):
# create distance node in order to connect to the pairs of decompose matrix of every pair of empty groups
dim = mc.createNode('distanceDimShape')
mc.connectAttr( pairs[k][0] + '.outputTranslate', dim + '.startPoint' )
mc.connectAttr( pairs[k][1] + '.outputTranslate', dim + '.endPoint' )
# add PlusMinusAverage node for extracting vector from selection1-selection2 and selection3-selection2
pma = mc.createNode( 'plusMinusAverage' )
# set Subtract operation of the PMA node
mc.setAttr( pma + '.operation', 2 )
mc.connectAttr( pairs[k][0] + '.outputTranslate', pma + '.input3D[0]' )
mc.connectAttr( pairs[k][1] + '.outputTranslate', pma + '.input3D[1]' )
# connect PMA vector output to appropriate input in angleBetween node
mc.connectAttr( pma + '.output3D', angle + '.vector' + str(k+1) )
# now angleBetween node calculate angle between two vectors, let's add a node (in my case it is a particle node) that will show that value in viewport
part = mc.particle( p=(0,0,0) )
# add custom double type attribute that will receive angle value from angleBetween node
mc.addAttr( part[-1], ln='angle', at='double' )
mc.setAttr( part[-1] + '.particleRenderType', 2)
# add boring stuff to connect custom attribute to displayed one
mc.connectAttr( angle + '.axisAngle.angle', part[-1] + '.angle' )
mc.addAttr( part[-1], ln='pointSize', at='long', min=1, max=60, dv=2 )
mc.addAttr( part[-1], ln='selectedOnly', at='bool', dv=0 )
mc.addAttr( part[-1], dt='string', ln='attributeName' )
# tell particle node which attribute we want to display
mc.setAttr( part[-1] + '.attributeName', 'angle' , type='string' )
# change our attribute default color to yellow one
mc.setAttr( part[-1] + '.overrideEnabled', 1 )
mc.setAttr( part[-1] + '.overrideRGBColors', 1 )
mc.setAttr( part[-1] + '.overrideColorR', 1 )
mc.setAttr( part[-1] + '.overrideColorG', 1 )
mc.setAttr( part[-1] + '.overrideColorB', 0.2 )
# connect position of the particle notation node to the second group, so it will show the angle right on place
mc.connectAttr( grps + '.translate', part[0] + '.translate' )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment