【NUKE教程】Nuke Python 用PySide扩展Nuke

10 九月, 2016
13
0

nukepyside
可以用python和pyside来扩展nuke的UI,6.3v5以上已经自带PySide了。

第一个PySide窗口

启动nuke,在脚本编辑器输入:

from PySide import QtGui
label = QtGui.QLabel("hello world")
label.show()

可停靠的 PySide Widgets

用pyside创建可停靠的widget,nukescripts.panels模块可以帮到忙
定义如下:

registerWidgetAsPanel(widget, name, id, create=False)
    registerWidgetAsPanel(widget, name, id, create) -> PythonPanel
    Wraps and registers a widget to be used in a NUKE panel.
    widget - should be a string of the class for the widget
    name - is is the name as it will appear on the Pane menu
    id - should the the unique ID for this widget panel
    create - if this is set to true a new NukePanel will be returned     that wraps this widget

下面的代码创建了一个可定靠的table widegt:

import nuke
import PySide.QtCore as QtCore
import PySide.QtGui as QtGui
from nukescripts import panels
class NukeTestWindow(QtGui.QWidget):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.setLayout(QtGui.QVBoxLayout())
        self.myTable    = QtGui.QTableWidget()
        self.myTable.header = ['Date', 'Files', 'Size', 'Path' ]
        self.myTable.size = [ 75, 375, 85, 600 ]
        self.myTable.setColumnCount(len(self.myTable.header))
        self.myTable.setHorizontalHeaderLabels(self.myTable.header)
        self.myTable.setSelectionMode(QtGui.QTableView.ExtendedSelection)
        self.myTable.setSelectionBehavior(QtGui.QTableView.SelectRows)
        self.myTable.setSortingEnabled(1)
        self.myTable.sortByColumn(1, QtCore.Qt.DescendingOrder)
        self.myTable.setAlternatingRowColors(True)
        self.myTable.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
        self.myTable.setRowCount(50)
        self.layout().addWidget(self.myTable)
        self.myTable.setSizePolicy(QtGui.QSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Expanding))
        self.setSizePolicy(QtGui.QSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Expanding))
panels.registerWidgetAsPanel('NukeTestWindow', 'Test table panel', 'uk.co.thefoundry.NukeTestWindow')

这个例子在pane菜单添加了可停靠的pane. 给pane添加widget,打开pane时挨着Properties的panel.
可以运行下列代码:

pane = nuke.getPaneFor('Properties.1')
panels.registerWidgetAsPanel('NukeTestWindow', 'Test table panel', 'uk.co.thefoundry.NukeTestWindow', True).addToPane(pane)
A Web browser panel example

下面的例子创建了可停靠web browder,同时用了信号槽
要使用,把例子添加到Nuke path或者拷贝粘贴到脚本编辑器里,然后从Pane菜单选择 web browser:

## example PySide panel that implements a simple web browser in Nuke
## JW 12/10/11
import nuke
import nukescripts
from nukescripts import panels
from PySide.QtGui import *
from PySide.QtCore import *
from PySide.QtWebKit import *
class WebBrowserWidget(QWidget):
  def changeLocation(self):
    url = self.locationEdit.text()
    if not url.startswith( 'http://' ):
      url = 'http://' + url
    self.webView.load( QUrl(url) )
  def urlChanged(self, url):
    self.locationEdit.setText( url.toString() )
  def __init__(self):
    QWidget.__init__(self)
    self.webView = QWebView()
    self.setLayout( QVBoxLayout() )
    self.locationEdit = QLineEdit( 'http://www.google.com' )
    self.locationEdit.setSizePolicy( QSizePolicy.Expanding, self.locationEdit.sizePolicy().verticalPolicy() )
    QObject.connect( self.locationEdit, SIGNAL('returnPressed()'),  self.changeLocation )
    QObject.connect( self.webView,   SIGNAL('urlChanged(QUrl)'),     self.urlChanged )
    self.layout().addWidget( self.locationEdit )
    bar = QToolBar()
    bar.addAction( self.webView.pageAction(QWebPage.Back))
    bar.addAction( self.webView.pageAction(QWebPage.Forward))
    bar.addAction( self.webView.pageAction(QWebPage.Stop))
    bar.addAction( self.webView.pageAction(QWebPage.Reload))
    bar.addSeparator()
    self.layout().addWidget( bar )
    self.layout().addWidget( self.webView )
    url = 'http://www.thefoundry.co.uk/'
    self.webView.load( QUrl( url ) )
    self.locationEdit.setText( url )
    self.setSizePolicy( QSizePolicy( QSizePolicy.Expanding,  QSizePolicy.Expanding))
## make this work in a .py file and in 'copy and paste' into the script editor
moduleName = __name__
if moduleName == '__main__':
  moduleName = ''
else:
  moduleName = moduleName + '.'
panels.registerWidgetAsPanel( moduleName + 'WebBrowserWidget', 'Web Browser','uk.co.thefoundry.WebBrowserWidget')

Migrating from PyQt Applications

在普通pyside和pyqt程序是兼容的,仅仅把import从 Pyqt4 改成 PySide。
PyQt的例子:

from PyQt4 import QtGui
label = QtGui.QLabel("Hello World")
label.show()

pyside的例子:

from PySide import QtGui
label = QtGui.QLabel("Hello World")
label.show()