Using Shrink Wrap with Python

Hi, I’m fairly new to Python but I’m trying to put something together that will cycle through layers/objects in a rhino project and run the shrink wrap function, As far as I can wrap function is being applied on the input meshes but nothing is being created or output.

The printed layers shows all layers in the model, and you can see each mesh being selected as part of the last few lines added for debugging.

Has anyone had any success using Mesh.ShrinkWrap ?

import rhinoscriptsyntax as rs
import Rhino as r

options = ("Layer Based", "Object Based")

WrapMethod = rs.ListBox(options, "Pick Wrap Method")

WrapRes = rs.GetReal("Wrap Resolution in meters", 2, 0.0)
WrapOffset = rs.GetReal("Wrap Offset (expansion) in meters", 0.0, 0.0)

layers_with_shrinkwrap = []  # Create an empty list to store layer names

if WrapMethod == "Object Based":
    arrLayers = rs.LayerNames()
    props = r.Geometry.ShrinkWrapParameters()
    props.FillHolesInInputObjects = True
    props.PolygonOptimization = 0
    props.Offset = WrapOffset
    props.TargetEdgeLength = WrapRes 
    for strLayer in arrLayers:
        if not rs.IsLayerEmpty(strLayer):
            rs.UnselectAllObjects()
            objs = rs.ObjectsByLayer(strLayer)
            if objs:
                for obj in objs:
                    mesh = rs.coercemesh(obj)
                    if mesh:
                        result = r.Geometry.Mesh.ShrinkWrap(mesh, props)
                        if result:
                            layers_with_shrinkwrap.append(strLayer)  # Add the layer name to the list
                            rs.SelectObject(obj)  # Select the wrapped object for visualization
                            rs.Redraw()  # Redraw the viewport to visualize the wrapped object
                rs.UnselectAllObjects()

# Print the list of layers where ShrinkWrap was applied
print("Layers with ShrinkWrap applied:", layers_with_shrinkwrap)

Looks interesting Simon. Before calling commands that process Rhino Geometry using rhinoscriptsyntax, sc.doc = Rhino.RhinoDoc.ActiveDoc is needed.

To process GH geometry it needs to be set back again to sc.doc = ghdoc
So in my own code, once I’m sure there’re no silly errors in there, I like to put all the Rhino specific calls in a try: block, and stick the above in a finally:. That’s the default for other GH components, and helps you avoid frustrating bugs.
.

If that doesn’t work I might try ShrinkWarp out later.

Thanks James, I tried adding the sc.doc = Rhino.RhinoDoc.ActiveDoc and still no luck…

I’m not using GH if that makes any difference…

update:

Finally got it working, I was missing the following:

result_id = sc.doc.ActiveDoc.Objects.Add(result)
1 Like