How to set a world's shader to the world with python, please?

Is my question how clear:

bpy.data.worlds[‘myWorld’] is my shader but I want apply this shader to my context.scene please?

@spirou4D: I am not 100% sure if I understand what you want (but it’s early yet and on a Sunday morning :slight_smile: but this is what I do in my sibl.py script:

first in my operator i make sure that i am in the correct context (the node editor, working on world nodes), i.e. that means that my operator only is available from the node editor

@classmethod
def poll(cls, context):
space = context.space_data
return space.type == ‘NODE_EDITOR’ and space.shader_type == ‘WORLD’ and space.tree_type == ‘ShaderNodeTree’ and space.texture_type == ‘OBJECT’

The in the operator it self I can access the node tree:

space = context.space_data
node_tree = space.node_tree
node_active = context.active_node
node_selected = context.selected_nodes

new nodes can be added with the bpy.ops.node.add_node operator. new nodes must be linked to other nodes by manipulating their sockets. You might want to check the functions in https://github.com/varkenvarken/blenderaddons/blob/master/sibl.py

But maybe all you want is to make a given world the active one for a scene? in that case something like this should work i guess:

context.scene.world = bpy.data.worlds[‘myWorld’]

regards,

– Michel.

1 Like

i need help! please… in your case you are making sure you are in the right context but how do i FORCE SET the context to be in the shader editor so i can execute bpy.ops.node.nw_add_textures_for_principled() ?

@varkenvarken
less is more :wink:

bpy.context.scene.world = bpy.data.worlds['my_World']

1 Like