2

I'm trying to run a script in blender, using the GPU available from google colab pro.

I use the following code to install and configure Blender:

import os
os.environ["LD_PRELOAD"] = ""

!apt remove libtcmalloc-minimal4
!apt install libtcmalloc-minimal4

os.environ["LD_PRELOAD"] = "/usr/lib/x86_64-linux-gnu/libtcmalloc_minimal.so.4.3.0"

!wget https://download.blender.org/release/Blender2.93/blender-2.93.0-linux-x64.tar.xz
!tar -xf blender-2.93.0-linux-x64.tar.xz

# if you still get any errors regarding LD_PRELOAD you can check whether a good path is applied (if the version of library has not changed)
!dpkg -L libtcmalloc-minimal4 

!ln -s /content/blender-2.93.0-stable+blender-v293-release.84da05a8b806-linux.x86_64-release/blender /usr/local/bin/blender
!blender -v

After that, I run my script as follows:

!blender -b -noaudio --python /content/code/generate_images.py -E CYCLES -a --cycles-device CUDA 

But it gives the following error:

Unable to open a display

The script generate_images.py contains a series of operations (camera and light are created, and the camera is moved along a trajectory of pre-defined positions), and at each position of the camera an image is rendered. I noticed that the error arises when the the following line is reached:

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

The script runs perfectly locally on my laptop. Do you have any clue about the problem?

Lorenzo
  • 51
  • 4
  • Blender can do [headless rendering](https://blender.stackexchange.com/questions/31255/can-blender-render-on-systems-without-a-gui). Looks like maybe you need to just pass in the `--background` flag? – Random Davis Jan 05 '22 at 16:46
  • The argument -b should be the same of --background – Lorenzo Jan 05 '22 at 16:50
  • Please don't include solutions in the Question section; instead opt to add an answer in the Answers section below as a self-answer. This makes it easier for others to find when looking at your question in the future. – esqew Jan 05 '22 at 17:19

2 Answers2

1

I was able to solve the issue by adding

bpy.context.scene.render.engine = 'CYCLES'

in the python script to be executed in Blender, right before

bpy.ops.render.render(write_still=True)
Lorenzo
  • 51
  • 4
0

You should render once on your machine before bringing it to colab to render

Nttkkhanh
  • 11
  • 1