2
$\begingroup$

How do I add in a blender python script a hdri to the world node.

scnworld = bpy.data.worlds.get("World")
scnImage = scnworld.node_tree.nodes.new('ShaderNodeTexEnvironment') 
scnImage.location = (-350, 250)
scnImage.image = bpy.data.images.load('H:\\blender__word_hdri\\green_sanctuary_2k.hdr') 
scnImage.image.colorspace_settings.name = 'Non-Color'
link = scnworld.node_tree.links.new –
$\endgroup$
2
  • $\begingroup$ here is some code but how do I link $\endgroup$ Jan 26, 2021 at 23:46
  • $\begingroup$ scnworld = bpy.data.worlds.get("World") scnImage = scnworld.node_tree.nodes.new('ShaderNodeTexEnvironment') scnImage.location = (-350, 250) scnImage.image = bpy.data.images.load('H:\\blender__word_hdri\\green_sanctuary_2k.hdr') scnImage.image.colorspace_settings.name = 'Non-Color' link = scnworld.node_tree.links.new $\endgroup$ Jan 26, 2021 at 23:47

1 Answer 1

7
$\begingroup$

Similar to How to load an image from disc and assign it to a newly created image texture node? use BlendDataImages.load() to load and assign the image to the Environment Texture node:

enter image description here

import bpy

C = bpy.context
scn = C.scene

# Get the environment node tree of the current scene
node_tree = scn.world.node_tree
tree_nodes = node_tree.nodes

# Clear all nodes
tree_nodes.clear()

# Add Background node
node_background = tree_nodes.new(type='ShaderNodeBackground')

# Add Environment Texture node
node_environment = tree_nodes.new('ShaderNodeTexEnvironment')
# Load and assign the image to the node property
node_environment.image = bpy.data.images.load("//hdri.exr") # Relative path
node_environment.location = -300,0

# Add Output node
node_output = tree_nodes.new(type='ShaderNodeOutputWorld')   
node_output.location = 200,0

# Link all nodes
links = node_tree.links
link = links.new(node_environment.outputs["Color"], node_background.inputs["Color"])
link = links.new(node_background.outputs["Background"], node_output.inputs["Surface"])

Related: Control Cycles material nodes and material properties

$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .