Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rework for Python 3 #15

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 24 additions & 13 deletions engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import inspect
import logging
import traceback
import six
from functools import wraps
from distutils.version import LooseVersion

Expand Down Expand Up @@ -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:
Expand All @@ -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
"""

Expand Down Expand Up @@ -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
Expand All @@ -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"
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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.
Expand All @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion python/tk_substancepainter/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
import application
from . import application
from .menu_generation import MenuGenerator
11 changes: 6 additions & 5 deletions python/tk_substancepainter/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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_()
4 changes: 4 additions & 0 deletions python/tk_substancepainter/menu_generation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
18 changes: 11 additions & 7 deletions startup.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import sys
import shutil
import hashlib
import six
import socket
from distutils.version import LooseVersion

Expand Down Expand Up @@ -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:
Expand All @@ -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
"""

Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions startup/bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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))
Expand All @@ -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
Expand Down