質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

Unity3D

Unity3Dは、ゲームや対話式の3Dアプリケーション、トレーニングシュミレーション、そして医学的・建築学的な技術を可視化する、商業用の開発プラットフォームです。

MacOS(OSX)

MacOSとは、Appleの開発していたGUI(グラフィカルユーザーインターフェース)を採用したオペレーションシステム(OS)です。Macintoshと共に、市場に出てGUIの普及に大きく貢献しました。

Q&A

解決済

2回答

1550閲覧

IronPythonからの実行でのみ出るエラーの解決

sk36

総合スコア13

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

Unity3D

Unity3Dは、ゲームや対話式の3Dアプリケーション、トレーニングシュミレーション、そして医学的・建築学的な技術を可視化する、商業用の開発プラットフォームです。

MacOS(OSX)

MacOSとは、Appleの開発していたGUI(グラフィカルユーザーインターフェース)を採用したオペレーションシステム(OS)です。Macintoshと共に、市場に出てGUIの普及に大きく貢献しました。

0グッド

0クリップ

投稿2018/11/14 17:44

前提・実現したいこと

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などが使えていないです。そちらについても良い方法ご存知の方いらっしゃいましたらご教授願います。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

fiveHundred

2018/11/15 09:16

普通のPython(CPython)とIronPythonは全く別物なので、pyautoguiがIronPythonに対応しているかどうかすら怪しいですが、そこらへんは確認していますか?(Processクラスでpythonコマンドを実行したほうが楽なような気がします)
guest

回答2

0

自己解決

解決の目処が立ったためメモ

参考

リンク内容
リンク内容
リンク内容

やったこと

Python側をローカルにアプリケーションサーバーとして立てておき、メインアプリ側からhttpリクエストを送る。(一番上のリンクより引用)
受け渡しはunity側はWWWForm.Addfield()を使い、受け取るpython側はrequest.formで

Unity

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class test : MonoBehaviour { 6 7 // Use this for initialization 8 9 10 // Update is called once per frame 11 void Update () { 12 13 } 14 IEnumerator Start() 15 { 16 WWWForm form = new WWWForm(); 17 form.AddField("inputX", 8); 18 WWW www = new WWW("http://localhost:5000/test_api", form); 19 20 yield return www; 21 22 Debug.Log(www.text); 23 24 //processedTex = www.texture; 25 } 26} 27

python

1# python 2 3from flask import Flask,request 4 5app = Flask(__name__) 6 7 8 9 10@app.route("/test_api", methods=["POST"]) 11def test(): 12 13 x=request.form["inputX"] 14 15 return str(x) 16 17if __name__ == "__main__": 18 app.debug = True 19 app.run()

この例ではform.AddField("inputX", 8)で"inputX"として8を渡し、それをx=request.form["inputX"]で受け取っている
returnされたxを確認したらちゃんと8になってた(数字を変えればちゃんと変わった)ので、あとはtest()内部にやりたかった処理を入れるだけ

投稿2018/11/17 23:06

sk36

総合スコア13

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

「普通のPython(CPython)とIronPythonは全く別物なので(以下略)」
全くの認識不足でした。これまでこの分野はほとんど触らずに来たためかIronPythonをただの仲介プロセス(なんて言うんだろう?)と誤認していました。
processで外部に値を渡せるみたいなのでそちらの方法に切り替えようと思います。お騒がせしました。

投稿2018/11/15 12:58

sk36

総合スコア13

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問