Nuke Python 用PySide擴(kuò)展Nuke

可以用python和pyside來擴(kuò)展nuke的UI,6.3v5以上已經(jīng)自帶PySide了。

第一個PySide窗口

啟動nuke,在腳本編輯器輸入:

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


可??康?PySide Widgets

用pyside創(chuàng)建可停靠的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

下面的代碼創(chuàng)建了一個可定靠的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

下面的例子創(chuàng)建了可停靠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()
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 雖然nuke內(nèi)置了pyside,但是想用pyqt還是可以的。配置pyqt需要幾個步驟,當(dāng)然Python26也要安裝...
    N景波閱讀 1,085評論 0 0
  • 快捷鍵,菜單,菜單項 的做法。這類代碼一般在menu.py里面。至于如何安裝請參看installing Plug-...
    N景波閱讀 1,888評論 0 3
  • afinalAfinal是一個android的ioc,orm框架 https://github.com/yangf...
    passiontim閱讀 15,889評論 2 45
  • 本章的例子幫你初步了解Nuke Python API的使用。 腳本大小寫敏感,需要輸入正確才能運行??截悤r注意縮進(jìn)...
    N景波閱讀 7,999評論 0 14
  • 使用下文描述的nuke.add...()函數(shù),當(dāng)有變量事件(比如,創(chuàng)建節(jié)點,加載腳本)時就自動調(diào)用python函數(shù)...
    N景波閱讀 2,959評論 0 1

友情鏈接更多精彩內(nèi)容