Skip to content
Home » Extrude in Blender Along Function Graphs Using Python

Extrude in Blender Along Function Graphs Using Python

Spread the love

Today we’re going to use Python to extrude in Blender along a function graph. We’ll have a look at the linear function and the quadratic function. I’m using the 2.82 version of Blender.

Here’s the video version:

And now the steps.

Step 1 – Add a Plane

Delete the default cube. We’re not going to need it. Now we need a single vertex to extrude it along the graph of a function. There is no option to add a single vertex in the menu, but we can easily work around it by creating any mesh object, selecting all its vertices in edit mode, deselecting one vertex (the one that we want to keep) and deleting all the others. So, let’s create a simple mesh. How about a plane? With the plane selected go to edit mode and deselect the vertex you want to keep. The other three vertices should be selected.

Add a Plane

Step 2 – Leave a Single Vertex

Hit X on your keyboard and select Vertices to delete the three selected vertices. Now there is only one vertex left. Select it (it may not be well visible, but you probably remember where it is) and move to the 3D cursor, which is in the world center, so at location 0, 0, 0. To move the vertex use the Shift + S shortcut and select Selection to Cursor. Now the vertex is at the beginning of the coordinate system.

Leave a Single Vertex

Step 3 – Create a Script

Now go to the Scripting workspace and create a new script. Import the bpy module:

import bpy

Step 4 – Adjust the View in the 3D View Editor

In the 3D View editor, which is in the upper left corner, go to top view and pan the view so that the vertex (which is at the same location as the cursor) is closer to the lower left corner of the editor. This way you will mostly see the first quarter of the coordinate system, so the one where both the x values and the y values increase. We are going to graph the linear function only in this quarter.

Adjust the View in the 3D View Editor

Step 5 – Extrude in Blender along a Linear Function Graph

So, the first function we’re going to have a look at is the linear function. Let’s assume our linear function is very simple:

f(x) = x, or in another form y = x. We are only going to extrude in the XY plane.

But what function do we need to extrude something in Blender? Well, in its basic form, so without most of the available keyword arguments it may have, the function has the following syntax:

bpy.ops.mesh.extrude_region_move(TRANSFORM_OT_translate={"value":(dx, dy, dz)})

where dx, dy and dz are the translations (not the locations) along the corresponding axes.

If the function is linear and has the form y = x, then the values for x between 0 and 5 are:

xydxdy
00original x locationoriginal y location
1111
2211
3311
4411
5511

Your Panda3D Magazine

Make Awesome Games and Other 3D Apps

with Panda3D and Blender using Python.

Cool stuff, easy to follow articles.

Get the magazine here (PDF).

As you can see, as the x value increases by 1, so does the y value. The increment is 1 unit each time. So now we can write a for loop and in the loop let’s extrude the vertex in each iteration along the graph of the function y = x. I’m going to expand the range a bit and use the values between 0 and 10.  Here’s the code:

for x in range(10):
    bpy.ops.mesh.extrude_region_move(TRANSFORM_OT_translate={"value":(1, 1, 0)})

Step 6 – Run the Linear Function Script to Extrude in Blender

Run the script and then zoom in in the 3D View editor. As you can see, the vertex has been extruded 10 times along the graph of the linear function y = x.

Run the Linear Function Script

Step 7 – Clear the 3D View Editor

And now let’s try out another linear function, y = 2x – 1. First we need to clear the 3D View editor to get rid of the vertices. In the 3D View editor select all, then deselect the original vertex and delete the selected ones.

Clear the 3D View Editor

Step 8 – Another Linear Function

Now the function is linear and has the form y = 2x – 1, so the first values are:

xydxdy
0-1original x locationoriginal y location – 1
1112
2312
3512
4712
5912

As you can see, when the value of x is 0, the value of y is -1, which is not in the first quarter. The graph of the function enters the first quarter at x = 0.5. Then y = 2 * 0.5 – 1 = 0. So, let’s move the vertex 0.5 units along the X axis: G X 0.5

Another Linear Function

Step 9 – Write Script to Extrude in Blender along the Graph

Now let’s make the first extrusion to the point 1, 1 and then let’s continue in a loop:

import bpy

bpy.ops.mesh.extrude_region_move(TRANSFORM_OT_translate={"value":(0.5, 1, 0)})

for x in range(10):
    bpy.ops.mesh.extrude_region_move(TRANSFORM_OT_translate={"value":(1, 2, 0)})

Python Jumpstart Course

Learn the basics of Python, including OOP.

with lots of exercises, easy to follow

The course is available on Udemy.

Step 10 – Run the Script for the Other Linear Function

If you run the script now, the vertex will be extruded along the graph of the function:

Run the Script for the Other Linear Function

Step 11 – Extrude in Blender along a Quadratic Function Graph

Delete all the vertices except the first one and then select the vertex. Now let’s use the quadratic function. In its most basic form y = x ** 2. The graph of this function is a parabola. This time we we’ll be graphing the function in the first and second quarter of the coordinate system. Let’s limit ourselves to the range between x = -3 and x = 3 and let’s take only integer values into consideration for now. The values are :

xydxdy
-39original x locationoriginal y location
-241-5
-111-3
001-1
1111
2413
39 5

Step 12 – Move the Original Vertex

As we’ll be using the part of the graph between x = -3 and x = 3, the location of the original vertex must be changed. Move the vertex to the beginning of this range, so to the location -3, 9, 0. The easiest way to do it is by setting the X, Y and Z location in the properties panel, which you can open by hitting N on your keybord. Type in the values for X and Y, leaving Z at 0. Now the vertex is located at these coordinates.

Move the Original Vertex

Step 13 – Code the Quadratic Function Extrusion

You can hit N again to close the panel. Here’s the code:

import bpy

for x in range(-3, 3):
    # This is how we calculate how much the Y location should change.
    dy = x * 2 + 1
    bpy.ops.mesh.extrude_region_move(TRANSFORM_OT_translate={"value":(1, dy, 0)})

Blender Jumpstart Course

Learn the basics of 3D modeling in Blender.

step-by-step, easy to follow, visually rich

The course is available on Udemy and on Skillshare.

Step 14 – Run the Quadratic Function Script

Run the script. This is what you should get:

Run the Quadratic Function Script

Step 15 – Add More Points

Well, this works for a simple scenario like this. But suppose you need more points along the graph. To this end we can use the linspace function from the numpy module, which is included in Blender. Let’s create 100 points. But first remove all the vertices except the one from which you want to extrude. Make sure it’s at x = -3 and y = 9. Also, make sure to select the vertex so that the script can run.

Now type in the following script. All necessary explanations are in the comments (which you naturally don’t have to type).

import bpy

# We need the linspace function, which is in the numpy module. So, let's import
# the module with an alias, as we usually do.
import numpy as np

# The linspace function returns evenly spaced numbers over a specified interval.
# We want 100 (hence the third argument) evenly spaced numbers between -3 and 3.
# Both negative and positive 3 will be included. These will be the X coordinates.
xcoords = np.linspace(-3, 3, 100)

# The Y coordinates will be calculated as squares of the X coordinates. I'm using
# a list comprehension to create the list of Y values).
ycoords = [x ** 2 for x in xcoords]

# We can use the enumerate function in our loop. This way the indices of the
# elements of the list will be available.
for n, x in enumerate(xcoords):
    # The X and Y translations should be only calculated if the index is less
    # than 99. Otherwise the index would be out of bounds after adding 1 to it. 
    if n < 99:
        # The X and Y translations are calculated as differences between
        # neighboring elements.
        dx = xcoords[n+1] - xcoords[n]    
        dy = ycoords[n+1] - ycoords[n]
        bpy.ops.mesh.extrude_region_move(TRANSFORM_OT_translate={"value":(dx, dy, 0)})

Step 16 – Run the Modified Script

And this is what you should get when you run the script:

Run the Modified Script


Spread the love

Leave a Reply