01: Print Function and Variables

print() Function

lets type our first code here:

				
					print('Hello Compositors!')
# Result: Hello Composiors!


				
			

This line of code uses the print() function to output the text “Hello Compositors!” to the Script Editor.

Let’s take some help from the documentation. To understand any built-in function, we can type in our nuke script editor help(name of the function), for example:

				
					help(print)

Python Library Documentation: built-in function print in module builtins

print(...)
    print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)
    
    Prints the values to a stream, or to sys.stdout by default.
    Optional keyword arguments:
    file:  a file-like object (stream); defaults to the current sys.stdout.
    sep:   string inserted between values, default a space.
    end:   string appended after the last value, default a newline.
    flush: whether to forcibly flush the stream.

				
			

This provides the documentation for the print() function, explaining its parameters and default behavior. The print() function prints the values provided to it, separated by spaces (sep=' ') and followed by a newline character (end='\n').

‘sep’ example:

The sep parameter specifies a string to insert between values. The default is a space.

				
					#value example:

print('I', 'Love', 'Nuke', 15)

# Result: I Love Nuke 15

# 'sep' Example
#string inserted between values, default a space

print('I', 'Love', 'Nuke', 15, sep=('_') )

# Result:I_Love_Nuke_15


				
			

By setting sep='_', underscores are used instead of spaces to separate the values.

 

‘end’ example:

The end parameter specifies what to append at the end of the output. The default is a newline character.

				
					print('Hello Composiors!')
print('I', 'Love', 'Nuke', 15)

# Result: 
#Hello Composiors!
#I Love Nuke 15


print('Hello Composiors!', end='\n\n')
print('I', 'Love', 'Nuke', 15)

# Result: 
#Hello Composiors!

#I Love Nuke 15


print('Hello Composiors!', end='*')
print('I', 'Love', 'Nuke', 15)
# Result: Hello Composiors!*I Love Nuke 15
				
			

Here, the print() function uses the default end='\n', so each print() call outputs on a new line. Setting end='\n\n' adds two newline characters after the first print, creating a blank line between the two outputs.

Here, end='*' appends an asterisk instead of a newline.

What are Variables?

Variables are containers for storing data values. They allow you to store information that can be referenced and manipulated in a program.

How to assign a Variable:

				
					a = 'Hello Compositors!'

b = 'I love Nuke' 

c = '15'

print(a+b+c)

# Result: Hello Compositors!I love Nuke15
				
			

This code assigns strings to variables a, b, and c, then concatenates and prints them. Notice that there is no space between “Nuke” and “15”.

Oops! We need a space between “Nuke” and “15”. It’s easy to fix using the end parameter to create a space between these two

				
					a = 'Hello Compositors!'

b = 'I love Nuke' 

c = '15'

print(a+b,end=' '+c)

# Result: Hello Compositors!I love Nuke 15

#file and flush we will learn later class

				
			

Practical Example in Nuke:

Let’s create a node in the node graph and have Nuke print the name of the selected node:

				
					print(nuke.selectedNode().knob.size().Value())

# Result: Blur1


				
			

This code uses nuke.selectedNode() to get the currently selected node in Nuke and prints its name.

We can write it in a better way using a variable:

				
					selNode = nuke.selectedNode()

print(selNode.name())

print(selNode.name())
# Result: Blur1

				
			

By assigning nuke.selectedNode() to a variable selNode, we can reuse this variable to access the selected node’s properties.

The nuke.selectedNode() function in Nuke is useful for accessing the currently selected node in the Node Graph. It only selects a single node, not multiple nodes. We’ll cover multiple node selection later.

In this way, we can access any knob of the selected node. Let’s try more:

				
					selNode = nuke.selectedNode()

print('value is :', selNode.knob('size').getValue())

# Result: value is : 10.0

#lets try to find the value of mix knob:

selNode = nuke.selectedNode()

print('value is :', selNode.knob('mix').getValue())

# Result: value is : 1.0
				
			

Day 2 Exercise:

Create a Grade node and print some values of knobs: blackpoint, lift, gamma.

join my 60-day Nuke Python challenge!

Subscribe now!

2 thoughts on “01: Print Function and Variables”

  1. Nice
    Would be better if you have some kind of newletter style way so every time you post your day progress everyone gets alert who is subcribed and can followup. Good luck

Leave a Comment

Your email address will not be published. Required fields are marked *