Hi all,

i try to find a way to read a txt file based on the script name 
os.path.dirname(nuke.Root().name()) on ScriptLoad.

I have a panel with a String_Knob. If the knob is changed a txt file is written 
to the script path. With a Load Button i can load the txt file back into the 
string but i want nuke to do this on start without a button.

Its working with a txt file in the plugin path. But i think its because of how 
nuke starts - init and menu.py first?!

For me, who is new to python it must be something like this, but it doesnt work:


class TextFile(nukescripts.PythonPanel):
    def __init__(self):
        nukescripts.PythonPanel.__init__(self, 'Textfile', 'com.xy.Textfile')
        self.Load()
        
        # String
        self.Project = nuke.File_Knob('Name', 'Name')
        self.addKnob( self.Project )

       # KNOBS DEFINITION    
    def knobChanged( self, knob ):
        if knob == self.Name:
            self.WriteFile()

       # WRITE STRINGS TO TXT FILE IN THE NUKE_SCRIPT DIR    
    def WriteFile( self ):
        if (os.path.dirname(nuke.Root().name()) != ''):
        # NUKE_SCRIPT PATH AND TXT NAME DEFINITION
            scPath = os.path.dirname(nuke.Root().name())
            bin = scPath + '/' + 'TxtBin'
            file = "Textfile.txt"
        # MAKE DIRECTORY
            path = bin + os.sep + file
            if not os.path.exists(bin):
                os.mkdir(bin)
        # WRITE STRINGS
            shotinfo = open(path, "w")
            shotinfo.write('Name:' + self.Name.value() + '\n')

        # READ TXT FILE BACK TO STRINGS    
    def Load( self ):
        if (nuke.Root().name()) != 'Root' :
            # NUKE_SCRIPT PATH AND TXT NAME DEFINITION
            scPath = os.path.dirname(nuke.Root().name())
            bin = scPath + '/' + 'TxtBin'
            file = "Textfile.txt"
            infotext = bin + '/' + file
            openfile = open(infotext, 'r')
            array = []
            for line in openfile:
                x=line
                if (x.find('\n') > -1):
                    line = str(line).replace('\n', '',5)
                array.append( line )
            # READ LINE VALUES
            self.Name.setValue(str(array[0]).replace('Name:', '',1))
            self.WriteFile()


So why doesnt it work with self.Load() at the beginning? It seems that Nuke 
wants to set the Values for the string before the Panel is loaded. 
I dont have a solution. So if you know what i did wrong please let me know.



_______________________________________________
Nuke-python mailing list
[email protected], http://forums.thefoundry.co.uk/
http://support.thefoundry.co.uk/cgi-bin/mailman/listinfo/nuke-python

Reply via email to