Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Showcase Python for Unity

Discussion in 'Scripting' started by Maeslezo, Jul 23, 2022.

  1. Maeslezo

    Maeslezo

    Joined:
    Jun 16, 2015
    Posts:
    325
    Hello everyone,

    I've just published a plugin system for Unity, using python as scripting language.

    This project implements a full plugin system with python as scripting language. You can create your own python scripts and run them on Unity. You can:
    • Run python scripts
    • Run Unity Code from python scripts
    • Use your own project classes inside the python scripts
    I hope this will be helpful to someone

    Regards

    https://github.com/Maesla/PythonForUnity
     
  2. Hosnkobf

    Hosnkobf

    Joined:
    Aug 23, 2016
    Posts:
    1,096
    Hey @Maeslezo

    This looks great!
    Are you planning to maintain and extend this project? Would be great to see a kanban board, so we know what works and what doesn't work yet.

    I also have a question:
    When using a scripting language for game specific logic, you often want to wait some time or for something.
    So, is it possible to mimic coroutines with your Python integration? so something like:
    Code (csharp):
    1. DoSomething()
    2. Wait(1)
    3. DoSomethingElse()
    4. WaitForCertainCondition()
    5. DoTheFinalThing()
     
  3. Maeslezo

    Maeslezo

    Joined:
    Jun 16, 2015
    Posts:
    325
    Thank you!

    I am not planning to maintain and extend the project right now, because I have too much on my plate, but I'd love to accept PRs and/or re-take the project when I have more time.

    I was able to run async code as you mention, so I know is doable , but I think not in this version.
    The next feature I'd really want to add is virtual environments, so you don't have to rely on the main python installation
     
  4. SirVon1299

    SirVon1299

    Joined:
    Feb 9, 2022
    Posts:
    1
    Hi @Maeslezo! Currently trying to build an app that utilizes a python library librosa in a py script. So, I was wondering if I could use this? To possibly call the py script on unity C# and run the py script on build standalone win 64 app?
     
    Kurt-Dekker likes this.
  5. Maeslezo

    Maeslezo

    Joined:
    Jun 16, 2015
    Posts:
    325
    Hi @SirVon1299 !

    So there are two different issues here.

    This framework, Python for Unity is a full plugin system with python as scripting language
    This means that the whole system is designed to use different custom plugins that the application users can write and use in your application, loading all the plugins and running them inside the application

    In your case, if you want to use a python library, such as librosa, I suggest to use pythonnet directly, which is the library that I use under the hood
    https://github.com/pythonnet/pythonnet

    With this library you can use a python library in your c# code:
    https://github.com/pythonnet/pythonnet#embedding-python-in-net

    Using pythonnet into Unity has some caveats, so you can take my project as a example.
    You can start here:
    https://github.com/Maesla/PythonForUnity/tree/main/Assets/Microkernel/Python Engine System/Runtime
    https://github.com/Maesla/PythonForUnity/blob/main/Assets/Microkernel/Python Engine System/Tests/Editor/PythonEngineSytemTests.cs

    So, in summary, yes, you can use my project for building a build standalone win 64 that uses a python library but:
    1. I suggest using directly pythonnet
    2. Use my project as an example about how to configure pythonnet
    3. Take into account that your application should run on a machine that has python and librosa installed. Virtual environment are not supported yet in my framework, although they are on pythonnet
      1. https://github.com/pythonnet/pythonnet/wiki/Using-Python.NET-with-Virtual-Environments
     
    Last edited: Oct 19, 2022
  6. Magnilum

    Magnilum

    Joined:
    Jul 1, 2019
    Posts:
    241
    Hello,

    I am very interested in what you have done. I would like to use it only for running a python script which convert .STL files into .OBJ files.

    For this, I need to use the "aspose-3d" python library and execute 2 lines of code.

    python code:
    Code (JavaScript):
    1. import aspose.threed as a3d
    2. scene = a3d.Scene.from_file("test.stl")
    3. scene.save("test.obj")
    What do I need to make it work with your package. Since there are many addons, plugins etc, I don't know what to extract to take the strict necessary things without installing additional packages will not be used.

    Thank you in advance.
     
  7. Maeslezo

    Maeslezo

    Joined:
    Jun 16, 2015
    Posts:
    325
  8. Magnilum

    Magnilum

    Joined:
    Jul 1, 2019
    Posts:
    241
    Thank you for your answer. I am gonna have a look at this.

    And very nice job for your package, it is pretty nice.
     
    Maeslezo likes this.