Create Named Clipping Plane?

Hello,

I’m successfully creating a Clipping Plane in the model but failing to set its name attribute to my name argument.

I simply want to be able to call my function “CreateClippingPlane” and pass an optional name and height argument. The height arg is working. The name is not.

Thanks in advance for your help!

Here’s my code so far:

    def CreateClippingPlane(name=None, height=None):
        """Create a new, named clipping plane at specified height"""
        try:
            view = UI.viewport
            viewport = view.ActiveViewport

            print("CreateClippingPlane Call")
            # Check if a clipping plane matching the name already exists
            existing_clipping_planes = Display.GetAllClippingPlanes()

            if existing_clipping_planes is not None:
                # print(f"existing_clipping_planes: {existing_clipping_planes}")
                for cp in existing_clipping_planes:
                    print(f"existing clipping plane name: {cp.Name}")
                    if cp.Name == name:
                        print(f"clipping plane matching named: '{name}' already exists!")
                        return

                print(f"no clipping plane matching name: {name} found...")

            print("no clipping planes found in document")

            # If matching clipping plane is not found or none exist in document, continue
            height_plane = Plane(Point3d(0, 0, height), Vector3d(0, 0, -1))

            # cp_attr = Rhino.DocObjects.ObjectAttributes()
            # cp_attr.Name = name  # Set the clipping plane name to the user passed name above
            
            # cp_history = Rhino.DocObjects.HistoryRecord()

            clipped_viewports = [viewport.Id]

            cp = scriptcontext.doc.Objects.AddClippingPlane(
                plane=height_plane,
                uMagnitude=100,
                vMagnitude=100,
                clippedViewportIds=clipped_viewports
                )

            # cp = scriptcontext.doc.Objects.AddClippingPlane(
            #     height_plane,
            #     100,
            #     100,
            #     clipped_viewports,
            #     cp_attr,
            #     cp_history
            #     )


            if cp:
                cp_obj = rs.coercerhinoobject(cp, True, True)
                cp.Name = name
                # cp_obj.AddClipViewport(viewport, commit=True)
                # view.Redraw()  # Refresh the viewport
                print(f"cp {name} added to viewport {viewport.Name}")
            else:
                print("clipping plane not added...")


        except Exception as ex:
            Rhino.RhinoApp.WriteLine(f"CreateClippingPlane Exception: {ex}")

Try running cp_obj.CommitChanges() after assigning the name.

also this cp.Name = name is creating a new ‘Name’ attribute of the guid ‘cp’ rather than modifying the existing Name attribute of the RhinoObject ‘cp_obj’.

You need either cp_obj.Name = name or rs.ObjectName(cp, name).

1 Like

Really appreciate the help @Measure , worked flawlessly, thank you!