Scene.RayCast / Hidden Object Issues


I’m trying to figure out a way to possibly raycast/get the object/hit face under the mouse cursor, and ignore any objects that are “hidden” in the view, like during Isolate Mode.

I am currently using:
scene.ray_cast(viewlayer, ray_origin, view_vector)

But it appears that Blender when doing scene ray_cast, will hit objects, even if they are not visible, like during Isolation…? So it SEEMS like i have to actually get objects not visible to camera / hide them for real, then reveal again later…??

I got this working… BUT… with my highpoly scene/200+ objects, it can be pretty SLOW to unhide everything at the very end/load back into Memory… Like maybe 3-5 seconds or something to reload vs. instant.

Is there no way for Raycast to ignore those objects not visible/“hidden” during Isolate mode?
Or maybe I gotta use another raycast method/some other method…

I’m trying to get the Object/Face Under the Mouse, and use this:
bpy.ops.mesh.select_linked_pick(deselect=False, delimit={self.delimitType}, index=index)

I want to be able to Select Linked/use that command exactly like that. So it seems like that’s the best method to use, but I need to supply the Face Index under the mouse… The other select_linked method doesn’t give you the option for deselect, only select/replace your selection i think.

Thank you!

I guess I COULD also do something like

Raycast to Scene
Hit Object
If Object is not Visible in Viewport
Hide Object that was hit/not visible and store for later
Raycast FROM hit Object loc Or Camera again, along same Ray
If next object hit is Visible, then can use that…

But that seems a bit crazy as well, but maybe still faster the hiding/unhiding potentially hundreds of objects… hmmmmmmmmm

I got it working, doing basically what I said above.

I thought maybe I could do it another way/maybe still can, but might be slightly more complicated/break other things. Other option maybe is to get Mouse Location / Run the Select Command, so that objects selected. Then check visibility, altho the select may only work on Visible objects, so not sure about that anyways.

Right now I am doing a While Loop, checking a variable, called isVisible. In my While Loop I do raycast, and make sure Object is not None and check its viewport visiblity. If it’s invisible, I Hide it/ Add to an Array to unhide later, and the loop runs again. If next object that is raycast against is visible, i get outta the loop and can continue.

scene = context.scene
region = context.region
rv3d = context.region_data
coord = event.mouse_region_x, event.mouse_region_y
viewlayer = context.view_layer

view_vector = view3d_utils.region_2d_to_vector_3d(region, rv3d, coord)
ray_origin = view3d_utils.region_2d_to_origin_3d(region, rv3d, coord)

 isVisible = False

        #Need to Collect/Hide Objects "Visible", but hidden in the Viewport, so can Raycast to proper object/mesh instead.
        #Otherwise are hitting the invisible object/mesh infront of one we might want, and never get a proper selection.
        while(isVisible != True):
            result, location, normal, index, object, matrix = context.scene.ray_cast(viewlayer, ray_origin, view_vector)  #ray_target, ray_goal   # ray_origin, ray_target

            if object != None:
                isVisible = object.visible_get(view_layer=viewlayer)

                if isVisible == False:
                    object.hide_viewport = True
                    print("Hit Hidden Object: " + + ": Hiding and Raycast again!")

                if isVisible == True:
                    if object.type == 'MESH':
                        self.hitIndex = index
                        self.hitObject = object
                        print("Hit Visible Object/Index: " + + " Index:" + str(self.hitIndex))
                self.hitObject = None
                self.hitIndex = -1
                isVisible = True  #Set True here, so Loop stops, if clicking into Empty Space.  So can Clear my selection/continue my script in another spot. 

I noticed in my one addon/script, in 2.93, my bpy.ops.mesh. select_linked_pick no longer wants to seem to work…

I am still getting/setting the index as I was before, but something is not right… I wonder if the code/method was changed, or maybe I need to specify the object_index now…

bpy.ops.mesh.` `select_linked_pick` (*deselect=False* , *delimit={'SEAM'}* , *object_index=- 1* , *index=- 1* )
bmn = bmesh.from_edit_mesh(me)
index = len(bmn.verts)+ len(bmn.edges) + self.hitIndex
bpy.ops.mesh.select_linked_pick(deselect=False, delimit={self.delimitType}, index=index)

Ok, yeah that was it… I looked at the Scripting output and my index was same as what it was saying there, but i noticed the object_index was 0. So I added that into my own code, and now it works again.

bpy.ops.mesh.select_linked_pick(deselect=True, delimit={self.delimitType}, object_index=0, index=index)