Greetings, Community. I created a script that deletes all other nodes in the Shader editor that are not connected to the material output using the node wrangler
But my issue is that the first time I run it, my tab switches to the shader editor and then writes an error. But when I return to the text editor and run the script once more, everything is fine.
What could be the issue. Why that error the first time?
Your error is because your context is wrong, and your need to override it. I see you’ve already tried to do so, but your implementation isn’t quite right. See this to learn the right way:
I checked the link sent, but after I replaced the necessary parameters, I am still encountering an error. Here is the code
import bpy
override_context = bpy.context.copy()
area = [area for area in bpy.context.screen.areas if area.type == “ShaderNodeTree”][0]
override_context[‘window’] = bpy.context.window
override_context[‘screen’] = bpy.context.screen
override_context[‘area’] = area
override_context[‘region’] = area.regions[-1]
override_context[‘scene’] = bpy.context.scene
override_context[‘space_data’] = area.spaces.active
bpy.ops.node.nw_del_unused(override_context)
Please am I wrong somewhere? I just want to delete unused nodes in shader editor using node wrangler. Or if there is any other way this can be done through python, I’m open to it. Thank you.