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

Fix special case when empty scene #78

Merged
merged 2 commits into from
Jul 26, 2021

Conversation

2-REC
Copy link
Contributor

@2-REC 2-REC commented Jul 26, 2021

Issue

  • Maya's 'cmds.file' returns the empty string "" when the scene hasn't been saved yet (new "untitled" scene).
  • Python's 'normpath' function returns the string "." when the provided path is empty ("").

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

@mottosso
Copy link
Member

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
Copy link
Contributor Author

2-REC commented Jul 26, 2021

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
Copy link
Member

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
Copy link
Member

Looks great, well done!

@mottosso mottosso merged commit cabad4c into pyblish:master Jul 26, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants