6
$\begingroup$

I'm trying to render a simple scene using the Python API. I've created a red cube in blender and want to output it using the Python API. The only things I've changed from the default new file is the rendering engine to Cycles and the color of the cube to red.

Screenshot of UI

When rendering from within Blender (F12) I get the output I expect:

Render from Blender's UI

However when rendering from Python using blender --background --python generate.py simple.blend I don't get any shaders:

Render using Blender's Python API

The Python code is very simple:

import bpy
import os

this_dir = os.path.dirname(os.path.realpath(__file__))
render_path = os.path.join(this_dir, "render.jpg")

scene = bpy.context.scene
scene.render.engine = "CYCLES"
scene.render.image_settings.file_format = "JPEG"
scene.render.filepath = render_path

bpy.ops.render.render(write_still=True)

The console output looks like:

Blender 2.83.0 (hash unknown built 2020-06-21 00:00:00)
Read prefs: /home/user/.config/blender/2.83/config/userpref.blend
Fra:1 Mem:47.39M (0.00M, Peak 47.52M) | Time:00:00.00 | Mem:0.00M, Peak:0.00M | Scene, View Layer | Synchronizing object | Cube
Fra:1 Mem:47.40M (0.00M, Peak 47.52M) | Time:00:00.00 | Mem:0.00M, Peak:0.00M | Scene, View Layer | Initializing
Fra:1 Mem:47.28M (0.00M, Peak 47.52M) | Time:00:00.00 | Mem:0.00M, Peak:0.00M | Scene, View Layer | Waiting for render to start
Fra:1 Mem:47.30M (0.00M, Peak 47.52M) | Time:00:00.00 | Mem:0.00M, Peak:0.00M | Scene, View Layer | Loading render kernels (may take a few minutes the first time)
Fra:1 Mem:47.30M (0.00M, Peak 47.52M) | Time:00:00.00 | Mem:0.00M, Peak:0.00M | Scene, View Layer | Updating Scene
Fra:1 Mem:47.30M (0.00M, Peak 47.52M) | Time:00:00.00 | Mem:0.00M, Peak:0.00M | Scene, View Layer | Updating Shaders
Fra:1 Mem:47.80M (0.00M, Peak 47.80M) | Time:00:00.02 | Mem:0.00M, Peak:0.00M | Scene, View Layer | Updating Background
Fra:1 Mem:47.80M (0.00M, Peak 47.80M) | Time:00:00.02 | Mem:0.00M, Peak:0.00M | Scene, View Layer | Updating Camera
Fra:1 Mem:47.80M (0.00M, Peak 47.80M) | Time:00:00.02 | Mem:0.00M, Peak:0.00M | Scene, View Layer | Updating Meshes Flags
Fra:1 Mem:47.80M (0.00M, Peak 47.80M) | Time:00:00.02 | Mem:0.00M, Peak:0.00M | Scene, View Layer | Updating Objects
Fra:1 Mem:47.80M (0.00M, Peak 47.80M) | Time:00:00.02 | Mem:0.00M, Peak:0.00M | Scene, View Layer | Updating Objects | Copying Transformations to device
Fra:1 Mem:47.80M (0.00M, Peak 47.80M) | Time:00:00.02 | Mem:0.00M, Peak:0.00M | Scene, View Layer | Updating Objects | Applying Static Transformations
Fra:1 Mem:47.80M (0.00M, Peak 47.80M) | Time:00:00.02 | Mem:0.00M, Peak:0.00M | Scene, View Layer | Updating Hair Systems
Fra:1 Mem:47.80M (0.00M, Peak 47.80M) | Time:00:00.02 | Mem:0.00M, Peak:0.00M | Scene, View Layer | Updating Hair settings | Copying Hair settings to device
Fra:1 Mem:47.80M (0.00M, Peak 47.80M) | Time:00:00.02 | Mem:0.00M, Peak:0.00M | Scene, View Layer | Updating Particle Systems
Fra:1 Mem:47.80M (0.00M, Peak 47.80M) | Time:00:00.02 | Mem:0.00M, Peak:0.00M | Scene, View Layer | Updating Particle Systems | Copying Particles to device
Fra:1 Mem:47.80M (0.00M, Peak 47.80M) | Time:00:00.02 | Mem:0.00M, Peak:0.00M | Scene, View Layer | Updating Meshes
Fra:1 Mem:47.80M (0.00M, Peak 47.80M) | Time:00:00.02 | Mem:0.00M, Peak:0.00M | Scene, View Layer | Updating Mesh | Computing attributes
Fra:1 Mem:47.80M (0.00M, Peak 47.80M) | Time:00:00.02 | Mem:0.00M, Peak:0.00M | Scene, View Layer | Updating Mesh | Copying Attributes to device
Fra:1 Mem:47.80M (0.00M, Peak 47.80M) | Time:00:00.02 | Mem:0.00M, Peak:0.00M | Scene, View Layer | Updating Scene BVH | Building
Fra:1 Mem:47.80M (0.00M, Peak 47.80M) | Time:00:00.02 | Mem:0.00M, Peak:0.00M | Scene, View Layer | Updating Scene BVH | Building BVH
Fra:1 Mem:47.80M (0.00M, Peak 47.80M) | Time:00:00.02 | Mem:0.00M, Peak:0.00M | Scene, View Layer | Updating Scene BVH | Packing BVH triangles and strands
Fra:1 Mem:47.80M (0.00M, Peak 47.80M) | Time:00:00.02 | Mem:0.00M, Peak:0.00M | Scene, View Layer | Updating Scene BVH | Packing BVH nodes
Fra:1 Mem:47.80M (0.00M, Peak 47.80M) | Time:00:00.02 | Mem:0.00M, Peak:0.00M | Scene, View Layer | Updating Scene BVH | Copying BVH to device
Fra:1 Mem:47.80M (0.00M, Peak 47.80M) | Time:00:00.02 | Mem:0.00M, Peak:0.00M | Scene, View Layer | Updating Mesh | Computing normals
Fra:1 Mem:47.80M (0.00M, Peak 47.80M) | Time:00:00.02 | Mem:0.00M, Peak:0.00M | Scene, View Layer | Updating Mesh | Copying Mesh to device
Fra:1 Mem:47.80M (0.00M, Peak 47.80M) | Time:00:00.02 | Mem:0.00M, Peak:0.00M | Scene, View Layer | Updating Objects Flags
Fra:1 Mem:47.80M (0.00M, Peak 47.80M) | Time:00:00.02 | Mem:0.00M, Peak:0.00M | Scene, View Layer | Updating Images
Fra:1 Mem:47.80M (0.00M, Peak 47.80M) | Time:00:00.02 | Mem:0.00M, Peak:0.00M | Scene, View Layer | Updating Camera Volume
Fra:1 Mem:47.80M (0.00M, Peak 47.80M) | Time:00:00.02 | Mem:0.00M, Peak:0.00M | Scene, View Layer | Updating Lookup Tables
Fra:1 Mem:47.80M (0.00M, Peak 47.80M) | Time:00:00.02 | Mem:0.25M, Peak:0.25M | Scene, View Layer | Updating Lights
Fra:1 Mem:47.80M (0.00M, Peak 47.80M) | Time:00:00.02 | Mem:0.25M, Peak:0.25M | Scene, View Layer | Updating Lights | Computing distribution
Fra:1 Mem:47.80M (0.00M, Peak 47.80M) | Time:00:00.02 | Mem:0.25M, Peak:0.25M | Scene, View Layer | Updating Integrator
Fra:1 Mem:49.07M (0.00M, Peak 49.07M) | Time:00:00.03 | Mem:1.53M, Peak:1.53M | Scene, View Layer | Updating Film
Fra:1 Mem:49.08M (0.00M, Peak 49.33M) | Time:00:00.03 | Mem:1.28M, Peak:1.53M | Scene, View Layer | Updating Lookup Tables
Fra:1 Mem:49.08M (0.00M, Peak 49.33M) | Time:00:00.03 | Mem:1.53M, Peak:1.53M | Scene, View Layer | Updating Baking
Fra:1 Mem:49.08M (0.00M, Peak 49.33M) | Time:00:00.03 | Mem:1.53M, Peak:1.53M | Scene, View Layer | Updating Device | Writing constant memory
Fra:1 Mem:49.08M (0.00M, Peak 49.33M) | Time:00:00.03 | Mem:1.53M, Peak:1.53M | Scene, View Layer | Updating Device | Writing constant memory | Compiling render kernels
Fra:1 Mem:49.08M (0.00M, Peak 49.33M) | Time:00:00.03 | Mem:1.53M, Peak:1.53M | Scene, View Layer | Updating Device | Writing constant memory
Fra:1 Mem:49.08M (0.00M, Peak 49.33M) | Time:00:00.03 | Mem:1.53M, Peak:1.53M | Scene, View Layer | Rendered 0/510 Tiles
Fra:1 Mem:52.02M (0.00M, Peak 52.29M) | Time:00:00.14 | Remaining:00:02.51 | Mem:4.46M, Peak:4.59M | Scene, View Layer | Rendered 1/510 Tiles
...
Fra:1 Mem:49.08M (0.00M, Peak 52.29M) | Time:00:05.31 | Mem:1.53M, Peak:4.59M | Scene, View Layer | Rendered 510/510 Tiles
Fra:1 Mem:49.08M (0.00M, Peak 52.29M) | Time:00:05.31 | Mem:1.53M, Peak:4.59M | Scene, View Layer | Finished
Fra:1 Mem:47.47M (0.00M, Peak 52.29M) | Time:00:05.31 | Sce: Scene Ve:0 Fa:0 La:0
Saved: '/home/user/dev/blender-gen/render.jpg'
 Time: 00:05.60 (Saving: 00:00.28)

Read blend: /home/user/dev/blender-gen/simple.blend
Error: Not freed memory blocks: 8, total unfreed memory 0.008301 MB

Blender quit

How do I get shaders/textures working when rendering from Python?

$\endgroup$
6
$\begingroup$

You're not actually rendering the 'simple.blend' file. You're actually rendering the default blend file. This is because the order of command line arguments matters.

To explain further, your generate.py file runs and renders, then the simple.blend part of the argument gets interpreted and opens the blend file. To get this to work correctly you need to swap those parts of the command around, like so:

blender --background simple.blend --python generate.py
| improve this answer | |
$\endgroup$
  • 1
    $\begingroup$ Thank you so much. I've spending two days trying to figure this out. I wish I tried a different shape or that blender defaulted to an empty scene. $\endgroup$ – runfalk Jul 13 at 14:15
  • $\begingroup$ And now I understand why every tutorial starts with deleting the default cube! :) $\endgroup$ – Ron Jensen Jul 13 at 20:33

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.