Some problems with ClippingPlane Object

Hello,
I am writing a script to turn on/ off all the clipping planes in the active viewport depending on the on-off state of a selected clipping plane.
So, the logic is 1. check if a clipping plane is on/off in the active viewport, 2. turn all the clipping planes in the active viewport based on this check result.
But, I also need all the clipping planes in the document, including the ones hidden to work with the script.
https://developer.rhino3d.com/api/RhinoScriptSyntax/#selection-ObjectsByType
I use above method with the state = 0 to include the hidden clipping planes in my selection. However, it does not work. So, I tried to access RhinoCommon hoping it might override an apparent bug in the rhinoscriptsyntax. Now, my code is this:

import rhinoscriptsyntax as rs
import Rhino
import scriptcontext as sc
from System import Array

def ToggleAllClippingPlanes():
    view = sc.doc.Views.ActiveView
    viewport = view.ActiveViewport

    clippingPlaneEnumSettings = Rhino.DocObjects.ObjectEnumeratorSettings()
    clippingPlaneEnumSettings.NormalObjects = True
    clippingPlaneEnumSettings.LockedObjects = True
    clippingPlaneEnumSettings.HiddenObjects = True
    clippingPlaneEnumSettings.ObjectTypeFilter = Rhino.DocObjects.ObjectType.ClipPlane

    clippingPlanesList = sc.doc.Objects.GetObjectList( clippingPlaneEnumSettings)
    clippingPlanes = list(clippingPlanesList)

    testcp = rs.coercerhinoobject(clippingPlanes[0], True, True)
    IsClippingPlanesOn = testcp.AddClipViewport(viewport, commit = True)
    testcp.RemoveClipViewport(viewport, commit = True)

    if IsClippingPlanesOn:
        for clippingPlane in clippingPlanes:
            cp = rs.coercerhinoobject(clippingPlane, True, True)
            cp.AddClipViewport(viewport, commit = True)
    else:
        for clippingPlane in clippingPlanes:
            cp = rs.coercerhinoobject(clippingPlane, True, True)
            cp.RemoveClipViewport(viewport, commit = True)

if __name__=="__main__":
    ToggleAllClippingPlanes()

However, it does not work on the hidden clipping planes yet. Is it a bug or am I missing something here? Thanks for your replies.

Check if clipping plane is hidden and/or locked, and if yes, unhide it and/or unlock, modify its properties, then revert its state to hidden an/ or locked

Hi Adel,
Thanks for your reply.
If you mean to do that in the script, I cannot yet find a way to access the clipping planes that are hidden. Could you please share me a few lines?
If you meant to do that manually, it is not a solution for me as I am trying to reduce the manual procedures.

change to this:

    if IsClippingPlanesOn:
        for clippingPlane in clippingPlanes:
            #check if clipping plane is hidden. 
            hidden = rs.IsObjectHidden(clippingPlane)
            if (hidden):
                #show clipping plane
                rs.ShowObject(clippingPlane)

            cp = rs.coercerhinoobject(clippingPlane, True, True)
            cp.AddClipViewport(viewport, commit = True)

            #restore hidden status
            if (hidden):
                rs.HideObject(clippingPlane)
    else:
        for clippingPlane in clippingPlanes:
            #check if clipping plane is hidden. 
            hidden = rs.IsObjectHidden(clippingPlane)
            if (hidden):
                #show clipping plane
                rs.ShowObject(clippingPlane) 

            cp = rs.coercerhinoobject(clippingPlane, True, True)
            cp.RemoveClipViewport(viewport, commit = True)
            #restore hidden status
            if (hidden):
                rs.HideObject(clippingPlane)

you might also want to add the check for locked in a similar manner

Thanks for the code. The code below got me exactly how I want it to work.

import rhinoscriptsyntax as rs
import Rhino
import scriptcontext as sc
from System import Array

def ToggleAllClippingPlanes():
    view = sc.doc.Views.ActiveView
    viewport = view.ActiveViewport

    clippingPlaneEnumSettings = Rhino.DocObjects.ObjectEnumeratorSettings()
    clippingPlaneEnumSettings.NormalObjects = True
    clippingPlaneEnumSettings.LockedObjects = True
    clippingPlaneEnumSettings.HiddenObjects = True
    clippingPlaneEnumSettings.ObjectTypeFilter = Rhino.DocObjects.ObjectType.ClipPlane

    clippingPlanesList = sc.doc.Objects.GetObjectList( clippingPlaneEnumSettings)
    clippingPlanes = list(clippingPlanesList)

    hiddenClippingPlanes = []
    for clippingPlane in clippingPlanes:
        isHidden = rs.IsObjectHidden(clippingPlane)
        if (isHidden):
            hiddenClippingPlanes.append(clippingPlane)
            rs.ShowObject(clippingPlane)
    
    testcp = rs.coercerhinoobject(clippingPlanes[0], True, True)
    IsClippingPlanesOn = testcp.AddClipViewport(viewport, commit = True)
    testcp.RemoveClipViewport(viewport, commit = True)
    
    if IsClippingPlanesOn:
        for clippingPlane in clippingPlanes:
            cp = rs.coercerhinoobject(clippingPlane, True, True)
            cp.AddClipViewport(viewport, commit = True)
    else:
        for clippingPlane in clippingPlanes:
            cp = rs.coercerhinoobject(clippingPlane, True, True)
            cp.RemoveClipViewport(viewport, commit = True)

    for hiddenClippingPlane in hiddenClippingPlanes:
        rs.HideObject(hiddenClippingPlane)

if __name__=="__main__":
    ToggleAllClippingPlanes()

Your code worked if all clipping planes are normal; however when the clipping planes are hidden in the model, it added/ removed one clipping plane and one cp remained otherwise. Not all the clipping planes work simultaneously. I think AddClipViewport/RemoveClipViewport methods do not work very well together with hidden clipping planes.
So, I rearranged the code-flow as above, and it works as intended now. I will never fully understand the underlying reason why my code above works differently from yours.
Another mystery: I cannot interchange

    IsClippingPlanesOn = testcp.AddClipViewport(viewport, commit = True)
    testcp.RemoveClipViewport(viewport, commit = True)

Vs.

    IsClippingPlanesOn = testcp.AddClipViewport(viewport, commit = False)

I think commit parameter is broken. :sweat_smile:

Glad it worked. Strange that you got that behavior, since your script is pretty much identical.
Sometimes, you need to refresh the rhino view to see the results

This is problematic. Will fail in some instances

1 Like

Yes, now I realize that there should be a hidden object check for the clippingPlanes[0] too. In the current code, I put the hidden check above that piece so it works now.
Thanks for the help. Have a nice day!