RGB, Float values under pointer

118 views
Skip to first unread message

Arvid Schneider

unread,
Apr 7, 2014, 11:20:48 AM4/7/14
to python_in...@googlegroups.com
Hey Group, 

I am trying to return rgb/float values at the current pixel under the mouse pointer. How would you accomplish that?
The idea is, to press a button which enables the getColor method, so when I hover the mouse over mayas viewport (render view) and click MMB a rgb/float value is returned. 
I couldnt find a way yet using Qt or python.
Maybe someone has an idea.
Arvid

Marcus Ottosson

unread,
Apr 7, 2014, 11:35:18 AM4/7/14
to python_in...@googlegroups.com
Not sure about getting an arbitrary pixel regardless of what lies underneath the cursor, but if your looking to eye-drop an image within a Qt interface (which may be what the Render View is doing, I'm not sure), you can get the cursor position with QCursor.pos() and use that to determine a color within an image using QImage.pixel()

# Psuedo-code
current_position = QCursor.pos()
widget_under_cursor = QApplication.widgetAt(current_position)
relative_position = widget_under_cursor.mapFromGlobal(current_position)
color = my_image.pixel(relative_position)  # Returns a QRgb object


--
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/f31df7eb-d1af-4c40-8bc7-bc5e062c5553%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Marcus Ottosson
konstr...@gmail.com

Justin Israel

unread,
Apr 7, 2014, 5:49:21 PM4/7/14
to python_in...@googlegroups.com
For all areas of the application that are not the 3D viewport, you can render the widget to an image in memory and then query the color under the cursor:

#-------
globalPos = QtGui.QCursor.pos()
widget = QtGui.QApplication.widgetAt(globalPos)

img = QtGui.QImage(widget.size(), QtGui.QImage.Format_ARGB32)
widget.render(img)

pos = widget.mapFromGlobal(globalPos)
color = QtGui.QColor.fromRgb(img.pixel(pos))
#-------

But getting the color sample from the viewport is trickier because it is an OpenGL display and you have to use a different method (Maybe there is a better way than this):

#-------
import tempfile
import maya.OpenMaya as om
import maya.OpenMayaUI as mui

tmp = tempfile.NamedTemporaryFile()

globalPos = QtGui.QCursor.pos()
widget = QtGui.QApplication.widgetAt(globalPos)

view = mui.M3dView.active3dView() 
img = om.MImage()
view.readColorBuffer(img, True)
img.writeToFile(tmp.name, "png")

qimg = QtGui.QImage(tmp.name)
pos = widget.mapFromGlobal(globalPos)
color = QtGui.QColor.fromRgb(qimg.pixel(pos))
#-------


-- justin





Marcus Ottosson

unread,
Apr 8, 2014, 4:30:50 AM4/8/14
to python_in...@googlegroups.com
Oo, that's excellent. I figure there must be a way to sample the whole screen, like hitting Print Screen, and using that for eye-dropping.



For more options, visit https://groups.google.com/d/optout.



--
Marcus Ottosson
konstr...@gmail.com

Arvid Schneider

unread,
Apr 8, 2014, 4:33:25 AM4/8/14
to python_in...@googlegroups.com
Hey Guys,
Thank you very much for your Input Marcus and Justin,
I am currently working on a viewport render, and it would be great to be able query the rendered image color values.
Thanks for the help, ill keep you updated
Arvid
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsub...@googlegroups.com.



--
Marcus Ottosson
konstr...@gmail.com

--
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_maya+unsub...@googlegroups.com.

--
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_maya+unsub...@googlegroups.com.



--
Marcus Ottosson
konstr...@gmail.com

Reply all
Reply to author
Forward
0 new messages