Skip to content

Instantly share code, notes, and snippets.

@maty974
Last active March 14, 2024 09:09
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maty974/4739917 to your computer and use it in GitHub Desktop.
Save maty974/4739917 to your computer and use it in GitHub Desktop.
The Foundry Nuke hack works with "nukescripts.panels.registerWidgetAsPanel", to remove the disturbing widget contents margins set by Nuke.
'''
Created on Feb 8, 2013
@author: matthieuc
@contact: matthieu.cadet@gmail.com
'''
import PySide.QtGui as QtGui
def setNukeZeroMarginsWidget(widget_object):
try:
if widget_object:
target_widgets = set()
target_widgets.add(widget_object.parentWidget().parentWidget())
target_widgets.add(widget_object.parentWidget().parentWidget().parentWidget().parentWidget())
for widget_layout in target_widgets:
widget_layout.layout().setContentsMargins(0, 0, 0, 0)
except:
pass
@maty974
Copy link
Author

maty974 commented Feb 8, 2013

Usage:

put this in your the QtGui.QWidget class:

    def event(self, event):
        if event.type() == QtCore.QEvent.Type.Show:
            try:
                setNukeZeroMarginsWidget(self)
            except:
                pass

        return QtGui.QWidget.event(self, event)

@nicosalto
Copy link

This sounds pretty cool as i was looking for something like that. However, i didnt figure out how to use it!
i put this function into my QtGui.QWidget class and copy setNukeZeroMarginsWidget, but it doesnt change anything.

Also i got another question for you since you seem to know a lot about pyside panels.
I create a pyside panel with an .ui file that i dock into nuke, my panel is 620px high and for some reasons, nuke display only the 2/3. A part of it is cropped, which is really annoying. If i don't dock it, the panel look fine.

@manuelmacha
Copy link

First of all thanks to maty974 for the original code-snippet.
It didn't work out of the box for me as well but I ultimately got it going.

def setNukeZeroMarginsWidget(widget_object):

parentApp = QtGui.QApplication.allWidgets()    
parentWidgetList = []
for parent in parentApp:
    for child in parent.children():
        if widget_object.__class__.__name__ == child.__class__.__name__:
            parentWidgetList.append(parent.parentWidget())
            parentWidgetList.append(parent.parentWidget().parentWidget())
            parentWidgetList.append(parent.parentWidget().parentWidget().parentWidget())

            for sub in parentWidgetList:
                    for tinychild in sub.children():
                        try:
                            tinychild.setContentsMargins(0, 0, 0, 0)
                        except:
                            pass

Because setNukeZeroMarginsWidget was wrapped in a try/except block the function would exit as soon as it encountered an issue. In my case, one of the children of one of the widgets in parentWidgetList was a QObject, so it didn't have a setContentsMargins method. This caused the entire function to exit prematurely before setContentsMargins could be executed on the desired widgets.
By moving the try/except block further down the line we can still prevent the function from failing should we attempt to setContentsMargins on objects which don't allow that, however it won't exit prematurely any more.

@maty974
Copy link
Author

maty974 commented May 22, 2017

I've updated the code, I think it is better to not parse all the application Widget with QtGui.QApplication.allWidgets()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment