######################################################################
#
#       CloneProp1a.py
#
#       Copyright (c) 2003 C. Cigas
#
#       craig@castironflamingo.com
#
#       Create a new instance of the selected prop identical to the
#       original.
#
#       The cheat we used with figures doesn't work here so we have
#       to hack it with something undocumented. This may not work in
#       future releases.
#
import poser, os
from string import *
#
#       Handle path conventions for Macs
#
def macifyPath(szPath):

        if(os.name == "mac"):
                return(replace(szPath, "\\", ":"))
        else:
                return(szPath)

channel = {}
channel["OriginX"] = [ "xOffsetA", "originX", "GetStringRes(1028,44)" ]
channel["OriginY"] = [ "yOffsetA", "originY", "GetStringRes(1028,45)" ]
channel["OriginZ"] = [ "zOffsetA", "originZ", "GetStringRes(1028,46)" ]
channel["Scale"] = [ "propagatingScale", "scale", "GetStringRes(1028,5)" ]
channel["xScale"] = [ "propagatingScaleX", "xScale", "GetStringRes(1028,6)" ]
channel["yScale"] = [ "propagatingScaleY", "yScale", "GetStringRes(1028,7)" ]
channel["zScale"] = [ "propagatingScaleZ", "zScale", "GetStringRes(1028,8)" ]
channel["yRotate"] = [ "rotateY", "yrot", "yrot" ]
channel["zRotate"] = [ "rotateZ", "zrot", "zrot" ]
channel["xRotate"] = [ "rotateX", "xrot", "xrot" ]
channel["xTran"] = [ "translateX", "xtran", "GetStringRes(1028,12)" ]
channel["yTran"] = [ "translateY", "ytran", "GetStringRes(1028,13)" ]
channel["zTran"] = [ "translateZ", "ztran", "GetStringRes(1028,14)" ]
channel["xOffB"] = [ "xOffsetB", "xOffB", "xOffB" ]
channel["yOffB"] = [ "yOffsetB", "yOffB", "yOffB" ]
channel["zOffB"] = [ "zOffsetB", "zOffB", "zOffB" ]

def SpewChannelBlock(hFile, act):
        for parm in act.Parameters():
                try:
                        res = "\t"*2 + channel[parm.Name()][0] + " " + channel[parm.Name()][1] + "\n"
                        res = res + "\t"*3 + "{\n"
                        res = res + "\t"*3 + "name " + channel[parm.Name()][2] + "\n"
                        res = res + "\t"*3 + "initValue 0\n"
                        res = res + "\t"*3 + "hidden " + str(parm.Hidden()) + "\n"
                        res = res + "\t"*3 + "forceLimits " + str(parm.ApplyLimits()) + "\n"
                        res = res + "\t"*3 + "min " + str(parm.MinValue()) + "\n"
                        res = res + "\t"*3 + "max " + str(parm.MaxValue()) + "\n"
                        res = res + "\t"*3 + "trackingScale " + str(parm.Sensitivity()) + "\n"
                        res = res + "\t"*3 + "keys\n"
                        res = res + "\t"*4 + "{\n"
                        res = res + "\t"*4 + "static 1\n"
                        res = res + "\t"*4 + "k 0 0\n"
                        res = res + "\t"*4 + "}\n"
                        res = res + "\t"*3 + "interpStyleLocked 0\n"
                        res = res + "\t"*3 + "staticValue " + str(parm.Value()) + "\n"
                        res = res + "\t"*3 + "}\n"
                        hFile.write(res)
                except:
                        pass
#
#       Export callback
#
def exportCallback(expAct):
        if expAct.Name() == oldAct.Name():
                return 1
        else:
                return 0

global oldAct

oldAct = poser.Scene().CurrentActor()
if oldAct.IsProp():
        filePath = os.path.dirname(poser.AppLocation()) + macifyPath("\\Runtime\\Libraries\\Props\\" + oldAct.Name())
        #
        #       Set the export callback (undocumented)
        #
        ImEx = poser.Scene().ImExporter()
        ExOp = ImEx.ExportOptions("obj", None)
        ImEx.Export("obj", None, filePath + ".obj", ExOp, exportCallback)
        os.remove(filePath + ".obj")
        os.remove(filePath + ".mtl")
        #
        #       Save the prop
        #
        poser.Scene().SaveLibraryProp(filePath + ".pp2")
        #
        #       Graft in the missing DNA (channel block)
        #
        hFileIn  = open(filePath + ".pp2","r")
        try:
                os.remove(filePath + ".txt")
        except:
                pass
        hFileOut = open(filePath + ".txt","w")
        bFound = 0
        while 1:
                szLine = hFileIn.readline()
                if not szLine:
                        break
                hFileOut.write(szLine)
                if szLine == "\tchannels\n":
                        bFound = 1
                        hFileOut.write(hFileIn.readline())
                        SpewChannelBlock(hFileOut, oldAct)
                        hFileOut.write(hFileIn.readline())
        hFileIn.close()
        hFileOut.close()
        #
        #       Load the clone, if the graft didn't take just pretend
        #       nothing happened.
        #
        if bFound:
                os.remove(filePath + ".pp2")
                os.rename(filePath + ".txt", filePath + ".pp2")
                poser.Scene().LoadLibraryProp(filePath + ".pp2")
        os.remove(filePath + ".pp2")
        os.remove(filePath + ".png")
