pythonblender

Blender Addon Question - Change World Color


Is there a way to add an option to change the World Surface Color and Strength settings via a button?

I already coded out the panel and everything, but maybe it's just based on the specific context, which I currently have: world = context.world Which removes everything from my label that I created. This is confusing me, maybe there is already a question similar out there or here and I haven't found it. I apologize for the repeated question, if that's the case.

Thank you.

import bpy

class Panel(bpy.types.Panel):
    bl_label = "Panel"
    bl_idname = "PT_PANEL"
    bl_space_type = 'VIEW_3D'
    bl_region_type = 'UI'
    bl_category = 'Panel'
    
    def draw(self, context):
        layout = self.layout
        
        #world = context.world

        row = layout.row()
        row.label(text="Select a shader")
        row.operator()


def register():
    bpy.utils.register_class(Panel)


def unregister():
    bpy.utils.unregister_class(Panel)


if __name__ == "__main__":
    register()

Solution

  • It is not entirely clear what you mean when you refer to the world surface and color, but I am going to assume that you mean the default surface on the default world in a scene's "World Properties" panel.

    In that case, you can set the color and strength as follows:

    import bpy
    
    # Set the color to green
    bpy.data.worlds["World"].node_tree.nodes["Background"].inputs[0].default_value = [0,1,0,1]
    # Set the strength to 0.5
    bpy.data.worlds["World"].node_tree.nodes["Background"].inputs[1].default_value = 0.5