Grasshopper

algorithmic modeling for Rhino

I am writing a C# component which takes an ironpython function as a parameter. The component executes the function and correctly reads the output value. This part works well. However, I am having difficulty reading any messages generated by the function:

python code in component:

            def test(x):
                        x=x+1
                        print "Test"
                        return x

            a = test

c# code snippet (compiled):

            ScriptEngine pyEngine = Python.CreateEngine();

            dynamic result = pyEngine.Operations.Invoke(f.Value, param);

            object return_object = (object) result;

Note that f.Value = the Ironpython function test().

This code works fine, I just can't capture the print statement.

Anyone have any ideas?

Views: 1074

Replies to This Discussion

We don't really have a great way to redirect output of print statements to a custom location currently. You could set a variable in the ScriptEngine to something like a StringBuilder and then WriteLine to that inside of your script.

geez, that last reply was totally wrong (must not have had enough coffee yet today.) There is an Output property on the PythonScript class that you create in RhinoCommon. Now that I look at your sample a little closer, I don't exactly know what it is that you are doing.

There is a class in Rhino.Runtime.HostUtils called PythonScript. It has a static Create function that you can use to create an instance. This class has the Output property as well as functions for executing scripts.

Hi Steve, I have done it before as you suggest, but am currently using the Scriptengine class from Microsoft.Scripting.Hosting, which givse you the method "invoke" to execute a python function. In my code, I have the function created in a Python component and passed into the C# component (f.Value). The scriptengine class does not have a simple "output" property; instead, one can set an "Outputwriter", which I have not been able to work with.

RSS

About

Translate

Search

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service