The Blender Python API does not provide a special handlers for tracking View Layers switching. However, if it needs to perform some action when switching them, we can track their switching using the message bus.
First, let’s define a function that will be executed when switching View Layers. For example, it can set different positions of the Render Region for different view layers.
Open the Text Editor in Blender and define the function:
1 2 3 4 5 6 7 8 9 10 11 12 |
def on_view_layer_change(): bpy.context.scene.render.use_border = True if bpy.context.view_layer.name == 'ViewLayer': bpy.context.scene.render.border_min_x = 0.0 bpy.context.scene.render.border_min_y = 0.0 bpy.context.scene.render.border_max_x = 0.5 bpy.context.scene.render.border_max_y = 0.5 else: bpy.context.scene.render.border_min_x = 0.5 bpy.context.scene.render.border_min_y = 0.5 bpy.context.scene.render.border_max_x = 1.0 bpy.context.scene.render.border_max_y = 1.0 |
The first command of the function turns on the Region Border mode.
Next, for the first View Layer with the “ViewLayer” name, the region border will be set in the lower left part of the screen – from the lower left corner to the center.
And for the second View Layer (and for all the others), the region border will be set in the upper right part of the screen – from its center to the upper right corner.
Now let’s subscribe our function as an event handler when the View Layers switches using the message bus “msgbus”.
1 2 3 4 5 6 |
bpy.msgbus.subscribe_rna( key=(bpy.types.Window, 'view_layer'), owner=bpy, args=(), notify=on_view_layer_change ) |
The first “key” parameter is passed a pointer to the object being tracked, in our case it is the window type “bpy.types.Window” and the “view_layer” property, the change of which needs to be tracked.
The second “owner” parameter is the owner of the event. In common case, the “bpy” is most often used.
The third “args” parameter is used to pass additional values to the handler function. In our case, passing something to our function is not required.
And the last “notify” parameter is a pointer to a function that will be executed when the property specified in the first parameter changes. Here we set our “on_view_layer_change” function for resizing the region border.
Execute the script by pressing the Run Script button in the Text Editor window.
Now, when switching View Layers, the location of the render region border changes.