Skip to content

Instantly share code, notes, and snippets.

@patwooky
Last active March 9, 2023 07:45
Show Gist options
  • Save patwooky/e4f5aa6337dc90368aac6d20773e2e03 to your computer and use it in GitHub Desktop.
Save patwooky/e4f5aa6337dc90368aac6d20773e2e03 to your computer and use it in GitHub Desktop.
This script compares values of each attribute with a "central value" if the values deviate within a certain threshold from the central value, set the value to the central value.
'''
written by Patrick Woo ([email protected])
20230309
version 001_02
change log
v001_02
- now the tool reports number of attributes and number of objects changed
This script compares values of each attribute with a "central value"
if the values deviate within a certain threshold from the central value,
set the value to the central value.
This is good for "fixing" attribute values that show up in channel boxes as -0
when the actual value is a very small number like 0.00005452.
In these cases we may want to just set them to 0 rightaway
eg,
when ctrVal is 0, threhsold is 0.009, these values will result from various incoming values
0.001 => 0
0.00042 => 0
0.009 => 0.009
0.0089 => 0
0.0091 => 0.0091
-0.00045 => 0
-0.009 => -0.009
-0.0089 => 0
-0.0091 => -0.0091
'''
from pprint import pprint as pp
import pymel.core as pm
# pm.getAttr('.rx')
def zeroSmallValues(attrList, ctrVal=0, thresh=0.0009):
'''
zero small values
rounds out small values. read the main doc string above
attrList - <list> a list of string attributes that will be processed. examples:
['tx', 'ty', 'tz']
['translateX', 'translateY', 'translateZ']
['object001.tx', 'object001.rotateY']
ctrVal - <float> the centre value which small fluctuations of value within the threshold range will be snapped to.
thresh - <float> the threshold value which if the values are lesser than this threshold will be ignored and set to the centre value
'''
print(attrList)
changedAttrCounter = 0
changedObjs = [] # contains the names of the changed objects
for thisAttr in attrList:
valDiff = abs(ctrVal - thisAttr.get())
if valDiff < thresh:
thisAttr.set(ctrVal)
print('{} set to {}'.format(thisAttr, ctrVal))
changedAttrCounter += 1
# add the name of the changed object to changedObjs
if not thisAttr.node() in changedObjs:
changedObjs.append(thisAttr.node())
# end if valDiff
# end for thisAttr
print('')
print('{} attributes changed in {} objects'.format(changedAttrCounter, len(changedObjs)))
return
# ----
# main
# ----
# get all selected objects. children in hierarchies need to be explicitly selected
# (not just by selecting the parents)
objList = pm.ls(sl=True)
targetAttrList = ['tx', 'ty', 'tz', 'rx', 'ry', 'rz']
attrList = [pm.PyNode(x).attr(y) for x in objList for y in targetAttrList]
# pp(attrList) # print out for dignostics
# call the function
zeroSmallValues(attrList, ctrVal=0, thresh=0.0009)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment