Great. Double check what a
is, from the return of rs.coerceguid
. The code assumes the call was successful, but a lot of those functions can return None
.
Finally, I’m not sure whether you have Rhino or Grasshopper geometry in Srf
. Try right clicking it and embedding it to get Grasshopper geometry.
If you’ve got an actual Rhino guid / shape, and possibly an ephemeral Grasshopper reference/guid to a Rhino object, the above won’t work. If it’s a true Rhino guid, then you can play around with:
import Rhino
import scriptcontext as sc
sc.doc = Rhino.RhinoDoc.ActiveDoc
I wrote a helper function that attempts to handle strings, guids, grasshopper Geom, Rhino geom, and ephemeral GH references to Rhino objects (if it refers to a Rhino Obj from a param, but the guids change every time the GH canvas is recomputed):
# MIT License
# Copyright (c) [2021] [Cardiff University]
import System
import Rhino
def get_geom_and_source_else_leave(obj):
#type(type[any])-> type[any], type[any]
""" Leaves Geometric objects and non-guid strings alone.
Otherwise searches Rhino doc and ghdoc with obj as guid.
"""
if hasattr(Rhino.Geometry, type(obj).__name__):
return obj, 'SOURCE_IS_ALREADY_GEOM'
# So that we can call it on curves and it will leave them alone
if isinstance(obj, basestring):
# To leave legend tags, group names and layer names alone
try:
obj = System.Guid(obj)
except: # (TypeError, ValueError):
return obj, 'SOURCE_IS_STRING'
for source in (Rhino.RhinoDoc.ActiveDoc, ghdoc):
geom = source.Objects.FindGeometry(obj)
if geom is not None:
return geom, source
else:
msg = 'Type: %s, val: %s, not supported by get_geom_and_source_else_leave'
msg %= (type(obj), obj)
raise NotImplementedError(msg)
Calling that to get geom, source
, allows source
to be tested, and then perhaps do sc.doc = source
and then use rs
to manipulate geom