C# to Blender, how?

Hello, i try send and receive python commands via a command port from C# to blender in windows, this is easy with maya, for example I open a port in maya and send commands in this way:

string message = “move -absolute 30 30 30; refresh;”;
stream.Write(data, 0, data.Length);
Console.WriteLine(“Sent: {0}”, message);

but I try in blender but not can, this is posible?, if this is posible how? Thanks.

yes you can use execnet with ironpython

Blender doesn’t have ‘ports’ as I understand they work in maya.

If you absolutely have to do it, you could just start an infinite thread inside Blender that scans a specific filename.txt for commands.

Your C# would simply write the command to the file.
The Blender thread would read the file and process the command.

The commands could simply be API instructions that get executed on-the-fly.

Blender has no capability to create a socket and wait for commands on it?

here is the link for execnet

http://codespeak.net/execnet/

Python has that capability and blender knows python…

----edit----

Wouldn’t be too hard to set up an RPC server and pass the commands to blender.

thanks for the answers, i try with this methods and if will be have good results i public them.

wow you are still alive

If the Blender has to capability to create a socket , it is better you communicate through a socket program from c#. Creating a socket program is easy in c#. http://csharp.net-informations.com/communications/csharp-socket-programming.htm the link explain basic socket program in C# , If you have a machine address and a port number , you can communicate from c# to any other language.

Lee.

+1 For the sockets! This is what I would do… :slight_smile:

I would like to say that I have been successful making external cpython and another blender talk to blender. In both cases I used XMLRPC which is of course based on sockets. Python comes with modules for XMLRPC clients and servers. Of course this also open the door of blenders communicating remotely or sending blender to the cloud. Imagine a blender community interacting online from inside blender.

The bridge I created is already implemented for my Ephestos project (a new gui and visual programming language) and there is a server and client module. Still are very simple and small but I plan to expand them with loads of features in the future.

https://github.com/kilon/Ephestos/blob/master/proteas/myserver.py

https://github.com/kilon/Ephestos/blob/master/proteas/myclient.py

The execnet library that I recommended earlier which also uses sockets , does not seem to like embedded pythons so it fails to import with blender python.

Hello.

I just trying to do the same thing. I want to create a rectangle in blender from c#. It seems there is a way using ironpyton but i just can´t find a way. Any idea will be apreciated.
Thank you so much.

Any news here I would like to know.

I have used my approach to succesfully allow Pharo

http://pharo.org/

to be used as a replacement or together with blender python. Not only I can execute blender python commands but also even handle blender python errors. Its still a very simple socket bridge but I have improved it a lot.

You can find my solution as a blender addon here

which you are free to adjust on your needs but please note you will need to make also code at the side of your favorite programming language , which will have to open a socket as well and handle sending python commands as strings.

My code is super simple really, I just use a blender modal operator that updates according to its timer to examine its own socket for any input strings and executes them as python commands and returns an output to a secondary socket connecting the other language.

My addon can be used to connect Blender python to any language out there. But of course it comes with limitations.

@kilon, I have to know if it is worth venturing into this new language.What I seek is performance, and I know that C# has advantages on this issue.

I did even a question in exchange stack -> http://blender.stackexchange.com/questions/53691/how-to-make-an-addon-with-c

I think you misunderstood me , you dont need to read the pharo code to understand what to do, this is why i did not link to the pharo code anyway

Opening a socket and sending python commands as strings must be super simple in any language, so its up to you to make it for C#. Sockets by themselves are super simple, how you handle the connections can be the complex part but thats it us to you. Obviously there are protocols out there that can do very complex type of communication and you can do complex language parsing from one language to another but you dont need if all you want is to execute simple commands or just make python code work with C#.

In short the simpler your demands the simpler the code needed.

This whole workflow is not new or rare, its a very popular field called IPC which stands for Inter Process Communication and you can find a ton of material online, including libraries etc. I choose to make my own implementation mainly for learning purposes because IPC really interests me as field and generally making Pharo use Python libraries.

Pharo is a language I love very, very , very much. But is a deeply personal choice. A choice I dont promote , market or impose on other people.

Blender should support a decent cross platform language like .Net core …not this python crap

Hi, I came across this post many years after and few months :stuck_out_tongue:
@Koolio recently I was working on this topic, but in a less elegant and much more inconvenient way.
What I did was to use Shared Memory since I am working in windows and this is being supported out of the box for both Python and C#, my intention was to access all the Blender’s features and the API from C#, but soon I realized that it would be much faster and easier for me to parse bytes rather than trying to address to the python shell itself (forgive me if I am making terminology mistakes). I still looking for better and more optimized solutions, my repo on github stills private, so if you want to take glance of my project you may check this video.

1 Like

Very good idea. Is this how it works?

https://docs.microsoft.com/en-us/dotnet/standard/io/memory-mapped-files?redirectedfrom=MSDN

https://docs.python.org/2/library/mmap.html