"Pack" and Send a Large Set of Data Without Losing the Data Tree

20261008 TRANSFERING DATA masks.gh (26.1 KB)

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

Thank you. I imagine this takes effort, so I will take it for what it is.

See my last post in the other thread.

Code is literally 3 rows of code for “packing” and 1 row for “unpacking”…

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

  1. 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))
  2. 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)
  3. 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)

20261008 TRANSFERING DATA.gh (39.9 KB)

You can use Format, or the expression component with Format inside, to save some space in canvas; or maybe you can cluster it. your choice

OR MAYBE: a step back from all this trees and/or coding stuff (like “why are we all getting lost in these rabbitholes”)

completely different way is with the Data Input / Output.

  • 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

Naming conflicts (if you are using one and the same file for all Data Output components). Nice thinking outside the box.

You can’t do that
every instance of Data Output must be linked to its own unique file, while different Data Input can refer to the same file

That sucks.

@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 last refreshed. As I say: stupid dangerous.

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

THATS IT !!! yes this is what i need @maje90 many thanks :wink:

3 rows !!! incredible, very easy and very useful at least for me , i will use it a lot :wink: