I’ve been working on a script to create a 2x4 plank with lap joints at both ends using Blender’s Python API. The goal is to use the script to make woodworking simulations more precise and visually accurate. The lap joints are cut at half the plank’s height and width and are positioned at both ends in opposing directions. This setup is intended to intersect with other planks at a 90-degree angle to form a proper lap joint.
Here is the code I’m using:
import bpy
Define the dimensions in inches
plank_width = 1.5 # 1.5 inches
plank_height = 3.5 # 3.5 inches
plank_length = 140 # 140 inches (11 feet 8 inches)
cut_length = 1.75 # Half the length of the lap joint (1.75 inches)
Create the main plank with exact dimensions
bpy.ops.mesh.primitive_cube_add(size=2, location=(0, 0, 0))
plank = bpy.context.active_object
plank.dimensions = (plank_length, plank_width, plank_height)
Ensure the plank’s location is reset after applying dimensions
plank.location = (0, 0, plank_height / 2)
Create the cut for the lap joint at one end (top side) and position it correctly within the plank boundaries
bpy.ops.mesh.primitive_cube_add(size=1, location=(-plank_length / 2 + cut_length / 2, 0, plank_height / 4))
cut1 = bpy.context.active_object
cut1.dimensions = (cut_length, plank_width, plank_height / 2)
Create the cut for the lap joint at the other end (bottom side, opposite direction) and position it correctly within the plank boundaries
bpy.ops.mesh.primitive_cube_add(size=1, location=(plank_length / 2 - cut_length / 2, 0, plank_height * 3 / 4))
cut2 = bpy.context.active_object
cut2.dimensions = (cut_length, plank_width, plank_height / 2)
Apply transformations to the cuts
bpy.ops.object.transform_apply(location=True, rotation=True, scale=True)
Ensure the plank is selected
plank.select_set(True)
bpy.context.view_layer.objects.active = plank
Perform the boolean difference operation to create the lap joints in order
mod_bool1 = plank.modifiers.new(name=‘LapJoint1’, type=‘BOOLEAN’)
mod_bool1.operation = ‘DIFFERENCE’
mod_bool1.object = cut1
bpy.context.view_layer.objects.active = plank
bpy.ops.object.modifier_apply(modifier=‘LapJoint1’)
mod_bool2 = plank.modifiers.new(name=‘LapJoint2’, type=‘BOOLEAN’)
mod_bool2.operation = ‘DIFFERENCE’
mod_bool2.object = cut2
bpy.context.view_layer.objects.active = plank
bpy.ops.object.modifier_apply(modifier=‘LapJoint2’)
Delete the cuts
bpy.data.objects.remove(cut1, do_unlink=True)
bpy.data.objects.remove(cut2, do_unlink=True)
print(“First plank with lap joints created successfully in imperial units.”)