For a few years now, I have not been able to copy code from PyCharm and some other sources and then paste them into the Nuke script editor.
Somehow, when pasting code from PyCharm into the script editor, absolutely nothing happens.
The annoying workaround is to copy from PyCharm, paste in a simpler text editor (gedit, notepad++, …), then copy from there and finally paste into Nuke.
One way around this is to use the excellent KnobScripter plugin by Adrian Pueyo, but it’s not always installed everywhere, and as great as it is, I still like to code in a full IDE (which is not incompatible with KnobScripter, pasting in NodeScripter from PyCharm does work).
Still, this was bothering me, so when my friend/colleague complained about it a little while ago it added oil to the fire and I decided it was time to dig a bit deeper.
Figuring out what’s wrong
I was convinced that the Script Editor refused to paste the content because the Text copied from PyCharm is “Rich Text”. When I copy it to a Text Editor which supports Rich Text, the colors and other text highlighting from PyCharm are included in the Paste:
PyCharm has an option to “Copy as PlainText”. I thought I could make this the default for Ctrl+C and be done with it. However, after a quick test, it turned out that Nuke did not accept that Plain Text either! What?
Apparently, the Rich Text was not the problem, so what could it be?
The next step was to see what could be the difference between pasting from PyCharm, which didn’t work, and pasting the same content from Notepad++ which did work. I needed to take a look at the contents of the Clipboard, and luckily Qt provides an API for just that.
The Clipboard can contain data in multiple formats, represented by the MIME data formats.
Qt told me that my clipboard from PyCharm contained the following formats:
- application/x-qt-windows-mime;value=”Rich Text Format”
- application/x-qt-windows-mime;value=”JAVA_DATAFLAVOR:application/x-java-jvm-local-objectref; class=com.intellij.codeInsight.editorActions.FoldingData
The Clipboard from Notepad++ only contained ‘text/plain’.
I tried removing each format one by one, and as soon as I removed ‘text/uri-list’, pasting worked again!
That was it, Nuke’s script editor refuses to paste anything that contains a uri-list.
The Nuke folder installation contains a Python package that looks A LOT like the source code for the script editor: C:\Program Files\Nuke14.0v4\pythonextensions\site-packages\foundry\ui\scripteditor
I thought I might be able to edit this file to fix it, but after some testing, it turned out that this might only be a draft, or a leftover, as no amount of changes ever accounted to anything, and even deleting the file didn’t seem to affect the Script Editor in Nuke.
Maybe that was for the best, I would not recommend modifying bundled part of Nuke anyways.
Instead, I resorted to writing an event filter, which removes uri-list data from the clipboard as you press Ctrl+V, which in turn means the native script editor will accept it.
Here is the code, simply pasting it into your menu.py would do the job.
from PySide2 import QtCore, QtWidgets class MimeCleaner(QtCore.QObject): """ Event filter to clean-up MimeData when pasting from PyCharm""" def __init__(self): super(MimeCleaner, self).__init__() QtWidgets.QApplication.instance().installEventFilter(self) def eventFilter(self, widget, event): if event.type() is QtCore.QEvent.KeyPress and event.key() == QtCore.Qt.Key_V: if QtWidgets.QApplication.keyboardModifiers() & QtCore.Qt.ControlModifier: if isinstance(widget, QtWidgets.QPlainTextEdit): self.sanitize_mime() return False def remove(self): QtWidgets.QApplication.instance().removeEventFilter(self) @staticmethod def sanitize_mime(): clipboard = QtWidgets.QApplication.clipboard() mime_data = clipboard.mimeData() new_mime_data = QtCore.QMimeData() for mime_format in mime_data.formats(): if mime_format not in ['text/uri-list']: new_mime_data.setData(mime_format, mime_data.data(mime_format)) clipboard.setMimeData(new_mime_data) mdc = MimeCleaner()