Would like to be able to change the material node displayed in the Node Editor from Python after running an operator to create a new material node in Python. In the Node Editor, I can click "New" and have a new node created and displayed (see the image below). I can also select which material to display. Is there a way to do this from Python?

enter image description here

Use Case

I am linking the material from another .blend file and would like to display the new material in the editor so the user can immediately start working on it. The new material node is not linked to any object by default, that is left up to the user. The code looks like this:

with bpy.data.libraries.load(blend) as (data_from, data_to):
    data_to.materials = ['fancy material']
# Change the Node editor display to show the new node?

While trying to figure out how to change the displayed material node, I found this code that displays the template_ID but do not know how to change it display my new material nodes. (Edited for brevity)

class NODE_HT_header(Header):
    bl_space_type = 'NODE_EDITOR'

    def draw(self, context):
        snode = context.space_data
        # Custom node tree is edited as independent ID block
        layout.template_ID(snode, "node_tree", new="node.new_node_tree")

Realizing this isn't the best "blender" behavior, is it possible?

  • 1
    $\begingroup$ You'd like to assign an imported material to a object in your scene, right? $\endgroup$
    – p2or
    Commented Jan 30, 2016 at 13:23
  • $\begingroup$ The node editor automatically shows the material from the selected object in the scene, so I think there is no need to write any node editor code. If there is a reason, please provide a use case and let me know what else you need - removed my answer. $\endgroup$
    – p2or
    Commented Jan 31, 2016 at 10:17
  • 1
    $\begingroup$ @poor, added more details and a use case. Basically, I want to change which material node is displayed in the editor from Python code. Just the same as if I had clicked New or selected the material node. Thanks! $\endgroup$ Commented Jan 31, 2016 at 12:47


You must log in to answer this question.

Browse other questions tagged .