Advertisement
zhanj

Python To Mel Converter

May 17th, 2016
483
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.43 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # Python to Mel converter
  3. # Allows running of Python code by dragging into the Maya viewport
  4.  
  5. # Created 21/04/2015
  6. # Jason Dixon
  7. # internetimagery.com
  8.  
  9. # Usage
  10. # cat <inputFile> | py2mel.py > <outputFile>
  11.  
  12. # OR
  13. # py2mel.py -input <inputFile> -output <outputFile>
  14.  
  15. import io
  16. import tokenize
  17. import argparse
  18. import datetime
  19. import sys
  20.  
  21. #====================================================================
  22. # EDIT BEGIN
  23. #====================================================================
  24. # Change input to the location of your .py file
  25. # Change output to the location where you want the output file to be
  26. #====================================================================
  27.  
  28. input = "C://Users//Desktop//Tommy//input_file.py"
  29. output = "C://Users//Desktop//Tommy//output_file.mel"
  30.  
  31. #====================================================================
  32. # EDIT END
  33. #====================================================================
  34.  
  35. parser = argparse.ArgumentParser(
  36.     description="Convert Python file to Melscript (using python interpreter).",
  37.     epilog="Use either standard in and out, the -input -output flags or a combination of both. ie: cat INPUTFILE | py2mel.py > OUTPUTFILE")
  38. parser.add_argument("-i", "--input", help="Input file for processing.", type=argparse.FileType('r'))
  39. parser.add_argument("-o", "--output", help="Output file for processing.", type=argparse.FileType('w'))
  40. parser.add_argument("-s", "--shelf", help="Optional! Name to give to shelf icon if dropping causes shelf icon.", type=str)
  41. args = parser.parse_args()
  42.  
  43.  
  44. def remove_comments_and_docstrings(source):
  45.     """
  46.    http://stackoverflow.com/a/2962727
  47.    """
  48.     io_obj = io.StringIO(source)
  49.     out = ""
  50.     prev_toktype = tokenize.INDENT
  51.     last_lineno = -1
  52.     last_col = 0
  53.     for tok in tokenize.generate_tokens(io_obj.readline):
  54.         token_type = tok[0]
  55.         token_string = tok[1]
  56.         start_line, start_col = tok[2]
  57.         end_line, end_col = tok[3]
  58.         ltext = tok[4]
  59.         if start_line > last_lineno:
  60.             last_col = 0
  61.         if start_col > last_col:
  62.             out += (" " * (start_col - last_col))
  63.         if token_type == tokenize.COMMENT:
  64.             pass
  65.         elif token_type == tokenize.STRING:
  66.             if prev_toktype != tokenize.INDENT:
  67.                 if prev_toktype != tokenize.NEWLINE:
  68.                     if start_col > 0:
  69.                         out += token_string
  70.         else:
  71.             out += token_string
  72.         prev_toktype = token_type
  73.         last_col = end_col
  74.         last_lineno = end_line
  75.     return out
  76.  
  77.  
  78. def stringify(data):
  79.     return remove_comments_and_docstrings(data).replace("\\", "\\\\").replace("\"", "\\\"").replace("\n", r"\n")
  80.  
  81.  
  82. def version():
  83.     version = "v1.0.2"
  84.     return "py2mel.py %s, compiled %s\n\n" % (version, datetime.datetime.today())
  85.  
  86.  
  87. def python_interpret(data):
  88.     output = "// %s" % version()
  89.     output += "python(\"%s\");" % stringify(data)
  90.     return output
  91.  
  92.  
  93. def python_shelf(data, name):
  94.     code = "# %s" % version()
  95.     code += data
  96.     return "shelfButton -l \"%s\" -c \"%s\" -stp \"python\" -i \"daisyLarge.png\" -p `tabLayout -query -selectTab $gShelfTopLevel`;" % (name, stringify(code))
  97.  
  98. inp = open(input)
  99. out = open(output, 'w')
  100. data = python_shelf(inp.read(), args.shelf) if args.shelf else python_interpret(inp.read())
  101. out.write(data)
  102. inp.close()
  103. out.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement