Hi everyone,
I have tried for days to solve this python for grasshopper issue without success, I am hoping I could get some advice here…
What I’m Trying to Do:
I’m working with a Grasshopper Python component to process G-code data stored in a data tree (structured like {8}10
, representing 8 layers, each with 10 lines of G-code).
For each branch (i.e., each layer), I want to:
- Insert a block of starter G-code lines at the beginning, and
- Insert a block of ending G-code lines at the end.
The goal is to transform a {8}10
tree into {8}12
, where each branch contains the original 10 lines plus 1 starter line and 1 ending line (or more, depending on the number of non-empty lines in the start/end blocks).
My Inputs:
paths
: ADataTree[String]
containing G-code lines per layer, e.g.,{0}
,{1}
, …,{7}
. I have set the input to Tree access and hint to String.starter_code
: A multi-line string of G-code to insert at the start of each branch.end_code
: A multi-line string of G-code to insert at the end of each branch.
The Problem:
Despite the script running without error, the output:
- Has only 1 out put of strings structured like {0}0.
- I tried changing input to item access, then the output becomes a data tree with {8}10 structure, and all items returns as empty.
- It seems like I can only either obtain the data structure, or the content flattened as 1 string.
Any ideas what might have happened?
here’s the code I have when the input was set to items:
Python component code used in Grasshopper
Copy this code into the Python component in Grasshopper
def RunScript(self, paths, starter_code, end_code):
“”"
Main function of the Grasshopper Python component
Input parameters:
paths: Tree - Path data tree (e.g., structure like {8}10)
starter_code: String - G-code to insert at the start of each layer
end_code: String - G-code to insert at the end of each layer
Output parameter:
gcode: Tree - Processed G-code data tree (e.g., structure like {8}12)
"""
import Grasshopper.Kernel.Data.GH_Path as GH_Path
import Grasshopper.DataTree as DataTree
import System
# If no input data, return None
if paths is None:
return None
# Set default codes if not provided
if starter_code is None:
starter_code = "; === Layer Start ===\nG21 ; Set units to millimeters\nG90 ; Absolute positioning"
if end_code is None:
end_code = "; === Layer End ===\nG1 Z+0.3 ; Lift nozzle\nG4 P1000 ; Pause 1 second"
# Create output data tree
output_tree = DataTree[System.String]()
# Loop through each branch (layer)
for i in range(paths.BranchCount):
branch_path = paths.Path(i)
branch_data = paths.Branch(branch_path)
# Add starter code
for line in starter_code.split('\n'):
if line.strip(): # Add only non-empty lines
output_tree.Add(line, branch_path)
# Add original path data
for item in branch_data:
output_tree.Add(str(item), branch_path)
# Add end code
for line in end_code.split('\n'):
if line.strip(): # Add only non-empty lines
output_tree.Add(line, branch_path)
return output_tree
a = output_tree
and the output I got:
test.gh (30.8 KB)
Thanks a lot!!