justinfx / MayaSublime

Send selected Python and MEL code snippets from SublimeText to Maya via commandPort
MIT License
149 stars 38 forks source link

Passing on __file__ with execfile #10

Closed fredrikaverpil closed 11 years ago

fredrikaverpil commented 11 years ago

May I suggest the following amendment to PY_CMD_TEMPLATE?

   PY_CMD_TEMPLATE = "import traceback\n" \
                      "import __main__\n" \
                      "g = __main__.__dict__.copy()\n" \
                      "g['''__file__'''] = '''{2}'''\n" \
                      "try:\n" \
                        "\t{0}(r'''{1}''', g, g)\n" \
                      "except:\n" \
                        "\ttraceback.print_exc()"

And to mCmd:

mCmd = self.PY_CMD_TEMPLATE.format(execType, mCmd, file_path)

This will cause 'file' to get sent over to Maya and which will point to the script's file path, which is very useful. I'm using 'file' to import other scripts and image files which reside on paths relative to the script file getting executed.

fredrikaverpil commented 11 years ago

I made a pull request instead.