2
$\begingroup$

I'm rendering an image from the command line using this command:

blender "file.blend" --background --render-output "output name" --engine CYCLES --render-format PNG --use-extension 1 --render-frame 1

However, I'd also like to set the image path for an image node named "Target Node" in one of the materials as well as the world material.

How can I do this? Do I need a python script? If so, how do I pass the value to it from the command line.

$\endgroup$

2 Answers 2

3
$\begingroup$

You can use a script for this.

  1. You can read command line arguments with the sys module's argv property.
  2. Access the image filepath through the image texture node (lines 9-14).

Code:

import bpy
import sys
from os.path import isfile

# Take image path as the last command line argument
imgPath = sys.argv[-1]

o  = bpy.data.objects['Cube'] # Replace with your actual object's name
t  = o.active_material.node_tree
im = t.nodes['Image Texture'].image

#For the world texture, use something like this:

w  = bpy.data.worlds['World'] # Replace with your actual world's name
w.use_nodes = True
t  = w.node_tree
im = t.nodes['Texture'].image

# If provided image exists set is as the image texture node's image
if isfile( imgPath ): im.filepath = imgPath

Run blender with the script and provided image path thus:

blender.exe pathToBlend\scene.blend -b -P pathToScript\script.py --render-output renderPath --engine CYCLES --render-format PNG --use-extension 1 --render-frame 1 -- pathToImage\image.jpg
$\endgroup$
0
0
$\begingroup$

If you are trying to change the image on a material:

python script:

import bpy
import sys
from os.path import isfile

# Take image path as the last command line argument
imgPath = sys.argv[-1]
texture = bpy.data.materials['texture'].node_tree.nodes["Image Texture"].image
# If provided image exists set is as the image texture node's image
if isfile( imgPath ): texture.filepath = imgPath

bat file

cd "C:\Program Files\Blender Foundation\Blender 3.1"
blender.exe "C:\Users\b\iCloudDrive\nhandee\rendertest.blend" -b -P "C:\Users\b\iCloudDrive\folder\script.py" --render-output "C:\Users\b\iCloudDrive\folder\x####" -a -- "C:\Users\b\iCloudDrive\folder\2.png"

similar to the accepted answer except for materials that have a pricipled bsdf with an image as the base color

$\endgroup$

You must log in to answer this question.

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