Good evening!

I encountered a situation when I needed to draw a freehand-ish plot to use in my Python program as a testing example. Essentially, all I needed was a list of vertical positions of points, because they would be evenly distributed on x-axis (I simply did x=numpy.array(range(len(y))))). Since I like Python, I’ve decided to do it in a program that is not really designed for this particular task.

Blender

A mindblowingly powerful and multi-purpose 3D editor, even though it is open-source and available at the official website for free. I used the version 2.79b (version 2.8 is in alpha or beta when I’m writing this). What’s important, it has a fully functional Python console built into the program itself!

I’ve prepared a little example file for you here. Now here’s a crash course that will allow you to edit the graph and get the list of y-coordinates of points.

Crash course

Important! The window is divided into several contexts. They are basically subwindows. The example I’ve provided has three: the 3D view where the graph object is, the Python console, and the text editor (actually there are four contexts there, but the info context’s window body is hidden above, you see only the menus. But that’s irrelevant for now). When you press hotkeys expecting a certain behaviour in a certain context, your mouse pointer should hover over this context. For example, if you are typing something in Python console below, your mouse pointer should be above the Python console. If you’re typing in text editor on the right, hover over it. If you’re working in the 3D view, pressing the hotkeys, mouse pointer should be there.

Editing the curve

Note! Everything in this subsection is happening in 3D view, so hover the mouse cursor over it when you press the keys!

Press Tab to enter Edit Mode. Now you can select the vertices with your right mouse button. I know, Blender is kinda weird. If you don’t like this idea, you can press Ctrl+Alt+U to open preferences, go to Input tab and under Select With change it to Left.

There are several ways to select multiple vertices:

  • press B and drag left mouse button to box-select;
  • press C and drag left mouse button to select with a circle, like a brush; you can rotate the mouse wheel to change the radius of the circle;
  • hold Ctrl and drag left mouse button to lasso-select.

To move a vertex, press G. You may press X or Z afterwards to constrain movement to horizontal or vertical axis. Press left mouse button to put a vertex there or right mouse button to cancel movement.

Oh, by the way, if you accidentally rotate the 3D view and it is no longer Front Orthographic, press Numpad 1 to rotate the view back. The Python commands provided in this tutorial will only work from that view, because the X and Z axes are used.

To delete a vertex, press X and select Vertices in the menu.

The edges between vertices will not affect the final result. The only thing that matters is the order of vertices on X-axis. If you want, you may delete the edges by pressing X and selecting Only Edges & Faces.

Yet, there are some cool tricks that can be done on edges, so here’s a quick list of operations.

Select two vertices and press F to connect them with an edge. Now you can press W and select Subdivide from the menu. This will add a vertex on the edge. You can make it add even more by changing Number of cuts in the menu on the right (if you don’t see it, press T).

Note! Once you’re done editing the points, press Tab again to return to Object Mode, or else the points will not be internally updated and Python command will not read them right.

Getting the list of vertical coordinates.

Everything in this subsection is happening in Blender’s Python console, so hover the mouse cursor over it when you press the keys!

Now all you have to do is copy this code snippet into the console:

[i[1]*100 for i in sorted([(v.co.x,v.co.z) for v in bpy.context.active_object.data.vertices], key=lambda x: x[0])]

The array of Z (vertical) coordinates will be printed in the console. You can also assign it to a variable (a in my example) and export it to a file using, for instance

with open('/tmp/001.txt','w') as f: f.write(str(a))

You can play around with the console to achieve whatever effects you desire, it is pretty much a completely functional Python console which can also work with objects inside Blender workspace.

Conclusion

This was mostly a quick lifehack. Blender is a powerful tool, you can even edit videos and make games (although Blender Game Engine will be discontinued in 2.8+) in it! And there are even more things you can do with Python.

Thanks for tuning in. I’ll see you eventually.