Jan 30, 2011

Nuke commandline render and python

Happy new year nukers, and non-nukers! First post this year, hopefully many more will follow it.
For the beginning, a little about commandline rendering with nuke, using pythonscript (got the idea from a mailing list post of mine). I mean with nuke you can't only execute a nuke file, but a pythonscript too. Like this:
nuke6.1.exe -x -F 1-30 myscript.py
One can think, that this is useful for creating nodes on the fly, like a read node, a write node, and execute that for converting sequences to another format. That is an option, but you also can open existing scripts and modify them. For this you can use the scriptOpen command. For example, if you have a nuke script called test_v001,nk, make a file called test.py, and write in it:

nuke.scriptOpen("e:\\Projects\\test_v001.nk")
(Replace the path with the path to your nuke file) After this in the .py file you can do modifications on your nukefile. Knob changes etc. And that's it. You can execute with the above mentioned cmdline command:
nuke6.1.exe -x -F 1-30 test.py

There is another option to do that. Slight change, but important, and you can do much more with it than rendering. You can not only execute, but literally open nuke files in command line (terminal) mode, hold it in memory, do changes (blindly) to that. For example I use that for my archiving script, open many nukefiles one by one, collect the used readnodes, and put those in a list etc. So I'm not executing the nukefile, just open it. But if I want I can render it too. I put a line more in the previous test.py file, so it will look like this:

nuke.scriptOpen("e:\\Projects\\test_v001.nk")
#do some modification on knobs if you need
nuke.execute('Write1', 1, 30, 1)


Here I called an execute command at the end of my pythonscript (I must know the name of my writenode)
I will use this script in my commandline command, that is almost the same as before:
nuke6.1.exe -t -F 1-30 test.py

But notice the -t flag! That means the test.py is not executed as a render, but treated as normal a pythonscript, just like if I put that in script editor in nuke. It will render the nukefile anyway, because the execute command in the test.py But as I said the great thing is to have an option to execute python scripts in terminal mode with the nuke interpreter.

11 comments:

  1. Awesome! very helpful.

    Do you happen to know if there is a way to run eg an analyse from the command line?

    I'm trying to command line run f_steadiness, but trhough the gui you have to click "analyse". I can't seem to work out how to force it to analyse prior to writing it out?

    Cheers

    ReplyDelete
  2. Thanks! I assume you want to run an existing nuke scene that has a steadiness and a write node in it? If so, you can write a little code in a pythonscript, something like this:
    nuke.scriptOpen("....") # your script path
    n = nuke.toNode('F_Steadiness1')
    k = n.knob('analyse')
    k.execute()
    w = nuke.toNode('Write1')
    nuke.execute(w, 1,15)

    As you see the analyse button can be "pressed" with execute() in python. Replace the node names with yours.
    Run this pythonscript witn nuke -t, or you can leave out the writenode part and execute from commandline with nuke -x.
    Hope this helps (let me know if not),
    Gabor

    ReplyDelete
  3. Thanks heaps Gabor! that got me a large part of the way there...

    Just for kicks, I have the following:

    n = nuke.toNode('F_Steadiness1')
    k = n.knob('analyse')
    k.execute()
    print "steadiness complete"

    w = nuke.toNode('Write1')
    nuke.execute(w, 1,15)
    print "write complete"

    Curiously enough, "steadiness complete" appears instantaneously, and then it begins writing the files out... It doesn't offer up any error or any reason why it isn't analysing. (and the renders confirm that it's skipping this part of the process) Is this the same in your experience? Might there be some other reason why it's not executing the analyse do you think?

    ReplyDelete
  4. Hi Gabor, great tutorial.

    I just want to know how to save an output file (log.txt or whatever) so I can have a record of what got rendered and when.
    I created a little batch file with:

    Echo. >>c:\somelogfile.txt
    Echo.=================== >>c:\somelogfile.txt
    echo.%Date% >>c:\somelogfile.txt
    echo.%Time% >>c:\somelogfile.txt
    C:\"Program Files"\Nuke6.3v4\Nuke6.3.exe -t -F 1-500 R:\somerenderfile.py
    pause


    .. so I can keep a track but want to know how to get all the render times and output location in it.

    Chris

    ReplyDelete
  5. Hi, sorry for the late reply! I usually just open a terminal, and start nuke render like this (with your example):
    C:\"Program Files"\Nuke6.3v4\Nuke6.3.exe -t -F 1-500 R:\somerenderfile.py > c:\temp\renderlogfile.txt

    This ">" flag redirects output to the txt file, no need the batchfile.
    Hope this helps,
    Gabor

    ReplyDelete
    Replies
    1. Aha - that works!
      ...but one last thing.
      The logfile.txt is great, has everything in there that normally pops up on the command prompt, however, nothing is displayed on the command prompt whilst rendering.
      Can't I have both?
      Chris

      Delete
  6. This comment has been removed by the author.

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete
  7. I tried nuke.execute with four arguments like this
    nuke.execute('Write1', 1, 30, 1)
    and got error
    nuke.execute(writeNode,startFrame,endFrame,incr)
    TypeError: execute() takes at most 3 arguments (4 given)
    execute() takes at most 3 arguments (4 given)

    ReplyDelete
    Replies
    1. Hi, I tried this and it worked for me. But if you use 1 as increment like now, you can leave that because that is the default, so you can get away with the 3 arguments, like:
      nuke.execute('Write1', 1, 30)
      BTW what nuke version are you using?

      Hope this helps,
      Gabor

      Delete