Creating your first Python script in Blender

In this tutorial we take you through creating a python script that will perform the typical initial steps for setting up mirror-modifier based box modeling work:

-Add a cube

-Take it to edit mode

-Scale it to half the size on x-axis

-Move it 0.5 units right on the x-axis

-Select the face that is on the center axis

-Delete that face

-Add the mirror modifier

-Turn on clipping for the mirror modifier

Here’s the finished script:

import bpy
import bmesh

bpy.ops.mesh.primitive_cube_add(size=2, view_align=False, enter_editmode=True, location=(0, 0, 0))

bpy.ops.mesh.select_all(action='SELECT')

bpy.ops.transform.resize(value=(0.5, 1, 1))

bpy.ops.transform.translate(value=(0.5, 0, 0))

bpy.ops.mesh.select_all(action='DESELECT')

obj = bpy.context.edit_object
me = obj.data

bm = bmesh.from_edit_mesh(me)

bm.faces.active = None

for face in bm.faces:
    if face.calc_center_bounds()[0]== 0:
        face.select = True
                
bpy.ops.mesh.delete(type='FACE')

bpy.ops.object.modifier_add(type='MIRROR')
bpy.context.object.modifiers["Mirror"].use_clip = True

Now let’s walk through the script line by line:

import bpy
= adds Blender’s python interpreter so that we get access to Blender’s code library

import bmesh
= adds the bmesh library, this helps us edit the mesh in edit mode

bpy.ops.mesh.primitive_cube_add(size=2, view_align=False, enter_editmode=True, location=(0, 0, 0))
= add the default cube primitive, give it a size of 2 Blender units, don’t align it to the view, take it to edit mode and set it’s location coordinates to 0, 0, 0

bpy.ops.mesh.select_all(action=’SELECT’)
=select the entire mesh

bpy.ops.transform.resize(value=(0.5, 1, 1))
= scale the mesh to 0.5 (half of the original size) on the x-axis and leave y and z as is (1).

bpy.ops.transform.translate(value=(0.5, 0, 0))
=move the selection 0.5 Blender units on the x-axis

bpy.ops.mesh.select_all(action=’DESELECT’)
= deselect everything

obj = bpy.context.edit_object
= store a reference of the object we are currently editing to the “obj” variable (at least I think that’s what this line does)

me = obj.data
= store the data of the current object to a variable called “me”

bm = bmesh.from_edit_mesh(me)
= fill a bmesh object with the data from the current mesh

bm.faces.active = None
= make sure there is no active face

for face in bm.faces:
if face.calc_center_bounds()[0]== 0:
face.select = True

= loop through all the faces of the bmesh object and if the center point of a face equals zero on the x-axis (signified by [0]), select it

bpy.ops.mesh.delete(type=’FACE’)
= delete the selected face

bpy.ops.object.modifier_add(type=’MIRROR’)
= add the mirror modifier

bpy.context.object.modifiers[“Mirror”].use_clip = True
= turn on clipping for the mirror modifier (to prevent vertices from crossing to the other side of the mirror boundary)

Write this script to a new text document in the Blender text editor and click on the “run” button to execute it! Happy modeling!

Leave a Reply

Your email address will not be published.