简体   繁体   中英

Render using a blender script in google colab

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?

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)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM