Created
July 30, 2013 21:43
-
-
Save sbaer/6117280 to your computer and use it in GitHub Desktop.
Using python inside of a RhinoCommon command
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
Rhino.Runtime.PythonScript m_py_script; | |
Rhino.Runtime.PythonCompiledCode m_compiled_script; | |
protected override Rhino.Commands.Result RunCommand(RhinoDoc doc, Rhino.Commands.RunMode mode) | |
{ | |
// create an instance of a python script processor | |
if (m_py_script == null) | |
m_py_script = Rhino.Runtime.PythonScript.Create(); | |
if (m_py_script == null) | |
{ | |
Rhino.RhinoApp.WriteLine("Error: could not load python scripting engine"); | |
return Rhino.Commands.Result.Failure; | |
} | |
// Always set the doc for the script so it knows what it is working with | |
// when it is executed | |
m_py_script.ScriptContextDoc = doc; | |
// you can either directly execute the script with a string (or filename) | |
m_py_script.ExecuteScript(@" | |
import rhinoscriptsyntax as rs | |
for i in range(10): | |
rs.AddLine([0,0,0],[i+10,i*2,i*i]) | |
"); | |
// or... | |
// if the script isn't changing and we want to reuse it over and over | |
// we can eliminate the parsing stage and use a "compiled" script object | |
// This is what we use in ghPython and only recreate the compiled script | |
// when the text actually changes. | |
// I would assume the script would be much larger than this example | |
if( m_compiled_script==null ) | |
m_compiled_script = m_py_script.Compile("print 'hi'"); | |
m_compiled_script.Execute(m_py_script); | |
return Rhino.Commands.Result.Success; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment