Trying to work with nodes in a ShaderNodeTree on a material I've just created, and I'm getting the dreaded "context is incorrect" error.
Here's some code I successfully ran in the Blender UI:
def test(): C.window.workspace = D.workspaces['Shading'] new_material = D.materials.new('color_ramp') new_material.use_nodes = True C.active_object.data.materials.append(new_material) C.window.screen.areas.type = 'NODE_EDITOR' C.window.screen.areas.ui_type = 'ShaderNodeTree' spaces = C.window.screen.areas.spaces return spaces
With an object selected, this correctly switches the window, creates a new material, assigns it to the object, switches the area to the Node Editor, and even has the main node selected in the Node Editor area. Calling
data = test() and then calling
and does not allow me to deselect the main node, nor does it allow me to add new nodes. From the code of the
poll() function for nodes/node trees, I was able to see the context needs to be in the Node Editor and have a node tree selected. Calling
data.node_tree in the UI correctly prints the node_tree, but interestingly enough my program I'm building does not.
Does anyone know the correct context for working with nodes/node trees?
EDIT: I added some print statements to the function to see if I could see what was going wrong with the context. Looks like it isn't changing the Workspace properly, which could I suppose be part of the issue? Even though it's in the Node Editor area...I'm not quite sure.