I am experimenting with embedding IronPython in a Unity 3D project and I've run into some errors I can't seem to figure out.
I have the following script attached to a GameObject in Unity.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using IronPython;
using IronPython.Modules;
using System.Text;
public class IronPythonWrapper : MonoBehaviour {
void Start() {
ScriptTest();
}
public static void ScriptTest() {
// create the engine
var engine = IronPython.Hosting.Python.CreateEngine();
// and the scope (i.e. the Python namespace)
var scope = engine.CreateScope();
// execute the Python script
engine.ExecuteFile("Assets/Scripts/ipscript.py");
// grab the variable from the Python scope
string commands = scope.GetVariable<string>("commands");
Debug.Log(commands);
}
}
The goal is to have the IronPython engine instantiated above execute the following simple Python script, called ipscript.py
.
commands = "Script executed."
When I enter play mode (in Unity) I get the following error.
MissingMemberException: 'ScopeStorage' object has no attribute 'commands'
Does anyone have any idea what I should investigate to resolve this problem? Perhaps I need to implement some error trapping so I can see any exceptions IronPython is generating when it executes the Python script?
Your python execution is not properly passing the scope. Your commands
variable is therefore created on a transient scope that is discarded after executing the python file and the explicitly created scope has no idea what commands
is.
The snippet should read like
engine.ExecuteFile("Assets/Scripts/ipscript.py", scope);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.