Python: how to get name of active render layer

Using Python, how do I get the name of the active render layer?

I have three render layers. All three are enabled. But only 1 can be "active." (highlighted in blue when I select it)

I would like to write a script that gets the active render layer name, appends it to the render Output filename, and only renders this layer.

Thank you.

bpy.context.scene.render.layers.active.name gets the name of the active render layer.

Use the following snippet to get the active render layer's name

bpy.context.scene.render.layers.active.name


Note: These steps build on each other; the complete code is shown at the end.

1. Get the scene

scene = bpy.data.scenes['SceneName']

• gets the scene "SceneName"

scene = bpy.data.scenes[0]

• gets the scene at index 0

scene = bpy.context.scene

• gets the current scene

2. Get the render layers

layers = scene.render.layers

• gets the render layers of the current scene (context.scene)

3. Get the active layer

active = layers.active


4. Get its name

name = active.name


Summary:

scene = bpy.context.scene  # get the scene
layers = scene.render.layers  # get the list of layers
active = layers.active  # get the active layer from the list
name = active.name  # get the name of the active layer

• Excellent. Just what I was looking for. Thank you Oct 17, 2016 at 17:23
• Isn't this just what I posted? Oct 17, 2016 at 17:25
• @RayMairlot The one liner is the same of course, but I felt it worth explaining just a bit more about how it came out. Your answer is correct (I up-voted it before I even wrote mine), but I don't think that it is very useful for those who want to understand it rather than just cut and paste :-) Oct 17, 2016 at 17:28
• I think if you felt it needed it expanding you should have commented that. If I see an answer that doesn't have enough information, I don't just generally post the same answer with slightly more detail. Oct 17, 2016 at 17:33