With the increased use of AI, I’ve been adding more and more custom Python components to my Grasshopper definitions.
Sometimes I end up copying the same Python component multiple times throughout a definition. Later, I realize the script needs an update, which means I have to find every copy and update them manually.
Is there a better way to manage this?
Ideally, I’m looking for something similar to Rhino Blocks, where multiple instances reference the same source, so updating the Python script once would update all instances automatically.
Is there an existing Grasshopper workflow or feature for this?
For GhPython I use this workflow (copied from our internal forum at work):
For a long time I’ve been using the same code to get/export GhPython code to files on disk. And then read that in order to update components on canvas where the exported code has a newer version. While that works fine, it’s also quite convoluted for the case I’m facing most often these days. Basically, that I often have several instances of the same GhPython component on canvas, where I want to update the code across all instances whenever I edit the code locally in one instance. So here’s a new simpler script that grabs all named/versioned GhPython canvas code and updates relevant components:
Here’s the code and example file (note: it assumes the documentation described page 139/140 here):
"""
Get and update named/versioned GhPython components code on canvas. Assumes
the AHD documentation style (i.e. version is YYMMDD field in docstring and
component name has been set. Orange warning is raised on affected components.
Inputs:
Toggle: Run component {item,bool}
Outputs:
Remarks:
Author: Anders Holden Deleuran (BIG CPH)
Rhino: 7.38.24338.17001
Version: 250404
"""
ghenv.Component.Name = "UpdateGhPythonCode"
ghenv.Component.NickName = "UPC"
import Grasshopper as gh
def getCodeProperties(ghComp):
""" Get GhPython component code, version and name """
# Get code
code = ghComp.Code
# Get version
version = None
for l in code.lower().split("\n"):
l = l.strip()
if l.startswith("version"):
lastSix = l[-6:]
if lastSix.isdigit():
version = int(lastSix)
break
# Get name
name = None
if "GhPython Script" not in ghComp.Name:
name = ghComp.Name
return code,version,name
def getCanvasGhPythonCode(ghDocument):
""" Build dictionary with all named/versioned GhPython components. Records
latest version if multiple component instances are present """
ghPythonCode = {}
for obj in ghDocument.Objects:
if type(obj) is type(ghenv.Component):
code,version,name = getCodeProperties(obj)
if version and name:
if name not in ghPythonCode:
ghPythonCode[name] = (version,code)
else:
if ghPythonCode[name][0] < version:
ghPythonCode[name] = (version,code)
return ghPythonCode
def updateCanvasGhPythonCode(ghDocument,ghPythonCode):
""" Update named/versioned GhPython components code on canvas """
cn = 0
wh = gh.Kernel.GH_RuntimeMessageLevel.Warning
for obj in ghDocument.Objects:
if type(obj) is type(ghenv.Component):
if obj.InstanceGuid != ghenv.Component.InstanceGuid:
code,version,name = getCodeProperties(obj)
if version and name:
if name in ghPythonCode:
if ghPythonCode[name][0] > version:
obj.Code = ghPythonCode[name][1]
obj.AddRuntimeMessage(wh,"Code updated, parameters may have changed!")
cn += 1
return cn
# Clear output/check input
ghenv.Component.Message = None
if Toggle:
# Get Grasshopepr document and call functions
ghDocument = ghenv.Component.OnPingDocument()
ghPythonCode = getCanvasGhPythonCode(ghDocument)
count = updateCanvasGhPythonCode(ghDocument,ghPythonCode)
# Set component message
ghenv.Component.Message = "Updated: {}".format(count)
Last I checked, the new Python script editor does not have the same mutable Code property. But if/when it does, it should be pretty straightforward to write something similar for it