前提・実現したいこと
pythonで書かれたプログラムをunityで読み込みたい。
センサからの情報をpythonで機械学習して出力するプログラムをpythonで書きました。
その出力情報をunity側で受け取り、その値に応じてunity内部にある物体を動かしたいと考えています。
(unityでpythonで書かれたプログラムを使おうとするのではなく
pythonでの出力結果をそのままunityで受け取れれば早いのですが、そちらの方法でも何かアドバイスがありましたらよろしくお願いします。)
そこでこのサイトを参考にまずはpythonで書かれたコードをunityで読み込もうと思いました。
unityでpythonを使う。
上記のサイトを参考にこのようにしたのですが、plguinsの.dllが一つ足りません(3年前の記事だからか?)
発生している問題・エラーメッセージ
するとこのようなエラーが出ました。
調べるとunityEngineというモジュールが見つからないということなのですが、、、?
ImportException: No module named UnityEngine Microsoft.Scripting.Actions.Calls.MethodCandidate+Caller.Call (System.Object[] args, System.Boolean& shouldOptimize) (at <9661a627b9df4cc6a8147ea97a8e7cf4>:0) IronPython.Runtime.Types.BuiltinFunction+BuiltinFunctionCaller`5[TFuncType,T0,T1,T2,T3].Call4 (System.Runtime.CompilerServices.CallSite site, IronPython.Runtime.CodeContext context, TFuncType func, T0 arg0, T1 arg1, T2 arg2, T3 arg3) (at <9f680c4831e54be9b4f3102a0faa460a>:0) IronPython.Runtime.Importer.Import (IronPython.Runtime.CodeContext context, System.String fullName, IronPython.Runtime.PythonTuple from, System.Int32 level) (at <9f680c4831e54be9b4f3102a0faa460a>:0) IronPython.Runtime.Operations.PythonOps.ImportTop (IronPython.Runtime.CodeContext context, System.String fullName, System.Int32 level) (at <9f680c4831e54be9b4f3102a0faa460a>:0) Microsoft.Scripting.Interpreter.FuncCallInstruction`4[T0,T1,T2,TRet].Run (Microsoft.Scripting.Interpreter.InterpretedFrame frame) (at <9661a627b9df4cc6a8147ea97a8e7cf4>:0) Microsoft.Scripting.Interpreter.Interpreter.Run (Microsoft.Scripting.Interpreter.InterpretedFrame frame) (at <9661a627b9df4cc6a8147ea97a8e7cf4>:0) Microsoft.Scripting.Interpreter.LightLambda.Run2[T0,T1,TRet] (T0 arg0, T1 arg1) (at <9661a627b9df4cc6a8147ea97a8e7cf4>:0) IronPython.Compiler.PythonScriptCode.RunWorker (IronPython.Runtime.CodeContext ctx) (at <9f680c4831e54be9b4f3102a0faa460a>:0) IronPython.Compiler.PythonScriptCode.Run (Microsoft.Scripting.Runtime.Scope scope) (at <9f680c4831e54be9b4f3102a0faa460a>:0) IronPython.Compiler.RuntimeScriptCode.InvokeTarget (Microsoft.Scripting.Runtime.Scope scope) (at <9f680c4831e54be9b4f3102a0faa460a>:0) IronPython.Compiler.RuntimeScriptCode.Run (Microsoft.Scripting.Runtime.Scope scope) (at <9f680c4831e54be9b4f3102a0faa460a>:0) Microsoft.Scripting.SourceUnit.Execute (Microsoft.Scripting.Runtime.Scope scope, Microsoft.Scripting.ErrorSink errorSink) (at <790e4a6ad97d4a53a26736959b8e2995>:0) Microsoft.Scripting.SourceUnit.Execute (Microsoft.Scripting.Runtime.Scope scope) (at <790e4a6ad97d4a53a26736959b8e2995>:0) Microsoft.Scripting.Hosting.ScriptSource.Execute (Microsoft.Scripting.Hosting.ScriptScope scope) (at <790e4a6ad97d4a53a26736959b8e2995>:0) (wrapper remoting-invoke-with-check) Microsoft.Scripting.Hosting.ScriptSource.Execute(Microsoft.Scripting.Hosting.ScriptScope) PythonLoader.Start () (at Assets/Script/PythonLoader.cs:21)
該当のソースコード
C#
1using UnityEngine; 2using System.Collections; 3using System.IO; 4 5public class PythonLoader : MonoBehaviour 6{ 7 void Start() 8 { 9 string script; 10 var filename = Application.dataPath + "/../../test.py"; 11 12 using (StreamReader sr = new StreamReader(filename, System.Text.Encoding.UTF8)) 13 { 14 script = sr.ReadToEnd(); 15 } 16 17 var scriptEngine = IronPython.Hosting.Python.CreateEngine(); 18 var scriptScope = scriptEngine.CreateScope(); 19 var scriptSource = scriptEngine.CreateScriptSourceFromString(script); 20 21 scriptSource.Execute(scriptScope); 22 } 23} 24______________________________ 25test.py 26 27import clr 28clr.AddReferenceByPartialName('UnityEngine') 29import UnityEngine 30 31import sys 32sys.path.append(UnityEngine.Application.dataPath + '/../../Lib') 33 34import datetime 35 36 37def print_message(): 38 UnityEngine.Debug.Log('Test message from Python!') 39 UnityEngine.Debug.Log(datetime.datetime.today()) 40 41 42print_message() 43 44
試したこと
pythonloaderによって
test.pyはしっかり読み込めていますが、test.pyのなかにあるunityEngineが読み込めていないようです。
unityでpythonで書かれたプログラムを使おうとするのではなく
pythonでの出力結果をそのままunityで受け取れれば早いのですが、そちらの方法でも何かアドバイスがありましたらよろしくお願いします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/11/08 12:18