0
$\begingroup$

I am trying to programmatically generate and render a Blender scene with blender --background --python foo.py. The rendered image and the reference image is different.

How can I generate and render a scene without using the Blender interface?

Rendered image:

rendered

Reference image:

reference

foo.py (taken from https://blender.stackexchange.com/a/57032)

import bpy
from math import radians

# Add the base cube shape
bpy.ops.mesh.primitive_cube_add()

# Create a reference to the active object (which is the cube since we just added it)
cube = bpy.data.objects[ bpy.context.object.name ]

# Add a new subdivision surface modifier and set its view and render subdivision levels to 4
subsurf1 = cube.modifiers.new( "Subsurf", "SUBSURF" )
subsurf1.levels = subsurf1.render_levels = 4

# Add a new displacement modifier, set its texture to a new noise texture, and set the displacement strength to 0.15
disp = cube.modifiers.new( "Disp", "DISPLACE" )
disp.texture = bpy.data.textures.new( "DisplaceNoise", "NOISE" )
disp.strength = 0.15

# Add a 2nd subdivision surface modifier, and set its view and render levels to 2
subsurf2 = cube.modifiers.new( "Subsurf", "SUBSURF" )
subsurf2.levels = subsurf1.render_levels = 2

# Add a camera and set its location and rotation values
bpy.ops.object.camera_add( location = (0, -4, 0 ), rotation = (radians(90),0,0) )
camera = bpy.data.objects[ bpy.context.object.name ]
bpy.context.scene.camera = camera

# Give our rock object a new material
cube.active_material = bpy.data.materials.new("Rock.Mat")
mat = cube.active_material # Create a shorter reference variable to the material

# Set material properties
mat.diffuse_intensity  = 0.45
mat.specular_intensity = 0.1
mat.use_shadeless      = True

# Create a new musgrave texture and apply it to our materials as its active texture
mat.active_texture = bpy.data.textures.new( "Diffuse", "MUSGRAVE" )
mat.texture_slots[0].color = (0,0,0)         # Set base texture color to black
mat.texture_slots[0].texture_coords = 'ORCO' # Set texture coordiantes to object

# Set render filepath and render
bpy.context.scene.render.filepath = "/tmp/rock.png"
bpy.ops.render.render( write_still = True )

Standard output:

Read new prefs: /home/xgjxcxzk/.config/blender/2.76/config/userpref.blend
blender(BLI_system_backtrace+0x30) [0x1361b10]
blender(BLI_exists+0x70) [0x135e7e0]
blender(BLI_is_dir+0x9) [0x135e8a9]
blender() [0xfef59f]
blender() [0x98853d]
blender(wm_homefile_read+0x13e) [0x9892be]
blender(WM_init+0xde) [0x98b0be]
blender(main+0xe11) [0x95cd21]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf0) [0x7f04ed9a8830]
blender(_start+0x29) [0x97a2b9]
BLI_assert failed: /build/blender-40PcOX/blender-2.76.b+dfsg0/source/blender/blenlib/intern/storage.c:230, BLI_exists(), at '!BLI_path_is_rel(name)'
Fra:1 Mem:19.90M (0.00M, Peak 19.90M) | Time:00:00.01 | Mem:0.00M, Peak:0.00M | Scene, RenderLayer | Synchronizing object | Camera.001
Fra:1 Mem:19.90M (0.00M, Peak 19.90M) | Time:00:00.01 | Mem:0.00M, Peak:0.00M | Scene, RenderLayer | Synchronizing object | Cube.001
Fra:1 Mem:19.90M (0.00M, Peak 20.51M) | Time:00:00.01 | Mem:0.00M, Peak:0.00M | Scene, RenderLayer | Synchronizing object | Cube
Fra:1 Mem:19.90M (0.00M, Peak 20.51M) | Time:00:00.01 | Mem:0.00M, Peak:0.00M | Scene, RenderLayer | Synchronizing object | Lamp
Fra:1 Mem:19.90M (0.00M, Peak 20.51M) | Time:00:00.01 | Mem:0.00M, Peak:0.00M | Scene, RenderLayer | Synchronizing object | Camera
Fra:1 Mem:19.90M (0.00M, Peak 20.51M) | Time:00:00.01 | Mem:0.00M, Peak:0.00M | Scene, RenderLayer | Initializing
Fra:1 Mem:19.90M (0.00M, Peak 20.51M) | Time:00:00.01 | Mem:0.00M, Peak:0.00M | Scene, RenderLayer | Loading render kernels (may take a few minutes the first time)
Fra:1 Mem:19.90M (0.00M, Peak 20.51M) | Time:00:00.01 | Mem:0.00M, Peak:0.00M | Scene, RenderLayer | Waiting for render to start
Fra:1 Mem:19.90M (0.00M, Peak 20.51M) | Time:00:00.01 | Mem:0.00M, Peak:0.00M | Scene, RenderLayer | Updating Scene
Fra:1 Mem:19.90M (0.00M, Peak 20.51M) | Time:00:00.01 | Mem:0.00M, Peak:0.00M | Scene, RenderLayer | Updating Shaders
Fra:1 Mem:19.90M (0.00M, Peak 20.51M) | Time:00:00.08 | Mem:0.00M, Peak:0.00M | Scene, RenderLayer | Updating Background
Fra:1 Mem:19.90M (0.00M, Peak 20.51M) | Time:00:00.08 | Mem:0.00M, Peak:0.00M | Scene, RenderLayer | Updating Camera
Fra:1 Mem:19.90M (0.00M, Peak 20.51M) | Time:00:00.08 | Mem:0.00M, Peak:0.00M | Scene, RenderLayer | Updating Objects
Fra:1 Mem:19.90M (0.00M, Peak 20.51M) | Time:00:00.08 | Mem:0.00M, Peak:0.00M | Scene, RenderLayer | Updating Objects | Copying Transformations to device
Fra:1 Mem:19.90M (0.00M, Peak 20.51M) | Time:00:00.08 | Mem:0.00M, Peak:0.00M | Scene, RenderLayer | Updating Objects | Applying Static Transformations
Fra:1 Mem:19.90M (0.00M, Peak 20.51M) | Time:00:00.08 | Mem:0.00M, Peak:0.00M | Scene, RenderLayer | Updating Meshes Flags
Fra:1 Mem:19.90M (0.00M, Peak 20.51M) | Time:00:00.08 | Mem:0.00M, Peak:0.00M | Scene, RenderLayer | Updating Meshes
Fra:1 Mem:19.90M (0.00M, Peak 20.51M) | Time:00:00.08 | Mem:0.00M, Peak:0.00M | Scene, RenderLayer | Updating Mesh | Computing normals
Fra:1 Mem:19.90M (0.00M, Peak 20.51M) | Time:00:00.08 | Mem:0.00M, Peak:0.00M | Scene, RenderLayer | Updating Mesh | Copying Mesh to device
Fra:1 Mem:19.90M (0.00M, Peak 20.51M) | Time:00:00.08 | Mem:0.11M, Peak:0.11M | Scene, RenderLayer | Updating Mesh | Computing attributes
Fra:1 Mem:19.90M (0.00M, Peak 20.51M) | Time:00:00.08 | Mem:0.11M, Peak:0.11M | Scene, RenderLayer | Updating Mesh | Copying Attributes to device
Fra:1 Mem:19.90M (0.00M, Peak 20.51M) | Time:00:00.08 | Mem:0.11M, Peak:0.11M | Scene, RenderLayer | Updating Scene BVH | Building
Fra:1 Mem:19.90M (0.00M, Peak 20.51M) | Time:00:00.08 | Mem:0.11M, Peak:0.11M | Scene, RenderLayer | Updating Scene BVH | Building BVH
Fra:1 Mem:19.90M (0.00M, Peak 20.51M) | Time:00:00.08 | Mem:0.11M, Peak:0.11M | Scene, RenderLayer | Updating Scene BVH | Packing BVH triangles and strands
Fra:1 Mem:19.90M (0.00M, Peak 20.51M) | Time:00:00.08 | Mem:0.11M, Peak:0.11M | Scene, RenderLayer | Updating Scene BVH | Packing BVH nodes
Fra:1 Mem:19.90M (0.00M, Peak 20.51M) | Time:00:00.08 | Mem:0.11M, Peak:0.11M | Scene, RenderLayer | Updating Scene BVH | Copying BVH to device
Fra:1 Mem:19.90M (0.00M, Peak 20.51M) | Time:00:00.08 | Mem:0.35M, Peak:0.35M | Scene, RenderLayer | Updating Objects Flags
Fra:1 Mem:19.90M (0.00M, Peak 20.51M) | Time:00:00.08 | Mem:0.35M, Peak:0.35M | Scene, RenderLayer | Updating Images
Fra:1 Mem:19.90M (0.00M, Peak 20.51M) | Time:00:00.08 | Mem:0.35M, Peak:0.35M | Scene, RenderLayer | Updating Camera Volume
Fra:1 Mem:19.90M (0.00M, Peak 20.51M) | Time:00:00.08 | Mem:0.35M, Peak:0.35M | Scene, RenderLayer | Updating Hair Systems
Fra:1 Mem:19.90M (0.00M, Peak 20.51M) | Time:00:00.08 | Mem:0.35M, Peak:0.35M | Scene, RenderLayer | Updating Hair settings | Copying Hair settings to device
Fra:1 Mem:19.90M (0.00M, Peak 20.51M) | Time:00:00.08 | Mem:0.35M, Peak:0.35M | Scene, RenderLayer | Updating Lookup Tables
Fra:1 Mem:19.90M (0.00M, Peak 20.51M) | Time:00:00.08 | Mem:0.60M, Peak:0.60M | Scene, RenderLayer | Updating Lights
Fra:1 Mem:19.90M (0.00M, Peak 20.51M) | Time:00:00.08 | Mem:0.60M, Peak:0.60M | Scene, RenderLayer | Updating Lights | Computing distribution
Fra:1 Mem:19.90M (0.00M, Peak 20.51M) | Time:00:00.08 | Mem:0.60M, Peak:0.60M | Scene, RenderLayer | Updating Particle Systems
Fra:1 Mem:19.90M (0.00M, Peak 20.51M) | Time:00:00.08 | Mem:0.60M, Peak:0.60M | Scene, RenderLayer | Updating Particle Systems | Copying Particles to device
Fra:1 Mem:19.90M (0.00M, Peak 20.51M) | Time:00:00.08 | Mem:0.60M, Peak:0.60M | Scene, RenderLayer | Updating Film
Fra:1 Mem:19.90M (0.00M, Peak 20.51M) | Time:00:00.08 | Mem:0.60M, Peak:0.60M | Scene, RenderLayer | Updating Integrator
Fra:1 Mem:19.90M (0.00M, Peak 20.51M) | Time:00:00.08 | Mem:0.63M, Peak:0.63M | Scene, RenderLayer | Updating Lookup Tables
Fra:1 Mem:19.90M (0.00M, Peak 20.51M) | Time:00:00.08 | Mem:0.63M, Peak:0.63M | Scene, RenderLayer | Updating Baking
Fra:1 Mem:19.90M (0.00M, Peak 20.51M) | Time:00:00.08 | Mem:0.63M, Peak:0.63M | Scene, RenderLayer | Updating Device | Writing constant memory
Fra:1 Mem:19.90M (0.00M, Peak 20.51M) | Time:00:00.08 | Mem:0.63M, Peak:0.63M | Scene, RenderLayer | Path Tracing Tile 0/135
Fra:1 Mem:19.90M (0.00M, Peak 20.51M) | Time:00:00.08 | Mem:1.20M, Peak:1.20M | Scene, RenderLayer | Path Tracing Tile 4/135
Fra:1 Mem:19.90M (0.00M, Peak 20.51M) | Time:00:00.10 | Remaining:00:04.67 | Mem:1.05M, Peak:1.20M | Scene, RenderLayer | Path Tracing Tile 5/135
Fra:1 Mem:19.90M (0.00M, Peak 20.51M) | Time:00:00.10 | Remaining:00:04.68 | Mem:1.20M, Peak:1.20M | Scene, RenderLayer | Path Tracing Tile 6/135
...
Fra:1 Mem:19.90M (0.00M, Peak 20.51M) | Time:00:01.87 | Remaining:00:00.06 | Mem:0.88M, Peak:1.20M | Scene, RenderLayer | Path Tracing Tile 134/135
Fra:1 Mem:19.90M (0.00M, Peak 20.51M) | Time:00:01.88 | Remaining:00:00.04 | Mem:0.88M, Peak:1.20M | Scene, RenderLayer | Path Tracing Tile 135/135
Fra:1 Mem:19.90M (0.00M, Peak 20.51M) | Time:00:01.89 | Remaining:00:00.01 | Mem:0.82M, Peak:1.20M | Scene, RenderLayer | Path Tracing Tile 135/135, Sample 2/10
Fra:1 Mem:19.90M (0.00M, Peak 20.51M) | Time:00:01.89 | Remaining:00:00.00 | Mem:0.76M, Peak:1.20M | Scene, RenderLayer | Path Tracing Tile 135/135, Sample 5/10
Fra:1 Mem:19.90M (0.00M, Peak 20.51M) | Time:00:01.89 | Remaining:00:00.00 | Mem:0.69M, Peak:1.20M | Scene, RenderLayer | Path Tracing Tile 135/135, Sample 7/10
Fra:1 Mem:19.90M (0.00M, Peak 20.51M) | Time:00:01.89 | Mem:0.63M, Peak:1.20M | Scene, RenderLayer | Path Tracing Tile 135/135, Sample 10/10
Fra:1 Mem:19.90M (0.00M, Peak 20.51M) | Time:00:01.89 | Mem:0.63M, Peak:1.20M | Scene, RenderLayer | Finished
Fra:1 Mem:19.88M (0.00M, Peak 20.51M) | Time:00:01.90 | Sce: Scene Ve:0 Fa:0 La:0
Saved: /tmp/rock.jpg
 Time: 00:01.96 (Saving: 00:00.06)


Blender quit

When the script was released (July 2, 2016), the latest released version was 2.77a. I have tried with 2.76 (output above), 2.80 (stable) and 2.82 (alpha).

For 2.80 and 2.82, I get errors with the script:

AttributeError: 'Material' object has no attribute 'diffuse_intensity'

Commenting out the lines which cause errors, I get the same rendered image as in 2.76.

$\endgroup$
  • $\begingroup$ It seems like you are using Blender version 2.76 . What is rendered with import bpy; bpy.ops.render.render(write_still=True)? $\endgroup$ – Quantic_Solver Oct 14 at 7:30
  • $\begingroup$ @Quantic_Solver With import bpy; bpy.ops.render.render(write_still=True) in foo.py, I get an image named .jpg (no filename, just extension). The rendered image is here and seems to be a render of the default scene. $\endgroup$ – xgjxcxzk Oct 14 at 16:28
  • $\begingroup$ I have some scripts for Blender version 2.80, nothing advanced just some demo. For more examples. $\endgroup$ – Quantic_Solver Oct 15 at 15:08

Your Answer

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

Browse other questions tagged or ask your own question.