This script automates slicing a 3D object in Blender along a specified orientation, extracting vertex and normal data from the slices, and generating G-code compatible with a 5-axis CNC machine or 3D printer. The G-code includes transformations and rotational commands for 5-axis machining.
- Slicing: Slices the active Blender object into multiple layers based on its orientation.
- Data Extraction: Extracts world coordinates of vertices and normals from each slice.
- G-code Generation: Generates standard G-code commands for linear movements (
G1) with position and orientation data. - 5-Axis Transformation: Adds necessary rotations (
BandCaxes) for 5-axis machining. - Output: Final G-code is saved to a text file, ready for 5-axis machine use.
- Prerequisites
- Usage Instructions
- Notes
- Functions
create_slices_from_orientationextract_vertices_and_normalsgenerate_gcodefive_axis_transformprinterbreakermain
- Example Output
- Blender: Run the script within Blender’s scripting environment.
- Active Object: Ensure an object is selected and active in Blender.
- NumPy: Required for mathematical computations.
- Open Blender: Launch Blender and open the scripting workspace.
- Load Script: Copy and paste the script into the Blender text editor.
- Select Object: Ensure the object to slice is selected and active.
- Run Script: Execute by clicking
Run Scriptor pressingAlt+P. - G-code Output: The script saves the G-code file as
output_gcode.txtin your Documents. - Check Console: Progress and completion messages are shown in the console.
- Object Orientation: Ensure the object is correctly oriented before slicing.
- Number of Slices: Adjust
num_slicesinmainto change the number of layers. - Offset Values: Set
offsetto match machine characteristics. - Feed Rate: Feed rate is set to
F800; adjust if necessary. - Cleanup: Temporary planes are removed after slicing.
- Error Handling: Basic error checks for invalid G-code commands.
import bpy
import numpy as np
from mathutils import Matrix, Vector
def create_slices_from_orientation(obj, num_slices):
# Slices a given Blender object into specified layers along its orientation
sliced_objects = []
# [Implementation for calculating normal vector, bounding box, slice planes, and slicing loop]
return sliced_objects
def extract_vertices_and_normals(obj):
# Extracts world coordinates of vertices and normals from a Blender object
vertices_and_normals = []
# [Implementation for transforming vertices and normals to world space]
return vertices_and_normals
def generate_gcode(vertices_and_normals):
# Generates G-code commands from vertex positions and normals
gcode = []
for vertex, normal in vertices_and_normals:
# Format as G1 command with X, Y, Z, and I, J, K for orientation
return gcode
def five_axis_transform(g, offset=0.068):
# Transforms G-code commands for 5-axis kinematics, adding rotations
# [Implementation for normal vector, B and C angle calculations, transformations]
return result
def printer(data, offset=0.068):
# Applies final translation and formats as a G-code command
# [Implementation for final translation and formatting]
return result
def breaker(s):
# Parses G-code command string into components
parts = s.split()
result = (parts[0],) + tuple(float(p[1:]) for p in parts[1:])
return result
def main():
# Main function for orchestrating slicing, data extraction, G-code generation, and file writing
bpy.ops.object.mode_set(mode='OBJECT')
obj = bpy.context.active_object
sliced_objects = create_slices_from_orientation(obj, num_slices=10)
gcode = []
for slice_obj in sliced_objects:
vertices_and_normals = extract_vertices_and_normals(slice_obj)
slice_gcode = generate_gcode(vertices_and_normals)
transformed_gcode = [five_axis_transform(breaker(cmd)) for cmd in slice_gcode]
gcode.extend(transformed_gcode)
with open("/path/to/output_gcode.txt", "w") as f:
for line in gcode:
f.write(line + "\n")
print("G-code saved to output_gcode.txt")A sample G-code command generated by the script:
G1 F800 X12.3456 Y-7.8901 Z3.4567 B45.0000 C30.0000