Editing Rhino Options from Python - Run Command on Startup

Hi all,

I’d like to have a Rhino command run every time Rhino starts. I know that I can manually add the command here in options:

But I want to add the startup command from a Python script (or rvb script). I am implementing this change on many users’ machines and would like to have the startup commands added or removed from a script.

I saw on the forums how to update the PackageManger source setting in python:

settings = Rhino.PlugIns.PlugIn.GetPluginSettings(Rhino.RhinoApp.CurrentRhinoId, False)
s = settings.GetChild("Options").GetChild("PackageManager")
s.SetString("Sources","https://yak.rhino3d.com;" + packageFolder)

But I cannot figure out how to adapt this code to edit the startup commands. Can anyone help?

Thanks!

there is a lot of info in this old topic:

Thanks for the reply - I had read through that topic before posting but unfortunately it does not answer my question.

They discuss how to make .RVB scripts run at startup but what I want to do is to script adding a command to options to run at startup.

rhinoscript can:

StartupScriptCount
Returns the number of startup scripts.

StartupScriptList
Return the list of startup script files.

AddStartupScript
Adds a startup script to RhinoScript.

DeleteStartupScript
Removes a startup script to RhinoScript.

my guess is, if you add a file with .py this will also run.

You might also workarround by calling a command of a custom (your) (rhinocommon) plug-in.

rhinoscript is directly accessing the c++ sdk methods.
phyton and rhinoscriptsyntax are based on rhinocommon.


image from here

there might be difference of what is accessible.

i never tried, but you might also be able to add a startup script using a macro.
starting with
!-_documentproperties _options

because of this thread i think @stevebaer might be the right person to answer your question with more details / options / alternatives.

Thanks Tom_P!

I couldn’t get Rhino to run a py script from the startupscriptlist but thanks to your links I came up with a bit of a clunky workaround that is doing what I need.

If it helps anyone in the future: I have an .RVB script that calls the command that I want:

Option Explicit
Sub WakeUp()
	Dim commandName
	commandName = "GoodMorning"
	Rhino.Command commandName
End Sub
WakeUp

I am using another RVB script to add the initial script to the startup script list, I would have preferred to do this with python but I could only access AddStartupScript through RVB:

Option Explicit
Sub StartupEnable()
	Dim filePath
	filePath = "C:\my file\path here\WakeUp.rvb"
	Rhino.AddStartupScript(filePath)
End Sub
StartupEnable

I can then finally call the StartupEnable script from python to add the command to the startup script list:

scriptPath = r"C:\my file\path here\StartupEnable.rvb"
Rhino.RhinoApp.RunScript("-LoadScript " + scriptPath, True)

If it helps anyone, there is a way to add startup commands purely in python without that clunky RVB step. Here is the function I now use to add startup commands

def addStartupCommand(str_cmd):

    '''Adds a new command to the startup command list.'''

    addedCommand = False

    try:

        if str_cmd:

            str_cmd = str_cmd.lower()




            # Get settings for startup commands

            rhino_id = Rhino.RhinoApp.CurrentRhinoId

            settings = Rhino.PlugIns.PlugIn.GetPluginSettings(rhino_id, False)

            settings = settings.GetChild("Options").GetChild("General")




            # Fetch existing commands and add if not present

            commands = settings.GetString("StartupCommands").splitlines()

            if str_cmd and str_cmd not in commands: 

                commands.append(str_cmd)

                settings.SetString("StartupCommands", "\n".join(commands))

                addedCommand = True

    except Exception as e:

        try:

            print("Error in addStartupCommand: " + str(e))

        except Exception:

            print("Error in addStartupCommand: An unknown error occurred.")

    return addedCommand

And here is the function to delete them

def deleteRhinoScriptStartupScript(path):

    '''Removes a script from the RhinoScript startup script list.'''

    deletedPath = False

    try:

        if path:

            path = path.lower()




            # Get the RhinoScript plugin object

            rhinoScript_Id = Rhino.PlugIns.PlugIn.IdFromName("RhinoScript")

            rhinoScript = Rhino.RhinoApp.GetPlugInObject(rhinoScript_Id)




            # Remove the script if it exists in the startup list

            if rhinoScript:

                startupList = rhinoScript.StartupScriptList()

                if startupList:

                    for script in startupList:

                        if script.lower() == path:

                            rhinoScript.DeleteStartupScript(script)

                            deletedPath = True

    except Exception as e:

        try:

            print("Error in deleteRhinoScriptStartupScript: " + str(e))

        except Exception:

            print("Error in deleteRhinoScriptStartupScript: An unknown error occurred.")

    return deletedPath

Nice one. How do you find coding in Python now, compared to RVB?

I might not be the right person to ask since I started coding in python :wink: but I prefer coding everything in python. Especially since the ScriptEditor doesn’t support RVB