Blender Python: Modifiers
<<< Return to the Blender Python Notebook
Summary
Modifiers give us access to some power mesh manipulation features. This page shows how to programatically apply a modifier to a mesh.
Subdivision Modifier
Create Mesh Object
The following code generates a cube…
import bpy #Define vertices, faces, edges verts = [(0,0,0),(0,5,0),(5,5,0),(5,0,0),(0,0,5),(0,5,5),(5,5,5),(5,0,5)] faces = [(0,1,2,3), (7,6,5,4), (0,4,5,1), (1,5,6,2), (2,6,7,3), (3,7,4,0)] #Define mesh and object mymesh = bpy.data.meshes.new("Cube") myobject = bpy.data.objects.new("Cube", mymesh) #Set location and scene of object myobject.location = bpy.context.scene.cursor_location bpy.context.scene.objects.link(myobject) #Create mesh mymesh.from_pydata(verts,[],faces) mymesh.update(calc_edges=True)
Apply Modifier
We can apply a subdivision modifier to the mesh using the following method…
# subdivide modifier myobject.modifiers.new("subd", type='SUBSURF')
Increase Subdivisions
We can also increase the number of visible subdivisions….
# Increase subdivisions myobject.modifiers['subd'].levels = 3
Smooth Shading
While not really related to the modifier… we can also change the vertex shading to "smooth" using the following technique…
# show mesh as smooth mypolys = mymesh.polygons for p in mypolys: p.use_smooth = True
Final Code
import bpy #Define vertices, faces, edges verts = [(0,0,0),(0,5,0),(5,5,0),(5,0,0),(0,0,5),(0,5,5),(5,5,5),(5,0,5)] faces = [(0,1,2,3), (7,6,5,4), (0,4,5,1), (1,5,6,2), (2,6,7,3), (3,7,4,0)] #Define mesh and object mymesh = bpy.data.meshes.new("Cube") myobject = bpy.data.objects.new("Cube", mymesh) #Set location and scene of object myobject.location = bpy.context.scene.cursor_location bpy.context.scene.objects.link(myobject) #Create mesh mymesh.from_pydata(verts,[],faces) mymesh.update(calc_edges=True) # subdivide modifier myobject.modifiers.new("subd", type='SUBSURF') # Increase subdivisions myobject.modifiers['subd'].levels = 3 # show mesh as smooth mypolys = mymesh.polygons for p in mypolys: p.use_smooth = True