Skip to main content.
  1. Parametric custom objects with Python
    1. Parametric Plane-object
    2. GUI
    3. Geometry code
    4. Whole code

Parametric custom objects with Python

So, Blender 2.44 is out and one of the best news for me was that it is now possible to add custom objects to Add-menu with Python. This is a short overview of how this can be done.

top

Parametric Plane-object

Let's make a Blender's plane object but now with user-defined size (width/height).

This is the start of the file. Name is the text shown in the Add-menu. Important part here is the group name: "AddMesh" defines that this script is shown in Add-menu.

#!BPY
"""
Name: 'MyVeryOWNObject'
Blender: 244
Group: 'AddMesh'
"""
import BPyAddMesh
import Blender

top

GUI

Before we write the actual mesh code, we can make the GUI ready and test it.

Let's start with main function:

#!BPY
"""
Name: 'MyVeryOWNObject'
Blender: 244
Group: 'AddMesh'
"""
import BPyAddMesh
import Blender

def main():
	
	# Numeric input with default value of 1
	planeWidthInput = Blender.Draw.Create(1.0)
	
	# and same for height
	planeHeightInput  = Blender.Draw.Create(1.0)

	# array for popup window's content	
	block = []
	
	# add inputs to array with title, min/max values and tooltip
	block.append(("width: ", planeWidthInput, 0.01, 100, "width of the plane"))
	block.append(("height: ", planeHeightInput, 0.01, 100, "height of the plane"))
	
	# draw	popup if it is not open allready 
	if not Blender.Draw.PupBlock("Create plane",block):
		return

#let's call main
main()

When you run this (Alt + P), you should see something like this:

Now we can add the actual geometry code.

top

Geometry code

For plane, the geometry code is really simple. Just add four points (xyz) to a vertices array and then add their indexes to a face array and return both.


def add_mymesh(PREF_WIDTH, PREF_HEIGHT):

	verts = []
	faces = []
		

	verts.append([-(PREF_WIDTH/2),(PREF_HEIGHT/2),0.0])
	verts.append([-(PREF_WIDTH/2),-(PREF_HEIGHT/2),0.0])
	verts.append([(PREF_WIDTH/2),-(PREF_HEIGHT/2),0.0])
	verts.append([(PREF_WIDTH/2),(PREF_HEIGHT/2),0.0])
	
	faces.append([0,1,2,3])
	
	return verts, faces

Then we just call that from popup function.

verts, faces = add_mymesh(planeWidthInput.val, planeHeightInput.val)

And just one line more and we are finished. This creates the actual Blender object and puts that to the scene:

BPyAddMesh.add_mesh_simple('MyPlane', verts, [], faces) 

NOTE: BPyAddMesh is from experimental Python api that is about to change without warning!

top

Whole code

#!BPY
"""
Name: 'MyVeryOWNObject'
Blender: 244
Group: 'AddMesh'
"""
import BPyAddMesh
import Blender

def add_mymesh(PREF_WIDTH, PREF_HEIGHT):

	verts = []
	faces = []
		
	verts.append([-(PREF_WIDTH/2),(PREF_HEIGHT/2),0.0])
	verts.append([-(PREF_WIDTH/2),-(PREF_HEIGHT/2),0.0])
	verts.append([(PREF_WIDTH/2),-(PREF_HEIGHT/2),0.0])
	verts.append([(PREF_WIDTH/2),(PREF_HEIGHT/2),0.0])
	
	faces.append([0,1,2,3])
	
	return verts, faces

def main():
	
	# Numeric input with default value of 1
	planeWidthInput = Blender.Draw.Create(1.0)
	
	# and same for height
	planeHeightInput  = Blender.Draw.Create(1.0)

	# array for popup window's content	
	block = []
	
	# add inputs to array with title, min/max values and tooltip
	block.append(("width: ", planeWidthInput, 0.01, 100, "width of the plane"))
	block.append(("height: ", planeHeightInput, 0.01, 100, "height of the plane"))
	
	# draw	popup if it is not open allready 
	if not Blender.Draw.PupBlock("Create plane",block):
		return
	
	verts, faces = add_mymesh(planeWidthInput.val, planeHeightInput.val)
	
	BPyAddMesh.add_mesh_simple('MyPlane', verts, [], faces)

main()

Just place script to your ./blender/scripts -directory and you should have a custom plane!