1
$\begingroup$

I'm trying to find a way to fetch the active view layer name or render engine via a driver in shader nodes, the idea would be to use this as a way to customize the shader depending on the view layer or engine.

I tried making a Context type variable fetching the active view layer's name, which, judging from the absence of red marking or errors in the console, lets me think at least that part works.

Then I use the expression 1.0 if view_layer_name == "ViewLayer_004" else 0.0, and though it still doesn't give any error, it still doesn't return 1 when the current active layer is in fact ViewLayer_004.

enter image description here

I have a similar result when trying to access the active scene's render engine:

enter image description here

Using Depsgraph to access the active view_layer directly in the expression depsgraph.view_layer.name == "ViewLayer" kinda works, but not reliably: It seems the depsgraph's updates are a bit unpredictable, i have times when it doesn't update the driven value when switching layer (the below screenshot was taken from ViewLayer, yet the value is still 0).
And most importantly: when running a regular render (single frame or animation), it seems to never update after the first layer render:

enter image description here

Whereas manually rendering each layer using their render layer nodes does work, but that's not a realistic solution:

enter image description here

$\endgroup$
2
  • $\begingroup$ It seems to work for me if you just use the depsgraph driver namespace variable rather than creating a custom one. Like in this answer $\endgroup$ Commented Aug 3 at 19:43
  • $\begingroup$ It doesn't work for the active renderer, and though it can get the active view layer: It's not quite functional, the depsgraph doesn't seem to update predictably and especially at render: it doesn't seem to do so during a regular (single frame or animation) render, but running a single layer render from the compositor's render layer nodes makes it work. But having to manually run each layer's render isn't a satisfying solution. $\endgroup$ Commented Aug 3 at 22:29

1 Answer 1

0
$\begingroup$

Reopening my test file this morning, it seems that the Depsgraph method mentioned by @Jakemoyo for active layers now works... No idea why it didn't before. Maybe because I tried many things before it somehow corrupted the driver and reopening the file cleaned it up? It still means it's not 100% reliable.
I also found a way to make the render engine driver work, though not as user-friendly as I'd hope.

So i'll post these here, but if anyone finds more reliable and user-friendly ways to do it, please post it!

Active ViewLayer driver

Instead of using a context type driver variable which seem to only return a float, avoid driver variables altogether and use the expression depsgraph.view_layer.name == "ViewLayer" which will return 0 or 1 depending on whether the active ViewLayer name is ViewLayer.

enter image description here

Render Engine

The context type driver variable does work, but it only returns a float corresponding to the index of the render engine as seen in the render engines list, counted from zero.

So to get 1 when we use Eevee, which is the first engine, we need to return 1 when the variable contains 0, which is the following expression: 1 if engine_index == 0 else 0 with engine_index being a context type driver variable fetching the render.engine data path of the active scene:

enter image description here

It reliably works, but it's not the most user-friendly having to use an index number for render engines. But I suspect the order of render engines can be changed when third-party engines are installed. So it's probably not 100% reliable for anyone who doesn't stick to default engines.

Alternatively, using the depsgraph again can be done, which has the benefit to be able to work on a string:

  • 1 if depsgraph.scene.render.engine == "BLENDER_EEVEE_NEXT" else 0 requires knowing and writing the exact name of the engine
  • 1 if "EEVEE" in depsgraph.scene.render.engine else 0 allows a bit more leeway, especially useful these days due to how Eevee changes name in the API since the EEVEE_NEXT update and then being renamed EEVEE again.
$\endgroup$
2
  • $\begingroup$ Weird, mine does work with the active renderer like this: .2 if depsgraph.scene.render.engine == "BLENDER_EEVEE_NEXT" else .9. It did trip me up for a sec cause I thought it was just called EEVEE, then I double checked in the debugger. Example. Seems like it might be a bit finicky. $\endgroup$ Commented Aug 5 at 13:03
  • 1
    $\begingroup$ Yeah that's why I'd prefer to avoid depsgraph, it seems really fickle. First time when i tried it for the render engine like you showed, it said AttributeError: 'Scene' object has no attribute 'render'.... I feel like 1 if "EEVEE" in depsgraph.scene.render.engine else 0 is a tad more robust due to how eevee's name changes these days. $\endgroup$ Commented Aug 5 at 17:01

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.