Command line material colour change for batch rendering

hi =)

I have a client who needs to render out a bunch of product images - the product is made of 3 separate parts, each of which can have different colours. So part 1 might be blue, and part 2 might be white, while part 3 is red. There are 23 different colours, and (for now) they have 80ish different colour combinations… but that could easily become more :slight_smile: On top of that there is a 4th part in the product which is always the same material, but comes in three different sizes… so it’s 80ish * 3 images.

What I’m wondering is… is it possible to change material colours, set an output file name and render exclusively via the command line? With this large number of images, automation would be … well, necessary, really :slight_smile:

Hope there are some scripting / commandline geniuses out there :slight_smile:

You will need to have two files (blend) and (python) side by side.
Screenshot (68)

In the script you will have this.

import bpy

if __name__ == '__main__':
	print('scripts runs ok')
	print(f'there are {len(bpy.context.scene.objects)} objects in the scene')

	cubeobject = bpy.context.scene.objects['Cube']
	materialofcube = cubeobject.active_material
	print(f'material of cube is {materialofcube}')

	colorofmaterial = materialofcube.node_tree.nodes["Principled BSDF"].inputs[0].default_value
	print(f'color of cube is {colorofmaterial[0], colorofmaterial[1], colorofmaterial[2]}')

	# a "string:tuple" dictionary with colors
	manycolors = {
		'red':(0.9, 0.2, 0.2),
		'green':(0.2, 0.9, 0.2),
		'blue':(0.2, 0.2, 0.9),
		'pink':(0.9, 0.2, 0.9),
	}

	# loop all defined colors by "key:value"
	for thiscolor, thiscolorvalue in manycolors.items():
		print(f'color to be rendered is {thiscolor}')

		# update material colors
		colorofmaterial[0] = thiscolorvalue[0]
		colorofmaterial[1] = thiscolorvalue[1]
		colorofmaterial[2] = thiscolorvalue[2]

		# render the image
		bpy.context.scene.render.image_settings.file_format='JPEG'
		bpy.ops.render.render()

		# save the image
		imagename = f'cube-{thiscolor}.jpg'
		bpy.data.images['Render Result'].save_render(imagename)

	# quit blender
	bpy.ops.wm.quit_blender()

And now you will need to open the terminal and run this command:
D:\programs\graphics\blender\blender.exe test.blend --python script.py
(There also command line argument --background that won’t start GUI at all, it crashes some of my addons and produces more junk info on the terminal, so I won’t bother using it. However I would try it mostly on clean blender installations and for week-long rendering sessions, if needed).

The results are something like this. That you will need to adjust the script further and test little at a time.
Screenshot (71)

1 Like

thanks so much const - I’ll try that out right away :slight_smile: thanks for the help!

You’re welcome.