Display> World background in python


(tsurubaso) #1

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.delete()
OBJJ.select_by_type(extend=False, type='LAMP')
OBJJ.delete()

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

bpy.ops.texture.new()
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)

gradNode.select=True
#gradNode.image=imagew


I can not find a way to set active the Display>World bacground
I tried:
bpy.types.SpaceView3D.show_world
bpy.context.space_data.show_world = True but it is logic.
There is surely something with
bpy.data.screens[‘Scripting’].
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
gradNode.image=imagew
does not work.

A little bit long Post, sorry for this.

I will really really appreciate your help.


(AFWS) #2

#1

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

#2

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

bpy.ops.image.open()

(tsurubaso) #3

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