I am trying to apply textures through a python script. I would like to use blender cycles renderer. I thought following these advices would be sufficient, but sadly Blender does not show the equivalent python commands when applying manually the procedure. How would you do this ? Or, is it possible to force blender to show the python commands ?

Thanks in advance


This script does not do any unwrapping. List of available nodes

import bpy

mat_name = "MyMaterial"
image_path = #...

mat = (bpy.data.materials.get(mat_name) or

mat.use_nodes = True
nt = mat.node_tree
nodes = nt.nodes
links = nt.links

# clear
while(nodes): nodes.remove(nodes[0])

output  = nodes.new("ShaderNodeOutputMaterial")
diffuse = nodes.new("ShaderNodeBsdfDiffuse")
texture = nodes.new("ShaderNodeTexImage")
uvmap   = nodes.new("ShaderNodeUVMap")

texture.image = bpy.data.images.load(image_path)
uvmap.uv_map = "UV"

links.new( output.inputs['Surface'], diffuse.outputs['BSDF'])
links.new(diffuse.inputs['Color'],   texture.outputs['Color'])
links.new(texture.inputs['Vector'],    uvmap.outputs['UV'])

# distribute nodes along the x axis
for index, node in enumerate((uvmap, texture, diffuse, output)):
    node.location.x = 200.0 * index
| improve this answer | |
  • $\begingroup$ Thank you for your answer. I am not sure I fully understand: how to apply the texture to an object? Why doesn't blender show the "python commands" when manipulating textures for the cycles renderer? $\endgroup$ – waffle Mar 7 '16 at 20:34
  • $\begingroup$ Couldn't you just do nodes.clear() instead of looping and removing them? Also, is there a way to auto-layout them after adding them? They're positioned on top of each other otherwise. $\endgroup$ – Ray Jun 9 '18 at 14:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.