1
$\begingroup$

Im trying to create a script that bakes the ambient occlusion of an object to a texture map. For optimal performance, I want only the selected objects to be part of the bake process. That's why I decided to do the baking in another scene. I am however experiencing some issues regarding the context, since the active scene doesn't redraw until the python operator finishes. I've been able to circumvent this by using context overrides, but since baking needs the object to be selected, I've not yet been able to fix this part.

What goes wrong:

The code crashes blender, it started crashing when I added the select_set line (near the bottom) with a specific view layer. The error log is linked below, it's a EXCEPTION_ACCESS_VIOLATION.

When I remove this line, the baking will not start since it needs a selected object to start baking.

Here is the code:

def bake (self, context, active_object, context_objects):
    #hard coded for now, will free later
    bake_samples = 4
    res = 1024

    old_scene = bpy.context.window.scene 
    active_object = active_object
    selected_objects = context_objects

    #this does not work yet, but is not that imported
    if not active_object:
        self.report({'WARNING'}, "No object selected")
        return
    
    #check if scene exists, otherwise create it
    try:
        smr_scene = bpy.data.scenes['SMR_Bake']
    except:
        smr_scene = bpy.data.scenes.new(name='SMR_Bake')

    bpy.context.window.scene = smr_scene    

    #set some render settings
    smr_scene.render.engine = "CYCLES"
    smr_scene.cycles.samples = bake_samples
    smr_scene.cycles.bake_type = "EMIT"
    smr_scene.render.bake.margin = 2
    
    #check if the baking material exists, otherwise append it
    try:
        bpy.data.materials['SMR_Bake']
    except:    
        blendfile = context.scene.SMR.SMR_path + str(Path('/SMR.blend'))
        section   = str(Path("/Material/"))
        filename    = 'SMR_Bake'                
        filepath  = blendfile + section + filename
        directory = blendfile + section

        bpy.ops.wm.append(
            filepath=filepath, 
            filename=filename,
            directory=directory) 
    
    #set the baking material as override
    smr_scene.view_layers['View Layer'].material_override = bpy.data.materials['SMR_Bake']

    #link the objects to the new scene
    for obj in selected_objects:
        try:
            smr_scene.collection.objects.link(obj)
        except:
            print('{} already in collection'.format(obj.name))
    #this are external functions returning material data and creating nodes in the right places
    mat, nodes, links = get_mat_data()
    node_name = "SMR_Bake_Texture"
    check_node(node_name, 'ShaderNodeTexImage')
    node=nodes[node_name]
    
    #checking if the bake image exists, otherwise create it
    image_name = "SMR_Bake_{}".format(active_object.name)
    try:
        image = bpy.data.images[image_name]
    except:
        image = bpy.data.images.new(image_name, width=res, height=res)
    
    node.image = image
    node.select = True
    nodes.active = node
    
    try:
        l = node.outputs[0].links[0]
        mat.node_tree.links.remove(l)
    except:
        print('no links to remove')
    
    override = bpy.context.copy()
    override['scene'] = smr_scene 
    smr_scene.objects[active_object.name].select_set(state=True, view_layer= smr_scene.view_layers['View Layer'])
    smr_scene.view_layers['View Layer'].objects.active = smr_scene.objects[active_object.name]  
    bpy.ops.object.bake(override, 'INVOKE_DEFAULT')
    bpy.context.window.scene = old_scene 

This is the important part:

    override = bpy.context.copy()
    override['scene'] = smr_scene 
    smr_scene.objects[active_object.name].select_set(state=True, view_layer= smr_scene.view_layers['View Layer'])
    smr_scene.view_layers['View Layer'].objects.active = smr_scene.objects[active_object.name] 
    bpy.ops.object.bake(override, 'INVOKE_DEFAULT')

Anyone know what would fix this?

Wetransfer to the crash file: https://we.tl/t-pXheJmGPTJ

$\endgroup$
0

1 Answer 1

0
$\begingroup$

I dunno why it happens But looking in the log seems what i happened when some objects was on sculpt mode with "Lock Objects mode" Off

i turned it on and touched the objects back and it worked and then turned off

maybe that can help

$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .