Blender Command Line (CLI): Dynamic Rendering of Text for WWW and More

Had a bit of a “Eureka!” moment when I discovered this …

Blender command line …

blender -b cube.blend -o //file -F JPEG -x 1 -f 1

“cube.blend” is just the default blender scene saved to a .blend file.

The CLI output …

djbarney@djbarney-anubis ~/Pictures
$ blender -b cube.blend -o //file -F JPEG -x 1 -f 1
Read new prefs: /home/djbarney/.config/blender/2.69/config/userpref.blend
AL lib: UpdateDeviceParams: Failed to set 44100hz, got 48000hz instead
found bundled python: /home/djbarney/Installations/blender-2.69-linux-glibc211-i686/2.69/python
read blend: /home/djbarney/Pictures/cube.blend
Fra:1 Mem:4.83M (0.00M, Peak 6.26M) | Preparing Scene data
Fra:1 Mem:4.83M (0.00M, Peak 6.26M) | Preparing Scene data
Fra:1 Mem:4.83M (0.00M, Peak 6.26M) | Creating Shadowbuffers

[...]

Fra:1 Mem:4.86M (9.98M, Peak 15.26M) | Scene, Part 133-135
Fra:1 Mem:4.86M (9.93M, Peak 15.26M) | Scene, Part 134-135
Fra:1 Mem:4.86M (9.93M, Peak 15.26M) | Scene, Part 135-135
Fra:1 Mem:4.74M (9.89M, Peak 15.26M) Sce: Scene Ve:8 Fa:6 La:1
Saved: /home/djbarney/Pictures/file0001.jpg Time: 00:01.27 (Saving: 00:00.43)
Blender quit

The result …

file0001

This introduces some interesting possibilities.

  • Rendering of 3D images for WebGL sites that want their 3D data to be seen by non-WebGL browsers. WebGL has a software rendering mode but that can be very slow. A rendered image is guaranteed to display.
  • 3D rendered titles and other text where the text is dynamic. The text can be taken from the date and time and other sources of text that change, like stock market figures or number of page shares.
  • 3D rendering of sources of dynamic data that can be rendered into 3D graphs, visualisations and other graphics.

Blender could be implemented in this way using the “cgi-bin”¬†which is available on most hosts, although I have not tested this yet.

Altering the 3D scene is achieved using Python. This can be included as part of a Bash script or any other kind of scripting.

I’m currently working on a script to put a 3D time and date on my desktop background.

I’ll post that here when I have time. I made some time. Here was my previous background image …

dynamic-background-dreamchaser

Here it is now with the current time added to it in 3D !

dynamic-background

Blend file … text_6.blend.

Python. Already in the Blender text editor. Adjust the font path if necessary …

import bpy
import datetime
import math
from math import pi
from time import gmtime, strftime
# delete any meshes or text in the current drawing
def removeObjects( scn ):
 for ob in scn.objects:
 if ob.type == 'FONT' or ob.type == 'MESH':
 scn.objects.unlink( ob )
scn = bpy.context.scene
removeObjects( scn )

# create several materials - each text line will use a different material
def makeMaterial(name, diffuse, specular, alpha):
 mat = bpy.data.materials.new(name)
 mat.diffuse_color = diffuse
 mat.diffuse_shader = 'LAMBERT'
 mat.diffuse_intensity = 1.0
 mat.specular_color = specular
 mat.specular_shader = 'COOKTORR'
 mat.specular_intensity = 0.5
 mat.alpha = alpha
 mat.ambient = 1
 return mat

def setMaterial(ob, mat):
 me = ob.data
 me.materials.append(mat)

red = makeMaterial('Red', (1, 0, 0), (1, 1, 1), 1)
blue = makeMaterial('BlueSemi', (0, 0, 1), (1, 1, 1), 0.5)
green = makeMaterial('GreenSemi', (0, 1, 0), (1, 1, 1), 0.5)
yellow = makeMaterial('YellowSemi', (1, 1, 0), (1, 1, 1), 0.5)

mytime = strftime("%Y-%m-%d %H:%M:%S", gmtime())

# Create and name TextCurve object #1
bpy.ops.object.text_add(
location=(-3, 0, 3.6),
rotation=(90, 0, 0))
ob = bpy.context.object
ob.name = 'Text1'
# TextCurve attributes
ob.data.name = 'TextData1'
ob.data.body = mytime
fnt = bpy.data.fonts.load('/usr/share/fonts/truetype/ttf-liberation/LiberationMono-Bold.ttf')
ob.data.font = fnt
ob.data.size = 2.75
# Inherited Curve attributes
ob.data.bevel_depth = 0.1
ob.data.bevel_resolution = 5
ob.data.extrude = 0.5
setMaterial(ob, red)
bpy.ops.object.convert(target='MESH', keep_original=False)

The commands required to make the background image …

#!/bin/bash
#
# Tell MATE which image to use for the desktop background.
gsettings set org.mate.background picture-filename /home/djbarney/Pictures/dynamic-background.png
#
# Render the current time 3D text. Blender must be in system path ($PATH).
blender -noaudio -y -b text_5.blend -o //3d-time -F PNG -x 1 -f 1
#
# Trim the surrounding pixels from the render.
gm convert -trim 3d-time0001.png 3d-text-trimmed.png
#
# Place the text on the background image, using the original image source. Creating the image using
the image name you gave to MATE will dynamically change the image.
gm composite -geometry +275+700 3d-text-trimmed.png dynamic-background-dreamchaser.png dynamic-background.png

The bash script could be run every minute or so from cron or in some other way.