Skip to content

Instantly share code, notes, and snippets.

@whaison
Created February 17, 2017 12:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save whaison/ee9d8343623c5f2d16f3d0f9eda66285 to your computer and use it in GitHub Desktop.
Save whaison/ee9d8343623c5f2d16f3d0f9eda66285 to your computer and use it in GitHub Desktop.
MELFiletoPythonFile_UTF-8_v00135_regex.py
# -*- coding: cp932 -*-
import pymel.core as pm
import pymel.tools.mel2py as mel2py
import re
import math
import random
melCmd = """
global proc SaveFrText()
{
select `ls -type animCurve`;
int $startFrameInt =`findKeyframe -which first`;
int $nextFrameInt =`findKeyframe -timeSlider -which next`;
int $endFrameInt = `findKeyframe -which last`;
$startFrameStr=$startFrameInt+"";
$nextFrameStr=$nextFrameInt+"";
$endFrameStr=$endFrameInt+"";
print("$startFrameStr= "+$startFrameStr );
print("$nextFrameStr= "+$nextFrameStr );
print("$endFrameStr= "+$endFrameStr );
global string $saveDir;
global string $motionName;
print("$startFrameStr= "+$startFrameStr );
print("$nextFrameStr= "+$nextFrameStr );
print("$endFrameStr= "+$endFrameStr );
string $startToEnd=$startFrameStr+":"+$endFrameStr;
string $outputTxt="";
$outputTxt=$outputTxt+$motionName+","+$startFrameStr+","+$endFrameStr+",OnMotionStart"+",0.0,OnMotionEnd"+",1.0,";
print("$saveDir= "+$saveDir);
string $saveTxtPath=$saveDir+"anims.txt";
$fileExCheckHead = `filetest -f $saveTxtPath`;
if ($fileExCheckHead == 0)
{
print("file is None Make File $saveTxtPath= "+$saveTxtPath);
system("echo "+$outputTxt+"> "+$saveTxtPath);
}else{
print("file is exsist over write $saveTxtPath= "+$saveTxtPath);
system("echo "+$outputTxt+" >> "+$saveTxtPath);
}
}
"""
print "----------------mel code replace----------------------------"
#melCmd = melCmd.replace('$writable_bool','`file -q -writable`')
melCmd = melCmd.replace('"\n--', '"--')
melCmd = melCmd.replace('+"\n"', '')
melCmd = melCmd.replace('\n"', '"')
print("melCmd="+melCmd)
print "------------------------------------------------"
# mel2py.mel2pyStr
pyCmd = mel2py.mel2pyStr(melCmd,pymelNamespace='pm')
pyFixed_Dest = ""
#pyFixed_Dest = pyFixed_Dest + "# -*- coding: cp932 -*- \n"
pyFixed_Dest = pyFixed_Dest + "# -*- coding: utf-8 -*- \n"
#pyFixed_Dest = pyFixed_Dest + "# -*- coding: shift-jis -*- \n"
pyFixed_Dest = pyFixed_Dest + "from maya import cmds \n"
pyFixed_Dest = pyFixed_Dest + "from python import Debug \n"
pyFixed = pyCmd.replace("pymel.all","pymel.core")
pyFixed = pyFixed.replace("pm.pm.cmds","cmds")
pyFixed_Dest = pyFixed_Dest + pyFixed+"\n"
print "----------------------python sorce replace----------------------"
pyFixed_Dest = pyFixed_Dest.replace('import pymel.core as pm', 'import pymel.core as pm \nDebug=Debug.Debug')
pyFixed_Dest = pyFixed_Dest.replace('pm.file', 'cmds.file')
pyFixed_Dest = pyFixed_Dest.replace('pm.ls', 'cmds.ls')
pyFixed_Dest = pyFixed_Dest.replace('pm.select', 'cmds.select')
pyFixed_Dest = pyFixed_Dest.replace('pm.internalVar', 'cmds.internalVar')
pyFixed_Dest = pyFixed_Dest.replace('pm.keyframe', 'cmds.keyframe')
pyFixed_Dest = pyFixed_Dest.replace('pm.workspace', 'cmds.workspace')
pyFixed_Dest = pyFixed_Dest.replace('pm.getFileList', 'cmds.getFileList')
pyFixed_Dest = pyFixed_Dest.replace('=="1"', '==1')
pyFixed_Dest = pyFixed_Dest.replace('== "1"', '== 1')
pyFixed_Dest = pyFixed_Dest.replace('print("', 'print(u"')
pyFixed_Dest = pyFixed_Dest.replace('print "', 'print u"')
pyFixed_Dest = pyFixed_Dest.replace('pm.flushUndo()', 'cmds.flushUndo()')
pyFixed_Dest = pyFixed_Dest.replace('pm.parent', 'cmds.parent')
pyFixed_Dest = pyFixed_Dest.replace('pm.objExists', 'cmds.objExists')
pyFixed_Dest = pyFixed_Dest.replace('pm.rename', 'cmds.rename')
pyFixed_Dest = pyFixed_Dest.replace('pm.playbackOptions', 'cmds.playbackOptions')
pyFixed_Dest = pyFixed_Dest.replace('pm.mel.DebugLog', 'self.DebugLog')
pyFixed_Dest = pyFixed_Dest.replace('pm.currentTime', 'cmds.currentTime')
pyFixed_Dest = pyFixed_Dest.replace('pm.setKeyframe', 'cmds.setKeyframe')
pyFixed_Dest = pyFixed_Dest.replace('pm.setAttr', 'cmds.setAttr')
pyFixed_Dest = pyFixed_Dest.replace('pm.joint', 'cmds.joint')
pyFixed_Dest = pyFixed_Dest.replace('pm.expression', 'cmds.expression')
pyFixed_Dest = pyFixed_Dest.replace('pm.findKeyframe', 'cmds.findKeyframe')
#pyFixed_Dest = pyFixed_Dest.replace('maya.mel.eval("listRelatives -pa -ad `ls -sl -l`"', 'cmds.listRelatives(path=True, allDescendents =True,cmds.ls(selection=True, long=True))'))
#for Class
pyFixed_Dest = pyFixed_Dest.replace('():', '(self):')
pyFixed_Dest = pyFixed_Dest.replace('():', '(self):')
regex = r'print+'
#pattern = re.compile(regex)
#matchObj = pattern.match(pyFixed_Dest)
#matchObj = re.search(r'[a-z]+', pyFixed_Dest)
def repl(m):
inner_word1 = list(m.group(1)) #prin(\w\W) "prin" "t" " " real ['t', ' ']
#print("inner_word1= "+str(inner_word1))
inner_word2 = list(m.group(2)) #(.*) something strings "----1-----" real ['u', '"', '-', '-', '-', '-', '1', '-', '-', '-', '-']
#print("inner_word2= "+str(inner_word2))
inner_word3 = list(m.group(3))
#inner_word4 = list(m.group(4)) #(\") double cout real ['"']
print("inner_word123= "+str(inner_word1)+str(inner_word2)+str(inner_word3)+""+str(list(m.group(4)))+str(list(m.group(5)))+"")
#print("inner_word2= "+str(inner_word1)+str(inner_word2)+str(inner_word3))
#random.shuffle(inner_word)
#back= m.group(1) + "".join(inner_word) + m.group(3)
back="00000"+ str(m.end())
back=m.group(1) +m.group(2)+"("+m.group(3)+m.group(4)+m.group(5)+")" #for print
back=m.group(1) +"Debug.Log"+"("+m.group(3)+m.group(4)+m.group(5)+")" #for Debug.Log Debug Class is https://gist.github.com/whaison/fc625381dce126ca5d88d1f914ce89f0
return back
#text = "Professor Abdolmalek, please report your absences promptly."
text = """
print "----1----"
print "----2----"
print "----3----"
print "----4----"
print "----5----"
print "----6----"
print "----7----"
"""
#resub=re.sub(r"(\w)(\w+)(\w)", repl, text)
#resub=re.sub(r'(prin\w)', repl, text)
resub=re.sub(r'(.*)(prin\w\W)(.*)(\")(.*)', repl, text)
print("resub --------------------output start")
print resub
#print resub[:m.start()]
#print resub[m.end():]
print("resub --------------------output end")
pyFixed_Dest=re.sub(r'(.*)(prin\w\W)(.*)(\")(.*)', repl, pyFixed_Dest)
print("resub pyFixed_Dest --------------------output start")
print pyFixed_Dest
#print resub[:m.start()]
#print resub[m.end():]
print("resub pyFixed_Dest --------------------output end")
print("search --------------------output start")
email = "tony@tiremove_thisger.net"
m = re.search(r'print*', text)
print text[:m.start()] +"print("+ text[m.end():]
print("search --------------------output end")
print "------------------------------------------------"
print pyFixed_Dest
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment