Skip to content

Instantly share code, notes, and snippets.

@MitchellKehn
MitchellKehn / premultBracket.py
Created January 30, 2024 08:10
[premult bracket] wraps the selected node in an un/premult pair. it's... old #nuke
node_names = []
alts = []
for i in nuke.selectedNodes():
name = i['name'].value()
alt = i.ypos()
node_names.append(name)
alts.append(alt)
i.knob('selected').setValue(False) # Deselects nodes as it goes through.
@MitchellKehn
MitchellKehn / excel_drag_to_fill.py
Created January 21, 2024 01:46
[Excel-like drag to fill functionality] Adds drag-to-fill functionality to a QTableView #qt #pyside
from PySide2 import QtWidgets, QtGui, QtCore
def snap_value(value, snap_points, snap_threshold, verbose=False):
"""
Given a value and a list of "snapping points" it can snap to,
snap it to the closest value if it's within the snapping threshold.
"""
if not snap_points: return value
@MitchellKehn
MitchellKehn / rich_drag_and_drop.py
Last active January 21, 2024 01:50
[Drag and drop between item widgets with custom types] normally drag-and-drop between item widgets doesn't preserve subclass types for custom items. This enables that, with interchangeable drag and drop between all three types of item widgets. #python #pyside #qt
import abc
import json
import inspect
from collections import defaultdict
from typing import Type, Iterable
from functools import partial
from PySide2 import QtWidgets, QtGui, QtCore
@MitchellKehn
MitchellKehn / DeNodal.nk
Created January 24, 2022 06:41
[Nodal] it's Wordle for Nuke
set cut_paste_input [stack 0]
version 11.3 v4
push $cut_paste_input
NoOp {
name DeNodal1
knobChanged "def main():\n import re\n node = nuke.thisNode()\n \n fixedLetters = node\[\"fixedLetters\"].getValue().strip().lower()\n floatingLetters = node\[\"floatingLetters\"].getValue().strip().lower()\n antiLetters = node\[\"antiLetters\"].getValue().strip().lower()\n wordList = node\[\"wordList\"].getValue().split(\",\")\n \n if not any((char.isalpha() for char in fixedLetters)) \\\n and not floatingLetters \\\n and not antiLetters:\n node\[\"solutions\"].setValue(\"no input\")\n return\n \n fixedPattern = fixedLetters.replace(\"_\", r\"\\w\")\n candidateWords = \[]\n for word in wordList:\n if re.match(fixedPattern, word) \\\n and all((char in word for char in floatingLetters)) \\\n and not any((char in word for char in antiLetters)):\n candidateWords.append(word)\n \n if candidateWords:\n solution
@MitchellKehn
MitchellKehn / svg2png.py
Created August 10, 2021 11:00
[Batch Convert SVG-> PNG] uses PySide2 to convert SVGs to PNGs
from PySide2 import QtGui, QtSvg
import os
FOLDER = r"C:\Users\mkehn\Desktop\test_theme\theme\primary"
def convertPngToSvg(filepath, size):
folder, filename = os.path.split(filepath)
basename, ext = os.path.splitext(filename)
image = QtGui.QImage(size, size, QtGui.QImage.Format_ARGB32)
image.fill(0)
@MitchellKehn
MitchellKehn / sqlalchemyOrmNiceRepr.py
Created April 6, 2021 13:43
[SQLAlchemy ORM Pretty Repr] #orm #sqlalchemy
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
# monkey-patch a nice __repr__ method onto the Base class.
def _niceRepr(self):
"""
Nicely format class name and column values.
Looks up the inheritance hierarchy for inherited columns and displays those too.
"""
@MitchellKehn
MitchellKehn / RecursiveTabKnobs.nk
Last active January 22, 2021 07:03
[Tab_Knob in a Tab_Knob] an example of having nested tabs in a Nuke node. #nuke
NoOp {
name NoOp1
selected true
xpos 352
ypos 1985
addUserKnob {20 Foo l "" n -2}
addUserKnob {20 Bar l Settings}
addUserKnob {7 float}
addUserKnob {20 Baz l Fun}
addUserKnob {7 float}
@MitchellKehn
MitchellKehn / randomRotoshapeColors.py
Created January 6, 2021 01:17
[random rotoshape fill color] sets the fill color of all roto shapes to be a random RGB something. viewer needs a kick in the pants sometimes
import random
rp = nuke.selectedNode()
for element in rp['curves'].rootLayer:
if isinstance(element, nuke.rotopaint.Stroke) or isinstance(element, nuke.rotopaint.Shape):
attrs = element.getAttributes()
attrs.set("r", random.random())
attrs.set("g", random.random())
attrs.set("b", random.random())
@MitchellKehn
MitchellKehn / get publish descriptions by user.py
Created December 23, 2020 06:50
[get publish descriptions by user] #shotgun
from altpipe.shotgun.api import getShotgunConnection
from pprint import pprint
sg = getShotgunConnection()
user = sg.find_one("HumanUser", [["login", "is", "jtrudgian"]])
versions = sg.find("Version", [["user", "is", user]], ["description"], limit=1000)
descriptions = []
for version in versions:
@MitchellKehn
MitchellKehn / cameraSmooth.py
Created December 8, 2020 06:17
[Camera Smooth] Create a copy of the selected camera node, using a weighted average of surrounding frames #nuke
"""
Smooth Camera
Creates a new camera linked to the selected one, with a weighted average of the position/rotation curves.
"""
import nuke
def deselectAll():
for node in nuke.selectedNodes():
node.setSelected(False)