import Grasshopper as gh
from Grasshopper.Kernel.Data import GH_Path, GH_Structure
from Grasshopper.Kernel.Types import GH_ObjectWrapper
import RhinoCodePluginGH
import fnmatch
def main():
# 1. Group the paths by their first index
grouped_branches = {}
for m, mask in enumerate(M):
# Use GH_Structure[GH_ObjectWrapper] to satisfy IGH_Structure
grouped_branches[m] = GH_Structure[GH_ObjectWrapper]()
for i in range(T.BranchCount):
path = T.Path(i)
branch_data = T.Branch(i)
if fnmatch.fnmatch(str(path), str(mask)):
# Wrap raw python/rhino objects into Grasshopper Goo wrappers
wrapped_data = [GH_ObjectWrapper(item) for item in branch_data]
grouped_branches[m].AppendRange(wrapped_data, path)
# 2. Determine how many outputs we need based on the highest Entwine index found
target_count = len(M)
current_count = ghenv.Component.Params.Output.Count
# 3. Schedule a topology change if the output count doesn't match
if current_count != target_count:
def callback(doc):
params = ghenv.Component.Params
# Add missing outputs
while params.Output.Count < target_count:
new_idx = params.Output.Count
nickname = "B"
p = RhinoCodePluginGH.Parameters.ScriptVariableParam(nickname)
p.Name = "Matching Branches"
p.Access = gh.Kernel.GH_ParamAccess.tree
params.RegisterOutputParam(p)
# Remove excess outputs
while params.Output.Count > target_count:
last_idx = params.Output.Count - 1
params.UnregisterOutputParam(params.Output[last_idx])
params.OnParametersChanged()
ghenv.Component.ExpireSolution(True)
ghenv.Component.OnPingDocument().ScheduleSolution(5, gh.Kernel.GH_Document.GH_ScheduleDelegate(callback))
# 4. Safely push the modified GH_Structures to their respective outputs
for i in range(ghenv.Component.Params.Output.Count):
output_param = ghenv.Component.Params.Output[i]
output_param.ClearData() # Prevent data accumulation on refreshes
if i in grouped_branches:
output_param.AddVolatileDataTree(grouped_branches[i])
# Run the execution routine
main()
I think there is a solution and without coding or “static” components like the Explode tree (wich I honestly hate).
GH data structure is maybe boring but it’s solid and consistent
to pack the data, entwine with grafted input is the right way as said: each X stream is stored in a separate “folder” (the new path index added at the beginning at each path (0/1/2/ecc shown in the entwine component))
to extract each original stream you can split tree with a parametric mask, where you select the “folder” 0/1/2/ecc (you are selecting all branches that have as first index the number you input with the slider)
to have your selected data as was originally structured, you have to remove that extra index (at the start) remaining and shared by all paths, with a shift path (NOT with Simplify/Suirify, otherwise you might lose other indexes that actually need to stay - the same old endless discussions)
pro: even easier/faster to pack/unpack everything you want with ZUI always matching, also you can do it across different files (and it updates automatically, even if they are in different definitions in the same or even in different Rhino instances)
con (maybe not so much): you need to save a separate file for this component to work
@Fabio_Franchetti I was mislead by a copy/paste snafu. Apparently while you can’t define two different Data Output components to same file, it is possible copy/paste a Data Output component referring to the same file. And you can guess how dumb that is.
if you copypaste without changing the file path, the file will be fueled by only one of the two instances (I wouldn’t know wich one, maybe the latest created or the latest refreshed, or whatever)
the bad thing is that you don’t have a feedback (like errors or warnings) if you forget and have more data output pointing at the same file
The name of the destination file is shown in the black label under the component, so if you have a good enough naming convention for your files you should see if something’s doesn’t match in a real definition.
but anyway at least a warning would be appreciated