Developer Forum

Using bpy.ops.ui.eyedropper_color

Hello there,

It seems that it’s been asked a few times on the internet, but it never got any satisfying answer.
I am trying to pick a color in the viewport, I used to use bgl.glReadPixels but it seems it’s been deprecated in 2.91 in some way, it only returns black in my case and I was not able to find a workaround.

The Color widget picker on the other hand seems to be the perfect tool for the job. However I hit two issues:

  • I don’t want the color widget, I just want the color picker. bpy.ops.ui.eyedropper_color() in my own operator only returns {‘PASS_THROUGH’}.
  • Let’s say I accept to use a color property in the UI, I still want to run some functions before running the picker, which is not possible AFAIK? The set or update functions only happen after I finished picking.

If anyone has an idea how to add a simple color picker, I’d appreciate the help! :slight_smile:

I support it. I can get background and grid color but not object color. I get always black pixel on the object
buffer = bgl.Buffer(bgl.GL_FLOAT, [1, 3])
bgl.glReadPixels(self.mouse[0], self.mouse[1], 1, 1, bgl.GL_RGBA, bgl.GL_FLOAT, buffer)

1 Like

Any updates? Even I’m wanting to do the exact same, but am not able to.

I got it to work in the end, bgl is deprecated but you can use the new gpu module: GPU Module (gpu) — Blender Python API

Here is an example:

x,y = self.mouse_region_x, self.mouse_region_y

buffer = gpu.state.active_framebuffer_get()
pixel = buffer.read_color(x, y, 1, 1, 3, 0, 'FLOAT')
pixel.dimensions = 1 * 1 * 3
pix_value = [float(item) for item in pixel]
1 Like

Worked like a charm! Thankyou so much!

I’m not a developer (yet) but I want to hotkey the color picker to pick the Color Attributes in grease pencil while drawing/painting. is there any way to do that please?

1 Like