How to Slice Meshes to FDM G-code Using the G-code Skill in Text-to-CAD
The G-code skill is a CLI wrapper that converts 3D meshes into FDM G-code by orchestrating external slicer backends like OrcaSlicer, PrusaSlicer, or CuraEngine, with support for format conversion, dry-run validation, and geometric safety checks.
This guide explains how to use the G-code skill within the earthtojake/text-to-cad repository to transform STL, OBJ, and other mesh formats into printable FDM G-code. The skill acts as an intelligent abstraction layer—discovering installed slicers, handling file conversions, and validating output—while delegating the actual slicing computation to industry-standard engines.
Prerequisites: Installing and Discovering Slicer Backends
Before slicing, the skill must locate a compatible backend. It searches for OrcaSlicer, PrusaSlicer, or CuraEngine executables in three locations: environment variables, the system PATH, and macOS Application bundles under /Applications.
Backend Discovery Logic
The discover_backends() function in plugins/cad/skills/gcode/scripts/gcode_tool.py implements a priority-based search:
- Environment variables:
ORCASLICER_BIN,PRUSASLICER_BIN, orCURAENGINE_BINoverride automatic detection - System PATH: Standard binary locations
- macOS Applications: Scans for
OrcaSlicer*.appandPrusaSlicer*.appbundles viadiscover_bambu_studio()
Run the discovery command to verify your installation:
python -m plugins.cad.skills.gcode.scripts.gcode_tool discover
If no backends are found, the CLI reports missing executables and recommends installing OrcaSlicer first, as documented in slicer-backends.md.
Creating a Slicer Profile JSON
The skill requires a JSON profile that maps printer geometry to native slicer configurations. The load_profile() function validates this schema before execution.
Required Profile Fields
A minimal profile must include:
backend: String identifier (orcaslicer,prusa-slicer, orcuraengine)native_config: Absolute path to the slicer's native configuration filemachine: Object containingbed_size_mm,z_height_mm, and optionalmotion_bounds_mmfilament: Object withtype,nozzle_temp_c, andbed_temp_c
OrcaSlicer additionally requires native_settings and native_filaments arrays to specify process presets.
Example profile structure:
{
"backend": "orcaslicer",
"native_config": "/path/to/machine.json",
"native_settings": ["/path/to/process.json"],
"native_filaments": ["/path/to/filament.json"],
"machine": {
"bed_size_mm": [220, 220],
"z_height_mm": 250
},
"filament": {
"type": "PLA",
"nozzle_temp_c": 200,
"bed_temp_c": 60
}
}
Inspecting and Converting Mesh Inputs
The inspect_input() function classifies input files and determines preprocessing requirements before slicing.
Supported Formats and Conversion
The skill handles format variations transparently:
- Direct pass-through:
.stl,.obj, and unsliced.3mffiles flow directly to the slicer - Format conversion:
.ply,.glb, and.gltfmeshes convert to temporary STL viaconvert_mesh_to_stl()using trimesh - Pre-sliced detection: Bambu Studio-sliced
.3mffiles (containingMetadata/plate_N.gcode) are recognized and excluded from re-slicing - Unsupported rejection:
.step,.dxf, and.urdffiles trigger immediate errors with descriptive messages
Inspect any mesh to preview handling:
python -m plugins.cad.skills.gcode.scripts.gcode_tool inspect \
--input model.glb \
--json
Building and Executing Slice Plans
The core slicing workflow centers on build_slice_plan() and execute_slice() in gcode_tool.py.
Dry-Run Mode vs. Execution
The skill supports two execution modes controlled by CLI flags:
- Dry-run (
--dry-run): Generates the slice plan as JSON without executing, allowing command verification - Execution (
--execute): Runs the slicer viasubprocess.run()in a temporary directory
Always perform a dry-run first to validate the command structure:
python -m plugins.cad.skills.gcode.scripts.gcode_tool slice \
--input model.stl \
--output model.gcode \
--profile /absolute/path/to/profile.json \
--dry-run
Command Construction
The build_slice_plan() function constructs backend-specific command sequences. For OrcaSlicer, this generates:
OrcaSlicer --load-settings machine.json;process.json \
--load-filaments filament.json \
--outputdir /tmp/out \
--slice 0 input.stl
Execute the slice with:
python -m plugins.cad.skills.gcode.scripts.gcode_tool slice \
--input model.stl \
--output model.gcode \
--profile /absolute/path/to/profile.json \
--execute
The execute_slice() function handles output filename quirks and returns a JSON report containing the return code, command executed, stdout/stderr tails, and generated file metadata.
Validating Generated G-code
After slicing, validate_gcode_file() performs safety checks against the profile's machine constraints:
- Movement commands: Verifies presence of
G0-G3motion and extrusion instructions - Thermal safety: Confirms temperature commands (
M104,M109,M140,M190) exist - Geometric bounds: Validates that absolute XYZ coordinates remain within
machine.bed_size_mmandz_height_mm - Syntax warnings: Reports unknown G-code commands
Run validation with:
python -m plugins.cad.skills.gcode.scripts.gcode_tool validate \
--gcode model.gcode \
--profile /absolute/path/to/profile.json \
--json
Complete Workflow Example
This end-to-end example demonstrates the full pipeline from mesh to validated G-code:
# 1. Verify slicer installation
python -m plugins.cad.skills.gcode.scripts.gcode_tool discover
# 2. Inspect mesh compatibility
python -m plugins.cad.skills.gcode.scripts.gcode_tool inspect \
--input part.glb --json
# 3. Preview slice command
python -m plugins.cad.skills.gcode.scripts.gcode_tool slice \
--input part.glb \
--output part.gcode \
--profile /configs/printer_profile.json \
--dry-run
# 4. Execute slicing
python -m plugins.cad.skills.gcode.scripts.gcode_tool slice \
--input part.glb \
--output part.gcode \
--profile /configs/printer_profile.json \
--execute
# 5. Validate output
python -m plugins.cad.skills.gcode.scripts.gcode_tool validate \
--gcode part.gcode \
--profile /configs/printer_profile.json
Summary
- The G-code skill in
earthtojake/text-to-cadprovides a unified CLI for FDM slicing throughplugins/cad/skills/gcode/scripts/gcode_tool.py - Backend discovery prioritizes environment variables (
ORCASLICER_BIN, etc.), then PATH, then macOS Applications - Profile JSON requires
backend,native_config,machinedimensions, andfilamentparameters - Mesh conversion happens automatically via
convert_mesh_to_stl()for.ply,.glb, and.gltfformats using trimesh - Safety validation checks G-code bounds, temperatures, and motion commands via
validate_gcode_file() - Dry-run mode lets you preview exact slicer commands before execution with
--dry-run
Frequently Asked Questions
What slicer backends does the G-code skill support?
The skill supports OrcaSlicer, PrusaSlicer, and CuraEngine. It searches for these in environment variables (ORCASLICER_BIN, PRUSASLICER_BIN, CURAENGINE_BIN), the system PATH, and standard macOS Application directories. If multiple backends are present, the skill selects based on internal priority logic defined in gcode_tool.py.
Can I slice mesh formats other than STL?
Yes. While the skill passes .stl and .obj directly to slicers, it automatically converts .ply, .glb, and .gltf files to temporary STL format using the convert_mesh_to_stl() function and the trimesh library. However, CAD formats like .step or .dxf are not supported and will return an error during the inspect phase.
How does the validation ensure my G-code is safe for my printer?
The validate_gcode_file() function parses the generated G-code and checks that all absolute XYZ coordinates remain within the bed_size_mm and z_height_mm defined in your profile's machine section. It also verifies the presence of critical temperature commands (M104, M109, M140, M190) and basic motion commands (G0-G3) to prevent dry-running or out-of-bounds movements.
What is the difference between --dry-run and --execute?
--dry-run invokes build_slice_plan() to generate and display the exact command that would run—useful for debugging profile paths and arguments without consuming compute time. --execute triggers execute_slice(), which actually runs the slicer subprocess, handles temporary file management, and produces the final G-code 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 →