Ceci est une ancienne révision du document !
As I sit in self-imposed isolation, I have once again struggled with what to present to you this month. I'm fairly certain that I won't be repeating anything that I have presented before. This month, we will explore Blender and its Python scripting possibilities.
If you aren't aware of Blender, I'll give you a quick introduction as to what it is, what it does, and how to do something simple, before we get into the programming aspects. I will say from the start, I am just learning how to do things with Blender from Python (and I'm not really that good with Blender by itself), so bear with me.
From their website (blender.org), “Blender is the free and open source 3D creation suite. It supports the entirety of the 3D pipeline—modeling, rigging, animation, simulation, rendering, compositing and motion tracking, video editing, and 2D animation pipeline.”
I'm sure that you have heard of “Big Buck Bunny”, but have you heard of Agent 327?
https://www.youtube.com/watch?v=mN0zPOpADL4
Blender runs on almost any platform. The latest version is 2.8.2a, and you can download it at https://www.blender.org/download/
One other thing, before we get started. Blender has always gone through many changes, and the tutorials out there don't often catch up. In fact, many of the tutorials (and many books) are written and published based on a preview or pre-release version that many times is changed multiple times before the actual version release. A good example is when a tutorial (for Blender 2.8.x) refers to changing things in preferences by accessing the File | User Preferences menu. That no longer exists. It's actually located under Edit | Preferences. I, at this point, don't have a good place to point you to in order to find all the changes like this. Many API calls were changed as well, so you should keep the API documentation close at hand when attempting to learn from a tutorial or book on the subject. The Blender 2.8.2a API reference is located at https://docs.blender.org/api/. Luckily, the API documentation has a change log from the last release version (2.7.9) at https://docs.blender.org/api/current/change_log.html
Now, on with the fun!
Download Blender and un-pack it in a convenient folder and run it from the terminal command:
$ ./blender
The first screen you should see is something like that shown below.
This is the default ‘new project’, and contains three objects: a cube, a camera and a light.
I should tell you, right now, that Blender is NOT something you can learn in a single day or week. However, if you really want to learn it, within a week, with the right tutorials (and this is NOT one of those), you can become very conversant and effective. You will ALWAYS need to have the keyboard and mouse very close, since Blender uses both for most any action. We'll get into the programming aspect in a couple of minutes. For now, let's do something with our Blender scene (bottom left).
I took a screenshot of Blender, opened it into Inkscape, and labeled the camera and light for you (I think the cube is fairly obvious), and showed the three axes for you in order to help you understand the ways things will move.
If we take a VERY close look at the cube, which is our default object when we start Blender, you will see something interesting…
See that little circle with an “X” and a dot in the cube? That’s the 3D cursor. This is the origin of all the objects that we put into our blender scene. Even though it doesn’t look like it, it is at the intersection of the Green and Red layout lines, and the origin is in the center of the cube. That means that only the “top” half of the cube is above the imaginary plane which is the grid that is shown on the layout screen.
Let’s do some coding…
Change your screen to the Scripting window tab, which should be close to the center of the screen at the top. You should see something like the image bottom right.
The editor (shown top right) is located near the center of the screen. The Blender menu for the editor is laid out a bit differently than what you would normally expect.
Normally, we would expect File | New, File | Open, and File | Save options, but these are located under ‘Text’.
LARGE DISCLAIMER: While I've been a user off and on of Blender for many years and a Python programmer for almost 15 years, until I started working on this month's article, I've never tried to do anything that mixed the two. You are learning what I am learning almost in real time, but without the pain.
We’ll start our first program. I found a few small programs for Blender at: https://medium.com/@behreajj/creative-coding-in-blender-a-primer-53e79ff71e.
However, due to changes in the API from when it was written and when I tried to run it, it refused to run, so I broke it down into a simpler version.
Use Text | New to make the editor allow you to type in our code. Here's the program itself…
import bpy
Of course, we need to import the Blender API library. Next, we’ll create a function (below) that will delete everything in the default scene, so we can put what we want where we want it.
We need to code it this way, since we have to delete each of the existing objects one at a time. There is no “delete all objects” command that I could find. The bpy.data.objects command will provide a “list” of all the objects, and we can step through that, one at a time, to remove or delete each of the objects.
Next, we’ll define two variables, the first for the size of the object and one for the size of the “world” that we will be creating.
sz = 2
extents = 8.0
Next, we’ll create a single cube (yes, I know we just deleted one, but this shows how to create a new object) and set its location. Notice that we set the cube at Z axis of 1, so it’s above the “floor”, which in this case, is imaginary, but you can create one later on if you want (see above).
That’s our code. Now save the code as “test1.py” and click on the ‘Run Script’ button. The script will run and you will see the result in the upper left layout window. If you press the {F-12} keyboard button, you will see the scene rendered.
It’s pretty boring, so I won’t even show it here, but it was important to show how to create an object from code.
Now we’ll start a new script to something a bit more interesting…
We’ll call this Test2.py.
We’ll go ahead and import the library and use the clear_scene function we created in test1.py (see next page, top left).
Now we need to define a number of variables for our program. This will include the extents (size of our “world”), the count of cubes that we will create (on each layer), the spacing between each cube, and the size of each cube. The comments within the original code should be enough to help you understand what is happening.
# Size of grid extents = 8.0 # Number of cubes count = 10 # Spacing between cubes padding = 0.005 # Size of each cube sz = (extents / count) - padding
# To convert abstract grid position within loop to real-world coordinates. iprc = 0.0 jprc = 0.0 kprc = 0.0 countf = 1.0 / (count - 1) diff = extents * 2
# Position of each cube. z = 0.0 y = 0.0 x = 0.0
# Center of grid. centerz = 0.0 centery = 0.0 centerx = 0.0
Now we’ll create our cube of cubes (shown below).
The rest of our program will be just like the end of test1.py, in that we create a lamp and camera into the scene (next page).
Now, when we run the program, it will take some time. On my machine, it took over a minute and a half. Nothing happens on the blender screen, but if you look at the terminal window, you will see the output of the print statements that shows something is actually happening. Remember, we are creating 1000 cubes (10 x 10 x 10) along with the light and camera.
Shown right is what the result of test2.py looks like when rendered.
As always, I have uploaded these two programs onto pastebin…
Test1.py https://pastebin.com/enNDN0mh Test2.py https://pastebin.com/CaVXwhDQ
Until next month; stay safe, healthy, positive and creative!