3dSynth

Tutorials / Custom G-code

Open in the interactive tutorials reader

---
title: Tutorial 34 Custom Infill Generators
description: Designing the inside of your objects.
---

Tutorial 34 Custom Infill Generators

1. Lesson Header

2. Concept Introduction

Why Infill?
Infill saves time and material while providing internal support.
Standard patterns (Grid, Triangles) are strong but basic.
Custom Infill allows for:
- Variable Density: Dense near walls, sparse in center.
- Isotropy: Same strength in all directions (Gyroid).
- Aesthetics: Patterns visible through transparent walls.

3. Machine State Explanation

Space Filling.
We need an algorithm that takes a boundary (Polygon) and fills it with lines.
Simple: Scanline Fill (Grid).
Complex: Recursive Space Filling Curve (Hilbert).

4. Command Breakdown

5. Minimal Working Example

The Grid Algorithm.
1. Define bounding box (min_x, max_x, min_y, max_y).
2. Draw vertical lines every spacing mm.
3. Draw horizontal lines every spacing mm.
4. Clip lines to the polygon.

6. Visual Representation

Interactive preview is available in the interactive reader.

7. Build Exercise

Task: Write a Python script to generate a Grid Infill inside a Circle (R=50).
Spacing = 10mm.
Rotation = 45 degrees.

Algorithm:
1. Generate infinite grid lines.
2. Rotate them by 45 degrees.
3. Check intersection with Circle (x^2 + y^2 < R^2).
4. Write G-code for the segments inside.

8. Deep Insight Section

Gyroid Infill.
Gyroid is a minimal surface defined by sin(x)cos(y) + sin(y)cos(z) + sin(z)cos(x) = 0.
It is continuous in 3D, meaning the infill changes every layer to form a 3D structure.
This is the "Gold Standard" for strength-to-weight ratio.
Generating it requires evaluating the implicit function at every Z height.

9. Common Failure Modes

  1. Gaps: If your lines don't connect perfectly to the perimeter, the infill is weak. Slicers add "Infill Overlap" (usually 15%) to fuse them.
  2. Retractions: Grid infill crosses itself on the same layer. The nozzle hits the previous line, causing a bump. (Gyroid avoids this).

10. Real-World Application

Bone Scaffolds.
Medical 3D printing uses "Voronoi" or "Trabecular" infill that mimics bone structure.
This encourages bone cells to grow into the implant.
The density is varied based on the load the bone will carry.

11. Final Clean Version

The Grid Generator:

import math

filename = "grid_circle.gcode"
R = 50
spacing = 10

with open(filename, "w") as f:
    f.write("G28\nG1 Z0.2\n")
    
    # Vertical Lines
    for x in range(-R, R+1, spacing):
        # Calculate intersection with circle at this X
        # y^2 = R^2 - x^2
        if abs(x) >= R: continue
        y_span = math.sqrt(R**2 - x**2)
        
        # Draw line from -y to +y
        f.write(f"G0 X{x} Y{-y_span} F9000\n") # Travel
        f.write(f"G1 X{x} Y{y_span} E... F1000\n") # Print

    # Horizontal Lines
    for y in range(-R, R+1, spacing):
        if abs(y) >= R: continue
        x_span = math.sqrt(R**2 - y**2)
        
        f.write(f"G0 X{-x_span} Y{y} F9000\n")
        f.write(f"G1 X{x_span} Y{y} E... F1000\n")

    f.write("G28 X0 Y0\n")

12. Stretch Challenge

Challenge: Implement Lightning Infill.
Start from the top (Roof) and grow support downwards only where needed.
This forms a tree structure.
Requires knowing the 3D model geometry (Slicing logic).
Simpler Challenge: Implement Concentric Infill.
Offset the perimeter inwards by spacing.
Repeat until the center is filled.
Hint: Use a library like Shapely (Python) for polygon offsetting.