
class FrawExporter(KellerNukePlugin):

    def configurePlugin(self):
        self.menu =  nuke.menu("Animation")
        self.menu.addCommand("Export fraw...", "frawExporter.export_fraw()")
    


    def unconfigurePlugin(self):
        self.menu.removeItem("Export fraw...")
        pass
    



class ExportPanel(nukescripts.PythonPanel):
    def __init__(self):
        nukescripts.PythonPanel.__init__(self, 'Export fraw...')
        

        self.knob_startframe = nuke.Int_Knob("Start Frame")
        self.knob_startframe.setValue(nuke.Root()["first_frame"].getValue())
        self.addKnob(self.knob_startframe)

        self.knob_endframe = nuke.Int_Knob("End Frame")
        self.knob_endframe.setValue(nuke.Root()["last_frame"].getValue())
        self.addKnob(self.knob_endframe)
        
        self.knob_file = nuke.File_Knob("File")
        self.addKnob(self.knob_file)


def _writeFile(contents, file):
    s = "%s 2" % str(len(contents))
    
    for cont in contents:
        s = s + "\n%.5f %.5f 0.00000 0.00000" % (cont[0], cont[1])

    #print s
    f = open(file, "w")
    f.write(s)
    f.close()

def export_fraw():
   
    
    knob =  nuke.thisKnob()
    
    p = ExportPanel()
    ret = p.showModalDialog()
    
    
    start = p.knob_startframe.getValue()
    end = p.knob_endframe.getValue()
    
    fps = float(nuke.Root()["fps"].getValue())
    
    filename = p.knob_file.getValue()
    
    if isinstance(knob, nuke.Array_Knob):
        
        for comp in range(knob.width()):
            
            contents = list()
            for i in range(start, end+1):
            
                time = float(i)/fps
                chval = knob.valueAt(int(i))[comp]
                contents.append( (time,chval) )
                
        
        
        
        
        
            _writeFile(contents, os.path.join( os.path.dirname(filename),str(comp)+ "_" + os.path.basename(filename)))

    else:
        contents = list()
        for i in range(start, end+1):
        
            time = float(i)/fps
            chval = knob.valueAt(int(i))
            contents.append((time,chval))
        
        _writeFile(contents, filename)

        
    #file = p.knob_file.getValue()
    
    #f = open(file, "w")
    #f.write(s)
    #f.close()