0
$\begingroup$

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

bpy.ops.import_scene.fbx(filepath="./scene.fbx")

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

for obj in objects:
    # select the target object
    bpy.context.view_layer.objects.active = obj
    bpy.ops.object.mode_set(mode='VERTEX_PAINT')

    # 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
    bpy.ops.object.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.

$\endgroup$

0

You must log in to answer this question.

Browse other questions tagged .