Python
Set Custom Attributes on All New Nodes with Python
Especially when sharing scripts between artists, keeping tabs on who did what becomes a necessity. Labelling all newly created nodes with the users name, for example, might be a good idea. You can do that with a simple Python script in the script editor (or, if your pipeline is more advanced, put it in the menu.py and have it read out the logged in username or somesuch).
1 2 3 4 5 6 7 |
def userToLabel(): try: nuke.thisNode()['label'].setValue(activeUser) exept NameError: pass nuke.addOnUserCreate(userToLabel) |
Set Roto Shape Attributes
You can access the attributes of shapes with Python script, for example to set a random opacity value per shape in a given roto node.
1 2 3 4 5 6 7 |
import nuke import random roto = nuke.selectedNode() knob = roto['curves'] for shp in knob.rootLayer: attrs = shp.getAttributes() attrs.set( 'opc', random.random() ) |
There’s more in-depth information on the Foundry forums:
http://community.thefoundry.co.uk/discussion/topic.aspx?f=190&t=103073
http://community.thefoundry.co.uk/discussion/topic.aspx?f=190&t=102867
http://community.thefoundry.co.uk/discussion/topic.aspx?f=190&t=102009
Set Expression via Python
To set an expression e.g. on a Dropdown-Menu, you’ll have to use Python instead of being able to drag’n’drop one knob to the other.
In case you would like to switch the antialiasing inside a Scanline Renderer to “high” when rendering on the farm, but keep working in the GUI with “none”, you could use a line like this:
1 |
nuke.selectedNode()['antialiasing'].setExpression('$gui? 0:3') |
Of Course you can use this for every kind of knob. Quite useful to link transformation and rotation order in 3d space, too.
Copying rendered files to a different folder via python
If you do some versioning on a shot, and want to keep those files in your version-folders, but also want to have a folder that always contains the latest renders, you can do this with python, and don’t have to add a second Write-node in your script.
Just add the following in the after each frame field in your Write-Node.
1 2 3 4 5 6 7 |
import shutil srcFile = nuke.thisNode()['file'].evaluate() folder = os.path.join(path(srcFile).parent.parent) target = os.path.basename(srcFile) destFile = str(folder + "/VLATEST/" + target) if not os.path.isdir(os.path.dirname(destFile)): os.makedirs(os.path.dirname(destFile)) shutil.copy2(srcFile, destFile) |
What this does is the following:
It thinks you render everything in your e.g. “…/output/001/…” – folder.
Then it splits the path and exchanges the 001 by “VLATEST”. If this folder doesn’t exist, it creates the folder.
Then it copies everything over via shutil.copy2().
This script assumes that you don’t have versioning on the filename at the moment. If you want to edit this, you have to adjust your destFile.
Create render folders if they do not exist
To prevent your write nodes throwing “no such file or directory” errors, paste this bit of code into the python – before render section of the write:
1 2 3 |
import os if not os.path.isdir(os.path.dirname(nuke.thisNode()['file'].evaluate())): os.makedirs(os.path.dirname(nuke.thisNode()['file'].evaluate())) |
Copy files on os level via python
This script copies the file of a Read node to a specific folder.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
import shutil import os readNode = nuke.toNode('Read12') path = readNode["file"].getValue() destfolder = "C:/Users/sschweiger/Desktop/test/" srcfolder = path.split("/")[-2] srcname = path.split("/")[-1] srcSize = os.path.getsize(path) if not os.path.isfile(destfolder): nuke.message('Copypocess for Videofile '+path.split("/")[-1]+' will start after klicking "OK"!'+' Wait for Message before continue!') shutil.copy2(path, destfolder) destSize = os.path.getsize(destfolder+srcname) x=0 while (x!=1): if srcSize==destSize: x=1 else: x=0 nuke.message('Copied sucsessfully!') |
Disable knobs on a certain node with python
1 2 |
x = nuke.toNode("YourNode") x['knob'].setEnabled(False) |
To do this for all knobs, just put it in a loop:
1 2 3 4 5 |
x = nuke.toNode("YourNode") x['knob'].setEnabled(False) for i in x.allKnobs(): [i.name()].setEnabled(False) |
Execute writenode via python
1 2 3 |
write = nuke.toNode('YourWritenode') nuke.execute(write,firstframe,lastframe) |
Set keyframes in knobs with python
This example sets a knob to a defined value in all frames .
nuke.Root()[‘first_frame’].getValue() returns float whyever…
So thats the reason i put it in “int()”
1 2 3 4 5 6 7 8 9 10 11 12 |
x = nuke.toNode("YourNode") # get first an last frame from Script and convert them into integer first = int(nuke.Root()['first_frame'].getValue()) last = int(nuke.Root()['last_frame'].getValue()) x['knobname'].setAnimated(0) # set value in all frames for i in range(first,last): nuke.activeViewer().frameControl(i) x['knobname'].setValueAt(value,i) |
Create knobs with python
Syntax to create knobs (in this example a text-knob):
1 2 3 |
c = nuke.toNode("YourNode") text = nuke.Text_Knob(name,label,"text") c.addKnob(knob) |
And here a short list of the most-needed knobs:
1 2 3 4 |
nuke.PyScript_Knob(name,label,py-script) nuke.Text_Knob(name,label,Text) nuke.Boolean_Knob(name,label) nuke.Enumeration_Knob(name,label,['value1', 'value2', 'value3']) |