From 8d55868dbf8acd56c8c0dab1bf094bfd68065dea Mon Sep 17 00:00:00 2001 From: Mois Moshev Date: Tue, 15 Jun 2021 13:29:03 +0300 Subject: [PATCH] Rework for Python 3 --- engine.py | 37 ++++++++++++------- python/tk_substancepainter/__init__.py | 2 +- python/tk_substancepainter/application.py | 11 +++--- python/tk_substancepainter/menu_generation.py | 4 ++ startup.py | 18 +++++---- startup/bootstrap.py | 8 ++-- 6 files changed, 50 insertions(+), 30 deletions(-) diff --git a/engine.py b/engine.py index bd2bd75..575e6f4 100644 --- a/engine.py +++ b/engine.py @@ -18,6 +18,7 @@ import inspect import logging import traceback +import six from functools import wraps from distutils.version import LooseVersion @@ -48,7 +49,7 @@ def to_new_version_system(version): https://docs.substance3d.com/spdoc/version-2020-1-6-1-0-194216357.html The way we support this new version system is to use LooseVersion for - version comparisons. We modify the major version if the version is higher + version comparisons. We modify the major version if the version is higher than 2017.1.0 for the version to become in the style of 6.1, by literally subtracting 2014 to the major version component. This leaves us always with a predictable version system: @@ -62,9 +63,9 @@ def to_new_version_system(version): according to: https://docs.substance3d.com/spdoc/all-changes-188973073.html - Note that this change means that the LooseVersion is good for comparisons - but NEVER for printing, it would simply print the same version as - LooseVersion does not support rebuilding of the version string from it's + Note that this change means that the LooseVersion is good for comparisons + but NEVER for printing, it would simply print the same version as + LooseVersion does not support rebuilding of the version string from it's components """ @@ -139,7 +140,7 @@ def refresh_engine(scene_name, prev_context): # and construct the new context for this path: tk = tank.tank_from_path(new_path) ctx = tk.context_from_path(new_path, prev_context) - except tank.TankError, e: + except tank.TankError as e: try: # could not detect context from path, will use the project context # for menus if it exists @@ -154,7 +155,7 @@ def refresh_engine(scene_name, prev_context): ) engine.show_warning(message) - except tank.TankError, e: + except tank.TankError as e: (exc_type, exc_value, exc_traceback) = sys.exc_info() message = "" message += "Shotgun Substance Painter Engine cannot be started:.\n" @@ -336,7 +337,7 @@ def context_change_allowed(self): @property def host_info(self): """ - :returns: A dictionary with information about the application hosting + :returns: A dictionary with information about the application hosting his engine. The returned dictionary is of the following form on success: @@ -585,8 +586,8 @@ def post_app_init(self): def post_context_change(self, old_context, new_context): """ - Runs after a context change. The Substance Painter event watching will - be stopped and new callbacks registered containing the new context + Runs after a context change. The Substance Painter event watching will + be stopped and new callbacks registered containing the new context information. :param old_context: The context being changed away from. @@ -605,14 +606,19 @@ def post_context_change(self, old_context, new_context): def _run_app_instance_commands(self): """ - Runs the series of app instance commands listed in the + Runs the series of app instance commands listed in the 'run_at_startup' setting of the environment configuration yaml file. """ # Build a dictionary mapping app instance names to dictionaries of # commands they registered with the engine. app_instance_commands = {} - for (cmd_name, value) in self.commands.iteritems(): + if six.PY2: + command_iter = self.commands.iteritems() + else: + command_iter = self.commands.items() + + for (cmd_name, value) in command_iter: app_instance = value["properties"].get("app") if app_instance: # Add entry 'command name: command function' to the command @@ -644,7 +650,12 @@ def _run_app_instance_commands(self): else: if not setting_cmd_name: # Run all commands of the given app instance. - for (cmd_name, command_function) in cmd_dict.iteritems(): + if six.PY2: + command_iter = cmd_dict.iteritems() + else: + command_iter = cmd_dict.items() + + for (cmd_name, command_function) in command_iter: msg = ( "%s startup running app '%s' command '%s'.", self.name, @@ -756,7 +767,7 @@ def close_windows(self): # the original dialog list. self.logger.debug("Closing dialog %s.", dialog_window_title) dialog.close() - except Exception, exception: + except Exception as exception: traceback.print_exc() self.logger.error( "Cannot close dialog %s: %s", dialog_window_title, exception diff --git a/python/tk_substancepainter/__init__.py b/python/tk_substancepainter/__init__.py index 7f12a24..5143460 100644 --- a/python/tk_substancepainter/__init__.py +++ b/python/tk_substancepainter/__init__.py @@ -1,2 +1,2 @@ -import application +from . import application from .menu_generation import MenuGenerator diff --git a/python/tk_substancepainter/application.py b/python/tk_substancepainter/application.py index fdb941f..f995f35 100644 --- a/python/tk_substancepainter/application.py +++ b/python/tk_substancepainter/application.py @@ -134,13 +134,13 @@ def on_text_message_received(self, message): message_id = jsonData.get("id") # requesting data - if jsonData.has_key("method"): + if "method" in jsonData: # self.log_debug("client: request detected: %s" % (message)) method = jsonData.get("method") params = jsonData.get("params") self.engine.process_request(method, **params) - if jsonData.has_key("result"): + if "result" in jsonData: # self.log_debug("client: result detected: %s" % (message)) if message_id in self.callbacks: # self.log_debug( @@ -212,6 +212,7 @@ def get_current_project_path(self): return path def need_saving(self): + path = self.send_and_receive("GET_CURRENT_PROJECT_PATH") result = self.send_and_receive("NEEDS_SAVING", path=path) return result @@ -333,11 +334,11 @@ def toggle_debug_logging(self, enabled): if __name__ == "__main__": global client - app = QApplication(sys.argv) + app = QCoreApplication(sys.argv) client = Client(app) - version = get_application_version(client) + version = client.get_application_version() client.log_debug("application_version: %s" % version) - version2 = get_application_version(client) + version2 = client.get_application_version() client.log_debug("application_version2: %s" % version2) app.exec_() diff --git a/python/tk_substancepainter/menu_generation.py b/python/tk_substancepainter/menu_generation.py index e438910..b3fdb56 100644 --- a/python/tk_substancepainter/menu_generation.py +++ b/python/tk_substancepainter/menu_generation.py @@ -286,6 +286,10 @@ def get_documentation_url_str(self): app = self.properties["app"] doc_url = app.documentation_url # deal with nuke's inability to handle unicode. #fail + import six + if six.PY3: + from pyreadline.py3k_compat import unicode + if doc_url.__class__ == unicode: doc_url = unicodedata.normalize("NFKD", doc_url).encode( "ascii", "ignore" diff --git a/startup.py b/startup.py index bd8be6a..b42a42d 100644 --- a/startup.py +++ b/startup.py @@ -12,6 +12,7 @@ import sys import shutil import hashlib +import six import socket from distutils.version import LooseVersion @@ -44,7 +45,7 @@ def to_new_version_system(version): https://docs.substance3d.com/spdoc/version-2020-1-6-1-0-194216357.html The way we support this new version system is to use LooseVersion for - version comparisons. We modify the major version if the version is higher + version comparisons. We modify the major version if the version is higher than 2017.1.0 for the version to become in the style of 6.1, by literally subtracting 2014 to the major version component. This leaves us always with a predictable version system: @@ -58,9 +59,9 @@ def to_new_version_system(version): according to: https://docs.substance3d.com/spdoc/all-changes-188973073.html - Note that this change means that the LooseVersion is good for comparisons - but NEVER for printing, it would simply print the same version as - LooseVersion does not support rebuilding of the version string from it's + Note that this change means that the LooseVersion is good for comparisons + but NEVER for printing, it would simply print the same version as + LooseVersion does not support rebuilding of the version string from it's components """ @@ -275,7 +276,10 @@ def prepare_launch(self, exec_path, args, file_to_open=None): # Only the startup script, the location of python and potentially the file to open # are needed. args = "" - args = ["%s=%s" % (k, v) for k, v in required_env.iteritems()] + if six.PY2: + args = ["%s=%s" % (k, v) for k, v in required_env.iteritems()] + else: + args = ["%s=%s" % (k, v) for k, v in required_env.items()] args = '"&%s"' % "&".join(args) logger.info("running %s" % args) @@ -338,8 +342,8 @@ def _is_supported(self, sw_version): method. To check if the version is supported: - - First we make an exception for cases were we cannot retrieve the + + First we make an exception for cases were we cannot retrieve the version number, we accept the software as valid. Second, checks against the minimum supported version. If the diff --git a/startup/bootstrap.py b/startup/bootstrap.py index b05592a..740a45a 100644 --- a/startup/bootstrap.py +++ b/startup/bootstrap.py @@ -65,7 +65,7 @@ def start_toolkit_classic(): try: # Deserialize the environment context context = sgtk.context.deserialize(env_context) - except Exception, e: + except Exception as e: msg = ( "Shotgun: Could not create context! Shotgun Pipeline Toolkit" " will be disabled. Details: %s" % e @@ -78,12 +78,12 @@ def start_toolkit_classic(): try: # Start up the toolkit engine from the environment data logger.debug( - "Launching engine instance '%s' for context %s" % (env_engine, env_context) + "Launching engine instance '{}' for context {}".format(env_engine, env_context) ) engine = sgtk.platform.start_engine(env_engine, context.sgtk, context) logger.debug("Current engine '%s'" % sgtk.platform.current_engine()) - except Exception, e: + except Exception as e: msg = "Shotgun: Could not start engine. Details: %s" % e etype, value, tb = sys.exc_info() msg += "".join(traceback.format_exception(etype, value, tb)) @@ -100,7 +100,7 @@ def start_toolkit(): # Verify sgtk can be loaded. try: import sgtk - except Exception, e: + except Exception as e: msg = "Shotgun: Could not import sgtk! Disabling for now: %s" % e print(msg) return