justinfx / MayaSublime

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

REPL behaviour in MayaSublime #35

Open ESNV opened 5 years ago

ESNV commented 5 years ago

Hi,

Would it be possible to mimic in MayaSublime some of the REPL that exists in Maya's Script Editor?

For example, after first executing an assignment: foo = 'bar' then by highlighting and running only the "foo" part get: 'bar' as the output/result, instead of having to do: print(foo)

Cheers! Lalo.

justinfx commented 5 years ago

Sure. This can probably be implemented by using python's ast module, and parsing the selected code to see if its a single expression:

import ast

# ok: Wrap it in a   print (text)
ast.parse('x+1', mode='eval')

# not expression: raises exception
ast.parse('x=x+1', mode='eval')
SyntaxError: invalid syntax

# not expression: raises exception
ast.parse('x=0; x+=1; print x', mode='eval')
SyntaxError: invalid syntax