2

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 –
susu
  • 14,002
  • 3
  • 25
  • 48
  • here is some code but how do I link – Mattie Voohies Jan 26 '21 at 23:46
  • 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 – Mattie Voohies Jan 26 '21 at 23:47

1 Answers1

7

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

brockmann
  • 12,613
  • 4
  • 50
  • 93