Skip to content

Commit

Permalink
Must think in Python 2.7.
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffHanna committed Dec 4, 2019
1 parent 9d9b50c commit ca14520
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 13 deletions.
15 changes: 7 additions & 8 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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:**
Expand All @@ -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;
}
Expand Down Expand Up @@ -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);
}
Expand Down
19 changes: 14 additions & 5 deletions hello_world.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
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( )



if __name__ == '__main__':
main( )

0 comments on commit ca14520

Please sign in to comment.