How to make new Material Output node the active one?

I’m looking for the same functionality you can do manually by adding a new Material Output node and selecting it with a mouse click.

edit: I should have made myself more clear. I need a new Material Output node next to an existing one in an already set material and then make the new one the active output for the material.

import bpy

ntree = bpy.context.object.active_material.node_tree
for node in ntree.nodes:
	node.select = False
node = ntree.nodes.new('ShaderNodeOutputMaterial')
node.select = True
ntree.nodes.active = node

Thank you for your answer.

Unfortunately, that does not work as I want it. I should have made myself more clear. I need a new Material Output node next to an existing one in an already set material and then make the new one the active output for the material.

Isnt that the same than duplicating existing material output node and then do nothing.

Yeah, I’m kinda lost too. I think he wants to switch between materials that’s in the same node tree. Like if you had a diffuse shader that’s red connected to a output and a diffuse shader that’s blue connected to a output. When you click the output it changes the material on the object.

Edit: After reading again, I don’t think that’s what he wants either.

Actually, that’s what I want.

I need this to make my Principled Baker Addon work properly again, without even (temporary) changing existing materials. In the current version I’m just storing the sockets to the existing material output node and reconnect after the baking.

in short I need this algorithm:

  • create temporary material output node
  • make temp material output node somehow the active shader?
  • connected some nodes of the existing node tree to temp material output
  • bake
  • delete temp material output node (and other temp nodes)

None of this is a real problem except the 2nd point.