######################################################################
#
#       CloneFigure.py
#
#       Copyright (c) 2003 C. Cigas
#
#       craig@castironflamingo.com
#
#       Create a new instance of the selected figure identical to the
#       original. Handle the EMC Fixer load sequence if present.
#
#       Thanks to Charles Taylor for making EMC Fixer available.
#
import poser, os
from string import replace

global filePath
#
#       Handle path conventions for Macs
#
def macifyPath(szPath):
        if os.name == "mac":
                return(replace(szPath, "\\", ":"))
        else:
                return(szPath)
#
#       Clear off any temporary files
#
def purgeTempFiles():
        try:
                os.remove(macifyPath(filePath + ".cr2"))
        except:
                pass
        try:
                os.remove(macifyPath(filePath + ".png"))
        except:
                pass
        try:
                os.remove(macifyPath(filePath + "EMC.cr2"))
        except:
                pass
        try:
                os.remove(macifyPath(filePath + "EMC.png"))
        except:
                pass
                
filePath = macifyPath("Runtime\\Libraries\\clone")
curFig = poser.Scene().CurrentFigure()
if curFig:
        purgeTempFiles()
        #
        #       Check for EMC Fixer
        #
        fixFig = None
        for fig in poser.Scene().Figures():
                if fig.Name() == "EMC Fixer":
                        fixFig = fig
                        break
        #
        #       If EMC Fixer is found, save a copy then load that
        #       back in proper sequence
        #
        if fixFig:
                poser.Scene().SelectFigure(fixFig)
                poser.Scene().SaveLibraryFigure("Runtime:Libraries:cloneEMC.cr2")
                poser.Scene().SelectFigure(curFig)
                poser.Scene().SaveLibraryFigure("Runtime:Libraries:clone.cr2")
                poser.Scene().LoadLibraryFigure("Runtime:Libraries:cloneEMC.cr2")
                poser.Scene().SelectActor(fixFig.Actor("Body"))
                poser.Scene().LoadLibraryFigure("Runtime:Libraries:clone.cr2")
        else:
                poser.Scene().SelectFigure(curFig)
                poser.Scene().SaveLibraryFigure("Runtime:Libraries:clone.cr2")
                poser.Scene().LoadLibraryFigure("Runtime:Libraries:clone.cr2")
        purgeTempFiles()
        #
        #       Select the body actor just to be tidy.
        #
        try:
                poser.Scene().SelectActor(poser.Scene().CurrentFigure().Actor("Body"))
        except:
                pass
