Programmatically scan scenes using python

This tutorial (code snippet) shows you how to access the scanner directly via python. This allows you to create scans without manually setting the scanner parameters. It may be useful if the scanner information (i.e. the position/rotation) comes from external sources (i.e. files) instead of beeing designed within blender.

The following code will move the scanner to the position (0,0,0) and scan the scene to two files (scan00000.pcd for the ground truth and scan_noisy00000.pcd for the noisy scan) The scan is made with the Velodyne scanner, but the other scanners work very similar. Every scanner has a scan_advanced function, the parameters vary and have to be looked up in corresponding source file.

import bpy
from bpy import data as D
from bpy import context as C
from mathutils import *
from math import *

import blensor

"""If the scanner is the default camera it can be accessed 
    for example by["Camera"]"""
scanner =["Camera"]

"""Move it to a different location"""
scanner.location = (0,0,0)

"""Scan the scene with the Velodyne scanner and save it 
to the file "/tmp/scan.pcd"
Note: The data will actually be saved to /tmp/scan00000.pcd 
and /tmp/scan_noisy00000.pcd
blensor.blendodyne.scan_advanced(scanner, rotation_speed = 10.0, 
                                simulation_fps=24, angle_resolution = 0.1728, 
                                max_distance = 120, evd_file= "/tmp/scan1.pcd",
                                noise_mu=0.0, noise_sigma=0.03, start_angle = 0.0, 
                                end_angle = 360.0, evd_last_scan=True, 
                                add_blender_mesh = False, 
                                add_noisy_blender_mesh = False)

To run this code from the commandline save it as a .py file and run it as

./blender -P

or with a different blend file and in headless mode

./blender -b -P test.blend

Please note that BlenSor keeps running if you are not using it in headless mode. In this case your need to add


if you want BlenSor to quit afterwards. Running BlenSor with the GUI active might be useful to check on the progress of longer scans.

Further steps

The above code assumes that the BlenSor addon is activated in the default settings and that you are using the standard startup scene with one camera and the BlenSor cube or a premade Scene in a separate .blend file.

You can of course create whole scenes from your python scripts, but this goes beyond the scope of this tutorial. For more information please refer to the official Blender documentation

Uni Salzburg