DisplayModeDescription.ImportFromFile - Shared Resource

Hi @eirannejad,

Following your docs on using Shared resources I made some code hoping to utilize shared .ini files to deploy custom display modes with a plugin.

The code executes “without error” and I have a print statement to check the paths and such…

The paths seem good from what I can tell because the .ini files do exist where the path print statements show them to be.

However it fails to have the DisplayMode imported into Rhino show up in Document Properties. In the .ini file it says AddToMenu=y so I’m assuming it’s not “hidden”

[DisplayMode\guidStringHere]
AddToMenu=y

If I print all Display Modes in file this imported display mode does not show up, leading me to believe it is not being imported correctly and that my code is at fault.

Here’s the print statement for the paths/debugging:

Plugin path: C:\Users\username\AppData\Roaming\McNeel\Rhinoceros\packages\8.0\MyTestPlugin\0.1.1\MyTestPlugin.rhp
Plugin directory: C:\Users\username\AppData\Roaming\McNeel\Rhinoceros\packages\8.0\MyTestPlugin\0.1.1
Resource path: C:\Users\username\AppData\Roaming\McNeel\Rhinoceros\packages\8.0\MyTestPlugin\0.1.1\shared\MyDisplayModeToImport.ini
Display mode successfully imported from: C:\Users\username\AppData\Roaming\McNeel\Rhinoceros\packages\8.0\MyTestPlugin\0.1.1\shared\MyDisplayModeToImport.ini

Here is that code if anyone could shed some light on what I am doing wrong it would be greatly appreciated!

import Rhino
import System


# Function to import display mode from file
def ImportDisplayMode(resource_name):
    # Get plugin path
    plugin_path = Rhino.PlugIns.PlugIn.PathFromName("MyTestPlugin")
    if plugin_path:
        # Get the directory containing the plugin, without the .rhp file
        plugin_directory = System.IO.Path.GetDirectoryName(plugin_path)
        print(f"Plugin path: {plugin_path}")
        print(f"Plugin directory: {plugin_directory}")

        # Build the path to the shared resource
        shared_resource = str(System.IO.Path.Combine(plugin_directory, "shared", f"{resource_name}"))
        print(f"Resource path: {shared_resource}")

        # Try to import the display mode from file
        display_mode_id = Rhino.Display.DisplayModeDescription.ImportFromFile(shared_resource)
        if display_mode_id is None:
            Rhino.RhinoApp.WriteLine(f"Failed to import display mode from file: {shared_resource}")
            return None

        # Try to get the display mode by ID
        display_mode = Rhino.Display.DisplayModeDescription.GetDisplayMode(display_mode_id)
        if display_mode:
            Rhino.Display.DisplayModeDescription.AddDisplayMode(display_mode)
            return shared_resource, display_mode
        else:
            Rhino.RhinoApp.WriteLine(f"Display mode not found for ID: {display_mode_id}")
            return None
    else:
        Rhino.RhinoApp.WriteLine("Plugin path not found")
        return None


if __name__ == "__main__":
    resource_name = "MyDisplayModeToImport.ini"
    result = ImportDisplayMode(resource_name)
    if result:
        shared_resource, display_mode = result
        print(f"Display mode successfully imported from: {shared_resource}")
    else:
        print("Failed to import display mode")

1 Like

Kindly bumping this,

@dale is Rhino.DisplayModeDescription.ImportFromFile something you could help me with?

If you reference my code above ^ perhaps I’m using it wrong? I feel that I’m close hopefully.

Thanks for the help!

@michaelvollrath seems that the import function doesn’t work in Rhino 8.
For example, this should already be enough to import the display mode:

import Rhino
filename=r"C:\Users\gijsd\Downloads\test.ini"
display_mode_id = Rhino.Display.DisplayModeDescription.ImportFromFile(filename)

I’ll log a bug

2 Likes

Thanks @Gijs for testing, confirming, and reporting!

Looks like it is only failing in ScriptEditor:
RH-83960 importing display modes via RhinoCommon is not working in ScriptEditor

1 Like

@michaelvollrath I am not sure why I missed this post. Sorry for that. I am looking into this right now

1 Like

No worries at all! Thanks for checking it out!

1 Like

I found where the problem is and updated the ticket. The new editor starts a document undo record when executing the script and seems like the ImportFromFile fails to actually add the imported information to document.

While the ticket is being worked on, I added an option to the editor Run menu to toggle recording undos just in case you wanted to test. I can send you a 8.13 build

1 Like

Thanks @eirannejad and sure that would be great, I’m always willing to test a newer build.

Thank you!