Isotropix Forums

Useful script: finding color space attributes

Clarisse Scripting related topics

Useful script: finding color space attributes

Unread postby anemoff » Mon Nov 09, 2020 7:52 pm

Hi all,

Here is a script with 3 functions to find color space attributes:
  • find_all_color_space_attributes: get a list of all color space attributes, in all project objects.
  • find_undefined_color_space_attributes: get a list of all color space attributes with invalid color space, in all project objects. This one is particularly useful to find attributes that are using an invalid color space and that need to be fixed after changing the OCIO configuration.
  • find_matching_color_space_attributes: get a list of all color space attributes that use a specific (existing) color space, in all project objects.

I hope you find it useful! :)

python code

def find_all_color_space_attributes():
"""Find all color space attributes in all the project objects."""
objects = ix.api.OfObjectArray()
ix.application.get_factory().get_all_objects('ProjectItem', objects)
result = []
for i_obj in range(objects.get_count()):
obj = objects[i_obj]
for i_attr in range(obj.get_attribute_count()):
attr = obj.get_attribute(i_attr)
if attr.get_enum_type() == 'color_space':
result.append(attr.get_full_name())

return result

def find_undefined_color_space_attributes():
"""Find all color space attributes with an undefined color space in all the project objects."""
result = []
attrs = find_all_color_space_attributes()
for attr_path in attrs:
attr = ix.get_item(attr_path)
assert attr.get_enum_type() == 'color_space', '"{}" is not a color_space attribute'.foramt(attr_path)
if attr.get_long() == -1:
result.append(attr_path)

return result


def find_matching_color_space_attributes(cs_filter=None):
"""
Find all color space attributes in the project.
Color space attributes are attributes of type 'enum' with the filter 'color_space'.

Parameters
---------
cs_filter: string
> Name of the color space to search (case sensitive).
> If empty or invalid, the function will not filter the attribute's color space value.
> The color space name must exist in the current OCIO configuration.

Return
------
An array of all color space attributes found.
"""

cs_enum = ix.application.get_factory().get_enum('color_space')
assert cs_enum, '"color_space" enum not found'

# get the color space numeric value from the filter name, if provided
cs_filter_value = -1
if cs_filter is not None:
value = cs_enum.get_value(cs_filter)
if value >= 0:
# color space found
cs_filter_value = value
else:
# color space not found: no filtering, all atttributes will be returned
ix.log_warning('Color space "{}" not found. Check family and spelling. Filter ignored.'.format(cs_filter))

result = []
attrs = find_all_color_space_attributes()
for attr_path in attrs:
attr = ix.get_item(attr_path)
assert attr.get_enum_type() == 'color_space', '"{}" is not a color_space attribute'.foramt(attr_path)
if cs_filter_value >= 0:
# valid filter: add attributes with matching color_space only
if cs_filter_value == attr.get_long():
result.append(attr.get_full_name())
else:
# no filter or invalid: add all color_space attributes
result.append(attr.get_full_name())

return result

print('-' * 50)
attrs = find_all_color_space_attributes()
print('Found {} color_space attributes.'.format(len(attrs)))
for attr in attrs:
print(attr)

print('-' * 50)
filter = 'sRGB'
attrs = find_matching_color_space_attributes(filter)
print('Found {} color_space attributes matching "{}".'.format(len(attrs), filter))
for attr in attrs:
print(attr)

print('-' * 50)
attrs = find_undefined_color_space_attributes()
print('Found {} undefined color_space attributes.'.format(len(attrs)))
for attr in attrs:
print(attr)
Anthony Nemoff
Isotropix
R&D Engineer
User avatar
anemoff
 
Posts: 347
Joined: Wed Jan 13, 2016 10:10 am

Re: Useful script: finding color space attributes

Unread postby tsn » Tue Nov 10, 2020 9:17 am

Thanks for the script Anthony! Indeed very useful and always great to learn from you guys.
tsn
 
Posts: 29
Joined: Tue Mar 31, 2015 5:04 pm


Return to Scripting