Python interpreter in Unity

After working a bit with maya/blender, I noticed that the interpreter is very useful for programatically editing the scene.
Yes, unity gives us lots of flexibility for scripting, but its a bit overkill in some cases (for example, batch-placing objects in the scene at edit time).

I thought that unity could use an interpreter, so I made one!

To use, just import the attached unity package and go to Window->Python Interpreter.
If you want to see an example, have a look at the screenshot where I create a cube and change a few properties.

A few notes :
1 - This is pure python (I used IronPython), and not unity’s variant of python. However, you can access all of the unity classes with the same name conventions as the C# API.
2 - History colors : Green = commands, Blue = return values, Black = Python output, Red = Errors.
3 - The editor is really basic. I just did the simplest thing that is functional.

As for the future, the main problem is of course the editor. I would like to have
a - Autocompletion
b - command history via up/down arrows
c - ability to copy/paste text from the history view
d - ability to load/save scripts, and perhaps open additional tabs.

I feel that the EditorWindow API is lacking in its flexibility, for example the Input.XXX set of functions isnt available. How would I solve b?

Anyway, I will release it soon to the Asset Store (for free), and will be really happy if I’ll be able to add some of the features to the editor.

If anyone wants to contribute, either by improving the editor or by giving me tips on how to do it within a Unity EditorWindow, I’ll be very happy (code contributions will be credited, of course!).

As always, I’m open to comments, suggestions and bug reports for this script.
Have fun!

6 Likes

wow, this looks pretty awesome!! the fact that you can access the unity API with it is really really cool!

Now, what would really make it shine would be to be able to interpret code at runtime… That would be a complete modding interface in a box!

Really good work there! thanks for making it available for everyone!!

Cheers

I’m going to check this out. Many thanks!

better if we can use this as game script interpreter!!
Keep up the good work

I’m not sure I understand you. You can already open the interpreter whenever you want (including in-game from the editor) and type code which will be evaluated instantly.

Pretty awesome friend! Altought i suck at python so far =)

quite interesting work. Can we use the blender or maya functions inside the unity. ? or Can we use the Blender scripts inside the unity3D?

more examples please!

Wow, I had no idea it was that easy to set up. Iron Python go! Thanks for sharing it. :slight_smile:

It also seems easy to build an in-game GUI around this, which would make for a stupendously powerful console window. Runtime modding, administrative tasks, game master stuff. Major coolness.

[quote=[Noman];557652]
I’m not sure I understand you. You can already open the interpreter whenever you want (including in-game from the editor) and type code which will be evaluated instantly.
[/quote]

What I mean is, being able to interpret .py files loaded at runtime, from the standalone build, so as to enable scripting access to modders.

I assumed, from the OP, that the interpreter only did it’s thing while in editor mode (or running the unbuilt game from the game window)… but if it’s possible to load scripts and run them at runtime (i.e. the built client), then the modding potential is virtually limitless. :smile:

Currently there is a Lua interface on the Asset Store, that enables one to load a .lua script and interpret it at runtime… the downside is that the Lua scripts don’t have implicit access to the Unity API, so you have to expose methods to get and set pretty much anything that will talk to the Unity API in one way or another…

This here has the advantage that you can simply use the API from the script, so you can do just about anything in your script, regardless of whether your game exposes that function or not. (that can be a good thing and a bad thing I guess… depends on the kind of game you’re making)

Cheers

Thanks for the feedback! I’ll probably refactor the work so that you’ll be able to run code from inside the game as well (its the same thing, just need to move things around a bit).

As for having a real editor, I decided to tap into an existing one instead of writing one on my own. I attached a WIP pic :

You can write code in MonoDevelop, and then click in the menu and it will send it to the python interpreter and execute it there. No autocompletion yet, but its an editor.

I’ll probably release it soon, and then samples will be easy to copy/paste/move around.

1 Like

More specifically, i want to use Blender scripts or functions inside the Unity3D. For example Blender Script(Curve) is used for create curve. Is that possible to use this inside the Unity3D by using this tool.?

[quote=[Noman];558919]
Thanks for the feedback! I’ll probably refactor the work so that you’ll be able to run code from inside the game as well (its the same thing, just need to move things around a bit).
[/quote]

This would make this the single most useful unity extension ever!!
Can’t wait for a release!!

About editors, would it be at all possible to hook up the Unity API with Eclipse+PyDev? That has to be the best Python IDE out there, and it has all the autocompletion and nice features you’d expect from an IDE… if only we could get class info from Unity’s API in there, it’d be perfect! Something like the way Visual Studio can grab code info… I’m sure Unity has it’s Boo libraries exposed there somewhere… PyDev can get into that and provide all the autocompletion (well, at least as much as is possible with Python :wink: ).

Oh, and one more question:

Is it possible to call python functions from standard C# or JS scripts?
If it were possible to load in a .py file, and by script call various functions already defined there… imagine the modding possibilities…

Cheers

@cemC : You have to distinguish between programming language (C#,Python,Javascript etc) and environment API (Unity/Blender/Maya etc). While the language is the same, those blender functions use blender API calls which are not available when python code is ran from unity. So you can not run those scripts as is. However, if you would re-implement all the library functions they use you could use them.

@HarvesteR

1 ) I moved the python execution to another class, but for some reason I’m getting an error when I put that class outside of the Editor folder. I might raise that question to the unity guys. I’ll post the updated version here soon and people will be able to tackle it as well.

2 ) I integrated MonoDevelop with the environment, since it is the editor that comes when you install unity. I don’t want people who want to use this extension to have to install Eclipse. However, since everything is open source, anyone can extend PyDev to support this as well (the protocols are trivial)

3 ) Once problem 1 is solved, you’ll be able to create the python environment in a game object, and access its RunCode function from scripts. That will allow you to run python code from wherever you want (load .py file into string → send it to RunCode → done!)

Oh man, this is looking real good!

If I get some free time this week (here’s hoping), I’ll try to help out with those compilation problems.

Again, a million thanks for this! It’s shaping up to be a complete solution for my own project here. I’ll definitely help out in polishing it once we get to the Modding API stage of development (which should be next week) :slight_smile:

Cheers

Hi Noman, i just found python interpreter on the asset store; any luck with the runtime capabilities so far ?

I got it working eventually. I just submitted an update to the asset store that gets it working. Stay tuned :slight_smile:

Also note that since I put in more effort than I expected into this project (and ended up solving all of the issues on my own), this plugin will not remain free forever. I will be keeping it free until the end of the month (May 2011) for the sake of those who participated in the discussion here, but it will get a price tag (probably not a high one) afterwards.

Thanks for the update, great news!

http://forum.unity3d.com/threads/89455-Python-Interpreter-in-Unity-Free-until-31-5

[quote=[Noman];560423]
1 ) I moved the python execution to another class, but for some reason I’m getting an error when I put that class outside of the Editor folder. I might raise that question to the unity guys. I’ll post the updated version here soon and people will be able to tackle it as well.
[/quote]

Did you include the path where your class is located in the PYTHONPATH lib?