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 the __main__.__dict__ #11

Closed fredrikaverpil closed 11 years ago

fredrikaverpil commented 11 years ago

When sending a file to execfile() I also pass on 'file' which is great. Now my scripts can import or link to other files which locations are defined relative to the script file's file path ('file').

I wasn't sure on how to pull the latest file from your repo and go from there, so I copy-pasted the contents of your current MayaSublime.py and edited the two parts accordingly (which kind of messed up the diff that is shown here when typing this request) and made a commit. Seems like now GitHub wants to make two commits to your code – which wasn't what I intended.. This is all I was wishing to add to your repo:

   PY_CMD_TEMPLATE = textwrap.dedent('''
        import traceback
        import __main__
        g = __main__.__dict__.copy()    # added
        g['__file__'] = '{2}'           # added
        try:
            {0}({1!r}, g, g)            # changed
        except:
            traceback.print_exc() 
    ''')

and...

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

I'll close the issue I made regarding this.

fredrikaverpil commented 11 years ago

Please ignore this pull request. I'll redo it.