Page MenuHome

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

Description

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
        layout.operator("object.simple_operator")

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

    def execute(self, context):
        scene = bpy.data.scenes.new('Test')
        bpy.context.window.scene = scene
        testob = bpy.data.objects.new(name='Test', object_data=None)
        scene.collection.objects.link(testob)
        testob.select_set(True)
        return{'FINISHED'}

def register():
    bpy.utils.register_class(SimpleOperator)
    bpy.utils.register_class(SimplePanel)

if __name__ == "__main__":
    register()

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.

Details

Type
Bug

Event Timeline

Sebastian Parborg (zeddb) triaged this task as Confirmed, Medium priority.
Dalai Felinto (dfelinto) closed this task as Invalid.

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])