Sub topic 13
Optional: Building the Scene with Python
This chapter assumes you have Blender 4.x installed and understand basic vertices, edges, faces, and Object Mode. You should also know how to open the Scripting workspace and run a script. Python is a programming language. Blender provides the bpy module, which lets Python create and change Blender data.
Python is useful when a scene contains repeated objects or needs exact values. Instead of clicking through the same steps, you describe the steps once and run them again. The script below creates a ground plane, two low-poly trees, rocks, a camera, and a light.
The scripting workflow
A Blender script usually follows this order:
Operators such as bpy.ops.mesh.primitive_cone_add behave like Blender commands. They create an object and make it the active object. The active object can then be stored in a variable and modified.
A complete low-poly scene script
Open Blender's Scripting workspace, create a new text block, paste this script, and click Run Script.
import bpy
from mathutils import Vector
# Remove objects from the current scene.
bpy.ops.object.select_all(action="SELECT")
bpy.ops.object.delete(use_global=False)
def make_material(name, colour):
"""Create a simple Principled BSDF material."""
material = bpy.data.materials.new(name=name)
material.use_nodes = True
bsdf = material.node_tree.nodes.get("Principled BSDF")
bsdf.inputs["Base Color"].default_value = (*colour, 1.0)
material.diffuse_color = (*colour, 1.0)
return material
def set_flat_shading(obj):
"""Keep every polygon visibly faceted."""
for polygon in obj.data.polygons:
polygon.use_smooth = False
def add_tree(x, y, scale=1.0):
bpy.ops.mesh.primitive_cylinder_add(
vertices=6,
radius=0.25 * scale,
depth=2.0 * scale,
location=(x, y, 1.0 * scale),
)
trunk = bpy.context.object
trunk.data.materials.append(trunk_material)
set_flat_shading(trunk)
bpy.ops.mesh.primitive_cone_add(
vertices=6,
radius1=1.3 * scale,
radius2=0.0,
depth=2.4 * scale,
location=(x, y, 2.8 * scale),
)
foliage = bpy.context.object
foliage.data.materials.append(leaf_material)
set_flat_shading(foliage)
ground_material = make_material("Ground", (0.24, 0.34, 0.18))
trunk_material = make_material("Trunk", (0.30, 0.12, 0.04))
leaf_material = make_material("Leaf", (0.08, 0.38, 0.10))
rock_material = make_material("Rock", (0.32, 0.34, 0.36))
# Ground plane.
bpy.ops.mesh.primitive_plane_add(size=14, location=(0, 0, 0))
ground = bpy.context.object
ground.data.materials.append(ground_material)
# Repeated trees with different proportions.
add_tree(-3.0, 0.5, 1.0)
add_tree(0.0, 1.2, 0.75)
add_tree(3.0, 0.0, 1.2)
# Low-poly rocks.
for location, scale in [((-1.5, -1.5, 0.45), 0.7), ((2.0, -1.5, 0.6), 0.9)]:
bpy.ops.mesh.primitive_ico_sphere_add(
subdivisions=1,
radius=1.0,
location=location,
)
rock = bpy.context.object
rock.scale = (scale, scale * 0.8, scale * 0.7)
rock.data.materials.append(rock_material)
set_flat_shading(rock)
# Camera aimed at the centre of the scene.
bpy.ops.object.camera_add(location=(8, -10, 7))
camera = bpy.context.object
target = Vector((0, 0, 1.5))
camera.rotation_euler = (target - camera.location).to_track_quat("-Z", "Y").to_euler()
bpy.context.scene.camera = camera
# Large area light aimed at the same target.
bpy.ops.object.light_add(type="AREA", location=(4, -4, 8))
light = bpy.context.object
light.data.energy = 900
light.data.shape = "DISK"
light.data.size = 5.0
light.rotation_euler = (target - light.location).to_track_quat("-Z", "Y").to_euler()
scene = bpy.context.scene
scene.render.engine = "BLENDER_EEVEE_NEXT"
scene.render.resolution_x = 800
scene.render.resolution_y = 600
scene.render.resolution_percentage = 100
scene.render.filepath = "//low_poly_scene.png"
scene.world.color = (0.06, 0.09, 0.14)
bpy.ops.render.render(write_still=True)The helper functions reduce repetition. make_material creates a material and sets its Principled BSDF base colour. set_flat_shading changes each polygon so Blender does not interpolate its normals. The add_tree function accepts position and scale values, so the same design can produce trees with different proportions.
Common scripting errors
- An operator may fail if Blender is in Edit Mode. Switch to Object Mode before running object-creation operators.
bpy.context.objectmeans the current active object. Store it immediately after an operator if you need to modify that newly created object.- A material can exist without being visible if it is not appended to the object's material slots.
- Object coordinates describe the object's origin. A tree trunk at
z=1with depth2touches the ground because its centre is one unit above it.
Exercise
Exercise
Change the script so the middle tree uses a blue-green foliage material and the rocks are placed farther apart. Which values should you edit?
Show answer
Change the colour tuple passed to makematerial for leafmaterial, then change the x or y values in the rocks list.