# Sean Dooley
# 22 February 2012
# http://www.smdooley.com
# http://smdooley.com/Extras/Python/FGScripts/fgwalkthrough.html
# http://smdooley.com/Extras/Python/FGScripts/fgscripts.html

# ----------------------------------------------------------------------------------------
# if you change the root name in fgBake.py, change this variable
fgRoot = 'fgmap_single_'
# ----------------------------------------------------------------------------------------

import sys, os

def fg_scene_ma(frame=1,sceneName="test.ma"):
	attempt = open(sceneName, "r")
	
	s = ""

	for line in attempt:
		if (line.find('setAttr ".fgfn" -type "string" ') != -1):
			#line=("    setAttr \".fgfn\" -type \"string\" \"fgmap_single_%03d.fgmap\";\n" % (frame) )
			line=('    setAttr ".fgfn" -type "string" "%s%03d.fgmap";\n' % (fgRoot, frame) )
		s += line
	attempt.close()

	fout = open(sceneName, "w")
	fout.write(s)
	fout.close()

def readMA(var, name):
	print 'Changing Maya ASCII file to use the FG map for frame: %s' % var
	fg_scene_ma( int(var), name )


# used the code directly found the code at: http://stackoverflow.com/questions/2765664/pass-in-a-value-into-python-class-through-command-line
class function_call(object):
    def __init__(self, sysArgs):
        try:
            self.function = None
            self.args = []
            self.modulePath = sysArgs[0]
            self.moduleDir, tail = os.path.split(self.modulePath)
            self.moduleName, ext = os.path.splitext(tail)
            __import__(self.moduleName)
            self.module = sys.modules[self.moduleName]
            if len(sysArgs) > 1:
                self.functionName = sysArgs[1]
                self.function = self.module.__dict__[self.functionName]
                self.args = sysArgs[2:]
        except Exception, e:
            sys.stderr.write("%s %s\n" % ("PythonCall#__init__", e))

    def execute(self):
        try:
            if self.function:
                self.function(*self.args)
        except Exception, e:
            sys.stderr.write("%s %s\n" % ("PythonCall#execute", e))

if __name__=="__main__":
    function_call(sys.argv).execute()