Python for Unity first impression and VSCode setup.

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.

    "python.autoComplete.extraPaths": [ "C:\\path\\to\\your\\project\\Library\\PackageCache\\com.unity.scripting.python@2.0.0-preview.6\\Python\\site-packages"],
    "python.autoComplete.preloadModules": [ "unity_python"],
    "terminal.integrated.env.windows": {
        "PYTHONPATH" : "C:\\path\\to\\your\\project\\Library\\PackageCache\\com.unity.scripting.python@2.0.0-preview.6\\Python\\site-packages",
    },
    "python.pythonPath": "C:\\Python27\\python.exe",
    "[python]": {
    },

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

2 Likes

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.

1 Like

FYI, the OnUpdate bug is fixed in the 2.0.1 release (you can upgrade).

1 Like

Awesome, great news. I will be sure to check it out here soon.

Thanks for your feedback @MostHated .
The initial post has been updated with the new version containing the fix.

1 Like

Is there a way to generate stubs for use with IDE? For the UnityEditor and UnityEngine packages?

1 Like

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.

1 Like

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.

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

Hello, I want to find the documentation of Python for unity but I am not able to find it. Can you help?