Skip to content

Scripting

Fabian Brinkmann edited this page Aug 17, 2022 · 5 revisions

All parts of Mesh2HRTF can be scripted to speed up or document the work-flow. Scripting the NumCalc calculations and the post-processing is straight forward using the Mesh2HRTF API. The following thus only details how the Blender export can be scripted.

There are two possibilities for this:

  1. Call blender from the command using blender --background yourBlenderProject.blend --python yourPythonScript.py. This will open the Blender file yourBlenderProject and run the Python script yourPythonScript inside Blender in the background, i.e., without starting the Blender GUI.
  2. Execute Python Code from within Blender.

In both cases the Blender Python API can be used to automate everything that was done manually in the Tutorials: Importing meshes, creating and assigning materials, exporting Mesh2HRTF projects, installing or removing Blender AddOns, and more. Because it is more illustrative, we show how to export a Mesh2HRTF project using the second way.

Scripting Blender

  • Open Blender
  • Open a new text editor window:
    • Move the cursor to the bottom right corner of the main 3D Viewport window.
    • It becomes a Plus shape.
+ Click and pull to the left, opening a second *3D Viewport* window.
+ In the settings at the top, change this to _Text Editor_.
  • Now your screen should look something like this:
  • Scripted versions of the tutorials are contained in the Mesh2HRTF git repository in the folder Mesh2Input/Tutorials. The Python files can be dragged into Blender's tesxt editor. Here, we use the file for the first tutorial rigid_sphere_scattering.py
  • The script deletes the default cube that is visible, when opening Blender, creates the Reference and Point source objects, and generates the project folder.

  • Before running the script, adjust the file_path and program_path to your needs

  • To run the script, press the "Play" button at the top of the text editor. Once this is done, the Meh2HRTF project folder should have been generated at the specified location.

Note: The variables speedOfSound and densityOfMedium of mesh2input are string representations of the numbers. This was done in order to avoid rounding errors due to limited precision in Blender.

Enabling Python Tooltips

Python tooltips are a quick way to find the required Python commands to script an action in Blender.

  • Python Tooltips are enabled in the settings menue (Edit -> Preferences -> Interface -> Display).
  • Once enabled, the Python command will be displayed for each scriptable element when hovering the cursor over it. For example like this:

)