Display> World background in python

Hello to all,
I am playing a little with blender python,
my objective is to use blender to open HDRI image, and show them in background.
I have 2 different problems:
but first here is the code.

import bpy

#mettre en place Cycle et le node tree

scn = bpy.context.scene
scn.render.engine = 'CYCLES'
scn.world.use_nodes = True
wd = scn.world

# effacer tout ce qui traine, et vlan
OBJJ= bpy.ops.object
OBJJ.select_by_type(extend=False, type='MESH')
OBJJ.select_by_type(extend=False, type='LAMP')

#creer une texture y mettre l'image, connectrer tout ça.

nt = bpy.data.worlds[wd.name].node_tree
#désolé pour le naming, c'est de l'emprunt
gradNode = nt.nodes.new(type="ShaderNodeTexEnvironment")

backNode = nt.nodes['Background']
gradNode.location.x = backNode.location.x-300
gradNode.location.y = backNode.location.y

gradColOut = gradNode.outputs['Color']
backColIn = backNode.inputs['Color']
nt.links.new(gradColOut, backColIn)

#Une image sur mon PC, ref
imagew=bpy.ops.image.open(filepath="C:\\IMG_3015.JPG", directory="C:\\", files=[{"name":"IMG_3015.JPG", "name":"IMG_3015.JPG"}], relative_path=True, show_multiview=False)


I can not find a way to set active the Display>World bacground
I tried:
bpy.context.space_data.show_world = True but it is logic.
There is surely something with
but I can’t find the way.

I also tried:

for area in bpy.context.screen.areas:
    if area.type == 'SpaceView3D':
        area.show_world = TRUE

Also I need to connect the image. with the node
does not work.

A little bit long Post, sorry for this.

I will really really appreciate your help.


for area in bpy.context.screen.areas:
    for space in area.spaces:
        if space.type == 'VIEW_3D':
            space.show_world = True


Make sure you add “import os” at the top and replace this line

imagew=bpy.ops.image.open(filepath="C:\\IMG_3015.JPG", directory="C:\\", files=[{"name":"IMG_3015.JPG", "name":"IMG_3015.JPG"}], relative_path=True, show_multiview=False)

with these two

#Example ---- filepath = os.path.join('C:\\', 'Users', 'User_Name_Here', 'Desktop', 'picture_to_load.jpg')
filepath = os.path.join('C:\\', 'IMG_3015.JPG')			
imagew = bpy.data.images.load(filepath)

I was also having trouble when trying to use the

1 Like

I will carefully look at your answers. But a great thanks!!! :slight_smile: