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.

Python for Unity first impression and VSCode setup.

Discussion in 'Formats & External Tools Previews' started by MostHated, Jan 24, 2020.

  1. MostHated

    MostHated

    Joined:
    Nov 29, 2015
    Posts:
    1,200
    You can get a pretty nice setup going for python with autocomplete for unity_python. I was going to use the WSL system integration so I could more easily use PySide2 along with python2.7, but then Unity would only detect and use python.exe and won't work with python via WSL that I could figure out so far, though I only tried for a few moments by pointing it to the wsl uri (\\wsl$\Ubuntu18.04\usr\lib\python, though I didn't try wsl://UBUNTU1804/. Perhaps I will try later).

    Going to settings, then in the top right corner is a button to switch to json editing, you can add the following code after having installed the Python extension for VSCode, assuming you installed pything2.7 in the default location of C:\python2.7\.

    I am not 100% sure the line python.autoComplete.preloadModules still works, but I already had it in there for working with Houdini. Normally I would configure it to use a python interpreter and path for WSL so I can have more and better access to packages, but since Unity only uses python.exe for Windows, it would not help much as there isn't the same availability of packages and might end up being discrepancies.

    Code (CSharp):
    1.  
    2.     "python.autoComplete.extraPaths": [ "C:\\path\\to\\your\\project\\Library\\PackageCache\\com.unity.scripting.python@2.0.0-preview.6\\Python\\site-packages"],
    3.     "python.autoComplete.preloadModules": [ "unity_python"],
    4.     "terminal.integrated.env.windows": {
    5.         "PYTHONPATH" : "C:\\path\\to\\your\\project\\Library\\PackageCache\\com.unity.scripting.python@2.0.0-preview.6\\Python\\site-packages",
    6.     },
    7.     "python.pythonPath": "C:\\Python27\\python.exe",
    8.     "[python]": {
    9.     },





    This worked fine for me, at least, your milage may vary.

    It would be really nice if Unity could integrate its own python, not only to make things easier for end-users who don't want to have to deal with python, but also to keep away the extra windows and what not similar to Houdini, or well, any other DCC out there really that has python capability, which is pretty much all of them.

    -------------------------------
    The biggest issue out of the box seems to be that each time I try and run a python application (at least the GUI example that I copied and modified), it creates a new OnUpdate and doesn't seem to release them when closing it, so they just keep adding up and kill any sort of editor performance when trying to iterate, so you have to keep restarting Unity, which pretty much defeats the purpose of having python as a separate process anyways.





    All in all though, this is a great step in the right direction for Unity, really happy to see it.
    Thanks,
    -MH
     
    liudgervr likes this.
  2. bhudson

    bhudson

    Joined:
    Nov 21, 2014
    Posts:
    31
    Thanks for the feedback!

    I've logged the bug in the example of hooking up to OnUpdate too often, we'll look into it shortly.

    Shipping Python with the package is definitely a common request; we're looking into how to do that properly. Integrating with editors would be nice indeed, happy to see you've shared a way.
     
    MostHated likes this.
  3. bhudson

    bhudson

    Joined:
    Nov 21, 2014
    Posts:
    31
    FYI, the OnUpdate bug is fixed in the 2.0.1 release (you can upgrade).
     
    MostHated likes this.
  4. MostHated

    MostHated

    Joined:
    Nov 29, 2015
    Posts:
    1,200
    Awesome, great news. I will be sure to check it out here soon.
     
  5. Matjio

    Matjio

    Unity Technologies

    Joined:
    Dec 1, 2014
    Posts:
    98
    MostHated likes this.
  6. aka_anoop

    aka_anoop

    Joined:
    Jan 5, 2016
    Posts:
    7
    Is there a way to generate stubs for use with IDE? For the UnityEditor and UnityEngine packages?
     
  7. MostHated

    MostHated

    Joined:
    Nov 29, 2015
    Posts:
    1,200
    I realize that this is a super late reply, so sorry about that. I am sure you guys have a great handle on things, but just in case it helps any, Houdini is written in C++ and integrates extremely well with Python using http://www.swig.org/

    Keep up the great work. : D

    Is the info I posted in my initial post not what you are looking for? I showed how I managed to do exactly what you are asking. Unless I am misunderstanding you somehow, that is. The second and third items in the list in my second image show that there is access to UnityEngine and UnityEditor packages.
     
    liudgervr likes this.
  8. tran10

    tran10

    Joined:
    Nov 30, 2020
    Posts:
    13
    Hello,

    I have a script in C# for example:

    public class DraggableObject : MonoBehaviour
    {
    void OnMouseDown()
    {
    ....
    }
    }


    And I want to use AddComponent to add this script C# to gameObject by script python like this:

    import UnityEngine as UE
    go = UE.GameObject.CreatePrimitive(UE.PrimitiveType.Cube)
    go.AddComponent(DraggableObject)

    But I have the error : name 'DraggableObject' is not defined.
    Do you have some advise? or I need to import DraggableObject.cs?

    Thank you so much for your reponse.

    Hoa.
     
  9. MostHated

    MostHated

    Joined:
    Nov 29, 2015
    Posts:
    1,200
    I only recently saw version 4.x had been released. It had been a good while since I played around with the Unity implementation so I wanted to check it out, but noticed it specifically had Linux pulled out via compiler definitions (I have moved back 100% to linux since my original post). Naturally, I made the package a local package and went through to try and enable Linux, but it looks like one of the later steps of the process is to add and install a separate package of: com.unity.scripting.python.linux, which looks like it not obtainable currently?

    Any word on when this might become available to play around with? I suppose I could throw up a Windows VM, just to check out the current state of things, but I would rather not have to.
    Thanks,
    -MH
     
  10. sohamvyas73

    sohamvyas73

    Joined:
    Jun 12, 2020
    Posts:
    1
    Hello, I want to find the documentation of Python for unity but I am not able to find it. Can you help?