Color-code QLineEdits if channel is animated.

28 views
Skip to first unread message

yann19

unread,
Jan 14, 2020, 8:24:49 PM1/14/20
to Python Programming for Autodesk Maya
I have a couple of QLineEdit in my GUI, created using the Qt Designer.

For example, I have 3 QLineEdits in which they are connect to a node's translateX, translateY and translateZ.
These line edits are working as what I have expected but currently I am trying to replicate the what Maya has where the channel box fields will turns red if the time slider is right on a particular keyed frame/ticks, pinkish if in-between of 2 animated frames.

However, for me to update these QLineEdits with color, it seems that in almost all of my methods (eg. update_tx(), update_ty() etc) I will need to write something as follows:
def update_tx(node):
  palette
= QtGui.QPalette()
  palette.setColor(QtGui.QPalette.Base, QtGui.QColor())

    all_frames = cmds.keyframe(node, attribute=attr_name, selected=False, query=True, timeChange=True) or []
    current_frame = cmds.currentTime(query=True)

    if current_frame in all_frames:
      palette.setColor(QtGui.QPalette.Base, QtGui.QColor("red"))
    else:
    
palette.setColor(QtGui.QPalette.Base, QtGui.QColor("pink"))

    self.ui.transX_lineedit.setPalette(palette)

Adding on, as I have also created some custom events too, some of the involved methods may also needs to be implemented in the same manner.
As such, is there a better way that I can attempt this?

Marcus Ottosson

unread,
Jan 15, 2020, 1:59:11 AM1/15/20
to python_in...@googlegroups.com
Ooo, yes that looks slow, at least for anything more than a handful of channels.

What you could do instead is employ a QAbstractTableModel and a QTableView, probably wouldn't need a QStyledItemDelegate but could for pixel-level control. The view would look like the Channel Box (which I presume is already a QTableView) and the model would be able to output not only values for each row, but also a background color like what the Channel Box is doing.

The main hurdle in your case is listening to the data at a performant rate. I think what needs to happen is two things.

1. Listen for connect/disconnect events, there's a callback you can use that's nice and fast. Presumably what the Channel Box is using too
2. Once something is connected, listen for attribute change events. Whenever it changes, you traverse the connection to the associated curve node and query it for whether there's a key on the given frame.

In the case of (2) there's another optimisation you can, and probably have to, make which is that you can only really visualise keys on connections that go to curve nodes. Not connections that go to e.g. some other channel on some other node. I.e. the ones that turn yellow/blue in the Channel Box.

Another optimisation is monitoring when that curve node is changing, and then cache all of its values in your own app/window. Since there will presumably be a frame change more often then keys changing, you should be able to save some juice there.

Overall, it should be possible to get the same look, at a similar - if not very similar - performance to the channel box doing it this way. But, if what you have now is the equivalent a microwavable dish, then this approach is akin to serving at a fine-dining restaurant, edible 24-carat gold flakes, sweet potatoes topped with caviar, and squab alongside turkey. It's hard, is what I'm saying xD

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/d74766d2-29df-496c-9ccc-cd8d72ef81ad%40googlegroups.com.

yann19

unread,
Jan 22, 2020, 4:07:42 PM1/22/20
to Python Programming for Autodesk Maya
Hi Marcus, sorry for the late reply.

Say I am not going for the technicality/ code changed that is happening behind the scene. But rather trying to create a QLineEdit that is the same as the one you see in Maya.

I tried the following:
my_icon="full_keyed.png" #this is the red color label that we see in maya
self.ui.transX_lineedit.addAction(my_icon,QLineEdit.LeadingPosition)

As a start, this replication design is somewhat close to the ones we see in Maya, however, (see attached), I am having some issues such as:
* note the 'small' spacing between the left-outermost edge of the QLineEdit and the red icon
* Icon size is wrong - the image is saved with a 10x16 pixel size, I am expecting it to be a rectangular than squarish..

lineedit_test.jpg



Any insights?

Marcus Ottosson

unread,
Jan 22, 2020, 4:38:25 PM1/22/20
to python_in...@googlegroups.com

Maybe try a stylesheet instead.

from PySide2 import QtWidgets

channelbox = QtWidgets.QWidget()
layout = QtWidgets.QVBoxLayout(channelbox)
layout.setSpacing(0)

for row in range(15):
    layout.addWidget(QtWidgets.QLineEdit())

channelbox.setStyleSheet("""\
QLineEdit {
    border: 0px;
    border-left: 13px solid red;
    padding-left: 3px;
}
""")
channelbox.show()

# border: (Somehow) required for border-left to work
# border-left: Red colouring
# padding-left: Spacing between color and value

image.png

Bear in mind pixel values need to be multiplied by your display DPI scaling. E.g. a width of 5 px would get half as wide on a Retina display with a scaling of 2x. You can get scaling from Qt. Also might be able to vanquish the spacing between line edits with some more CSS. For the colour you could use CSS names like red, hex like #FF0000 or rgb(127, 0, 255).


--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.

yann19

unread,
Jan 22, 2020, 5:00:21 PM1/22/20
to Python Programming for Autodesk Maya
Hi, thank you for getting back.

Yes, this looks amazing!
I was using `QLineEdit{ border-width: 2px; border-style: solid; border-color: none none none red;` initially but your method works very well for my cause.

Going to try this out to see if I can make this channelbox-replication work. :D
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsub...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages