Blender scripts

This module contains scripts that call Blender directly (prefix bs_) as well as a few more common sub-modules that do not call Blender. All of these scripts must be called using rendering.blender_control which acts as an intermediary to decouple the HyperBlend’s code from Blender calls.

Note

Blender runs its own Python environment when called. That’s why the import statements in all bs_ files look a bit weird. The local HyperBlend modules have to be added to Blender’s Python manually.

Configuration

Reflectance lab

render_reflectance(wl, value)
set_camera()

Set reflectance camera active.

Render forest

call_blender_render(write_still=True, animation=False)

Saves current scene before calling Blender rendering.

If the rendering crashes, one can inspect the scene file to find out what went wrong.

composite_delete_masking_setup()

Material masking setup must be deleted for any other renders than visibility rendering.

Otherwise, visibility masks are rewritten for every rendered frame. NOTE: this deletes any existing ID Mask and File Output nodes from Compositing.

composite_material_mask()

Assign pass indices to materials for visibility maps.

Adapted from https://blenderartists.org/t/simple-script-to-create-a-unique-material-index-for-all-cycles-materials-in-the-scene/581087

Deletes any existing masking setup before building a new one.

composite_raw()

Set up render output (compositing) to produce raw render result.

This should used for any other renders but visibility maps. If this is not called, there is no connected sockets to output and the rendered image is completely black.

render_drone_hsi()
render_drone_rgb()
render_sleeper_rgb()
render_tree_rgb()
render_visibility_maps()
render_walker_rgb()
set_render_parameters(render_mode: str = 'spectral', camera: str = 'Drone RGB', res_x=512, res_y=512, res_percent=100)

Sets render parameters for spectral or RGB rendering.

Parameters:
  • camera – Name of the camera that is to be used for rendeering.

  • render_mode – Either ‘spectral’ ‘visibility’ or ‘rgb’.

  • res_x – Resolution to x-direction.

  • res_y – Resolution to y-direction.

  • res_percent – Resolution percentage. For example, use 50 to render an image where both x and y resolution are halved.

set_visibility(mode: str)

Render series

This is the rendering script that Blender runs when evoked by blender_control.py.

Object names in this file and the Blender scene file must match. We cannot use the constants.py file here as this script is run in Blender’s own Python environment, so all object names and such are hard coded.

Debug prints do not show if Blender output is directed to dev.null in blender_control.py.

TODO: explicitly set up render engine and such so that Blender’s default setting that are likely to change in future do not break the script.

class Range(start, end)

Range class for automatically checking input values by argparser.

clear_folders(clear_reference=False)

Clear render folders. Reference folder are also cleared if clear_reference=True.

Creates the folders if not exist, so it should be safe to call. This is mainly for debugging if run straight from Blender. Primarily, use clearing code in file_handling.py.

get_active_camera()

Returns the camera that is currently in use.

make_folders()

Create default folder if not exist.

render_leaf(imaging_type, wl, abs_dens, scat_dens, scat_ai, mix_fac, dry_run=True)

Renders the leaf target with given arguments.

render_reference(imaging_type, wl, dry_run=True)

Renders white reference in either reflectance or transmittance mode.

render_target(imaging_type, wl, target_name, render_path, dry_run=True)

General rendering function.

set_active_camera(cam_name)

Set rendering camera to be used. Must be either ‘refl’ or ‘tran’

toggle_cam()

Toggle from reflectance to transmittance camera and vice versa.

Render single

This is the rendering script that Blender runs when evoked by blender_control.py.

Object names in this file and the Blender scene file must match. We cannot use the constants.py file here as this script is run in Blender’s own Python environment, so all object names and such are hard coded.

Debug prints do not show if Blender output is directed to dev.null in blender_control.py.

TODO: explicitly set up render engine and such so that Blender’s default setting that are likely to change in future do not break the script.

class Range(start, end)

Range class for automatically checking input values by argparser.

clear_folders(clear_reference=False)

Clear render folders. Reference folder are also cleared if clear_reference=True.

Creates the folders if not exist, so it should be safe to call. This is mainly for debugging if run straight from Blender. Primarily, use clearing code in file_handling.py.

get_active_camera()

Returns the camera that is currently in use.

render_leaf(imaging_type, wl, abs_dens, scat_dens, scat_ai, mix_fac, dry_run=True)

Renders the leaf target with given arguments.

render_reference(imaging_type, wl, dry_run=True)

Renders white reference in either reflectance or transmittance mode.

render_target(imaging_type, wl, target_name, render_path, dry_run=True)

General rendering function.

set_active_camera(cam_name)

Set rendering camera to be used. Must be either ‘refl’ or ‘tran’

toggle_cam()

Toggle from reflectance to transmittance camera and vice versa.

Setup forest

_set_leaf_rgb(leaf_material_name: str)

Read leaf RGB values from a csv and set them to blend file.

init_cameras()
insert_diffuse_material_spectra(material_name)

TODO: Implement trunk data insertion. 1. Find the correct trunk csv file. This can be a general reflectance only file. 2. Read it 3. Find the tree it’s supposed to go with 4. Find the trunk material of that tree 5. Insert the data for each frame 6. Set the RGB color of the trunk material to match the csv file.

insert_leaf_data(leaf_material_names: str)

Reads spectral leaf material csv file and sets proper parameters to the Blender file.

Parameters:

leaf_material_names – Names of the leaf materials (must mach the ones used in the Blender file) as a single string that looks like a list of strings like so: “[‘Leaf material 1’, ‘Leaf material 2’,…]”.

insert_soil_data()

Reads soil data csv and inserts its content into the Blender file.

read_leaf_material_csv(file_name: str)

Read spectral leaf material parameters from a CSV file and return as a bunch of lists.

Returns:

band_list, wl_list, ad_list, sd_list, ai_list, mf_list

set_animation_frames(band_count)

Sets start and end frames to blend file.

Each wavelength band needs one frame.

set_leaf_material(leaf_material_name, band_list, ad_list, sd_list, ai_list, mf_list)

Set leaf material for all frames in band list.

Parameters:
  • leaf_material_name – Must be a name that can be found in Blender system_simulation scene file materials.

  • band_list – List of spectral bands (ints) that correspond to animation frames in Blender.

  • ad_list – List of absorbing particle densities (floats).

  • sd_list – List of scattering particle densities (floats).

  • ai_list – List of scatering anisotropies (floats).

  • mf_list – List of mixing factors (floats).

Forest constants

Names of things in system_simulation blend file. All of them are case-sensitive.

ctrl_default_std_of_value = 0.1

Default standard deviation assigned to numeric parameters in master system_simulation control files (as percentage 0-1).

key_ctrl_drone_hsi_fow = 'drone_hsi_fow'

Drone hyperspectral camera’s field of view.

key_ctrl_drone_rgb_fow = 'drone_rgb_fow'

Drone RGB camera’s field of view.

key_drone = 'Drone'

Drone object that has Drone HSI and Drone RGB cameras attached to it.

Forest control

This script is responsible for writing and reading system_simulation control toml files.

This functionality would be better placed under data.toml_handling, but due to Blender using different Python environment than the rest of the code, it would create problems with dependencies.

read_forest_control(system_sim_name: str, global_master=False) dict

Read forest control file.

Parameters:
  • system_sim_name – See system_sim_name. Ignored if global_master is True.

  • global_master

Returns:

read_toml_as_dict(directory: str, filename: str)

General purpose toml reading method.

Parameters:
  • directory – Path to the directory where the toml file is.

  • filename – Name of the file to be read. Postfix ‘.toml’ will be added if necessary.

Return dictionary:

Returns read toml file as a dictionary.

write_dict_as_toml(dictionary: dict, directory: str, filename: str)

General purpose dictionary saving method.

Parameters:
  • dictionary – Dictionary to be written as toml.

  • directory – Path to the directory where the toml should be written.

  • filename – Name of the file to be written. Postfix ‘.toml’ will be added if necessary.

write_forest_control(system_sim_name: str, control_dict: dict, global_master: bool = False)

Writes system_simulation control file.

Parameters:
  • system_sim_name – See system_sim_name. Ignored if global_master is True.

  • control_dict – Dictionary to be written.

  • global_master – If True, global master file is written to project root/Internal/. Needs to be done if there are changes made to the system_simulation template file. This will be kept safe in the Git repository. Default is False.

Forest utils