pyblish / pyblish-maya

Pyblish for Maya
GNU Lesser General Public License v3.0
66 stars 28 forks source link

Fix special case when empty scene #78

Closed 2-REC closed 3 years ago

2-REC commented 3 years ago

Issue

Hence the value set in the context for the current file is "." which is considered valid, though there is no current file.

mottosso commented 3 years ago

I wonder whether it would be safer to check even earlier? E.g.

if not cmds.file:
  # treat it as an unsaved scene

I can't think of any reason why normpath would return anything other than . when passed an empty string, but sometimes platform differences rear their ugly head. Maybe on Linux it returns a , and on Mac a 🪑. Who knows.

2-REC commented 3 years ago

Yes, you are right, I don't know either what is the behaviour in the other OSs and I cannot test it now.

I was thinking as well to do the check earlier, but I wanted to modify the code as little as possible. An alternative could be:

    def process(self, context):
        import os
        from maya import cmds

        """Inject the current working file"""
        current_file = cmds.file(sceneName=True, query=True)
        if current_file:
            # Maya returns forward-slashes by default
            normalised = os.path.normpath(current_file)
        else:
            normalised = ""

        context.set_data('currentFile', value=normalised)

        # For backwards compatibility
        context.set_data('current_file', value=normalised)

But the name of the variable "normalised" in the 'else' case doesn't make sense.

Or maybe the variable "normalised" could be removed and instead replace the value of "current_file" inline:

if current_file:
    # Maya returns forward-slashes by default
    current_file = os.path.normpath(current_file)

(avoiding the need for the 'else' clause)

mottosso commented 3 years ago

Or maybe the variable "normalised" could be removed and instead replace the value of "current_file" inline:

Yes, that makes sense. The normalised value is mostly a convenience, to turn slashes from back to forward or vice versa depending on the platform.

mottosso commented 3 years ago

Looks great, well done!