3

i'm unable to get inside a python script the path of th Environment texture that i assign in World->Color from the Blender UI.

I want to set the .hdr there and than get from there the path (C:/.../123.hdr) for other uses, but i need it inside a script.

Thanks for help!

enter image description here

PabloB
  • 53
  • 2
  • 4

1 Answers1

9

Finding things with the python console.

The scene has a world. context.scene.world. When nodes are being used the object will have a node tree. world.node_tree The tree has nodes. Consult the manual or the many questions / answers re nodes for more info.

Note: The properties space has a world member of context context.world other spaces may not

Here is a simple example using the python console. C is a convenience variable C = bpy.context

>>> C.scene.world
bpy.data.worlds['World']

>>> C.scene.world.node_tree
bpy.data.node_groups['Shader Nodetree']

Here I've used autocomplete CtrlSpace to expand available options

>>> C.scene.world.node_tree.nodes['
                                   Background']
                                   Environment Texture']
                                   World Output']
>>> C.scene.world.node_tree.nodes['Environment Texture'].image
bpy.data.images['Untitled']

See that the environment node has an associated image. Since I've simply added a blank untitled image it has no filepath. A loaded image will.

>>> env_img = C.scene.world.node_tree.nodes['Environment Texture'].image
>>> env_img.file
                _format
                path
                path_from_user(
                path_raw
>>> env_img.filepath
''

Adding an environment node, given you have a path to the image path

>>> world = C.scene.world
>>> world.use_nodes = True
>>> enode = C.scene.world.node_tree.nodes.new("ShaderNodeTexEnvironment")
>>> enode.image = bpy.data.images.load(path)

Related:

How to set a background using the cycles render engine with the API

batFINGER
  • 84,216
  • 10
  • 108
  • 233
  • 3
    The only thing that is missing is to then link the node: node_tree = C.scene.world.node_tree node_tree.links.new(enode.outputs['Color'], node_tree.nodes['Background'].inputs['Color']) – Martin R. Feb 05 '20 at 19:59