raycast template broken?

the operator_modal_view3d_raycast.py - template is not working in every situation. can someone confirm this? in the image attached you can see that i can only select the objects already selected.

post a .blend where the template fails :slight_smile:

Judging by the comments in the script, all it should do is let you add to your selection by left clicking only. Hence with nothing selected, run the script, hit space , type raycast, choose RayCast View Operator, then start left clicking on unselected stuff.

the blend file: ray.blend (508 KB)

yeah, that’s a little weird. If you rotate the scene upside down you can select the rest, interesting to debug! You will learn more by debugging this yourself :slight_smile:

is there any other way to get the object under the cursor?

like, press C, let go, cursor turns to a circle, and left click + drag over the objects will select them (add items to the current selection)

@zeffii: is this blenderscripting.blogspot.com your blog?

yep, i use it to keep track of all the cool stuff :slight_smile:

:spin: aaaaaaaaaahhhh!!! i spent so many hour on ‘how do i get the obj under the cursor’-issue and a simple bpy.ops.view3d.select_border/circle(…) does the whole trick! thank you!!!

and your blog is a treasure full of bpy-madness! i will spend a lot of time reading it. :slight_smile:

Although .ops methods can come with some side-effects (you will hear this statement repeatedly from others) sometimes the ops method is exactly what you want/need. Now you exposed an interesting behaviour of the raycast template, might still be worth debugging - for the journey.

Feel free to stick questions (or suggestions, improvements) on to that blog, i usually get around to answering questions or point into the right direction.

and again… the bpy.ops.view3d.select_border(…) does not work cause it selects all objs in its area and not only the one overlaying all the others.

maybe i should debug the raycast-tamplate…

i found the first bug in the template.

change

61:                 length_squared = (hit - ray_origin).length_squared

to

61:                length_squared = (matrix * hit - ray_origin).length_squared

the mistake was that the hit location is local but we need the global hit location. that means we need to map the location back to world space.

that fixes all issues using the PERSP-Perspective. But with the ORTHO-Perspective it still doesn’t work correctly in all cases.

appears to me that you have to factor in the view matrix too, matrix calculations aren’t my forte but : https://gist.github.com/3028278

and replace

    # get the ray from the viewport and mouse
        view_vector = view3d_utils.region_2d_to_vector_3d(region, rv3d, coord)
        ray_origin = view3d_utils.region_2d_to_origin_3d(region, rv3d, coord)

with

    # get the ray from the viewport and mouse
    if rv3d.view_perspective == 'ORTHO':
        view_vector = -1 * view3d_utils.region_2d_to_vector_3d(region, rv3d, coord)
        ray_origin = view3d_utils.region_2d_to_origin_3d(region, rv3d, coord) + view_vector * - ray_max / 2
    else:
        view_vector = view3d_utils.region_2d_to_vector_3d(region, rv3d, coord)
        ray_origin = view3d_utils.region_2d_to_origin_3d(region, rv3d, coord)

@zeffii

appears to me that you have to factor in the view matrix too, matrix calculations aren't my forte but : https://gist.github.com/3028278

that eye-location-code is nice. i’m going to try it out.

edit: tried it: seams that the eyelocation = rv3d.view_location in ORTHO and eyelocation = region_2d_to_origin_3d in PERSP