Search code examples
pythoncomparemayagetattr

Compare and grab items attributes


I have 3 locators in my scene, eg.
Locator01 - localScaleY value of 1
Locator02 - localScaleY value of 2
Locator03 - localScaleY value of 3

Each with a varying value in its localScaleY. I had wanted to compare this 3 Locators' localScaleY values and grab the one which is the highest (in this case it will be Locator03)

yMax = []
for yValue in pm.ls('locator*'):
    yMax.append(getAttr (yValue +'.localScaleY'))
    yMaxValue = max(yMax)
    print yMaxValue

So based on the above coding, is it a viable way to write as I will be comparing more items? Or perhaps there is a better way?


Solution

  • Build a generator of scale/object tuples, and take the max of that. By putting the scale first, max keys off that correctly.

    locators = ((getAttr(locator+'.localScaleY'), locator) for locator in pm.ls('locator*'))
    yMaxValue, locator = max(locators)
    

    A few outputs for reference:

    >>> list(locators)
    # Result: [(1.0, nt.Transform(u'locator01')),
               (2.0, nt.Transform(u'locator02')),
               (3.0, nt.Transform(u'locator03')),
               (1.0, nt.Locator(u'locator0Shape1')),
               (2.0, nt.Locator(u'locator0Shape2')),
               (3.0, nt.Locator(u'locator0Shape3'))] # 
    >>> yMaxValue
    # Result: 3.0 # 
    >>> locator
    # Result: nt.Locator(u'locator0Shape3') #