IronPython in Unity3D
Asked Answered
B

1

24

I am trying to use IronPython as an external scripting language for Unity3D. The necessary DLLs for IronPython's execution load just fine inside of Assets\Plugins. However, when I try to run the script I get this error:

PythonImportErrorException: No module named UnityEngine
IronPython.Modules.Builtin.__import__ (IronPython.Runtime.Calls.ICallerContext,string,object,object,object) <IL 0x0003b, 0x001cc>
(wrapper dynamic-method) object.__import__##5 (IronPython.Runtime.Calls.ICallerContext,object,object,object,object) <IL 0x0000e, 0x0004d>
IronPython.Runtime.Calls.FastCallableWithContextAny.Call (IronPython.Runtime.Calls.ICallerContext,object,object,object,object) <IL 0x00015, 0x00067>
IronPython.Runtime.Calls.BuiltinFunction.Call (IronPython.Runtime.Calls.ICallerContext,object,object,object,object) <IL 0x0000d, 0x00058>
IronPython.Runtime.Operations.Ops.CallWithContext (IronPython.Runtime.Calls.ICallerContext,object,object,object,object,object) <IL 0x00012, 0x000b0>
IronPython.Runtime.Importer.Import (IronPython.Runtime.PythonModule,string,IronPython.Runtime.List) <IL 0x0000d, 0x0006c>
IronPython.Runtime.Operations.Ops.Import (IronPython.Runtime.PythonModule,string) <IL 0x00007, 0x0003b>
(wrapper dynamic-method) object.<string>##1 (IronPython.Runtime.ModuleScope) <IL 0x0006b, 0x00210>

The script and the 'UnityEngine.dll' are located in the same folder. This is the script:

import clr
clr.LoadAssemblyFromFile("UnityEngine.dll")

import UnityEngine
from UnityEngine import *

Debug.Log("Hello World from Python!")
Brogle answered 1/8, 2012 at 19:28 Comment(9)
Out of interest: why IronPython?Rotl
It allows for me to work with Unity3D which is built on top of Mono.NETBrogle
Unity3d supports Boo out of the box, which is pretty close to Python; I'm wondering why you're wedging IronPython in there specifically.Rotl
By using IronPython, I can run scripts that can control the engine from outside of Unity3D.Brogle
Also boo is not python: different syntax, no "batteries included" (standard library which does all the hard work for you). There are virtually no existing examples or snippets for boo.Rodin
@Rodin Not sure if that was true then, but it certainly isn't now. The majority of the pages on the Unity Script Reference has Boo examples.Costar
@SixS you are right, the unity docs have examples. But try finding any larger example outside of that: Zilch. It looks like nobody is actually using Boo. And don't forget, "Boo IS NOT python", it is "inspired by Python" or something.Rodin
@dirkjot: It does include the standard .Net library, which is pretty extensive..Mcgaw
Boo is... really not Python. Oh, and it looks pretty dead too. (I very, very much dislike its C#-ish import statements... for obvious reasons when you compare them with Python's)Pourparler
B
19

So from a Unity script:

PythonEngine engine = new PythonEngine();
engine.LoadAssembly(Assembly.GetAssembly(typeof(GameObject)));
engine.ExecuteFile("apple.py");

And inside of a python script (mine was apple.py located in the same folder as the game.exe):

import UnityEngine
from UnityEngine import *

Debug.Log("Hello From IronPython!")

Edit #1

I should note that the reason I was getting an error before was because the runtime version was specified as a 4.0 instead of a 3.5 or lower.

Edit #2

If you need to access your scripts from IronPython, then you can load your assembly as well by doing:

engine.LoadAssembly(Assembly.GetAssembly(typeof(MyPlayerScriptOrSomething)));

Then to use it in script:

import MyPlayerScriptOrSomething

Note that you do not have to do the LoadAssembly for every script, just once to get the assembly.

Edit #3

The IronPython DLLs should be placed in a "Plugins" folder somewhere under "Assets" (most simply: Assets->Plugins. Here is my setup:

> Assets
> > Plugins
> > > IronMath.dll
> > > IronPython.dll
> > > Microsoft.Scripting.dll
> > > Microsoft.Scripting.Core.dll

Edit #4

Your scripts can be put anywhere your program has access to them. For instance, if you wanted to place "apple.py" directly under "C:\", you could execute that file by doing:

engine.ExecuteFile(@"c:\apple.py");

Edit #5

The version I am using is:

enter image description here

Brogle answered 1/8, 2012 at 20:25 Comment(7)
Were you able to run python scripts while you were using Unity in development mode (before you'd built an executable). I'm just trying to figure out where to put the IronPython.dll (I'm assuming Library/ScriptAssemblies) and where to put my python files. Any hints would be great.Groff
@Groff I updated the original post to reflect the answers to your question.Brogle
@StormKiernan did you use Python2.6 for .NET 2.0? I'm trying to reproduce this but without much luck :)Supposal
@StormKiernan my DLL doesn't seem to have a PythonEngine class so I can't call LoadAssembrly. I'm getting same error as you got before.Supposal
@Supposal I've updated my post with the version info. that I have for the IronPython.DLL.Brogle
Awesome comment. I'm old to Python, but new to Unity. The snippet above is "from a Unity script"...is this a C#, Boo, or Javascript file? Is that the entirety of the file (including the class headers if needed?) Is it possible to get the entire contents if it is not? Thanks!Unhallow
Related: theodox.github.io/2013/python_in_unity#.WPqsSIRcVBk.link and theodox.github.io/2013/unityrepl#.WPqtGUTnU4A.linkDeca

© 2022 - 2024 — McMap. All rights reserved.