Isotropix Forums

Sorting attribute presets by value

Clarisse Scripting related topics

Sorting attribute presets by value

Unread postby jboissinot » Tue Sep 24, 2019 3:22 pm

Hi,

Let say I have an attribute on an object that is set with multiple string presets in a dropdown list and I want to update the attribute list with other presets while keeping the presets ordered by value name. As such, I was wondering if it'd be possible to easily sort the presets of an object attribute by preset value? If not, my plan was to get all presets, remove them, sort them by value to eventually recreate the presets sorted by value.
Anyhow, I was just curious to know what you would recommend for doing this?

Thanks,
Jeremy
jboissinot
 
Posts: 75
Joined: Tue Jan 29, 2019 10:36 pm

Re: Sorting attribute presets by value

Unread postby anemoff » Tue Sep 24, 2019 5:07 pm

Hi,

There is no built-in API to reorder attribute presets, or automatically re-arrange them.

Your plan is the way to go:

python code

# 1. store current preset labels and values
presets = []
for i in range(attr.get_preset_count()):
presets.append((attr.get_preset_label(i), attr.get_preset_value(i)))

# 2. add the new ones to the lists
# ...

# 3. sort them:
# by label
from operator import itemgetter

presets.sort(key=itemgetter(0))
print 'by label', presets

# or by value
presets.sort(key=itemgetter(1))
print 'by value', presets

# 4. remove all presets from the attribute
attr.remove_all_presets()

# 5. add the new sorted presets to the attribute
for p in presets:
attr.add_preset(p[0], p[1])


Cheers,
Anthony Nemoff
Isotropix
R&D Engineer
User avatar
anemoff
 
Posts: 307
Joined: Wed Jan 13, 2016 10:10 am

Re: Sorting attribute presets by value

Unread postby jboissinot » Fri Sep 27, 2019 2:06 pm

Thanks for confirming this Anthony!

Jeremy
jboissinot
 
Posts: 75
Joined: Tue Jan 29, 2019 10:36 pm


Return to Scripting