### DocString-Based Component Documentation in GhPython Source: https://context7.com/mcneel/ghpython/llms.txt This example shows how to use a Google-style docstring at the top of a GhPython script to automatically document the component's description, input parameters, output parameters, and help remarks. It utilizes `Rhino.Geometry` for geometric operations. ```python """Constructs a recursive Koch curve. Inputs: x: The seed line. (Line) y: Number of recursive subdivisions. (int) Outputs: a: Resulting Koch curve as a list of lines. Help: See: http://mathworld.wolfram.com/KochSnowflake.html """ import Rhino.Geometry as rg import math def subdivide(line, level): if level == y: a.append(line) return f, t = line.From, line.To b_pt = f * (2/3.0) + t * (1/3.0) c_pt = f * (1/3.0) + t * (2/3.0) m = (f + t) * 0.5 v = (m - b_pt) v.Rotate(math.pi / 3, rg.Vector3d.ZAxis) e_pt = b_pt + v / math.cos(math.pi / 3) for seg in [(f, b_pt), (b_pt, e_pt), (e_pt, c_pt), (c_pt, t)]: subdivide(rg.Line(*seg), level + 1) if x is None: x = rg.Line(rg.Point3d(0,0,0), rg.Point3d(10,0,0)) if y is None: y = 4 a = [] subdivide(x, 0) # Output: a is automatically wired to the "a" output port ``` -------------------------------- ### Run Functions in Parallel with ghpythonlib.parallel.run Source: https://context7.com/mcneel/ghpython/llms.txt Distributes function calls over a list of inputs. The first item is processed serially for setup, and subsequent items run in parallel. Use `flatten_results=True` to get a single sequence of results. ```python import ghpythonlib.parallel as ghpar import Rhino.Geometry as rg # Compute the area of many surfaces in parallel surfaces = x # list of Rhino.Geometry.Surface objects fed from input x def compute_area(srf): amp = rg.AreaMassProperties.Compute(srf) return amp.Area if amp else 0.0 areas = ghpar.run(compute_area, surfaces, flatten_results=False) # areas[i] corresponds to surfaces[i]; list order is preserved ``` ```python # Flatten nested list results into a single sequence def divide_curve(crv): pts = [] rg.Curve.DivideByCount(crv, 10, True, pts) return pts a = ghpar.run(divide_curve, x, flatten_results=True) # a is a flat list of all division points across all input curves ``` -------------------------------- ### Control DocStorage Modes for Rhinoscriptsyntax Output Source: https://context7.com/mcneel/ghpython/llms.txt These modes control how geometry created by `rhinoscriptsyntax` is managed. `AutomaticMarshal` (default) returns GUIDs marshaled to RhinoCommon geometry. `InGrasshopperMemory` adds geometry to the Grasshopper virtual document (`ghdoc`). `InRhinoDoc` adds geometry to the active Rhino document (not recommended). ```python # Mode: DocStorage.AutomaticMarshal (default rhinoscriptsyntax mode) # rhinoscriptsyntax returns GUIDs; GhPython marshals them automatically # to RhinoCommon geometry when wired to downstream components. import rhinoscriptsyntax as rs pt = (0, 0, 0) cid = rs.AddCircle(pt, 5.0) # returns a GUID a = cid # output port receives the Circle geometry ``` ```python # Mode: DocStorage.InGrasshopperMemory (RhinoCommon / ghdoc mode) # Use ghdoc to add geometry to the Grasshopper virtual document. import Rhino.Geometry as rg circle = rg.Circle(rg.Point3d(0,0,0), 5.0) attr = Rhino.DocObjects.ObjectAttributes() ghdoc.Objects.AddCircle(circle, attr) # ghdoc is injected by the component a = circle # pass native geometry directly ``` ```python # Mode: DocStorage.InRhinoDoc (legacy Rhino document; not recommended) import rhinoscriptsyntax as rs rs.AddCircle((0,0,0), 5.0) # geometry is added to the active Rhino document ``` -------------------------------- ### Standard vs. Fast Component Invocation in GhPython Source: https://context7.com/mcneel/ghpython/llms.txt Compares standard component invocation with the FastComponent.Run path, which is automatically activated for whitelisted components when experimental_mode is enabled. The fast path handles batching for list inputs. ```python import ghpythonlib.components as ghcomp # Standard (non-fast) invocation — one call per data item points = [Rhino.Geometry.Point3d(i, 0, 0) for i in range(100)] lines = [ghcomp.Line(pt, Rhino.Geometry.Point3d(i, 10, 0)) for pt in points] ``` ```python import ghpythonlib.components as ghcomp ghcomp.experimental_mode = True pt_a_list = [Rhino.Geometry.Point3d(i, 0, 0) for i in range(500)] pt_b_list = [Rhino.Geometry.Point3d(i, 5, 0) for i in range(500)] # Passing lists triggers list-expansion; FastComponent.Run handles batching result = ghcomp.Line(pt_a_list, pt_b_list) # returns list of 500 Lines a = result ``` -------------------------------- ### Using `ghpythonlib.components` to Call Grasshopper Components Source: https://context7.com/mcneel/ghpython/llms.txt This snippet demonstrates how to use the `ghpythonlib.components` module to call built-in Grasshopper components directly from Python scripts. It shows how to pass arguments positionally or by keyword and how to handle single outputs versus named tuples. ```python import ghpythonlib.components as ghcomp import Rhino.Geometry as rg # Call the built-in "Line" component with two Point3d values pt_a = rg.Point3d(0, 0, 0) pt_b = rg.Point3d(10, 5, 3) line = ghcomp.Line(pt_a, pt_b) # returns a single Line object # Call "Vector 2Pt" — returns a named tuple: Output(vector, length) result = ghcomp.Vector2Pt(pt_a, pt_b, False) print(result.vector) # Rhino.Geometry.Vector3d print(result.length) # float # Keyword argument form (uses lower-cased parameter names) closest = ghcomp.CurveClosestPoint(point=pt_a, curve=some_curve) # Assign to component outputs a = line b = result.vector ``` -------------------------------- ### Import and Use RhinoScript Source: https://github.com/mcneel/ghpython/blob/master/Samples/helpText.html Import the rhinoscriptsyntax module to use RhinoScript functions. Assign the result of functions like AddLine to a variable. ```python import rhinoscriptsyntax as rs line = rs.AddLine((1, 2, 3), (10, 11, 12)) a = line ``` -------------------------------- ### Import and Use RhinoCommon Source: https://github.com/mcneel/ghpython/blob/master/Samples/helpText.html Import specific geometry types from Rhino.Geometry to create and manipulate objects directly. Assign new geometry instances to variables. ```python from Rhino.Geometry import Point3d, Line a = Line(Point3d(1, 2, 3), Point3d(10, 11, 12)) ``` -------------------------------- ### Enable Dynamic Script Generation with HiddenCodeInput Source: https://context7.com/mcneel/ghpython/llms.txt Setting `HiddenCodeInput = False` exposes a `code` input port, allowing external components to supply Python script text. This enables dynamic recompilation and execution of the script based on upstream changes. ```python # In an upstream Panel or string component, produce a script string: script_text = """ a = x * x b = x + y """ # That string feeds the "code" port of a GhPython component. # The GhPython component effectively becomes a higher-order function: # changing the script string at runtime changes what the component does. ``` ```python # Programmatic equivalent via C# / GH SDK (in another GhPython component): import GhPython.Component as ghpy import Grasshopper as gh for obj in ghDoc.Objects: # ghDoc is the Grasshopper document pycomp = obj.Attributes.GetTopLevel.DocObject if isinstance(pycomp, ghpy.ZuiPythonComponent): pycomp.HiddenCodeInput = False # expose code port pycomp.ExpireSolution(True) ``` -------------------------------- ### Manage Python Environment with ghenv Source: https://context7.com/mcneel/ghpython/llms.txt The `ghenv` global variable provides access to the IronPython engine, runtime, and scope. Use `clr.AddReference` to load .NET assemblies and `ghenv.DataAccessManager` for imperative output setting. ```python # Inside a GhPython component — ghenv is automatically injected import clr # Load an external .NET assembly and use its types clr.AddReference("System.Xml") from System.Xml import XmlDocument doc = XmlDocument() doc.LoadXml("42") node = doc.SelectSingleNode("//value") # Inspect the runtime environment from the script print(ghenv.Version) # GhPython assembly version, e.g. 0.6.0.3 print(type(ghenv.Component)) # Grasshopper.Kernel.GH_Component subtype print(ghenv.Script) # Rhino.Runtime.PythonScript instance # ghenv.DataAccessManager gives direct access to IGH_DataAccess # (advanced: set outputs imperatively instead of via variable assignment) ghenv.DataAccessManager.SetData(0, "hello from ghenv") ``` -------------------------------- ### Script Variables and I/O Binding in GhPython Source: https://context7.com/mcneel/ghpython/llms.txt This script demonstrates how GhPython automatically binds Grasshopper input parameters to Python variables and maps Python variables to output parameters. It includes default values for inputs and uses `rhinoscriptsyntax` for geometry creation. ```python # Component inputs: x (integer), y (float) # Component outputs: a (list of circle GUIDs), b (list of line GUIDs) import math import rhinoscriptsyntax as rs # Provide defaults when inputs are not connected if x is None: x = 24 if y is None: y = 0.3 circles = [] radii = [] for i in range(int(x)): pt = (i, math.cos(i), 0) cid = rs.AddCircle(pt, y) circles.append(cid) endPt = rs.PointAdd(pt, (0, 0.3, 0)) lid = rs.AddLine(pt, endPt) radii.append(lid) # Assign to output variable names a = circles # → output port "a" b = radii # → output port "b" ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.