這部分將如何處理格式
讀取格式
使用nuke.formats()可會獲取nuke支持的格式
scriptFormats = nuke.formats()
各種方法展示如下:
for f in scriptFormats:
print f.name()
print f.width()
print f.height()
print f.pixelAspect()
print 10*'-'
結果如下:
# Result:
PC_Video
640
480
1.0
----------
NTSC
720
486
0.910000026226
----------
PAL
720
576
1.09000003338
----------
HD
1920
1080
1.0
更多方法汉柒,請找dir(nuke.Format):
dir(nuke.Format)
# Result:
['__class__', '__delattr__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'add', 'fromUV', 'height', 'name', 'pixelAspect', 'scaled', 'setHeight', 'setName', 'setPixelAspect', 'setWidth', 'toUV', 'width']
節(jié)點也有一個format()方法來獲取當前的格式误褪。DAG中選中的節(jié)點:
node = nuke.selectedNode()
nodeFormat = node.format()
print nodeFormat.name()
當從正規(guī)化的紋理空間轉換到某種格式的像素空間時,fromUV和toUV就很上手碾褂,下列代碼給出了像素坐標的中心點:
nodeFormat.fromUV( .5, .5 )
# Result:
[1024.0, 778.0]
下面根據(jù)像素坐標兽间,給出正規(guī)化了的紋理坐標。
nodeFormat.toUV( 1024, 788 )
# Result: [0.5, 0.50642675161361694]
##### 添加格式
添加格式正塌,用TCL語法將其參數(shù)定義為字符串( 意味著 數(shù)值靠空格區(qū)分)嘀略,然后創(chuàng)建一個Format對象恤溶。至少應該定義 width, height帜羊, name:
```python
square2k = '2048 2048 square 2k'
nuke.addFormat( square2k )
這樣新格式就可用了:
- 在所有格式菜單UI中
-
通過python api
添加碰撞盒子或者設置像素寬高比咒程,在height和name間設置對應的值:
nuke.addFormat( '2048 2048 48 48 2000 2000 2 square 2k (bbox anamorphic)' )
![](https://docs.thefoundry.co.uk/products/nuke/developers/100/pythondevguide/_images/formats_02.png)
設置格式
想設置節(jié)點的格式,簡單地使用knob方法賦值新名字:
n = nuke.createNode( 'CheckerBoard2' )
n['format'].setValue( 'square 2k' )
當設置root的格式時讼育,方法相同:
nuke.root()['format'].setValue( 'square 2k' )
給root的代理knob設置格式帐姻,需要首先設置root使用format作為代理格式:
# DEFINE BASE AND PROXY FORMATS
square2k = '2048 2048 square 2k'
square1k = '1024 1024 square 1k'
# ADD FORMATS TO SESSION
for f in ( square2k, square1k ):
nuke.addFormat( f )
# SET THE ROOT TO USE BOTH BASE AND PROXY FORMATS
root = nuke.root()
root['format'].setValue( 'square 2k' )
root['proxy_type'].setValue( 'format' )
root['proxy_format'].setValue( 'square 1k' )
![](https://docs.thefoundry.co.uk/products/nuke/developers/100/pythondevguide/_images/formats_03.png)