Skip to content

sid0nair/5Axis-Gcode-Generator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Blender 5-Axis G-Code Generator Script

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.

Overview

  • 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 (B and C axes) for 5-axis machining.
  • Output: Final G-code is saved to a text file, ready for 5-axis machine use.

Table of Contents

  • Prerequisites
  • Usage Instructions
  • Notes
  • Functions
    • create_slices_from_orientation
    • extract_vertices_and_normals
    • generate_gcode
    • five_axis_transform
    • printer
    • breaker
    • main
  • Example Output

Prerequisites

  • 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.

Usage Instructions

  1. Open Blender: Launch Blender and open the scripting workspace.
  2. Load Script: Copy and paste the script into the Blender text editor.
  3. Select Object: Ensure the object to slice is selected and active.
  4. Run Script: Execute by clicking Run Script or pressing Alt+P.
  5. G-code Output: The script saves the G-code file as output_gcode.txt in your Documents.
  6. Check Console: Progress and completion messages are shown in the console.

Notes

  • Object Orientation: Ensure the object is correctly oriented before slicing.
  • Number of Slices: Adjust num_slices in main to change the number of layers.
  • Offset Values: Set offset to 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.

Code

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")

Example Output

A sample G-code command generated by the script:

G1 F800 X12.3456 Y-7.8901 Z3.4567 B45.0000 C30.0000

About

This generator creates G-Code for an in-house-built 5-axis Ender Neo, optimized for FANUC format. It enables precise slicing and multi-axis toolpaths for specific shapes and angles (not continuous contours). Built in Blender for broad accessibility, it supports advanced multi-axis setups efficientl

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages