Post #4865: You might have already resolved this issue, but I realized that I was involved i

📋 Metadata

🏷️ Tags

python-scripting intermediate solved

  • Python Scripting
  • Macro Editor

💬 Content

You might have already resolved this issue, but I realized that I was involved in that code too, so I thought I’d contribute my answer (apologies for being late).

When it comes to troubleshooting, it’s always nice to have an error message to work with. However, when I tried running the code with PME, I didn’t receive any errors.

So, I decided to run the code in Blender’s text editor instead:

import bpy

C = bpy.context
D = bpy.data
O = bpy.ops

m = C.active_object.active_material
[setattr(n.image.colorspace_settings,'name','Linear') for n in m.node_tree.nodes if n.type=="TEX_IMAGE" and n.select or (n.type=="GROUP" and [setattr(g.image.colorspace_settings,'name','Linear') for g in D.node_groups[n.node_tree.name].nodes if g.type=="TEX_IMAGE" and g.select])]
O.ed.undo_push(message="Set Linear")

This time, I received a helpful error message. It’s often a good idea to start reading the error from the last line:

Python: Traceback (most recent call last):
  File "\Text.001", line 7, in <module>
  File "\Text.001", line 7, in <listcomp>
TypeError: bpy_struct: item.attr = val: enum "Linear" not found in ('ACES2065-1', 'ACEScg', 'AgX Base Display P3', 'AgX Base Rec.1886', 'AgX Base Rec.2020', 'AgX Base sRGB', 'AgX Log', 'Display P3', 'Filmic Log', 'Filmic sRGB', 'Linear CIE-XYZ D65', 'Linear CIE-XYZ E', 'Linear DCI-P3 D65', 'Linear FilmLight E-Gamut', 'Linear Rec.2020', 'Linear Rec.709', 'Non-Color', 'Rec.1886', 'Rec.2020', 'sRGB')

The error message is telling us that “Linear” is not found in the list of available color spaces, and it even provides a list of what is available – very considerate!

So, to resolve the issue, you need to replace ‘Linear’ with one of the color space names provided in the traceback. Feel free to choose the one that fits your needs from the list. You can refer to the Default OpenColorIO Configuration for more details.

Also, it’s common in PME to condense code into one line, but this can make it hard to understand what the code is doing when you come back to it later. If this feature is important to you, it might be a good idea to maintain a script that is easy to read. It helps in identifying issues quickly and makes it easier to get support from others.

set_colorspace function

import bpy

def set_colorspace(material, colorspace_name):
    """
    Set the colorspace of texture image nodes and nodes within node groups in a material.

    Args:
    material: bpy.types.Material - The material in which the nodes will be modified.
    colorspace_name: str - The name of the colorspace to set.
    """
    # Loop through all nodes in the material's node tree
    for node in material.node_tree.nodes:
        # If node is a selected texture image, set its colorspace
        if node.type == "TEX_IMAGE" and node.select:
            node.image.colorspace_settings.name = colorspace_name
        # If node is a group, loop through nodes in the group and set colorspace for selected texture images
        elif node.type == "GROUP":
            group_nodes = bpy.data.node_groups[node.node_tree.name].nodes
            for group_node in group_nodes:
                if group_node.type == "TEX_IMAGE" and group_node.select:
                    group_node.image.colorspace_settings.name = colorspace_name


# Get the active material of the active object
active_material = bpy.context.active_object.active_material

# Name of the colorspace to set
colorspace_name = 'Linear Rec.709'

# Set the colorspace for texture images in the active material
set_colorspace(active_material, colorspace_name)

# Push the operation to the Undo stack
bpy.ops.ed.undo_push(message=f"Set {colorspace_name}")


# Reference: Colorspace available in 4.0
# ('ACES2065-1', 'ACEScg', 'AgX Base Display P3', 'AgX Base Rec.1886', 'AgX Base Rec.2020', 'AgX Base sRGB', 'AgX Log', 'Display P3', 'Filmic Log', 'Filmic sRGB', 'Linear CIE-XYZ D65', 'Linear CIE-XYZ E', 'Linear DCI-P3 D65', 'Linear FilmLight E-Gamut', 'Linear Rec.2020', 'Linear Rec.709', 'Non-Color', 'Rec.1886', 'Rec.2020', 'sRGB')

❤️ 2 likes


🔗 View on Blender Artists