From ca14520b251e74a7c0cf9954c6b6bf4572310452 Mon Sep 17 00:00:00 2001 From: Jeff Hanna Date: Wed, 4 Dec 2019 09:23:20 -0600 Subject: [PATCH] Must think in Python 2.7. --- Program.cs | 15 +++++++-------- hello_world.py | 19 ++++++++++++++----- 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/Program.cs b/Program.cs index b4a108e..f03c103 100644 --- a/Program.cs +++ b/Program.cs @@ -218,19 +218,18 @@ static string make_python_import_reload_wrapper(string python_filepath) /// Jeff Hanna, jeff@techart.online, November 22, 2019 string mod_name = Path.GetFileNameWithoutExtension(python_filepath); - string reload_cmd = String.Format("import contextlib\nimport importlib\nwith contextlib.suppress(NameError):\n\timportlib.reload({0})", - mod_name); - var reload_wrapper_filepath = System.IO.Path.Combine(System.IO.Path.GetTempPath(), "import_reload.py"); + string reload_cmd = String.Format("import imp\ntry:\n\timp.reload({0})\nexcept:\n\tpass", mod_name); + var reload_wrapper_filepath = System.IO.Path.Combine(System.IO.Path.GetTempPath(), "import_reload.py"); System.IO.File.WriteAllText(reload_wrapper_filepath, reload_cmd); - return reload_wrapper_filepath; + return reload_wrapper_filepath; } - + static string make_python_wrapper(string python_filepath) { /// For the python file being executed in 3ds Max this script writes a Maxscript wrapper file - /// that can be called to reimport that Python module so that the in-memory version is updated with + /// that can be called to reimport that Python module so that the in-memory version is updated with /// any changes made between script executions. /// /// **Arguments:** @@ -252,7 +251,7 @@ static string make_python_wrapper(string python_filepath) string reload_filepath = make_python_import_reload_wrapper( python_filepath ); string cmd = String.Format("python.ExecuteFile(\"{0}\");python.ExecuteFile(\"{1}\")", reload_filepath, python_filepath); var wrapper_filepath = System.IO.Path.Combine(System.IO.Path.GetTempPath(), "maxscript_python_wrapper.ms"); - System.IO.File.WriteAllText(wrapper_filepath, cmd); + System.IO.File.WriteAllText(wrapper_filepath, cmd); return wrapper_filepath; } @@ -291,7 +290,7 @@ static string mxs_try_catch_errors_cmd(string filepath) /// For python files, use passed filepath for location msg, no pos or line available location = String.Format("\"Error; filename: {0}\"", filepath); - /// Pass thru python.ExecuteFile() + /// Pass thru python.ExecuteFile() string reload_filepath = make_python_import_reload_wrapper( filepath ); run_cmd = String.Format("python.ExecuteFile(\"{0}\");python.ExecuteFile(\"{1}\")", reload_filepath, filepath); } diff --git a/hello_world.py b/hello_world.py index 3b52c83..ff9fa60 100644 --- a/hello_world.py +++ b/hello_world.py @@ -1,12 +1,22 @@ from pymxs import runtime +try: + import MaxPlus +except( ImportError ): + import shiboken2 + MaxPlus = None + from PySide2.QtWidgets import QMainWindow, QMessageBox, QWidget -import shiboken2 + def main( ): - main_window_widget = QWidget.find( runtime.windows.getMAXHWND( ) ) - main_window = shiboken2.wrapInstance( shiboken2.getCppPointer( main_window_widget )[ 0 ], QMainWindow ) - msg_box = QMessageBox( main_window ) + if MaxPlus: + main_window = MaxPlus.GetQMaxMainWindow( ) + else: + main_window_widget = QWidget.find( int( runtime.windows.getMAXHWND( ) ) ) + main_window = shiboken2.wrapInstance( shiboken2.getCppPointer( main_window_widget )[ 0 ], QMainWindow ) + + msg_box = QMessageBox( main_window ) msg_box.setText( 'Hello World!' ) msg_box.show( ) @@ -14,4 +24,3 @@ def main( ): if __name__ == '__main__': main( ) - \ No newline at end of file