write me a blender python script that will create a table
- User
write me a blender python script that will create a table
- Assistant
write me a blender python script that will create a table
Certainly! Below is a Blender Python script that creates a simple table. This script can be run within Blender's scripting environment. I'll include comments and explanations to help you understand each step.
import bpy
# Clear existing objects
bpy.ops.object.select_all(action='SELECT')
bpy.ops.object.delete(use_global=False)
# Set measurements for the table
table_length = 2.0 # X-axis
table_width = 1.0 # Y-axis
table_thickness = 0.1 # Z-axis thickness of the tabletop
leg_thickness = 0.1 # Thickness of the legs
leg_height = 1.0 # Height of the legs
# Create the tabletop
bpy.ops.mesh.primitive_cube_add(size=1, location=(0, 0, leg_height + table_thickness / 2))
tabletop = bpy.context.active_object
tabletop.name = 'Tabletop'
tabletop.scale.x = table_length / 2 # Adjust the scale because cube size is 1
tabletop.scale.y = table_width / 2
tabletop.scale.z = table_thickness / 2
# Function to create a leg at a specified location
def create_leg(x, y):
bpy.ops.mesh.primitive_cube_add(size=1, location=(x, y, leg_height / 2))
leg = bpy.context.active_object
leg.name = 'Leg'
leg.scale.x = leg_thickness / 2
leg.scale.y = leg_thickness / 2
leg.scale.z = leg_height / 2
return leg
# Positions for the four legs relative to the tabletop
x_positions = [(table_length / 2) - (leg_thickness / 2), - (table_length / 2) + (leg_thickness / 2)]
y_positions = [(table_width / 2) - (leg_thickness / 2), - (table_width / 2) + (leg_thickness / 2)]
# Create the four legs
legs = []
for x in x_positions:
for y in y_positions:
leg = create_leg(x, y)
legs.append(leg)
# Parent legs to the tabletop
for leg in legs:
leg.parent = tabletop
# Optionally, join all parts into a single mesh (uncomment the lines below to enable)
# bpy.ops.object.select_all(action='DESELECT')
# tabletop.select_set(True)
# for leg in legs:
# leg.select_set(True)
# bpy.context.view_layer.objects.active = tabletop
# bpy.ops.object.join()
Clear existing objects: Before creating the table, we clear the scene to ensure there are no conflicting objects.
bpy.ops.object.select_all(action='SELECT')
bpy.ops.object.delete(use_global=False)
Set measurements: We define variables for the table dimensions, making it easy to adjust sizes later.
table_length = 2.0 # Length along the X-axis
table_width = 1.0 # Width along the Y-axis
table_thickness = 0.1 # Thickness of the tabletop
leg_thickness = 0.1 # Thickness of the legs
leg_height = 1.0 # Height of the legs
Create the tabletop:
bpy.ops.mesh.primitive_cube_add(size=1, location=(0, 0, leg_height + table_thickness / 2))
tabletop = bpy.context.active_object
tabletop.name = 'Tabletop'
tabletop.scale.x = table_length / 2
tabletop.scale.y = table_width / 2
tabletop.scale.z = table_thickness / 2
Function to create a leg:
(x, y) position.def create_leg(x, y):
bpy.ops.mesh.primitive_cube_add(size=1, location=(x, y, leg_height / 2))
leg = bpy.context.active_object
leg.name = 'Leg'
leg.scale.x = leg_thickness / 2
leg.scale.y = leg_thickness / 2
leg.scale.z = leg_height / 2
return leg
Calculate leg positions:
Alt+P.After running the script, you will see a simple table model in the 3D viewport:
Feel free to adjust the measurements at the beginning of the script to customize the table's size. You can also modify the leg creation function to change the leg shape (e.g., using cylinders for round legs).
I hope this script helps you create and understand how to model a table in Blender using Python scripting! If you have any questions or need further assistance, feel free to ask.
Not yet included in a published release.
x_positions = [(table_length / 2) - (leg_thickness / 2), - (table_length / 2) + (leg_thickness / 2)]
y_positions = [(table_width / 2) - (leg_thickness / 2), - (table_width / 2) + (leg_thickness / 2)]
Create the legs:
legs list for further manipulation if needed.legs = []
for x in x_positions:
for y in y_positions:
leg = create_leg(x, y)
legs.append(leg)
Parent the legs to the tabletop:
for leg in legs:
leg.parent = tabletop
Optional: Join all parts into a single mesh:
# bpy.ops.object.select_all(action='DESELECT')
# tabletop.select_set(True)
# for leg in legs:
# leg.select_set(True)
# bpy.context.view_layer.objects.active = tabletop
# bpy.ops.object.join()