7
$\begingroup$

I'm just getting started with the Blender API, and I'm trying to set the background texture using the API.

Blender_cycles_background_debugging

In the GUI, I do the following:

  1. Choose the Cycles render engine bpy.context.scene.render.engine = 'CYCLES'
  2. Press the World context button,
  3. In the Surface menu, press Use nodes,
  4. In the Surface box, Background,
  5. In the Color box select Gradient Texture,
  6. In the box below that select Easing,

I tried to reproduce this in the console, with:

bpy.context.space_data.context = 'WORLD'
bpy.data.node_groups["Shader Nodetree"].nodes["Gradient Texture"].gradient_type = 'EASING'  

and I got the following error:

Traceback (most recent call last):
File "", line 1, in KeyError: 'bpy_prop_collection[key]: key "Shader Nodetree" not found'

I spent over an hour reading the doc and Googling, but I still can't do this in my script? Really appreciate any advice how to do this programatically/in a headless script?

Thanks

$\endgroup$
2

1 Answer 1

3
$\begingroup$

When you select the Gradient option for the Color item in the World Panel, it runs a number of calls to create the Gradient node and route its socket to the color input of the Background node.

To replicate that process, you can't just call the panel tool that you clicked on. I believe you have to manually add the gradient node to the World nodetree and connect the node to the background node via python.

import bpy

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

#select world node tree
wd = scn.world
nt = bpy.data.worlds[wd.name].node_tree

#create new gradient texture node
gradNode = nt.nodes.new(type="ShaderNodeTexGradient")

#find location of Background node and position Grad node to the left
backNode = nt.nodes['Background']
gradNode.location.x = backNode.location.x-300
gradNode.location.y = backNode.location.y

#Connect color out of Grad node to Color in of Background node
gradColOut = gradNode.outputs['Color']
backColIn = backNode.inputs['Color']
nt.links.new(gradColOut, backColIn)

#set gradient type to easing
gradNode.gradient_type = 'EASING'
$\endgroup$
4
  • 2
    $\begingroup$ I think this answer should probably have been a comment if you were intending to post the actual answer at a later date. That aside, the issue would also be, in this example, that the node tree doesn't even exist yet. $\endgroup$ Commented Jun 20, 2015 at 17:42
  • $\begingroup$ Hi thank you very much for pointing out that I hav'nt initialize a node tree. That makes sense :) Is there any scripts/examples on the wen how to do this? Just to give some context what I'd like to do is create lighting effects/animations like 3, 4, 5, and 6 here, blenderguru.com/articles/6-tips-for-better-lighting $\endgroup$
    – Ajay T
    Commented Jun 20, 2015 at 19:28
  • $\begingroup$ @RayMairlot sorry about that, started the script and then had to go out. I've posted it now. $\endgroup$ Commented Jun 21, 2015 at 3:40
  • 1
    $\begingroup$ Hey Todd,.., that works perfectly ,... really nice code, it's great. Thank you very much! $\endgroup$
    – Ajay T
    Commented Jun 21, 2015 at 10:22

You must log in to answer this question.

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