This is a simple introduction to scripting in Blender with Python.
Blender 2.5+
To run the examples, go with your favorite console to the example folder, make sure that the blender executable is an environment variable or in the PATH environment variable in Windows and run the following command.
blender -b -P simple_sphere.py
Another option is to open the script in Blender and run the script inside Blender, which is a nice way to test and tweak the files and to see and play with the generated result before rendering.
Some frequently used functions in blender, which will be used in most of the examples.
Simple rendering of a smooth sphere. First an icosphere is added with
bpy.ops.mesh.primitive_ico_sphere_add(location=(0, 0, 0))
obj = bpy.context.object
Then the subdivision surface modifier is added to the object to increase the resolution of the mesh and afterwards all the faces of the object are set to a smooth shading
modifier = obj.modifiers.new('Subsurf', 'SUBSURF')
modifier.levels = 2
modifier.render_levels = 2
mesh = obj.data
for p in mesh.polygons:
p.use_smooth = True
Alternatively the icosphere can be subdivided with the subdivisions
argument in the function
bpy.ops.mesh.primitive_ico_sphere_add(subdivisions=4, location=(0, 0, 0))
Parametric generation of a torus. The torus is created with the following parameterization of a grid of the variables u, v
where the values u, v are between 0 and 1 and are then mapped to x, y, z coordinates. In parametric_torus.py, the function torusSurface(r0, r1)
returns the surface parameterization function for a torus which is then used in createSurface(surface, n, m)
as the first argument, which creates the object from a n by m grid. The function createSurface(surface, n, m)
can be also used for other parameterizations such as surfaces of revolution or other parametric surfaces.
Generate random metaballs in Blender inspired by this tutorial.
This is a more advanced example for using a Voronoi diagram. The Voronoi diagram is implemented with the module scipy.spatial
which can be added with Scipy, or can be found in the Python distribution Anaconda. The steps to use Anaconda as the Interpreter in Blender 2.77 are shown in this solution.
This is an example for a fractal tetrahedron, where each tetrahedron is subdivided into smaller pieces with a recursive function. In order to create a material for the tetrahedron the material is assigned as shown here:
color = (0.5, 0.5, 0.5)
mat = bpy.data.materials.new('Material')
# Diffuse
mat.diffuse_shader = 'LAMBERT'
mat.diffuse_intensity = 0.9
mat.diffuse_color = color
# Specular
mat.specular_intensity = 0
obj.data.materials.append(mat)