Convert scene raycast to object raycast?

I’d like to raycast a specific object, but can’t seem to get it working.
What is needed when doing this?
I have tried multiplying by the objects matrix, but no luck…


co = event.mouse_region_x, event.mouse_region_y
region = context.region
r_data = context.space_data.region_3d
    
origin = region_2d_to_origin_3d(region, r_data, co)
direction = region_2d_to_vector_3d(region, r_data, co)

        
hit1, location1, normal, index, object, mat =\
          context.scene.ray_cast(context.view_layer, origin, direction)
        

you’re going to have to be more specific. what are the results you’re getting, if any? that’s a fairly standard raycast snippet and should work, so you’ll have to provide some context here. a blend file with the script in it would be even better.

note that you can’t raycast to a specific object with scene.ray_cast. as the name implies, it raycasts anything that is in the scene (and visible), so if your target object is obscured by another mesh- the other mesh will be hit. The only way to avoid this is to either hide the mesh ahead of time and then restore it (not recommended, it can be slow on dense scenes), or move the origin of the ray past the hit location and re-fire it recursively until either your target object is hit or nothing is hit.