
Node Editor Prototype
Last week while at Siggraph I attended a talk that mentioned ILM’s BlockParty. This is their internally developed modular rigging system. Modular rigging is an old idea, one I’ve been using for over a decade now, but the difference with BlockParty is how they chose to visualize the modules. They use a node layout similar to Maya’s Node Editor. Each node containing some visual queue as to what it is (usually an icon). This is a clever way to handle presenting and accessing a modular rigs data. Needless to say by the end of the talk I had a couple pages of notes outline how I could create my own version in Maya.
On my first Sunday back from Siggraph I spent a few hours throwing together a quick prototype. Below is a short video showing the UI in action followed by the entire source code. Please keep in mind this is a proof of concept. It is buggy and missing a lot of functionality.
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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 |
__author__ = 'abaker' """ @project: Pholidota @author: Austin Baker @created: 6/07/2015 @license: http://opensource.org/licenses/mit-license.php The MIT License (MIT) Copyright (c) 2015 Austin Baker Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. USAGE: """ ''' ============================================================ --- IMPORT MODULES ============================================================ ''' import PySide.QtGui as QtGui import PySide.QtCore as QtCore import maya.OpenMayaUI as OpenMayaUI from shiboken import wrapInstance ''' ============================================================ --- GRAPHICS CLASSES ============================================================ ''' class NodeLine(QtGui.QGraphicsPathItem): def __init__(self, pointA, pointB): super(NodeLine, self).__init__() self._pointA = pointA self._pointB = pointB self._source = None self._target = None self.setZValue(-1) self.setBrush(QtCore.Qt.NoBrush) self.pen = QtGui.QPen() self.pen.setStyle(QtCore.Qt.SolidLine) self.pen.setWidth(2) self.pen.setColor(QtGui.QColor(255,20,20,255)) self.setPen(self.pen) def mousePressEvent(self, event): self.pointB = event.pos() def mouseMoveEvent(self, event): self.pointB = event.pos() def updatePath(self): path = QtGui.QPainterPath() path.moveTo(self.pointA) dx = self.pointB.x() - self.pointA.x() dy = self.pointB.y() - self.pointA.y() ctrl1 = QtCore.QPointF(self.pointA.x() + dx * 0.25, self.pointA.y() + dy * 0.1) ctrl2 = QtCore.QPointF(self.pointA.x() + dx * 0.75, self.pointA.y() + dy * 0.9) path.cubicTo(ctrl1, ctrl2, self.pointB) self.setPath(path) def paint(self, painter, option, widget): painter.setPen(self.pen) painter.drawPath(self.path()) @property def pointA(self): return self._pointA @pointA.setter def pointA(self, point): self._pointA = point self.updatePath() @property def pointB(self): return self._pointB @pointB.setter def pointB(self, point): self._pointB = point self.updatePath() @property def source(self): return self._source @source.setter def source(self, widget): self._source = widget @property def target(self): return self._target @target.setter def target(self, widget): self._target = widget class NodeSocket(QtGui.QGraphicsItem): def __init__(self, rect, parent, socketType): super(NodeSocket, self).__init__(parent) self.rect = rect self.type = socketType # Brush. self.brush = QtGui.QBrush() self.brush.setStyle(QtCore.Qt.SolidPattern) self.brush.setColor(QtGui.QColor(180,20,90,255)) # Pen. self.pen = QtGui.QPen() self.pen.setStyle(QtCore.Qt.SolidLine) self.pen.setWidth(1) self.pen.setColor(QtGui.QColor(20,20,20,255)) # Lines. self.outLines = [] self.inLines = [] def shape(self): path = QtGui.QPainterPath() path.addEllipse(self.boundingRect()) return path def boundingRect(self): return QtCore.QRectF(self.rect) def paint(self, painter, option, widget): painter.setBrush(self.brush) painter.setPen(self.pen) painter.drawEllipse(self.rect) def mousePressEvent(self, event): if self.type == 'out': rect = self.boundingRect() pointA = QtCore.QPointF(rect.x() + rect.width()/2, rect.y() + rect.height()/2) pointA = self.mapToScene(pointA) pointB = self.mapToScene(event.pos()) self.newLine = NodeLine(pointA, pointB) self.outLines.append(self.newLine) self.scene().addItem(self.newLine) elif self.type == 'in': rect = self.boundingRect() pointA = self.mapToScene(event.pos()) pointB = QtCore.QPointF(rect.x() + rect.width()/2, rect.y() + rect.height()/2) pointB = self.mapToScene(pointB) self.newLine = NodeLine(pointA, pointB) self.inLines.append(self.newLine) self.scene().addItem(self.newLine) else: super(NodeSocket, self).mousePressEvent(event) def mouseMoveEvent(self, event): if self.type == 'out': pointB = self.mapToScene(event.pos()) self.newLine.pointB = pointB elif self.type == 'in': pointA = self.mapToScene(event.pos()) self.newLine.pointA = pointA else: super(NodeSocket, self).mouseMoveEvent(event) def mouseReleaseEvent(self, event): item = self.scene().itemAt(event.scenePos().toPoint()) if self.type == 'out' and item.type == 'in': self.newLine.source = self self.newLine.target = item item.parentItem().Input.inLines.append(self.newLine) self.newLine.pointB = item.getCenter() elif self.type == 'in' and item.type == 'out': self.newLine.source = item self.newLine.target = self item.parentItem().Output.outLines.append(self.newLine) self.newLine.pointA = item.getCenter() else: super(NodeSocket, self).mouseReleaseEvent(event) def getCenter(self): rect = self.boundingRect() center = QtCore.QPointF(rect.x() + rect.width()/2, rect.y() + rect.height()/2) center = self.mapToScene(center) return center class NodeItem(QtGui.QGraphicsItem): def __init__(self): super(NodeItem, self).__init__() self.name = None self.rect = QtCore.QRect(0,0,100,60) self.setFlag(QtGui.QGraphicsItem.ItemIsMovable) self.setFlag(QtGui.QGraphicsItem.ItemIsSelectable) self.initUi() # Brush. self.brush = QtGui.QBrush() self.brush.setStyle(QtCore.Qt.SolidPattern) self.brush.setColor(QtGui.QColor(80,0,90,255)) # Pen. self.pen = QtGui.QPen() self.pen.setStyle(QtCore.Qt.SolidLine) self.pen.setWidth(1) self.pen.setColor(QtGui.QColor(20,20,20,255)) self.selPen = QtGui.QPen() self.selPen.setStyle(QtCore.Qt.SolidLine) self.selPen.setWidth(1) self.selPen.setColor(QtGui.QColor(0,255,255,255)) def initUi(self): self.Input = NodeSocket(QtCore.QRect(-10,20,20,20), self, 'in') self.Output = NodeSocket(QtCore.QRect(90,20,20,20), self, 'out') def shape(self): path = QtGui.QPainterPath() path.addRect(self.boundingRect()) return path def boundingRect(self): return QtCore.QRectF(self.rect) def paint(self, painter, option, widget): painter.setBrush(self.brush) if self.isSelected(): painter.setPen(self.selPen) else: painter.setPen(self.pen) painter.drawRect(self.rect) def mouseMoveEvent(self, event): super(NodeItem, self).mouseMoveEvent(event) for line in self.Output.outLines: line.pointA = line.source.getCenter() line.pointB = line.target.getCenter() for line in self.Input.inLines: line.pointA = line.source.getCenter() line.pointB = line.target.getCenter() def contextMenuEvent(self, event): menu = QtGui.QMenu() make = menu.addAction('make') makeFromHere = menu.addAction('make from here') debugConnections = menu.addAction('debug connections') selectedAction = menu.exec_(event.screenPos()) if selectedAction == debugConnections: print 'Input' for idx,line in enumerate(self.Input.inLines): print ' Line {0}'.format(idx) print ' pointA: {0}'.format(line.pointA) print ' pointB: {0}'.format(line.pointB) print 'Output' for idx,line in enumerate(self.Output.outLines): print ' Line {0}'.format(idx) print ' pointA: {0}'.format(line.pointA) print ' pointB: {0}'.format(line.pointB) class NodeView(QtGui.QGraphicsView): """ QGraphicsView for displaying the nodes. :param scene: QGraphicsScene. :param parent: QWidget. """ def __init__(self, scene, parent): super(NodeView, self).__init__(parent) self.setObjectName('View') self.setScene(scene) self.setTransformationAnchor(QtGui.QGraphicsView.AnchorUnderMouse) self.setViewportUpdateMode(QtGui.QGraphicsView.SmartViewportUpdate) self.drag = False def wheelEvent(self, event): """ Zooms the QGraphicsView in/out. :param event: QGraphicsSceneWheelEvent. """ inFactor = 1.25 outFactor = 1 / inFactor oldPos = self.mapToScene(event.pos()) if event.delta() > 0: zoomFactor = inFactor else: zoomFactor = outFactor self.scale(zoomFactor, zoomFactor) newPos = self.mapToScene(event.pos()) delta = newPos - oldPos self.translate(delta.x(), delta.y()) def mousePressEvent(self, event): if event.button() == QtCore.Qt.MiddleButton and event.modifiers() == QtCore.Qt.AltModifier: self.setDragMode(QtGui.QGraphicsView.NoDrag) self.drag = True self.prevPos = event.pos() self.setCursor(QtCore.Qt.SizeAllCursor) elif event.button() == QtCore.Qt.LeftButton: self.setDragMode(QtGui.QGraphicsView.RubberBandDrag) super(NodeView, self).mousePressEvent(event) def mouseMoveEvent(self, event): if self.drag: delta = (self.mapToScene(event.pos()) - self.mapToScene(self.prevPos)) * -1.0 center = QtCore.QPoint(self.viewport().width()/2 + delta.x(), self.viewport().height()/2 + delta.y()) newCenter = self.mapToScene(center) self.centerOn(newCenter) self.prevPos = event.pos() return super(NodeView, self).mouseMoveEvent(event) def mouseReleaseEvent(self, event): if self.drag: self.drag = False self.setCursor(QtCore.Qt.ArrowCursor) super(NodeView, self).mouseReleaseEvent(event) ''' ============================================================ --- UI CLASSES ============================================================ ''' class SideBar(QtGui.QFrame): def __init__(self, parent): super(SideBar, self).__init__(parent) self.setObjectName('SideBar') self.initUi() def initUi(self): # Frame. self.setFixedWidth(200) # Central Layout. self.CentralLayout = QtGui.QVBoxLayout(self) # Buttons. self.AddBoxButton = QtGui.QPushButton('Add Box') self.CentralLayout.addWidget(self.AddBoxButton) # Connections. self.initConnections() def initConnections(self): self.AddBoxButton.clicked.connect(self.clickedAddBoxButton) def clickedAddBoxButton(self): window = self.window() box = NodeItem() window.Scene.addItem(box) box.setPos(window.Scene.width()/2, window.Scene.height()/2) class NodeWindow(QtGui.QMainWindow): def __init__(self, parent): super(NodeWindow, self).__init__(parent) self.setWindowTitle('Node Window') self.initUi() def initUi(self): # Window. self.setMinimumSize(400,200) # Central Widget. self.CentralWidget = QtGui.QFrame() self.CentralWidget.setObjectName('CentralWidget') self.setCentralWidget(self.CentralWidget) # Central Layout. self.CentralLayout = QtGui.QHBoxLayout(self.CentralWidget) # GraphicsView. self.Scene = QtGui.QGraphicsScene() self.Scene.setObjectName('Scene') self.Scene.setSceneRect(0,0,32000,32000) self.View = NodeView(self.Scene, self) self.CentralLayout.addWidget(self.View) # Side Bar. self.SideBar = SideBar(self) self.CentralLayout.addWidget(self.SideBar) # Color. self.initColor() def initColor(self): windowCss = ''' QFrame { background-color: rgb(90,90,90); border: 1px solid rgb(90,70,30); } QFrame#SideBar { background-color: rgb(50,50,50); border: 1px solid rgb(255,255,255); }''' self.setStyleSheet(windowCss) ''' ============================================================ --- SHOW WINDOW ============================================================ ''' def mayaMainWindow(): """ Return the Maya main window widget as a Python object :return: Maya Main Window. """ mainWindowPtr = OpenMayaUI.MQtUtil.mainWindow() return wrapInstance(long(mainWindowPtr), QtGui.QWidget) if __name__ == '__main__': mayaWindow = mayaMainWindow() nodeWindow = NodeWindow(mayaWindow) nodeWindow.show() |