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

Python Interpreter in Unity - Free until 31/5!

Discussion in 'Assets and Asset Store' started by noamgat, May 16, 2011.

  1. noamgat

    noamgat

    Joined:
    Nov 22, 2009
    Posts:
    125
    Hi guys,

    Python Interpreter in Unity page on Assert Store

    I just added the latest and greatest version of the python interpreter plugin to the asset store.
    What you get is :
    • An interactive python interpreter in the Unity editor that allows you to run python code that referenes unity objects
    • Integration with MonoDevelop that allows you to execute code edited in files
    • (.Net 2.0 API compatibility level required) A class that can execute dynamic python code in your builds. (Modding, anyone?)

    The plugin will be free until the end of the month, after which it will cost some amount.

    Screenshots and the readme (for more information) are attached to this post.
    You can also download a demo of it running as a standalone windows build.

    Have fun!
     

    Attached Files:

    Last edited: Nov 12, 2011
  2. digitalpowers

    digitalpowers

    Joined:
    Apr 6, 2011
    Posts:
    59
    Ok this is exciting, is there anyway to sandbox the code you execute at runtime? or does the python have access to everything in the game?
     
  3. hima

    hima

    Joined:
    Oct 1, 2010
    Posts:
    183
    Does this work on Mac? I'm trying to run the example, but I got one big error :/

    Code (csharp):
    1.  
    2. Internal compiler error. See the console log for more information. output was:warning CS1685: The predefined type `System.Runtime.CompilerServices.ExtensionAttribute' is ambiguous. Using definition from `System.Core, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e'
    3. /Applications/Unity/Unity.app/Contents/Frameworks/Mono/lib/mono/unity/System.Core.dll (Location of the symbol related to previous warning)
    4. /Users/Hima/Dropbox/mydev/Unity/TestJurassic/Assets/PythonInterpreter/Plugins/Microsoft.Scripting.ExtensionAttribute.dll (Location of the symbol related to previous warning)
    5.  
    6. Unhandled Exception: Mono.CSharp.InternalErrorException: Assets/PythonInterpreter/Scripts/PythonEnvironment.cs(24,12): PythonEnvironment.PythonEnvironment() ---> System.TypeLoadException: Could not load type 'System.CodeDom.CodeObject' from assembly 'System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.
    7.   at (wrapper managed-to-native) System.Reflection.MonoMethodInfo:get_method_info (intptr,System.Reflection.MonoMethodInfo)
    8.   at System.Reflection.MonoMethodInfo.GetMethodInfo (IntPtr handle) [0x00000] in <filename unknown>:0
    9.   at System.Reflection.MonoMethodInfo.GetAttributes (IntPtr handle) [0x00000] in <filename unknown>:0
    10.   at System.Reflection.MonoMethod.get_Attributes () [0x00000] in <filename unknown>:0
    11.   at System.Reflection.MethodBase.get_IsVirtual () [0x00000] in <filename unknown>:0
    12.   at Mono.CSharp.MemberCache.AddMethods (BindingFlags bf, System.Type type) [0x00000] in <filename unknown>:0
    13.   at Mono.CSharp.MemberCache.AddMethods (System.Type type) [0x00000] in <filename unknown>:0
    14.   at Mono.CSharp.MemberCache..ctor (IMemberContainer container) [0x00000] in <filename unknown>:0
    15.   at Mono.CSharp.TypeHandle..ctor (System.Type type) [0x00000] in <filename unknown>:0
    16.   at Mono.CSharp.TypeHandle.GetTypeHandle (System.Type t) [0x00000] in <filename unknown>:0
    17.   at Mono.CSharp.TypeHandle.GetMemberCache (System.Type t) [0x00000] in <filename unknown>:0
    18.   at Mono.CSharp.TypeManager.MemberLookup_FindMembers (System.Type t, MemberTypes mt, BindingFlags bf, System.String name, System.Boolean used_cache) [0x00000] in <filename unknown>:0
    19.   at Mono.CSharp.TypeManager.RealMemberLookup (System.Type invocation_type, System.Type qualifier_type, System.Type queried_type, MemberTypes mt, BindingFlags original_bf, System.String name, IList almost_match) [0x00000] in <filename unknown>:0
    20.   at Mono.CSharp.TypeManager.MemberLookup (System.Type invocation_type, System.Type qualifier_type, System.Type queried_type, MemberTypes mt, BindingFlags original_bf, System.String name, IList almost_match) [0x00000] in <filename unknown>:0
    21.   at Mono.CSharp.Expression.MemberLookup (Mono.CSharp.CompilerContext ctx, System.Type container_type, System.Type qualifier_type, System.Type queried_type, System.String name, MemberTypes mt, BindingFlags bf, Location loc) [0x00000] in <filename unknown>:0
    22.   at Mono.CSharp.Expression.MemberLookup (Mono.CSharp.CompilerContext ctx, System.Type container_type, System.Type qualifier_type, System.Type queried_type, System.String name, Location loc) [0x00000] in <filename unknown>:0
    23.   at Mono.CSharp.MemberAccess.DoResolve (Mono.CSharp.ResolveContext ec, Mono.CSharp.Expression right_side) [0x00000] in <filename unknown>:0
    24.   at Mono.CSharp.MemberAccess.DoResolve (Mono.CSharp.ResolveContext ec) [0x00000] in <filename unknown>:0
    25.   at Mono.CSharp.Expression.Resolve (Mono.CSharp.ResolveContext ec, ResolveFlags flags) [0x00000] in <filename unknown>:0
    26.   at Mono.CSharp.Invocation.DoResolve (Mono.CSharp.ResolveContext ec) [0x00000] in <filename unknown>:0
    27.   at Mono.CSharp.Expression.Resolve (Mono.CSharp.ResolveContext ec, ResolveFlags flags) [0x00000] in <filename unknown>:0
    28.   at Mono.CSharp.Expression.Resolve (Mono.CSharp.ResolveContext ec) [0x00000] in <filename unknown>:0
    29.   at Mono.CSharp.Assign.DoResolve (Mono.CSharp.ResolveContext ec) [0x00000] in <filename unknown>:0
    30.   at Mono.CSharp.SimpleAssign.DoResolve (Mono.CSharp.ResolveContext ec) [0x00000] in <filename unknown>:0
    31.   at Mono.CSharp.Expression.Resolve (Mono.CSharp.ResolveContext ec, ResolveFlags flags) [0x00000] in <filename unknown>:0
    32.   at Mono.CSharp.Expression.Resolve (Mono.CSharp.ResolveContext ec) [0x00000] in <filename unknown>:0
    33.   at Mono.CSharp.ExpressionStatement.ResolveStatement (Mono.CSharp.BlockContext ec) [0x00000] in <filename unknown>:0
    34.   at Mono.CSharp.StatementExpression.Resolve (Mono.CSharp.BlockContext ec) [0x00000] in <filename unknown>:0
    35.   at Mono.CSharp.Block.Resolve (Mono.CSharp.BlockContext ec) [0x00000] in <filename unknown>:0
    36.   at Mono.CSharp.ToplevelBlock.Resolve (Mono.CSharp.FlowBranching parent, Mono.CSharp.BlockContext rc, Mono.CSharp.ParametersCompiled ip, IMethodData md) [0x00000] in <filename unknown>:0
    37.   --- End of inner exception stack trace ---
    38.   at Mono.CSharp.TypeContainer.EmitConstructors () [0x00000] in <filename unknown>:0
    39.   at Mono.CSharp.TypeContainer.EmitType () [0x00000] in <filename unknown>:0
    40.   at Mono.CSharp.RootContext.EmitCode () [0x00000] in <filename unknown>:0
    41.   at Mono.CSharp.Driver.Compile () [0x00000] in <filename unknown>:0
    42.   at Mono.CSharp.Driver.Main (System.String[] args) [0x00000] in <filename unknown>:0
    43.  
    44.  
     
  4. digitalpowers

    digitalpowers

    Joined:
    Apr 6, 2011
    Posts:
    59
    My guess would be you have Compatibility set to .net 2.0 Subset instead of .net 2.0. Give that a look
     
  5. noamgat

    noamgat

    Joined:
    Nov 22, 2009
    Posts:
    125
    @himatako

    digitalpowers is correct, you need to set the .Net API compatibility level. Check out section 2 of the README (installation).

    @digitalpowers

    This plugin was written as a development tool and not a modding tool. So, in the current version they have access to the Unity (and UnityEditor) namespaces, which basically means they have access to everything (because of GameObject.FindXXXXX methods). In order to sandbox them, you would need to block the AddReference calls (I don't know how to do this, but I assume its possible) and directly expose the objects you wish to expose to the python environment. You would have to be very careful to not expose any Unity GameObjects or Components to the user, or they will be able to lookup. I might have a look at this soon to see how hard it would be...

    Googling a bit led me to AppDomains :
    http://stackoverflow.com/questions/4393153/sandbox-ironpython
    http://lists.ironpython.com/pipermail/users-ironpython.com/2007-April/004740.html

    I guess that is the general direction...
     
  6. hima

    hima

    Joined:
    Oct 1, 2010
    Posts:
    183
    @[Noman]
    Nope, it's already .net 2.0 Subset when I was trying it. I tried both .net 2.0 and .net 2.0 subset, none of them work
    EDITED : Never mind that. I restarted Unity and it works fine now :D

    So, is it possible to call custom script through Python? Do we need to expose or register functions/methods like in other embedded scripting language?
     
    Last edited: Jun 17, 2011
  7. noamgat

    noamgat

    Joined:
    Nov 22, 2009
    Posts:
    125
    The unity namespace is already exposed when you run, and I believe classes in the current assembly (the one that contains the user's code) is included automatically, so no need to expose stuff manually (I think).

    Since IronPython works via reflection, if you do something like

    obj = Unity.GameObject.Find("MyObject").GetComponent("MyComponent")

    You will automatically have access to all of your component's methods.

    If you want to be able to manually expose objects to you python environment you can add the following code to PythonEnvironment.cs :

    Code (csharp):
    1.     public void ExposeVariable(string name, object obj)
    2.     {
    3.         m_scriptScope.SetVariable(name, obj);
    4.     }
    And then use that function to directly send objects to the python scope.
     
  8. hima

    hima

    Joined:
    Oct 1, 2010
    Posts:
    183
    I see. Thank you very much! I tried it with SpriteManager2 and it works perfect. I can now run animation from python script, which is more than awesome ^ ^

    I was wondering, if we want to use script to control many many objects ( 500+), which one is better?
    1.) Having a shared central PythonEnvironment instance. Then each object does the following in update.
    Code (csharp):
    1.  
    2. void Update()
    3. {
    4.    ExposeVariable("scriptedObject", this);
    5.    PythonEnv.Instance.RunCommand( someScriptStringWeReadAtTheStartMethod );
    6. }
    7.  
    2.) Each object has their own PythonEnvironment and interpret script separately.

    Which one do you think will be better? Or am I approaching the problem correctly at all?
     
  9. noamgat

    noamgat

    Joined:
    Nov 22, 2009
    Posts:
    125
    I would say the first option is a better one. Not having looked too deeply into the IronPython sources, I assume there is a memory overhead of creating a script scope. So, having a centralized python environment in your code will probably keep your memory footprint lower.
     
  10. petrucio

    petrucio

    Joined:
    Aug 30, 2011
    Posts:
    126
    Just bought the python interpreter, and I'm having a similar error to himatako (I'm on PC, and using .NET 2.0 compatibility):

    Code (csharp):
    1. Internal compiler error. See the console log for more information. output was:warning CS1685: The predefined type `System.Runtime.CompilerServices.ExtensionAttribute' is ambiguous. Using definition from `System.Core, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e'
    2. E:\\Dev\Unity\Editor\Data\Mono\lib\mono\unity_web\System.Core.dll (Location of the symbol related to previous warning)
    3. E:\GameProjs\POC_AdvSandbox\Assets\PythonInterpreter\Plugins\Microsoft.Scripting.ExtensionAttribute.dll (Location of the symbol related to previous warning)
    4.  
    5. Unhandled Exception: System.TypeLoadException: Could not load type 'System.Security.SecurityTransparentAttribute' from assembly 'IronPython.Modules'.
    6.  
    7.   at (wrapper managed-to-native) System.Reflection.Assembly:InternalGetType (System.Reflection.Module,string,bool,bool)
    8.  
    9.   at System.Reflection.Assembly.GetType (System.String name, Boolean throwOnError, Boolean ignoreCase) [0x00000] in <filename unknown>:0
    10.  
    11.   at System.Reflection.Assembly.GetType (System.String name) [0x00000] in <filename unknown>:0
    12.  
    13.   at Mono.CSharp.RootNamespace.GetTypeInAssembly (System.Reflection.Assembly assembly, System.String name) [0x00000] in <filename unknown>:0
    14.  
    15.   at Mono.CSharp.RootNamespace.LookupTypeReflection (Mono.CSharp.CompilerContext ctx, System.String name, Location loc, Boolean must_be_unique) [0x00000] in <filename unknown>:0
    16.  
    17.   at Mono.CSharp.GlobalRootNamespace.LookupTypeReflection (Mono.CSharp.CompilerContext ctx, System.String name, Location loc, Boolean must_be_unique) [0x00000] in <filename unknown>:0
    18.  
    19.   at Mono.CSharp.Namespace.LookupType (Mono.CSharp.CompilerContext ctx, System.String name, Location loc) [0x00000] in <filename unknown>:0
    20.  
    21.   at Mono.CSharp.Namespace.Lookup (Mono.CSharp.CompilerContext ctx, System.String name, Location loc) [0x00000] in <filename unknown>:0
    22.  
    23.   at Mono.CSharp.TypeManager.CoreLookupType (Mono.CSharp.CompilerContext ctx, System.String ns_name, System.String name, Kind type_kind, Boolean required) [0x00000] in <filename unknown>:0
    24.  
    25.   at Mono.CSharp.TypeManager.InitCoreTypes (Mono.CSharp.CompilerContext ctx) [0x00000] in <filename unknown>:0
    26.  
    27.   at Mono.CSharp.Driver.Compile () [0x00000] in <filename unknown>:0
    28.  
    29.   at Mono.CSharp.Driver.Main (System.String[] args) [0x00000] in <filename unknown>:0
    30.  
     
    Last edited: Nov 1, 2011
  11. holyjewsus

    holyjewsus

    Joined:
    Mar 7, 2011
    Posts:
    624
    I want to purchase the interpreter as well, but before I do, I was curious can I import numpy or other python libraries?
     
  12. noamgat

    noamgat

    Joined:
    Nov 22, 2009
    Posts:
    125
    @petrucio

    I noticed that sometimes a Unity restart is required after switching to full .Net 2.0 compatibility mode.
    Also, the deciding factor is not what platform you are on, but what platform you are targeting. What are your current build settings?

    @redmacmanz

    IronPython is a managed implementation of python, so you can't use python libraries that use python<->c bindings, like numpy does. However, I noticed there is a port of the C parts of numpy to .NET, giving a port of numpy to IronPython, allowing you to use it :
    http://www.google.com/url?sa=t&rct=...lqixBA&usg=AFQjCNGpc71Z8NhS8XVRz0hKtqRNEcFphQ
    I have not tested this implementation of numpy, however. As for other libraries, you will need to check if they are pure python (good for you) or not (in which case you'll have to find ports like this one)
     
  13. noamgat

    noamgat

    Joined:
    Nov 22, 2009
    Posts:
    125
    Just noticed that that implementation might require IronPython 2.7rc1 (which requires .Net 4, which we don't have in unity), and this module uses IronPython 2.6.2 (because it is the last version that only requires .Net 2), so it might not work.

    You can download IronPython 2.6.2 for .Net 2 and try using the numpy port in clean C# before buying this module.
     
  14. petrucio

    petrucio

    Joined:
    Aug 30, 2011
    Posts:
    126
    @Noman

    I've searched around, I've seen those posts, and I've restarted and set full .Net compatibility for the target platform (standalone). The in-editor python interpreter window shows up but only echoes everything I type without really doing anything.
     
  15. noamgat

    noamgat

    Joined:
    Nov 22, 2009
    Posts:
    125
    I really don't know what to tell you. You say that you are running on windows, are targetting windows standalone, and have full .net 2 support (not the subset which is the default) ?
    Can you try and send me a minimalist project that reproduces this problem?
     
  16. petrucio

    petrucio

    Joined:
    Aug 30, 2011
    Posts:
    126
    Just creating an empty project with no assets and importing the Python Interpreter is enough to trigger the error, so I guess it's something on my end that you won't be able to reproduce there. Erasing the file InGamePythonInterpreter.cs (as opposed to non-existant PythonBehaviour.cs the doc mentions), made the Editor-only installation work, which is already worth my ten bucks, although I wanted to use eval() to auto-parse a JSON string.
     
  17. kpedersen

    kpedersen

    Joined:
    Oct 5, 2011
    Posts:
    9
    This is great work!

    Do you think there is any possibility to get this running on the web player (.net 2.0 subset)?

    I guess, IronPython would need to be compiled completely by .net 2.0 but I suspect the hard part would be removing certain IO and reflection from the code.

    But if this could be made to work that would be fantastic!

    Edit:
    Apparently IronPython works on silverlight and that is quite a restricted .NET environment.
     
    Last edited: Jun 12, 2012
  18. noamgat

    noamgat

    Joined:
    Nov 22, 2009
    Posts:
    125
    This will be very hard, as IronPython relies on some advanced namespaces (CompiledServices and stuff like that), and newer versions require .Net 4 to run. I worked with the last version that ran on .Net 2, and therefore am limited by its shortcomings.
     
  19. rockysam888

    rockysam888

    Joined:
    Jul 28, 2009
    Posts:
    650
    Is boo already python?
     
  20. holyjewsus

    holyjewsus

    Joined:
    Mar 7, 2011
    Posts:
    624
    Is this still supported for unity 3.5 and 4?
     
  21. noamgat

    noamgat

    Joined:
    Nov 22, 2009
    Posts:
    125
    Unity 3.5 is definitely supported. When 4.0 goes out of beta I will make sure its compatible there too. There haven't been changes that are supposed to break support, but I didn't personally test it yet.
     
  22. holyjewsus

    holyjewsus

    Joined:
    Mar 7, 2011
    Posts:
    624
    cool, good to hear. I'd like to call up a python script with a few arguments, and execute it from within unity getting data back from it? ( an array for instance)

    is this something thats possible.

    Let's assume I'm using only base python libraries.
     
  23. noamgat

    noamgat

    Joined:
    Nov 22, 2009
    Posts:
    125
    Definitely possible. In the C# (or other .net) code that calls unity, you have access to the global variables in the python virtual machine. So you can set the environment, run a script and then put as many python variable values back in your code. Return values of functions are accessible as well, I think (but even if not, just execute retVal = pyFunc() and then access the retVal global variable)
     
  24. holyjewsus

    holyjewsus

    Joined:
    Mar 7, 2011
    Posts:
    624
    very cool, I'll give it a shot tonight.
     
  25. cel

    cel

    Joined:
    Feb 15, 2011
    Posts:
    46
    can we use external python modules (pygame for example) with this or is it just dedicated to the unity api?
     
  26. holyjewsus

    holyjewsus

    Joined:
    Mar 7, 2011
    Posts:
    624
    so to read in a textual python script do I just read it in as a string? do I have to go through and add line breaks and spaces for indents or ... whats the process? For clarification this wouldn't be an editor script but something I'd like to run in game without a console, just having unity call this script and get some data back.

    also... how do I access a variables value from the python script... for example..

    for i range(100):
    i += 1

    how do I read the value of i back in unity in my c# script?
     
    Last edited: Nov 10, 2012
  27. holyjewsus

    holyjewsus

    Joined:
    Mar 7, 2011
    Posts:
    624
    hmm, I've been fighting for a while to get access to global variables or any variables in the python scripts... I know it should be simple but I can't find the same methods like Globals.Add that I see online.
     
  28. holyjewsus

    holyjewsus

    Joined:
    Mar 7, 2011
    Posts:
    624
    Not sure if this is the correct way to do it BUT:


    using the python environment expose variable method let me expose a unity variable into the python code, then using python enviornment.m_scriptScope ( which I had to make public), I could use GetVariable and get a python variable back into unity by name
     
  29. noamgat

    noamgat

    Joined:
    Nov 22, 2009
    Posts:
    125
    Yea, that sounds like a good way to communicate. The other way is to look at how PythonEnvironment's RunCommand works. The CommandResult struct contains returnValue of type object, which contains the result.

    So RunCommand("1+1").returnValue would be an object containing the number 2.

    Both are equally valid choices, use the one that is more comfortable in your situation.
     
  30. noamgat

    noamgat

    Joined:
    Nov 22, 2009
    Posts:
    125
    Broadly speaking, external python modules that are native python (don't use C extensions) will work, and most other ones will not. I think pygame uses SDL which is C, so it won't work.
    You can search for IronPython ports of python-c libraries. If you find that port, it will work.
     
  31. holyjewsus

    holyjewsus

    Joined:
    Mar 7, 2011
    Posts:
    624
    new question relating to this, so I import my python code as a text asset from the resources folder of unity, grab the text property and assign this to the code variable you expose.

    Where do I place external libraries so that iron python can import them when I call import xxxxx at the top of my file?

    Also, the standard library :( for instance, import random, have you tried this from within unity?

    and sorry for the endless stream of questions
     
    Last edited: Nov 14, 2012
  32. holyjewsus

    holyjewsus

    Joined:
    Mar 7, 2011
    Posts:
    624
    In response to anyone else playing around with this, I got the standard library working by copying python 2.6.2 library into my plugins folder, then adding sys.path.append(path to that lib in the plugins folder)

    now I can call import random! though not all methods seem to work.

    I still havn't figured out how to import an external library.
     
  33. noamgat

    noamgat

    Joined:
    Nov 22, 2009
    Posts:
    125
    I'm guessing you would place external libraries at the root folder of the project or where the built executable is placed.
    (When I was placing external DLLs, that was the location, it might be the same).
    This is just a hunch tho. Another alternative could be the location of the IronPython dll. I'm guessing one of them will work. You can test with your own ImportTest.py and run "import ImportTest" or something. This might even change from platform to platform.

    As for external libraries, your best bet is to check out the ironpython community, as what will work there will work here.

    For everyone else, just to let you know, I tested the extension on unity 4 (latest public release) and it works without modifications, so this plugin is Unity 4 compatible!
     
  34. ShawnMcCoo1

    ShawnMcCoo1

    Joined:
    Dec 26, 2011
    Posts:
    39
    What's the compatibility here?

    I have heard that it runs on Windows, Mac, and Android. But, what about iOS, WebPlayer, Flash, Linux, etc?
     
    Last edited: Nov 24, 2012
  35. noamgat

    noamgat

    Joined:
    Nov 22, 2009
    Posts:
    125
    The feature that causes this plugin to work/not work on a platform is the ability to select "Full .Net 2.0 compatibility" rather than ".Net 2.0 subset" (feature matrix here : http://docs.unity3d.com/Documentation/ScriptReference/MonoCompatibility.html ).
    In this page :
    http://docs.unity3d.com/Documentation/Manual/MobileDotnet.html
    They say that unity now supports the option to select Full .Net 2.0 compability in mobile (iOS and android).
    I don't know about the other platforms. I'm guessing that it will work on linux, won't work on flash. Dunno about web player.
    Try to search the documents for the .Net compatibility options for the different export platforms to get a final answer before you decide if this plugin fits your requirements or not.
     
  36. ShawnMcCoo1

    ShawnMcCoo1

    Joined:
    Dec 26, 2011
    Posts:
    39
    I'm ok with it not working on Flash and the capabilities of this plugin meet my needs. But, Is it possible that you could make a quick WebPlayer test build?
     
  37. noamgat

    noamgat

    Joined:
    Nov 22, 2009
    Posts:
    125
    At first shot it didn't work for me with webplayer build in unity 4.0. Sorry...

    Has anyone else here tried succeeded with this?
     
  38. ShawnMcCoo1

    ShawnMcCoo1

    Joined:
    Dec 26, 2011
    Posts:
    39
    I'd like to find a way to block execution in a Python script similar to the way that yield is used in Unity.

    For example, I'd like to call:

    ShowMessage("press any key to continue")

    and have it not progress to the next line until a key is pressed. Any recommendations?

    Edit: I'm working on integrating python's "yield" functionality with Unity's coroutines. I'll follow up as I make progress or hit challenges.
     
    Last edited: Nov 30, 2012
  39. ShawnMcCoo1

    ShawnMcCoo1

    Joined:
    Dec 26, 2011
    Posts:
    39
    Hey Noman,

    I've been learning Python (I haven't really used it before) and now I feel fairly solid with the language. I've also been digging into IronPython.

    My goal as stated before, is to figure out how to reasonably block execution. So, I've explored a few options.

    Mostly, right now I'm looking at creating methods that are scriptable actions. Then, yielding from those methods to create a generator. Something like:

    Code (csharp):
    1. def sequence():
    2.     yield "begin";
    3.  
    4.     ui.dialogue("Shawn", "yo, dude")
    5.     yield "wait_for_dialogue=Shawn"
    6.  
    7.     ui.dialogue("Justin", "hey, man")
    8.     yield "wait_for_dialogue=Justin"
    9.  
    Then, when it's time to activate this interaction, something like:

    Code (csharp):
    1. update = sequence();
    Then, in the update loop something like(c# pseudo code):

    Code (csharp):
    1. if(not blocked)
    2. {
    3.     python.runcommand("try { update.next(); } except { exception }");
    4. }
    Any thoughts?
     
  40. noamgat

    noamgat

    Joined:
    Nov 22, 2009
    Posts:
    125
    This makes sense. I don't have much experience with yield in python, but it sounds like it should work.
    If it does work, I would just make sure that I only catch the "yield finished" exception so that real exceptions do bubble up (or at least get logged)
     
  41. BBET

    BBET

    Joined:
    Dec 18, 2012
    Posts:
    40
    This is a great interpreter - works perfectly for me on Mac. Also Noman thanks for your support!

    But I need to make it work ingame on iOS.
    I have the iOS basic version and always get the error:
    Error building Player: SystemException: System.Net.Sockets are supported only on Unity iOS Pro. Referenced from assembly 'Mono.Data.Tds'.

    In my understanding I do not need any .Net Sockets, but I do not know how to exclude them.
    Has anybody an idea how I can get this work on iOS basic? Any help is highly appreciated!
     
  42. holyjewsus

    holyjewsus

    Joined:
    Mar 7, 2011
    Posts:
    624
    yes, I also wanted to use this in iOS basic, any ideas norman?
     
  43. EmeralLotus

    EmeralLotus

    Joined:
    Aug 10, 2012
    Posts:
    1,459
    This looks like a very python interpreter for unity, can someone offer a list of unity target platform that is supported.

    Thanks
     
  44. pretender

    pretender

    Joined:
    Mar 6, 2010
    Posts:
    862
    does this work in the web player and is there a possibility to use for example biopython with this?
     
  45. holyjewsus

    holyjewsus

    Joined:
    Mar 7, 2011
    Posts:
    624
    bump to old questions!?
     
  46. pjanec

    pjanec

    Joined:
    Aug 24, 2013
    Posts:
    1
    Is Unity 4.2 supported? Does it work with basic free (non Pro) version of Unity? Thanks...
     
  47. Mousechief

    Mousechief

    Joined:
    Aug 13, 2013
    Posts:
    1
    Also looking for iOS support on current Unity version.
     
  48. allanolivei

    allanolivei

    Joined:
    Oct 31, 2012
    Posts:
    6
    Why can not import random python library?

    **Python exception : No module named random
     
  49. holyjewsus

    holyjewsus

    Joined:
    Mar 7, 2011
    Posts:
    624
    you need to move random.py to your sys path, or add your installed python folder to your sys path... try searching sys path python
     
  50. Pi_3-14

    Pi_3-14

    Joined:
    May 9, 2012
    Posts:
    168
    I've just installed this, many thanks to the author!

    A think it would be a good idea if this asset shipped with a couple more examples that would cover common usage patterns.

    Firstly, how to execute a function in a .py file from Unity code, and how to go the other way, i.e. Trigger C# code from within the .py?

    I have seen how to execute a .py script from C#. But rather than have a dozen little .py files, I would rather have just one, which contains certain functions which I will call at certain moments from C# scripts.

    Also passing variables between C# and Python. I believe this has been covered in the above discussion, but it would be fantastic to get it in the asset's source.

    Would anyone care to detail all of this in a single post?

    Is it possible to get a .py to receive Update() etc events? I could manually wire it up if I could call .py functions from C#, but is there any other way?

    Also, is it possible to pass variables by reference? That might be cleaner than Messing with the global Python namespace...