Operating System: Ubuntu 18.04 LTS
Python Version: Python 3.10.11 Anaconda
BPY Version: bpy-3.5.0 (installed by pip)

I was trying to use bpy package to bake vertex color from textures. The scene was loaded from an FBX file. Here is my script code:

import bpy


# remove non-mesh objects
objects = []
for i in bpy.context.visible_objects:
    if i.type == "MESH":

for obj in objects:
    # select the target object
    bpy.context.view_layer.objects.active = obj

    # Add a new vertex color layer `vertex_color`
    obj.data.color_attributes.new(name="Vertex Color", type="BYTE_COLOR", domain="CORNER")
    vertex_color = obj.data.color_attributes.get("Vertex Color")

    # Switch to the Cycles render engine
    bpy.context.scene.render.engine = 'CYCLES'

    # Set the bake settings
    bpy.context.scene.cycles.bake_type = 'COMBINED'
    bpy.context.scene.cycles.view_from = 'ABOVE_SURFACE'
    bpy.context.scene.render.bake.use_selected_to_active = False

    # Enable specific bake contributions
    bpy.context.scene.render.bake.use_pass_direct = True
    bpy.context.scene.render.bake.use_pass_indirect = True
    bpy.context.scene.render.bake.use_pass_diffuse = True
    bpy.context.scene.render.bake.use_pass_glossy = True
    bpy.context.scene.render.bake.use_pass_transmission = True
    bpy.context.scene.render.bake.use_pass_emit = True
    bpy.context.scene.render.bake.target = "VERTEX_COLORS"

    # Set `vertex_color` active
    bpy.context.object.data.attributes.active_color = vertex_color

    # Perform the bake

Then it shows up:

RuntimeError: Error: Mesh does not have an active color attribute "bathroom"

where bathroom is the name of one of the meshes. It seems that the newly created vertex_color is not active when do the baking. I've googled out this, and tried setting up the mesh mode and object mode but none of them works for me.



You must log in to answer this question.

Browse other questions tagged .