I am trying to make a setup where a different node is connected to a node group's output depending on what Render Layer is being rendered. For example, if layer 1 is rendering, then RGB is connected. If Layer 2 is rendering, then RGB.001 is connected.
I already have a setup that let's me use a Driver that outputs a different value based on which layer is rendering, so I think this ought to be possible.
Here is that script:
def layer_number(depsgraph: bpy.types.Depsgraph):
name = depsgraph.view_layer.name
try:
layer_no = int(name.split(':')[0])
except:
layer_no = 0
return layer_no
This is based on this feature that was added in 2.9(scroll to bottom) that allows things from the dependency graph to be accessed in Drivers.
To use it, you create a Driver on a value with the expression layer_number(depsgraph). The value will then be a number you put at the start of the Render Layer name. This can then drive a mix node, or whatever else. It will update when switching layers in the viewport, or when F12 rendering. (The script needs to be registered or run of course.)
For various complex reasons that are too long and specific for this post, I can't use just this method of mixing between two different nodes. I need to plug in one or the other. I have a script that removes or creates links as needed, but I need to be able to trigger it and feed it the proper value based on which Render Layer is currently rendering.
So, the script needs to do something like this:
- Run each time a new Render Layer comes up, either in the viewport or during a full F12 render.
- Get a Value from the Dependency graph based on the Render Layer that's about to be rendered.
- Run the script/function that changes the node links.
- Proceed with the Render.
Is this possible? I am aware that there are ways to trigger things at the start or end of a render job, but generally that means before and after rendering all layers, and won't let you change things per layer. But since the above Driver feature was added, I am now thinking that you can change things per layer, even during a larger render job?
(I am aware that you can write command line batch render scripts that do a render job of an individual layer, then change something, then do a render job of the next layer. I am looking for solutions beyond that, as I want this to work when switching layers in the viewport, or rendering only individual layers in the compositor, etc.)