Python method equivalent to SetObjectDisplayMode?

I have a couple of scripts that will strip away any object display modes that the user has chosen. I would like to be able to detect an object’s display mode and restore it when the script finishes. I don’t see any Python methods to accomplish this. Has anyone added this type of functionality to their scripts? If so, how did you do it?

Thanks,

Dan

Hi Dan,

Did you find these already, it looks like they are what you are looking for:

https://developer.rhino3d.com/api/RhinoCommon/html/M_Rhino_DocObjects_ObjectAttributes_HasDisplayModeOverride.htm

https://developer.rhino3d.com/api/RhinoCommon/html/Overload_Rhino_DocObjects_ObjectAttributes_SetDisplayModeOverride.htm

-Willem

No I didn’t see these. This should be exactly what I need.

Thanks Willem,

Dan

1 Like

It seems that HasDisplayModeOverride returns a boolean. If I understand correctly that’s only going to tell me that there is a object display mode set, but not which one it is, so therefore, no real way to restore the object display mode that was previously set.

I guess another angle to take on this is to figure out if ExplodePolysurfaces can not turn off an existing display mode override. That’s probably my real issue here. Basically I want an exploded polysurface to still have the same object display mode that it had prior to exploding.

Did I miss something with the examples provided that could do this?

Thanks,

Dan

Have you seen this method to get the guid of the display mode override - I presume that would allow you to find the override in a table somewhere of from the display mode discription of the associated viewport?

https://developer.rhino3d.com/api/RhinoCommon/html/M_Rhino_DocObjects_ObjectAttributes_GetDisplayModeOverride.htm

https://developer.rhino3d.com/api/RhinoCommon/html/T_Rhino_Display_DisplayModeDescription.htm

Hi Dan

See if the below code gets you started to record the modes
I did not have time to also write a method to restore/set the modes but maybe you get that working based on this example?

import rhinoscriptsyntax as rs
import scriptcontext as sc
import Rhino


def get_displaymodename_byid(mode_id):
    #return name of displaymode by guid of displaymode
    modes = Rhino.Display.DisplayModeDescription.GetDisplayModes()
    for mode in modes:
        if mode.Id == mode_id:
            return mode.LocalName


def record_custom_displaymode(obj_id):
    
    robj = rs.coercerhinoobject(obj_id)
    """ create a record of custom displaymodes for this object
    Args: 
        Guid of the object to check
    Return:
        a list of tuples with custom displaymodes 
        (viewport_id, mode_id, mode_name)
        note that mode_name is just for human readability
    """
    
    records = []
    view_ids = sc.doc.Views
    for view in view_ids:
        #test if custom mode is present
        if robj.Attributes.HasDisplayModeOverride(view.MainViewport.Id):
            mode_id = robj.Attributes.GetDisplayModeOverride(view.MainViewport.Id)
            mode_name = get_displaymodename_byid(mode_id)
            records.append((view.MainViewport.Id, mode_id, mode_name))
    
    return records


#dictionary to store displaymodes for each id that has custom modes
displaymode_records = {}

obj_ids = rs.GetObjects('get objects to test')
for obj_id in obj_ids:
    cdm_record = record_custom_displaymode(obj_id)
    if cdm_record: displaymode_records[obj_id] = cdm_record

for key,value in displaymode_records.items():
    print key, value

HTH
-Willem

1 Like

Thanks Willem and Graham. This all looks very helpful.