1
$\begingroup$

I made a custom gizmo with OpenGL

enter image description here

I want to do something after clicking on points like print("Hello World") and change the color of selected point.

enter image description here

import bpy
import gpu
from gpu_extras import batch
import bgl
from random import random


positions = (
    (-1, -1, -1), (+1, -1, -1),
    (-1, +1, -1), (+1, +1, -1),
    (-1, -1, +1), (+1, -1, +1),
    (-1, +1, +1), (+1, +1, +1))
    
colors = (
    (1, 1, 1,1), (1, 1, 1,1),
    (1, 1, 1,1), (1, 1, 1,1),
    (1, 1, 1,1), (1, 1, 1,1),
    (1, 1, 1,1), (1, 1, 1,1))

shader = gpu.shader.from_builtin("3D_FLAT_COLOR")
batch = batch.batch_for_shader(shader, "POINTS", {"pos": positions, "color": colors})

def draw():
    shader.bind()
    bgl.glPointSize(5)
    batch.draw(shader)

bpy.types.SpaceView3D.draw_handler_add(draw, (), "WINDOW", "POST_VIEW")
$\endgroup$
  • 1
    $\begingroup$ Related blender.stackexchange.com/questions/193145/… $\endgroup$ – batFINGER Sep 3 at 23:33
  • $\begingroup$ Hi @batFINGER this question is different I just want to know is it possible to use raycast to select the OpenGL points? $\endgroup$ – Seyed Morteza Kamali Sep 4 at 4:28
  • $\begingroup$ A solution with no raycast is effectivelly in the code indicated by @batFINGER. lines 105/110. $\endgroup$ – lemon Sep 4 at 5:22
  • $\begingroup$ @lemon hmmm do you mean Find the closest corner using a dot product from the view. I actually confused by that code because it was complex so I tried to find the easy way. $\endgroup$ – Seyed Morteza Kamali Sep 4 at 5:31
  • 1
    $\begingroup$ @SeyedMortezaKamali similarly as if your point shares the same 2d coord as the mouse (or close enough) Can see no evidence in question re it being about raycasting, Is it supposed to be easy? Have offered this advice before, for say an hour, disconnect from the internet and resist the urge to ask another question here (or via comment) until you understand how something works. If need be add some print statements to see the values. Resist the urge on getting an error and posting "I tried .... It doesn't work". The fine line between seeking answers and just wanting them given to you. $\endgroup$ – batFINGER Sep 4 at 6:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.