How to Generate G-code for FDM Printing with the G-code Skill
The G-code Skill in the earthtojake/text-to-cad repository converts STL, OBJ, 3MF, PLY, GLB, and GLTF meshes into FDM-ready .gcode files by orchestrating real slicer CLIs (OrcaSlicer, PrusaSlicer, or CuraEngine) through a validated, dry-run workflow.
This skill provides a complete pipeline for transforming 3D models into printable instructions without directly interfacing with hardware. According to the source code in skills/gcode/scripts/gcode_tool.py, the tool enforces static validation and strict profile contracts to ensure generated G-code matches your specific printer capabilities and filament settings.
Architecture and Key Components
The G-code Skill separates concerns across metadata declarations, CLI automation, and validation layers. Understanding these components ensures reliable integration into your CAD workflow.
Backend Discovery and CLI Implementation
At skills/gcode/scripts/gcode_tool.py, the discover_backends function locates slicer executables by checking $PATH, environment variables (ORCASLICER_BIN, PRUSASLICER_BIN, CURAENGINE_BIN), and common macOS application bundles. This discovery mechanism supports OrcaSlicer, PrusaSlicer, and CuraEngine, returning a JSON report of available backends.
The CLI helper implements four primary sub-commands: discover, inspect, slice, and validate. The build_backend_command function constructs exact command-line invocations based on your wrapper profile's native_settings and native_filaments fields, ensuring the selected slicer receives correct parameters.
Profile Contract and Static Validation
The profile system uses a JSON wrapper format defined in skills/gcode/SKILL.md. This contract references native slicer profiles while supplying printer-wide bounds, filament settings, and optional motion_bounds_mm constraints. The validate_gcode_file function (also in gcode_tool.py) parses generated output to count movement, temperature, and extrusion commands, verify absolute XYZ limits against your profile, and flag unknown commands or relative-position warnings.
For meshes in non-STL formats (PLY, GLB, GLTF), the convert_mesh_to_stl function uses trimesh to create temporary STL files before slicing, ensuring universal input compatibility.
Step-by-Step Workflow for FDM G-code Generation
Follow this sequence to convert 3D models into validated printer instructions. Each step corresponds to specific CLI invocations defined in the skill implementation.
1. Discover Available Slicers
Before processing, verify that a supported slicer is installed and accessible. The discovery command outputs JSON detailing which backends are available on your system.
python skills/gcode/scripts/gcode_tool.py discover
This checks environment variables and standard installation paths, prioritizing OrcaSlicer, then PrusaSlicer, then CuraEngine unless explicitly overridden.
2. Inspect the Input Mesh
Validate that your mesh file is supported and determine if format conversion is required. The inspect command analyzes geometry and reports file type compatibility.
python skills/gcode/scripts/gcode_tool.py inspect \
--input models/part.stl \
--json
The tool supports STL, OBJ, 3MF, PLY, GLB, and GLTF inputs. If you provide PLY, GLB, or GLTF, the skill automatically handles temporary STL conversion during the slice phase.
3. Prepare the Wrapper Profile
Create a JSON profile that points to your native slicer configuration and defines printer constraints. As documented in skills/gcode/SKILL.md (lines 63-84), the wrapper must include:
native_settings: Path to your slicer's native profilenative_filaments: Filament-specific configurationsprinter_bounds: Physical limits andmotion_bounds_mm(optional)
This abstraction allows the same workflow to work across different slicer backends without modifying native profiles.
4. Execute the Slice with Dry-Run Verification
Always perform a dry-run first to verify the generated command without writing files. This prevents filesystem pollution and allows inspection of the exact slicer invocation.
python skills/gcode/scripts/gcode_tool.py slice \
--input models/part.stl \
--output /tmp/part.gcode \
--profile /path/to/profile.json \
--backend auto \
--dry-run
Once verified, execute the actual slice. The tool handles backend-specific output naming conventions and moves the result to your specified output path.
python skills/gcode/scripts/gcode_tool.py slice \
--input models/part.stl \
--output /tmp/part.gcode \
--profile /path/to/profile.json \
--backend auto \
--execute
5. Validate the Generated G-code
Run static analysis to ensure the G-code meets your profile constraints and contains valid command structures. This step catches issues like out-of-bounds movements or unsupported G-code dialects before downstream use.
python skills/gcode/scripts/gcode_tool.py validate \
--gcode /tmp/part.gcode \
--profile /path/to/profile.json
For programmatic consumption, use the JSON output format:
python skills/gcode/scripts/gcode_tool.py validate \
--gcode /tmp/part.gcode \
--profile /path/to/profile.json \
--json
Python API Integration
For embedded workflows, import the validation and profile logic directly from the skill scripts. This allows custom automation without shelling out to the CLI.
from pathlib import Path
from skills.gcode.scripts.gcode_tool import load_profile, validate_gcode_file
# Load your wrapper profile
profile = load_profile(Path("my_profile.json"))
# Validate generated G-code
result = validate_gcode_file(Path("output.gcode"), profile)
print(result["ok"], result["errors"])
This Python interface exposes the same validation engine used by the CLI, ensuring consistency across automation scripts and manual workflows.
Summary
- Backend Flexibility: The G-code Skill supports OrcaSlicer, PrusaSlicer, and CuraEngine, discovered via environment variables or system PATH.
- Format Support: Handles STL, OBJ, 3MF, PLY, GLB, and GLTF through automatic conversion to STL when necessary.
- Safety First: Implements a mandatory dry-run workflow before filesystem operations and static validation before downstream handoff.
- Profile Abstraction: Uses a JSON wrapper contract to decouple printer settings from specific slicer implementations.
- CLI and Python APIs: Full functionality available via
skills/gcode/scripts/gcode_tool.pyor direct Python imports from the skill modules.
Frequently Asked Questions
Which slicer backends does the G-code Skill support?
The skill supports OrcaSlicer, PrusaSlicer, and CuraEngine. The discover_backends function in skills/gcode/scripts/gcode_tool.py searches for these executables in environment variables (ORCASLICER_BIN, PRUSASLICER_BIN, CURAENGINE_BIN), system PATH, and common macOS application directories, defaulting to auto-detection if no specific backend is specified.
Can I use mesh formats other than STL?
Yes. The skill accepts OBJ, 3MF, PLY, GLB, and GLTF files. The convert_mesh_to_stl function automatically converts these formats to temporary STL files using trimesh before slicing, provided the library is installed in your environment.
Why does the skill use a wrapper profile instead of native slicer profiles?
The JSON wrapper profile abstracts printer-specific constraints (bounds, filament settings, motion limits) from the slicer's native configuration format. This allows the validate_gcode_file function to perform static analysis against your physical printer capabilities regardless of which backend (Orca, Prusa, or Cura) generated the G-code, ensuring consistent safety checks across different slicing engines.
How does the validation step ensure G-code safety?
The validate_gcode_file function parses the generated G-code to verify that all XYZ coordinates fall within the motion_bounds_mm defined in your profile, counts critical commands (movement, temperature, extrusion), and flags unknown G-code or relative positioning modes that might indicate incompatible slicer output. This static analysis occurs before any downstream printer-specific skills (such as $bambu-labs) receive the file.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →