Running scripts inside Rhino commands (-_Runscript vs. compiled plugins)

Hi All,

For a while now I have been looking into some specific script and Rhino commands intetactions, and finally after @Helvetosaur’s question I am writing down a question and few observations that hopefully are interesting for scripters and also that we can get some insight from Rhino team:

So:
it looks like it is currently possible to run scripts while in-command in Rhino. It can just be macro-ed via usual:

 _-Runscript (  [script goes here]  )

It actually opens up a lot more possibilities for interactive scripts or enhancing macros. I will show some examples below.

But the main question / problem I ran into is: while “-_Runscripted” scripts work well inside commands from macros/buttons, the very same scripts compiled into Rhino commands with script compiler, do not work. So right now I don’t know a way to have a compiled RhinoScript to be able to run transparently while in command, and I think it would be great to have this option. Is it possible to add a switch to the compiler if the command can be executed inside others ? I guess this is a question for @dale, @DavidRutten and @stevebaer.

To make a simple test, I use:

_Move
_Pause
_-Runscript
(
Rhino.Print "In-command test - the script is working OK now."
)

The same one-liner script ( Rhino.Print “In-command test - the script is working OK now.” ) compiled as a Rhino command used in the same macro replacing the “runscript” portion will not work:

Move
Pause
InCommand_test

It will just echo the command name but nothing happens. The compiled script attached: InCommandTest.rhp (6.5 KB) The command is: “incommand_test”. Should autocomplete and run fine on its own, but not inside Rhino commands. Please note, there is nothing inside the script that would break the Rhino command that is active. So I am hoping this can be achievable to make compiled scripts run inside Rhino commands.

Here are a few more things to note on using scripts inside commands:

  1. It is possible to run a script in command that is ran inside another script. The same example will look like this (need to use chr(34) for quotes and chr(13) for line breaks):

    Call Rhino.Command("_Move _Pause _-Runscript (" & chr(13) & "Rhino.Print " & chr(34) & “In-command test - the script is working OK now” & chr(34) & chr(13) & “)”)

  2. if doing the above, the local variables from the ‘host’ script will not be visible in the ‘nested’ script, but they will work if using global variables:


    Dim strGlobal : strGlobal = "Global variable"
    Call Main()
    Sub Main()
    	Call Rhino.Command("_Move _Pause _-Runscript (" & chr(13) & "Call Rhino.Print (strGlobal & " & chr(34) & " + In-command test - the script is working OK now" & chr(34) & ")" & chr(13) & ")")
    End Sub
  1. As I mentioned above, this functionality can help a lot with enhancing interactivity of scripts by using regular Rhino commands in more controlled way. Many things can happen while in command like color changes, selection/deselection, display mode changes, prompts… a lot to try here. Below is an example of a macro (made just for demonstration sake, not really useful) that uses the in-command script to deselect the transformed geometry while in command and adds/changes/deletes text dots in each command step.
    Also please note that “~” before macro-ed commands will suppress the command-line options of Rhino commands, so in this case we don’t see the usual Copy=Yes/No in Rotate3D command. This is useful for interactive scripts if you don’t want to expose all the options but just use specific command functionality. The macro below.

Hope this is interesting and we can get a way to make it work with compiled scripts too.

–jarek



    -_Runscript (
    Dim global_idDot
    global_idDot=Rhino.AddTextDot("Select Objects to rotate",array(0,0,0))
    )
    
    _Rotate3D
    
    _~Pause
    
    -_Runscript (
    Call Rhino.UnselectAllObjects()
    )
    
    -_Runscript (
    Call Rhino.TextDotText(global_idDot,"Start of rotation axis?")
    Call Rhino.ObjectColor(global_idDot,vbRed)
    )
    
    ~_Pause
    
    -_Runscript (
    Call Rhino.TextDotText(global_idDot,"End of rotation axis?")
    Call Rhino.ObjectColor(global_idDot,vbBlue)
    )
    
    ~_Pause
    
    -_Runscript (
    Call Rhino.TextDotText(global_idDot,"Angle or first reference point?")
    Call Rhino.ObjectColor(global_idDot,vbYellow)
    )
    
    ~_Pause
    
    -_Runscript (
    Call Rhino.TextDotText(global_idDot,"Secondreference point?")
    Call Rhino.ObjectColor(global_idDot,vbGreen)
    )
    ~_Pause
    -_Runscript (
    Call Rhino.DeleteObject(global_idDot)
    )

Perhaps an issue or over site with the script compiler, for if I create a test, script runner command in a RhinoCommon plug-in that contains the following code:

protected override Result RunCommand(RhinoDoc doc, RunMode mode)
{
  RhinoApp.RunScript("_Move", false);
  RhinoApp.RunScript("_Pause", false);
  RhinoApp.RunScript("_-RunScript (Rhino.Print \"In-command test.\")", false);
  return Result.Success;
}

the command works as expected. I’ve reported this.

http://mcneel.myjetbrains.com/youtrack/issue/RH-31820

Hi Dale,

The youtrack item is not public, but thanks for adding it, and most of all good to know that the current functionality is not purposeful and can be fixed with the compiler. Actually I am working on a project right now where the in-command compiled scripts would be very helpful. Who should we bug to the compiler fix ?

many thanks,

–jarek

You should be able to see and comment on the YouTrack item now.

As far as who is working on the project, @stevebaer and @DavidRutten would be the ones. I’m not sure how much time they have for this, as I know they are working on other projects at the moment.

Hi Dale,

would this video you have created still be valid and would result in a plugin that runs within commands?

With no hint on the updated compiler I am a bit desperate to get it to work.

thank you,

–jarek

Yes the directions presented should still work. You can use any version of Visual Studio >= 2010. If your VS is > 2010, then just make sure to set the target frame work to .NET Framework 4.0 (in project settings).

Hi @dale,

Just noticed that the ‘Runscript’ inside macros behavior changed in V6 WIP compared to V5 (for worse).
We used to be able to run a script inside a script with a macro-like command:


    Call Main()
    Sub Main()
    	Rhino.Prompt "New location? (pick start and end points"
    	Call Rhino.Command("~_Move ~_-Runscript (" & chr(13) & "Call Rhino.UnselectAllObjects()" & chr(13) & ") ~_NoEcho ~_Pause ~_NoEcho ~_Pause ", False)
    End Sub 

Start with a pre-selected object - this snippet should use Move command with unselecting the moving object.
Works fine in V5, not anymore in V6.

This is just a sample, we have a lot of tools that rely on this functionality working with more complex macros. Could this still work as in V5 please ?

thanks,

–jarek

great, I will give it a shot. Thank you again for the tutorial.

Hi @dale - just checking if you can see that on your end (V5 vs V6 with the snippet above). I think I typed that post the minute you were responding to my other question !

thanks,

–jarek