1

In my scene I have an object that's using 4 different materials. Each material has it's own image node that looks like this:

Image Node

How would I go about selecting a material by name and changing MyImage.png to a string I can input in python? I'm currently using this code to load the scene and render it.

def renderImage():
   bpy.ops.wm.open_mainfile(filepath="../scenes/bodyshot.blend")
   scene = bpy.context.scene        
   obj_camera = bpy.context.scene.camera 
   scene.render.image_settings.file_format = 'PNG'
   scene.render.filepath = "renderOutput.png"
   bpy.ops.render.render(write_still = 1)

renderImage()

Edit: Solved thanks to lemon. The code I used to render the image:

mat = bpy.data.materials.get("shirt")
node = mat.node_tree.nodes["shirtImageNode"]
image = bpy.data.images.load("myShirtImage.png")
node.image = image

1 Answers1

2

To get a material by its name:

mat = bpy.data.materials[ mat_name ]

To get a node in it by its name:

node = mat.node_tree.nodes[ node_name ]

To get all the image texture nodes in a material:

nodes = [n for n in mat.node_tree.nodes if n.type == 'TEX_IMAGE']

For the image part, I presume it is loaded and exist in Blender.

If yes, to get it from its name:

image = bpy.data.images[ image_name ]

If no, you can load it:

bpy.data.images.load( filepath )

Setting an image in a image texture node:

image_node.image = image

where 'image_node' is one of the nodes obtained as 'TEX_IMAGE' above.

and where 'image' is the object obtained from bpy.data.images

lemon
  • 60,295
  • 3
  • 66
  • 136