Skip to content

Instantly share code, notes, and snippets.

@softyoda
Created February 25, 2025 16:34
Show Gist options
  • Save softyoda/01391ab7d955a49298fdf38333d168d7 to your computer and use it in GitHub Desktop.
Save softyoda/01391ab7d955a49298fdf38333d168d7 to your computer and use it in GitHub Desktop.
Human in house in sphere in cube in cylinder in pyramid
Make a bpy blender python script that will draw :
maxima
* a human that is inside a house
* the house is inscribed in a sphere
* the sphere is inscribed in a cube
* the cube is inscribed in a cylinder
* the cylinder is inscribed in a pyramid
Before starting, think deeply about how you could draw those different elements using primitive shapes and how to follow each instruction to be truth.
@softyoda
Copy link
Author

Grook 3 (💡Think) :

Below is a Blender Python script using the bpy module to create a scene as specified: a human inside a house, the house inscribed in a sphere, the sphere inscribed in a cube, the cube inscribed in a cylinder, and the cylinder inscribed in a pyramid. Each object is represented with primitive shapes, and their sizes and positions are calculated to satisfy the "inscribed" relationships, meaning each shape fits inside the next, touching its boundaries where appropriate.

Approach

  • Human: Represented as a vertical cylinder, approximating a simplified human form (e.g., 2 units tall, 0.5 units radius).
  • House: Modeled as a cube, large enough to contain the human (e.g., side length 4 units).
  • Sphere: A UV sphere that encloses the house cube, with a radius equal to the cube's space diagonal divided by 2, so the cube's vertices lie on the sphere.
  • Cube (outer): A cube containing the sphere, with side length equal to the sphere's diameter, so the sphere touches all six faces.
  • Cylinder: A cylinder containing the outer cube, with a radius based on the cube's face diagonal and height equal to the cube's side length, aligned along the z-axis for simplicity.
  • Pyramid: A square-based pyramid (tetrahedron-like) containing the cylinder, with a base at z=0 and apex above, sized so the cylinder fits inside, touching the lateral faces or base perimeter as needed.

To ensure all objects are visible and above the Blender ground plane (z=0), positions are adjusted with a z-offset. The script uses mathematical relationships to determine sizes and placements.

Script

import bpy
import math

# Calculate mathematical constants
sqrt3 = math.sqrt(3)
sqrt6 = math.sqrt(6)
center_z = 2 * sqrt3  # Central z-coordinate after shifting to keep all objects above z=0

# 1. Add human (cylinder)
# Height 2, radius 0.5, positioned so its base is just above z=0 after shifting
bpy.ops.mesh.primitive_cylinder_add(
    radius=0.5,
    depth=2,
    location=(0, 0, center_z - 1)  # Center at z = 2*sqrt3 - 1, so base at z ≈ 1.464
)

# 2. Add house (cube)
# Size 4, centered to contain the human
bpy.ops.mesh.primitive_cube_add(
    size=4,
    location=(0, 0, center_z)  # Center at z = 2*sqrt3 ≈ 3.464, ranges from z ≈ 1.464 to 5.464
)

# 3. Add sphere
# Radius is half the cube's space diagonal (4 * sqrt3 / 2 = 2 * sqrt3)
bpy.ops.mesh.primitive_uv_sphere_add(
    radius=2 * sqrt3,  # ≈ 3.464
    location=(0, 0, center_z)  # Same center, ranges from z = 0 to z = 4*sqrt3
)

# 4. Add outer cube
# Side length is sphere's diameter (4 * sqrt3)
bpy.ops.mesh.primitive_cube_add(
    size=4 * sqrt3,  # ≈ 6.928
    location=(0, 0, center_z)  # Ranges from z = 0 to z = 4*sqrt3
)

# 5. Add cylinder
# Radius based on cube's face diagonal (4*sqrt3 * sqrt2 / 2 = 2*sqrt6), height = cube's side
bpy.ops.mesh.primitive_cylinder_add(
    radius=2 * sqrt6,  # ≈ 4.899
    depth=4 * sqrt3,  # ≈ 6.928
    location=(0, 0, center_z)  # Ranges from z = 0 to z = 4*sqrt3
)

# 6. Add pyramid (custom mesh)
# Base side length matches cylinder's diameter (4*sqrt6), apex height above cylinder top
b = 4 * sqrt6  # ≈ 9.798
h = 6 * sqrt3  # ≈ 10.392, above cylinder top at 4*sqrt3 ≈ 6.928
vertices = [
    (-b/2, -b/2, 0),  # Base corners
    (b/2, -b/2, 0),
    (b/2, b/2, 0),
    (-b/2, b/2, 0),
    (0, 0, h)         # Apex
]
faces = [
    (0, 1, 2, 3),  # Base
    (0, 1, 4),     # Lateral faces
    (1, 2, 4),
    (2, 3, 4),
    (3, 0, 4)
]

# Create and link pyramid mesh
mesh = bpy.data.meshes.new("Pyramid")
mesh.from_pydata(vertices, [], faces)
mesh.update()
obj = bpy.data.objects.new("Pyramid", mesh)
bpy.context.collection.objects.link(obj)

Explanation of Each Step

  1. Human (Cylinder):

    • Size: Radius 0.5, height (depth) 2.
    • Location: (0, 0, 2*sqrt3 - 1), so it extends from z ≈ 1.464 to z ≈ 3.464, standing inside the house.
    • Purpose: A simple proxy for a human, fitting within the house cube.
  2. House (Cube):

    • Size: 4 units.
    • Location: (0, 0, 2*sqrt3), extends from z ≈ 1.464 to z ≈ 5.464.
    • Inscription: Contains the human; its space diagonal (4√3 ≈ 6.928) determines the sphere’s radius.
  3. Sphere:

    • Radius: 2*sqrt3 ≈ 3.464, half the house cube’s space diagonal, so the cube’s vertices lie on the sphere.
    • Location: (0, 0, 2*sqrt3), extends from z = 0 to z = 4*sqrt3 ≈ 6.928.
    • Inscription: The house cube is inscribed in this sphere (vertices touch the sphere).
  4. Outer Cube:

    • Size: 4*sqrt3 ≈ 6.928, the sphere’s diameter, so the sphere touches all six faces.
    • Location: (0, 0, 2*sqrt3), same range as the sphere.
    • Inscription: The sphere is inscribed in this cube.
  5. Cylinder:

    • Radius: 2*sqrt6 ≈ 4.899, half the outer cube’s face diagonal (4√3 * √2 ≈ 9.798), ensuring the cube fits inside when aligned along the z-axis.
    • Depth: 4*sqrt3 ≈ 6.928, matching the cube’s height.
    • Location: (0, 0, 2*sqrt3), from z = 0 to z = 4*sqrt3.
    • Inscription: The cube is inscribed in the cylinder, with its base square’s corners touching the cylinder’s circular base.
  6. Pyramid:

    • Base: Square at z = 0, side b = 4*sqrt6 ≈ 9.798, equal to the cylinder’s diameter.
    • Apex: (0, 0, 6*sqrt3) where 6*sqrt3 ≈ 10.392, above the cylinder’s top.
    • Construction: Custom mesh with 5 vertices and 5 faces (base + 4 lateral triangles).
    • Inscription: The cylinder fits inside, with its base circle inscribed in the pyramid’s base square, and its height contained within the tapering pyramid.

Notes

  • Positioning: All objects share a common center at z = 2*sqrt3 ≈ 3.464, shifted upward so the lowest point (cylinder base, pyramid base) is at z = 0. This keeps the scene above the Blender ground plane.
  • Simplifications: The human and house are basic primitives for simplicity. The pyramid uses a custom mesh since Blender lacks a square-based pyramid primitive.
  • Visualization: In Blender, you may adjust materials (e.g., wireframe for outer shapes) or camera settings to better view the nested objects, though this script focuses on creation as requested.

This script fulfills the query by creating a nested sequence of shapes, each inscribed in the next, using Blender’s Python API. Run it in Blender’s Python console or script editor to generate the scene.

@softyoda
Copy link
Author

human house

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment