Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Working Python binding?

Discussion in 'Scripting' started by blockimperium, Mar 29, 2017.

  1. blockimperium

    blockimperium

    Joined:
    Jan 21, 2008
    Posts:
    452
    Is there a working Python binding out there? I am working with kids that are learning game development in Python and rather than have them stuck with Pygame, I wanted to use Unity but I haven't found a binding that works with a modern version of Unity. Anyone know of one that still works?
     
  2. keenanwoodall

    keenanwoodall

    Joined:
    May 30, 2014
    Posts:
    595
    I don't know, but if you googled it and didn't find anything I doubt it exists.
     
  3. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,144
    There are a couple of things on the asset store, but I don't know if they will do what you want. Search for Python on the store. They aren't free however, so it may not work for you.
     
  4. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    8,988
  5. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,380
    Technically speaking, if you wrote your code separate from unity in IronPython you could compile it (target .net 3.5) and include it in your project and access said scripts.
    http://ironpython.net/

    Thing is... this development pipeline might be a little complicated for beginner developers.

    PERSONALLY speaking though... especially since I've tutored both kids and adults at programming... introducing them to more than one language early on can actually be a good learning experience. It doesn't lock them into a single language, and also it contrasts the syntax from the actual programming logic. It helps in seeing how the concept of say something like a variable, or object identity vs the syntax of how you define a variable or an object in a specific language.
     
  6. blockimperium

    blockimperium

    Joined:
    Jan 21, 2008
    Posts:
    452
    Thanks for all the assistance to all who have provided it!

    We are currently taking a look at both the Python Interpreter linked on the store and IronPython. The biggest concern about IronPython, as you stated, is getting the workflow together. We are going to try with the basic interpreter first (since we can just embed it into the interface) and start there - though I do like the idea of building a parallel web-based environment with IronPython and accessing the scripts that way.
     
  7. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    The performance for IronPython can be garbage if you're interpreting the raw Python scripts at runtime. We tried to use it to facilitate some moddability and threw it out because executing more than a handful of scripts at a time caused a big hitch in the framerate. Just something to be aware of.

    Also - Unity will still compile Boo which is based on Python. You'll have to create the assets manually since they removed the menu entries but it should work.
     
    blockimperium and lordofduct like this.
  8. blockimperium

    blockimperium

    Joined:
    Jan 21, 2008
    Posts:
    452
    Fortunately for these its more a matter of providing a way for them to test programming concepts as opposed to be a real game. So the performance isn't too much an issue as we're really looking at providing more of a sandbox and letting them provide a Python script to solve a challenge.
     
  9. KaletheQuick

    KaletheQuick

    Joined:
    Feb 12, 2016
    Posts:
    41
    Been working on this forever. It's super finicky, but I think I have it worked out.

    These were the resources that helped me figure it out.
    http://techartsurvival.blogspot.com/2013/12/embedding-ironpython-in-unity-tech-art.html
    http://shrigsoc.blogspot.com/2016/07/ironpython-and-unity.html

    Previously I had needed to use old versions of IronPython because the unity .net is an old one. They had recently updated the .net version with Unity 2017, so I decided to go back and try it. It is tough. I'm still having trouble accessing some functions and variables from unity and mono-derived objects.

    Once you have the required .dlls, Try this code:

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using IronPython;
    5. using IronPython.Modules;
    6. using System.Text;
    7.  
    8.  
    9. public class testScript : MonoBehaviour {
    10.     public bool runScriptOnce;
    11.  
    12.     [Space]
    13.     [TextArea]
    14.     public string pythonNotTerminalLol;
    15.     public bool runDatShit;
    16.  
    17.     [Space]
    18.     [TextArea]
    19.     public string pythonUpdateLoop;
    20.     public bool runFakeUpdateLoop;
    21.  
    22.     Microsoft.Scripting.Hosting.ScriptEngine localEngine;
    23.     Microsoft.Scripting.Hosting.ScriptScope scope;
    24.  
    25.     void Start () {
    26.         localEngine = IronPython.Hosting.Python.CreateEngine ();
    27.         scope = localEngine.CreateScope ();
    28.         localEngine.Runtime.LoadAssembly(typeof(GameObject).Assembly);
    29.         localEngine.Runtime.LoadAssembly(typeof(TestComponent).Assembly);
    30.         localEngine.Execute ("import UnityEngine as unity\nimport System.Collections as system", scope);
    31.     }
    32.  
    33.     void Update () {
    34.         if (runScriptOnce) {
    35.             ScriptTest ();
    36.             runScriptOnce = false;
    37.         }
    38.  
    39.  
    40.         if (runDatShit) {
    41.             localEngine.Execute (pythonNotTerminalLol, scope);
    42.             runDatShit = false;
    43.         }
    44.  
    45.         if (runFakeUpdateLoop) {
    46.             localEngine.Execute (pythonUpdateLoop, scope);
    47.         }
    48.     }
    49. }
    throw it on some object in the inspector, and click the checkboxes to activate/submit things.

    I usually start with this:
    def log(string):
    unity.Debug.Log(string)
    so that in the console or a fakeUpdate you can just type log(message) saves lots of keystrokes.

    it is VERY finicky, but I did just get it to work about 30 mintues ago. But, I can do cool things, like THIS:

    player = unity.GameObject()
    player.name = "Player"
    def fakeUpdate():
    player.transform.position += unity.Vector3(0.1,0,0)

    then put "fakeUpdate()" in the "Python Update Loop" thing and it will call it every frame when you have 'run' checked.
     
  10. KaletheQuick

    KaletheQuick

    Joined:
    Feb 12, 2016
    Posts:
    41
    Alright. New problems found. You CAN call unity.GetComponent<etc> from python, but since GetComponent can return a bool, python always casts it to a bool. This made it really hard to access the components that I wanted. So some way around that will be needed. To test if it could even figure out monobehaviors I put one as a static singleton, which I was then able to access with TestComponent._instance. But that would only work if there was only one in the scene, but it's an example of what you need to do to circumvent that shortcoming.
     
  11. ll3v3ll

    ll3v3ll

    Joined:
    Nov 14, 2014
    Posts:
    11