Execute python function from .Net plugin

Hello! I recently started developing a simple .net plugin for visual components. The plugin gets information from a database, then passes this information to visual components by writing it to the output. This works, but it doesn’t feel like the best solution. Is it possible to somehow call a python function from a .net plugin? If so, how do i do this? Is there some documentation i’m missing?

Kind regards.

 

In a Python script, you can use vcApplication.findCommand(“netCommand”) to get a handle for the netCommand, which can be used to call a .NET command registered with the VC product.

Here is a tutorial for how to do that.

http://academy.visualcomponents.com/lessons/register-a-command/?course=135

Thanks for the quick reply! But it really doesn’t answer my question. I know that i can use findCommand to get a handle for a command declared in my .Net plugin, but i want to get a handle to a python function declared in one of my models. Is this even possible?

Ah, sorry. I misread. No, you cannot get a handle for a function in a script. You can, however, make the function a Python command can call it in .NET or bind the function to a component property’s OnChanged event, so whenever you change the value of the property you know that function will be called.

Yeah, using a components property seems to be the best solution for me.

Thanks for the help, really appreciate it.

No problem, and the Add-ons course has a lesson that shows how to do that. Another thread that might interest you

https://forum.visualcomponents.com/forums/topic/tcp-ip-two-way-communication/

One more question! How do you set the value of a string property from the plugin?

I can obtain a reference to the IProperty in my component, but how do you dynamically set a value on said property? I’ve tried createAndSetNewValue, but I get the exception “System.NotSupportedException: ‘Specified method is not supported.’”

If you are working with .NET language and .NET API, an IProperty or IProperty<T> object has a Value property, which is a getter and setter. So use that to set the value of a component property.

createAndSetNewValue… VC .NET API uses Pascal case for its naming convention, so it’s CreateAndSetNewValue.

Hi! Sorry for being unclear, I meant CreateAndSetNewValue. Forgot to capitalize create.

I’ve tried to set the Value of my IProperty with a string value, but i got an access violation exception.

I do not know what you are doing then. If I want to set the value of the Name property, for example, I could do this using both IProperty and IProperty<T>.

        public void ChangeName()
        {
            if (app.World.Components.Count > 0)
            {
                ISimComponent _comp = app.World.Components[0];
                _comp.Name = "Example";
                IProperty _prop = _comp.GetProperty("Name");
                _prop.Value = "Another Example";
                IProperty<string> _propT = (IProperty<string>)_comp.GetProperty("Name");
                _propT.Value = "Last Example";
            }
            
        }

Edit: If I had to guess, I would think you were not getting the property object first, and then setting the value.

Dear MY Friends,

I am a primary user of Visual Components, could you please instruct me how to connect VC with database? If possible, could you help me record a simple teaching video. Thanks a lot!

Best Wishes.

Depends on the database, but try this
https://github.com/mkleehammer/pyodbc/wiki/Getting-started

I have used SQLite module with VC, but I don’t have access to those scripts atm.

Hi zesty,

The database I use is SQL Server. The webpage link you sent me is the method that Visual Components connects with SQL Server.

How to create behaviors in .NET?

Hy,

image

Regards
Feature

  • Thank you. I succeeded. Thank you!

You’re welcome

Regards Feature

Hello Zesty,

How can I define a Python Command that is callable from a .Net plugin? The other way around is well explained in the lessons.
I have a piece of code in python that I don’t want to translate into C# to include it into my current project.

Thanks a lot!

For anybody who is interested. I found a solution on my own. To search for python commands you have to use IApplication Interface, not ICommandRegistry:

IApplication app = IoC.Get<IApplication>();
IPythonCommand pythonCommand = app.FindPythonCommand("TestCMD");  <-- Name of your command defined in __init__.py file: cmd = loadCommand("TestCMD", cmduri)
pythonCommand.Execute();

If there is another, more elegant way to access python code from .net, I would be happy to hear about them. :slight_smile:

@joshua_beck I was just about to respond. Does this help? You can use ExecuteCommand2() method as well as the others in the image. However, ExecuteCommandMethod() has a funky third parameter that requires Matrix[] object, so I recommend using ExecutePythonCommandMethod().

1 Like