PySide/ PyQt

PySide/ PyQt Statusbar with colored Popup

I recently wrote an PySide UI where I needed Feedback for the user when an exception occured. I did not catch the underlying functions in any special way and just let them propagate to the top. But since the command line was hidden I looked for a way to make those exceptions visible to the user – and especially in a readable and noticable way.

So I implemented this Statusbar. Nothing fancy and coded real quick but maybe it helps someone else

<pre>from PySide import QtGui, QtCore

class ExceptionStatusBar(QtGui.QStatusBar):
    EXCEPTION_COLOR = QtGui.QColor("red")

    def __init__(self, level, **kwargs):
        super(ExceptionStatusBar, self).__init__(**kwargs)

        # Prepare background
        self.setAutoFillBackground(True)

        # Insert exception hook
        import sys
        sys.excepthook = self.create_excepthook(sys.excepthook)

    #
    # Background Property
    # -----------------

    def set_color(self, color):
        self.setStyleSheet("background-color: rgb({0}, {1}, {2});"
                           .format(color.red(), color.green(), color.blue()))

    def get_color(self):
        # Will not be used
        return self.palette().color(QtGui.QPalette.Background)

    backgroundColor = QtCore.Property(QtGui.QColor, get_color, set_color)

    def create_excepthook(self, sys_hook):
        def custom_hook(exc_type, exc_value, tb):
            self.handle_exception(exc_type, exc_value)
            return sys_hook(exc_type, exc_value, tb)

        return custom_hook

    def handle_exception(self, exc_type, msg):
        # Need to make it a property of the instance instead it is garbage collected
        self._anim = QtCore.QPropertyAnimation(self, "backgroundColor")
        self._anim.setDuration(800)
        self._anim.setStartValue(QtGui.QColor(self.EXCEPTION_COLOR))
        self._anim.setEndValue(self.palette().color(QtGui.QPalette.Background))

        # Actually showing the message
        self.showMessage("{0}: {1}".format(exc_type.__name__, msg), timeout=2000)

        self._anim.start()</pre>

By inserting my own excepthook in the sys.excepthook I get access to all Exceptions that will eventually happen. The QPropertyAnimation animates the Background Color.

Standard
Python, VRED

Custom Key Value Pairs in VRED (Resolved

Ok so I am trying for some time now to get something like custom attributes on a VRED node. I would like to use this as storage for meta data in the scene. But the VRED Python API, as always, does not give much help.

But so far I managed to store some information on an ValuePair attachment which is stored on every node in the AttachmentMap attachment.

vred_attachment_valuepair

RESOLVED:
Ok so apparently this was just an issue due to the old API (VRED 2016) where the function getMString was missing!

Two function to set and get key value pairs may look like this (fast prototype – no testing):
Note that this code only works in VRED > 2016

import vrFieldAccess
import vrNodeUtils

def get_value_pair(node, key):
    fields = node.fields()

    if not fields.hasAttachment("ValuePair"):
        raise IndexError("Node '{0}' has no key '{1}'"
                         .format(node.getName(), key))

    attachment = vrFieldAccess.vrFieldAccess(fields.getAttachment("ValuePair"))

    keys = attachment.getMString("key")

    if key in keys:
        return attachment.getMString("value")[keys.index(key)]

    raise IndexError("Node '{0}' has no key '{1}'"
                     .format(node.getName(), key))

def set_value_pair(node, key, value):
    fields = node.fields()

    if not fields.hasAttachment("ValuePair"):
        attachment = vrNodeUtils.createAttachment("ValuePair")
        fields.addAttachment(attachment)
        attachment = vrFieldAccess.vrFieldAccess(attachment)
    else:
        attachment = vrFieldAccess.vrFieldAccess(fields.getAttachment("ValuePair"))

    keys = attachment.getMString("key")
    values = attachment.getMString("value")

    if key in keys:
        values[keys.index(key)] = value
    else:
        keys.append(key)
        values.append(value)

    attachment.setMString("key", keys)
    attachment.setMString("value", values)

    return True
Standard