Python In Unity



Unity

IronPython lets you import dotnet assemblies as if they were python modules, and since Unity and the Unity Editor are dotnet assemblies you can get access to the entire Unity environment just by importing them into your interpreter. First, we need to load the assemblies to make them available to the intpereter itself. Python for Unity provides: A Python Script Editor window principally aimed at Technical Artists who want to run short scripts and easily create new. An in-process API aimed at Pipeline Technical Directors who want to call studio scripts implemented in Python from C#. An out-of-process API aimed. Start to Finish Unity Games and Python Coding Master Unity and Blender: Make 40 Games and Low Poly Art We cover basic programming concepts for people who have never programmed before. This course covers key topics in Python and coding in general, including variables, loops, and classes.

⚡️ A very fast, simple, and general inter-process communication example between Unity3D C# and Python, using ZeroMQ.

PS. It looks slow in the GIF above because I put a delay of one second between each message so that you can see itworking.

Core Pillars

Python In Unity
  • very fast — ZeroMQ is a networking library that allows you to send huge amount of data from server to client in a short period of time. I’m talking about casually sending/receiving 10,000 requests per second.
  • simple — You don’t have to explicitly open and bind a socket or know anything about low-level networking.
  • general — You can use this to send/receive any kind of data request. You can send image, video, text, JSON, file, or whatever you want. In this example, we are sending text.
  • inter-process — The communication is done inside the same machine. Which means very low-latency.

Introduction

  • Have you ever tried to communicate C# code in Unity3D with Python before but could not find a satisfying solution?
  • Have you ever tried implementing communication protocol using file read/write and found out that it’s a stupid approach?
  • Have you ever tried communicating using Web HTTP request and found out that it’s stupidly slow and high latency?
  • Have you ever tried communicating using socket/TCP/UDP stuff, but it feels like you are reinventing the wheel and youare becoming a network engineer?
  • Have you ever tried to communicate by emulating a serial port, and found out that it’s not how cool guys do work?
  • Have you ever tried to send Unity input to python and do some scientific work (maybe even machine learning task)and return the output to Unity?
  • Have you ever tried to build a .dll from python or even rewrite everything in C# because you don’t know how tocommunicate between python and C# processes?
  • Have you ever tried to embed IronPython or Python.NET inside Unity but it doesn’t allow you to install youramazing external python libraries? (And its minimal power is pretty ridiculous compared to your external python)
  • Have you ever tried to export a TensorFlow Protobuf Graph (Deep learning model) and use TensorFlowSharp orOpenCVForUnity to import the graph inside Unity because you want to use the model to predict stuff in Unity, but itdoesn’t allow you to use/utilize your new NVIDIA GeForce RTX 2080Ti, and it’s also hard to code?
  • Tried MLAgents, anyone?

If you answer Yes to any of these questions but it seems you have found no solutions,then this repository is definitely for you!(If you answered Yes to all questions, you and me are brothers! 😏)

I’ve tried a lot. With a lot of searching on the internet, I’ve found no solutions that is simple, fast, and generalenough that I can apply to any kind of communication between Python and Unity3D. All I’ve done in the past were simplya hack to either get my scientific computation work in Unity instead of python, or communicate between the processes painfully.

Until I found ZeroMQ approach from this repository(and some head scratching).

Solution Explanation

I’ve built a request-reply pattern of ZeroMQ where Python (server) replies whenever Unity (client) requestsa service from Python.

The idea is to create a separate thread inside Unity that will send a request to python, receive a reply and log the replyto the console.

Getting Started

  1. Clone this repository using git clone https://github.com/off99555/Unity3D-Python-Communication.git command.
  2. Open UnityProject (its dll files are targeting .NET 4.x version) and run Assets/NetMQExample/Scenes/SampleScene.
  3. Run python file PythonFiles/server.py using command python server.py on a command prompt.
  4. You should start seeing messages being logged inside Unity and the command prompt.
Python

Specifically, Unity will send request with a message Hello 10 times, and Python will simply reply World 10 times.There is a one second sleep between each reply on the server (to simulate long processing time of the request).

Please read the comments inside PythonFiles/server.py and UnityProject/Assets/NetMQExample/Scripts/ and you willunderstand everything more deeply.

The most important thing is that you should follow the 4 getting started steps first. Don’t skip it! ❣️

After you’ve understood most of the stuff but it’s not advanced enough, you should consult the officialØMQ - The Guide.

Requirements

  • PyZMQ is the Python bindings for ZeroMQ. You can install it usingpip install pyzmq command or see more installation options here orhere.
  • NetMQ is a native C# port of ZeroMQ. Normally you need to install this usingNuGet package manager inside Visual Studio when you want to build a .NET application, or you could install using.NET CLI. But for this repository here, you don’t need to do any of the installation because we’ve already includedAsyncIO.dll and NetMQ.dll for you inside UnityProject/Assets/NetMQExample/Plugins/ directory.If you want to build your own dll files, please take a look atthis issue.

Known Issues

Based on this issue, the NetMQ implementation is not working nicely with Unity. If you create more than one ZeroMQ client in Unity, the Unity editor will freeze.

Troubleshooting

  • While both server and client are running and communicating fine, I kill the server process, restart the server, then both server and client seem to not be communicating anymore. Why don’t they continue communicating? Is this a bug?

    No, this is the expected behavior of ZeroMQ because of the simplicity of the code. It’s mentioned in the guidehere. If you want to make the code better, which is notthe focus of this example, you can learn more about ZeroMQ as suggested in the screenshot below.

    The problem is that when you restart the server, the server won’t reconnect to the old client anymore. You have to restart the client also.

Disclaimer

This repository is designed to be a minimal learning resource for getting started. It’s not a fully working high-level package.After you understand the example, my job is done.

Most of the code are just copies from the official ZeroMQ tutorial. I try to make this as simple to grasp as possible,so I only log the message to the console and nothing fancy. This is to minimize the unnecessary learning curve.

TODO

Python is a great programming language due to a multitude of reasons such as a long list of libraries ready to solve almost any problem you might encounter. For me, however, the biggest reason to prefer coding in Python with Unity over C# is that I simply don't have to type as much; no type declarations, semicolons or curly brackets, not to mention return types, scopes and so on… 😴

Yet, online I can only find outdated guides or worse yet 'why don't you just learn C#, it's easy and blah blah' comments. 😫 But I like Python. 🥺Therefore, this post teaches you how to use IronPython with Unity. Just remember that IronPython only supports Python 2.

Use Python with Unity

Python 3 In Unity

First you will have to download IronPython dll files and place them into Assets/Plugins directory. The easiest way to do this is by downloading my Unity package.

Python

Import my Unity package to your game project

IronPython requires .NET 4.5, and Unity doesn't like this by default. That's why you will have to change the .NET version by going to Edit -> Project Settings -> Player. Make sure that the Scripting Runtime Version is .NET 4.x (this requires you to restart Unity) and that API Compatibility level is .NET 4.x

How to code in Python

Assume you have a small code snippet greeter.py in Python like this:

You can use it from C# like this

For a similar example, see the PythonExample prefab in my Unity package.

As you can see, the Python class can be directly used as an object in C#, and better yet, if you provide the path to the AssetsPluginsLib folder included in my Unity package, you can use anything in Python standard library! In addition, if you also provide the path to your Python script, you can import anything in that path as usual.

Next steps

If you want to use any external Python libraries (the ones you install with pip), you can just download and extract them to the
AssetsPluginsLib folder. This will make them available for IronPython.

Note: Using absolute paths is fine when you are doing development, but if you ever decide to publish your game, you will need to resolve the paths dynamically and distribute the standard library in AssetsPluginsLib and your Python scripts with the game executable.

Unity Python Support

Finally you can have the best of two worlds: Unity's dead easy world editor and Pythonic simplicity. ☺️