Page MenuHome

Object not in View Layer error when trying to select a new object in a new scene from a panel.
Closed, InvalidPublic


System Information
Operating system: Windows 8.1

Blender Version
Broken: 2.80, 2019-07-10

Short description of error
In a python script, calling object.select_set(True) on a just-created object in a just-created scene can cause the error: "RuntimeError: Error: Object 'Test' not in View Layer 'View Layer'!".
Strangely, this error only happens when the operator performing these functions is called from a panel.
It does not happen when the operator is called directly from the F3 search menu.

Exact steps for others to reproduce the error
Put the following code in a text area and run the script:

import bpy

class SimplePanel(bpy.types.Panel):
    bl_label = "Simple Panel"
    bl_space_type = 'PROPERTIES'
    bl_region_type = 'WINDOW'
    bl_context = "scene"

    def draw(self, context):
        layout = self.layout

class SimpleOperator(bpy.types.Operator):
    bl_idname = "object.simple_operator"
    bl_label = "Simple Object Operator"

    def execute(self, context):
        scene ='Test')
        bpy.context.window.scene = scene
        testob ='Test', object_data=None)

def register():

if __name__ == "__main__":

Click the "Simple Object Operator" button in the new "Simple Panel".

Note: ive tried a wide variety of update functions thrown in various places, including view_layer.update(), view_layer.objects.update() both on scene, and context.scene, and context.evaluated_depsgraph_get().update(), nothing seems to help.



Event Timeline

Sebastian Parborg (zeddb) lowered the priority of this task from Needs Triage by Developer to Confirmed, Medium.

Thanks for the report, but you are creating a new scene in your script, you need to pass the new view_layer to the select_set function:
testob.select_set(True, view_layer=scene.view_layers[0])