RhinoApp.RunScript() return false with Rhino.Inside

I’m writing a console application in C# to automate Grasshopper scripts and Rhino Commands using Rhino.Inside. Automating GH scripts works like a charm, but I’m struggling to get the RhinoApp.RunScript(script, false) to work. Looking at some threads around seems to get it to work with the added class attribute [CommandStyle(Style.ScriptRunner)], but most of these threads are with plugins and not Rhino.Inside.

As you can see in the screenshot I’ve tried the same function in Rhino Python Editor and it works as expected.

How can I make this method work with Rhino.Inside?

As a sidenote I’ve also tried with Rhino.RhinoApp.ExecuteCommand(doc, script), but this method can’t execute a line of script as I understand.

Hi @Fredrik_Jacobsen,

Try this:

https://developer.rhino3d.com/api/rhinocommon/rhino.runtime.pythonscript/executefile

– Dale

Thanks, I’ll try tomorrow.

But, Python was just an example for me to see if things work. I know Python, but not Rhino that much.

Doesn’t the RunScript method work in Rhino.Inside context?

What if I want to run other Rhino commands?

A workaround may be creating the py-file at runtime with the commands and execute the RunScript method inside Python, but seems kind of hacky.

@dale

var pyScript = Rhino.Runtime.PythonScript.Create();
var result = pyScript.ExecuteFile(filePath);

This works for running a python script, but I’m still curious of why I can’t use RhinoApp.RunScript() or if it something I’m doing wrong.

The ExecuteFile(path) method works, but the similar methods doesn’t or am I also doing this wrong?

This doesn’t work:

var pyScripCompile = pyScript.Compile(pyScriptGen.PyScript);
Console.WriteLine($"PythonScript:\n{pyScriptGen.PyScript}");
pyScripCompile.Execute(pyScript);

Nor does this, even if it returns true:

result = pyScript.ExecuteScript(pyScriptGen.PyScript);

Update. These methods can run python script, but they fail to run something like this which works fine in Rhino Python Editor:

So, I’m back to the why the RunScript method doesn’t work from outside Rhino.

import Rhino
script = "-RunPythonScript E:\\run_py.py _Enter"
result = Rhino.RhinoApp.RunScript(script, False)

EDIT:
Tried this as well, but no cigar:

import rhinoscriptsyntax as rs
script = "-RunPythonScript E:\\run_py.py _Enter"
#result = Rhino.RhinoApp.RunScript(script, False)
result = rs.Command(script)

Added an issue here.

RunScript requires the existence of a RhinoDoc which by default is not created with a “headless” console application. You may need to create a doc and set it as the active doc.

Thanks @stevebaer.

As you can see in the screenshot in post 1 I’ve created a headless rhinoDoc. Did also try to set it as the active doc, but that’s commented out. I can try again of course.

@stevebaer

Any clues of what I can try?

Two tries. one without overriding the RunCommand() method and one where I do.


Created an issue here instead of the one mentioned earlier:

I’ll need to get everything set up in a debugger to be able to give any clear answer on what is happening. We probably just never tried to get RunScript working in a headless environment.

2 Likes

That would be nice :+1:

1 Like

@stevebaer, did you get around to look at this?

Anybody?

Hi Fredrik,
Sorry, I have not had a chance to dig into this yet.

Did a last attempt just passing in the arguments to RhinoCore, but this didn’t work either.

arguments:runs fine in the command area of rhino. “-RunPythonScript ““E:\run_py.py”” _enter”

Instead of using nowindow I’m using hidden, but would be nice if this get fixed at some point.

1 Like

I am also facing this issue, any update ?