Using Blender to run Python and visualizing the Fourier Series

“Coding Druid” series is my horizontal programming practice notes, each part around a topic like mathematical, physics, electronic, graphics, sound, etc., implemented in several programming languages.


Coding Druid
Part: Math
Chapter: Fourier Series
Section: Python (in Blender)

In this Chapter, I have visualized Fourier Series separately using JavaScript (React):

fs-js-5

and Unity:

fs-unity

This section is Python again.


Playground options

In the note Sine function visualization using Python I used Jupyter Notebook as Python IDE/Playground.
Jupyter is a great online IDE for Python. You can basically do real-time programming, see the running results as you write, and mix code and documentation.

Jupyter

This will be easy, without having to think about how to set up a local Python environment, or how to visualize/render the results, and so on.
Just focus on Python code first, and write and view the results directly in the browser.

sine-py-jupyter

Jupyter is good, but it is not wild enough. If you want to do richer visualizations, instead of just drawing formula curves, then using Jupyter to run Python is a bit limited.

I need a Python runtime that does not have to worry too much about the implementation of graphics rendering. And also need the rendering effect is powerful and rude.

Blender and TouchDesigner are two software that meet this criteria.

Blender

blender-logo

Blender is an open source 3D content creation software. Its core functions can be compared to 3D Max, Maya, etc. It is size-small and simple but has rough functions. It crosses multiple operating systems. For details, see its official website:
https://www.blender.org/

blender-ui

Blender’s ecosystem is built on Python. Plugins, extensions, advanced customization features, and more can be written in Python.
In fact, you can also do real-time interactive games, which is a bit like Unity, of course, Blender is more focused on the production of 3D content.

TouchDesigner

TouchDesigner

TouchDesigner is a graphical programming tool that can be compared to other tools such as [MaxMSP] (https://cycling74.com/), [vvvv] (https://vvvv.org/), etc.
TouchDesigner’s extended scripting language is also Python.

For me, playing graphical programming with MaxMSP is enough for now.

So this time I use Blender as the IDE for Python programming.


Hello World

Firstly download the 2.8x installation file on Blender’s official website. Mac, Windows, and Linux all are supported.

Blender has a huge update since version 2.80. I don’t know exactly how big it is, because the last time I used Blender more than ten years ago. It was still version 2.4x, and I tried to use Python on Parametric Building Generation.
Anyway, I just use version 2.80 as a new software.

Lauched after installation:

blender-index

It has many functions, such as modeling, mapping, animation, rendering, etc. It is recommended to check the official tutorial.

This article is just focus on running Python.

blender-scripting

There is a Console in Scripting, which works out of the box, and you can enter Python code directly into it.
A century of tradition, let’s hello world:

blender-hello

It is inconvenient to write only in the Console. So click Text in the picture. This is actually a code text editor. Click New to start playing:

blender-scripting

Try hello world agian:

print ("hello world from Text Editor")

blender-hello-from-text-0

Input code and click Run Script. You can see that the text “hello world from Text Editor” is not printed in the console area on the left.

To debug the code in Text Editor you need to open the Console that comes with the operating system. Offical tips here:
https://docs.blender.org/api/current/info_tips_and_tricks.html#use-the-terminal

For Windows, just open the system Console in the help menu.

For Mac, firstly open the system Terminal (if you have Blender open, close it firstly), then open Blender in Terminal:

  • Open Terminal
  • Right click on Blender (.app) in the Application and select “Show Package Contents” or “View Package Contents”
  • Find Contents-> MacOS-> Blender
  • Drag Blender into the Terminal window
  • Enter
blender-terminal-drag

After opening the system Terminal, re-enter the text in Blender Scripting:
print ("hello world from Text Editor") 
Then Run Script, and now you can find the info printed in Terminal:

blender-terminal-print

Grease Pencil

Grease Pencil is a new feature of Blender version 2.80. It is a special Blender object that allows you to draw 2D graphics in 3D space, make traditional 2D animations, cut, move, and even make storyboards.

Grease Pencil


An example of a Grease Pencil object in the 3D environment.

As mentioned earlier, Blender is a software deeply integrated with Python. Basically all software operations have corresponding Python APIs to use.

After laying so much ground, it can finally be summarized as:

Drawing 2D graphics in Blender with calling Grease Pencil API using Python.

It’s a bit like drawing with code in Processing and P5js, but at the same time, you can use Blender’s powerful software functions directly to modify the textures, materials, etc.
At this point, it’s a bit like Unity’s operation.
If unfold Grease Pencil, this article will go further and further. So just list the reference resources here:

Note the bold content above: Blender 2.8 Grease Pencil Scripting and Generative Art, its author has encapsulated the commonly used drawing functions. An excerpt is given below. For details, please click the original text above.

For example, wrap a draw_line function to draw a line between two points:

blender-python

Then draw circles, curves:

blender-draw

Then use the Material function of Blender:

blender-gp-m
blender-gp-m

You can even combine Blender’s animation feature:

blender-gp-m

Fourier Series

Finally back to the topic …

Recall the simplified formula of Fourier Series:

fs-mathshot

Mathematical knowledge notes on Fourier Series, see Fourier Series Visualization Using React Hooks.

Write formula logic in Python, and call the Blender Grease Pencil API for drawing and rendering:

code

The complete source code can be found later.

Final effect:

fs-blender

Remember that? Grease Pencil actually draws 2D graphics in 3D space:

fs-blender

References


Talk is cheap. Show me the code!

This demo and the Coding Druid series is open source here:
https://github.com/avantcontra/coding-druid

You can find more resources in my site floatbug.com.

You may buy me a coffee in my Patreon. There are many articles, patches, source code and some advanced Patron-only content there.

Also you can get some free patches/codes in Gumroad shop.

Your encouragement is my driving energy!

Cheers~

Contra


Comments

4 responses to “Using Blender to run Python and visualizing the Fourier Series”

  1. […] is Python (Blender) version. There are JavaScript (React) version, […]

  2. dkkwon Avatar
    dkkwon

    Thank you very much. I found your blog while looking for a way to draw math graphs in Blender. It helped me so much. I’m very grateful. Have a good day and be healthy.

    1. Cool! My pleasure it could help you a little.

  3. […] In [Experimental Programming], I generally use Blender as a Python runtime environment out of the box, like:Using Blender to run Python and visualizing the Fourier Series. […]