前提・実現したいこと
Unityからの動作に応じてマウスポインタを動かす。
(Macで)マウスポインタを動かすモジュールがpythonでしか見つからなかったため、Ironpythonを用いてC#とpythonを連携させて実現しようとした
###環境
Mac OS High Sierra(10.13.6)
Unity(2017.2.0f3)
IronPython(2.7.5)
Python(3.7.1)
PyAutoGUI
発生している問題・エラーメッセージ
Unityから実行した時のものです。
pythonを単独でターミナルから実行した時は問題なく実行できます
SyntaxErrorException: invalid syntax IronPython.Runtime.ThrowingErrorSink.Add (Microsoft.Scripting.SourceUnit sourceUnit, System.String message, SourceSpan span, Int32 errorCode, Severity severity) IronPython.Compiler.Parser.ReportSyntaxError (Int32 start, Int32 end, System.String message, Int32 errorCode) IronPython.Compiler.Parser.ReportSyntaxError (Int32 start, Int32 end, System.String message) IronPython.Compiler.Parser.ReportSyntaxError (System.String message) IronPython.Compiler.Parser.AddTrailers (IronPython.Compiler.Ast.Expression ret, Boolean allowGeneratorExpression) IronPython.Compiler.Parser.AddTrailers (IronPython.Compiler.Ast.Expression ret) IronPython.Compiler.Parser.ParsePower () IronPython.Compiler.Parser.ParseFactor () IronPython.Compiler.Parser.ParseExpr (Int32 precedence) IronPython.Compiler.Parser.ParseExpr () IronPython.Compiler.Parser.ParseComparison () IronPython.Compiler.Parser.ParseNotTest () IronPython.Compiler.Parser.ParseAndTest () IronPython.Compiler.Parser.ParseOrTest () IronPython.Compiler.Parser.ParseExpression () IronPython.Compiler.Parser.FinishArgListOrGenExpr () IronPython.Compiler.Parser.AddTrailers (IronPython.Compiler.Ast.Expression ret, Boolean allowGeneratorExpression) IronPython.Compiler.Parser.AddTrailers (IronPython.Compiler.Ast.Expression ret) IronPython.Compiler.Parser.ParsePower () IronPython.Compiler.Parser.ParseFactor () IronPython.Compiler.Parser.ParseExpr (Int32 precedence) IronPython.Compiler.Parser.ParseExpr () IronPython.Compiler.Parser.ParseComparison () IronPython.Compiler.Parser.ParseNotTest () IronPython.Compiler.Parser.ParseAndTest () IronPython.Compiler.Parser.ParseOrTest () IronPython.Compiler.Parser.ParseExpression () IronPython.Compiler.Parser.ParseRaiseStmt () IronPython.Compiler.Parser.ParseSmallStmt () IronPython.Compiler.Parser.ParseSimpleStmt () IronPython.Compiler.Parser.ParseStmt () IronPython.Compiler.Parser.ParseSuite () IronPython.Compiler.Parser.ParseClassOrFuncBody () IronPython.Compiler.Parser.ParseFuncDef () IronPython.Compiler.Parser.ParseStmt () IronPython.Compiler.Parser.ParseFileWorker (Boolean makeModule, Boolean returnValue) IronPython.Compiler.Parser.ParseFile (Boolean makeModule, Boolean returnValue) IronPython.Runtime.PythonContext.ParseAndBindAst (Microsoft.Scripting.Runtime.CompilerContext context) IronPython.Runtime.PythonContext.CompilePythonCode (Microsoft.Scripting.SourceUnit sourceUnit, Microsoft.Scripting.CompilerOptions options, Microsoft.Scripting.ErrorSink errorSink) IronPython.Runtime.PythonContext.GetScriptCode (Microsoft.Scripting.SourceUnit sourceCode, System.String moduleName, ModuleOptions options, IronPython.Compiler.CompilationMode mode) IronPython.Runtime.PythonContext.GetScriptCode (Microsoft.Scripting.SourceUnit sourceCode, System.String moduleName, ModuleOptions options) IronPython.Runtime.PythonContext.CompileModule (System.String fileName, System.String moduleName, Microsoft.Scripting.SourceUnit sourceCode, ModuleOptions options, Microsoft.Scripting.ScriptCode& scriptCode) IronPython.Runtime.PythonContext.CompileModule (System.String fileName, System.String moduleName, Microsoft.Scripting.SourceUnit sourceCode, ModuleOptions options) IronPython.Runtime.Importer.LoadFromSourceUnit (IronPython.Runtime.CodeContext context, Microsoft.Scripting.SourceUnit sourceCode, System.String name, System.String path) IronPython.Runtime.Importer.LoadModuleFromSource (IronPython.Runtime.CodeContext context, System.String name, System.String path) IronPython.Runtime.Importer.LoadPackageFromSource (IronPython.Runtime.CodeContext context, System.String name, System.String path) IronPython.Runtime.Importer.LoadFromDisk (IronPython.Runtime.CodeContext context, System.String name, System.String fullName, System.String str) IronPython.Runtime.Importer.ImportFromPathHook (IronPython.Runtime.CodeContext context, System.String name, System.String fullName, IronPython.Runtime.List path, System.Func`5 defaultLoader) IronPython.Runtime.Importer.ImportFromPath (IronPython.Runtime.CodeContext context, System.String name, System.String fullName, IronPython.Runtime.List path) IronPython.Runtime.Importer.ImportTopAbsolute (IronPython.Runtime.CodeContext context, System.String name) IronPython.Runtime.Importer.ImportModule (IronPython.Runtime.CodeContext context, System.Object globals, System.String modName, Boolean bottom, Int32 level) IronPython.Modules.Builtin.__import__ (IronPython.Runtime.CodeContext context, System.String name, System.Object globals, System.Object locals, System.Object fromlist, Int32 level) Microsoft.Scripting.Interpreter.FuncCallInstruction`7[IronPython.Runtime.CodeContext,System.String,System.Object,System.Object,System.Object,System.Int32,System.Object].Run (Microsoft.Scripting.Interpreter.InterpretedFrame frame) Microsoft.Scripting.Interpreter.Interpreter.Run (Microsoft.Scripting.Interpreter.InterpretedFrame frame)
該当のソースコード
Unity
1using UnityEngine; 2using System.Collections; 3using System.Collections.Generic; 4using IronPython.Hosting; 5using Microsoft.Scripting.Hosting; 6 7public class PythonLoader : MonoBehaviour 8{ 9 10 ScriptEngine engine; 11 ScriptScope scope; 12 ScriptSource source; 13 14 15 16 private void Start(){ 17 engine = Python.CreateEngine(); 18 scope = engine.CreateScope(); 19 source = engine.CreateScriptSourceFromFile(@"/Users/(ユーザー名)/desktop/HandDemo4/Assets/Using/script/pythontest.py"); 20 } 21 22 public void Pointer_Moved(int x,int y){ 23 24 //scope.SetVariable("inputX", 1+x*20); 25 //scope.SetVariable("inputY", 1+y*20); 26 27 //Pythonのソースを指定 28 source.Execute(scope); 29 30 } 31}
Python単独での動作の場合import pyautoguiより上の文がなくても動作を確認
python
1import sys 2 3#__future__を用いるため 4sys.path.append("/Users/(ユーザー名)/.pyenv/versions/3.7.1/lib/python3.7") 5#pyautoguiのある場所 6sys.path.append("/Users/(ユーザー名)/.pyenv/versions/3.7.1/lib/python3.7/site-packages") 7 8import pyautogui 9 10#本番ではinputX,inputYを用いる 11x=10 12y=5 13#(x,y)にマウスを移動 14pyautogui.moveTo(x,y,0)
試したこと
appendをexpendやinsertに変更
→No module named _future_やNo module named pyautoguiと出る。
ただしこちらも単独では問題なし。
補足情報(FW/ツールのバージョンなど)
単独実行で問題が発見できず、またUnityを経由させている都合上printなどが使えていないです。そちらについても良い方法ご存知の方いらっしゃいましたらご教授願います。
回答2件
あなたの回答
tips
プレビュー