Page MenuHome

PyAPI: Object.ray_cast returns wrong face indices with some modifier combinations
Open, Confirmed, MediumPublic

Description

System Information
Operating system: Windows-7-6.1.7601-SP1 64 Bits
Graphics card: AMD Radeon HD 6900 Series ATI Technologies Inc. 4.5.13399 Core Profile Context 15.200.1062.1003

Blender Version
Broken: version: 2.82 (sub 3), branch: master, commit date: 2019-11-30 17:33, hash: rBf1ac64921b49
Worked: (optional)

Short description of error
With some modifier combinations, Object.ray_cast returns face indices that don't match the indices the object would have if the modifiers were applied or when using Object.evaluated_get. I tested this also in 2.79 and it doesn't work there either.

Attached is a blend file which includes a sligthly modified version of the operator_modal_view3d_raycast.py template. It now prints the indices into the console. I included two different test scenarios just in case.

Exact steps for others to reproduce the error

  1. Open the attached blend file
  2. Open System Console
  3. Run the "operator_modal_view3d_raycast.py" script

Case 1:

  1. Select "Bevel + Decimate" object
  2. See that the Decimate modifier's face count is 24
  3. Search for "RayCast View Operator" and press Enter
  4. Click on the object's faces and check the printed indices inside the Console
  5. Confirm that they go above 23

Case 2:

  1. Enable Developer Extras in Preferences > Interface
  2. Select "Boolean + Bevel Applied" object
  3. Switch into edit mode
  4. Inside Viewport Overlays popover, enable indices
  5. Select the top face and check the index
  6. Switch into object mode
  7. Search for "RayCast View Operator" and press Enter
  8. Click on "Boolean + Bevel" object's top face and check the printed index inside the Console
  9. Confirm that it doesn't match the index of the object which has the modifiers applied

Details

Type
Bug

Event Timeline

Germano Cavalcante (mano-wii) lowered the priority of this task from Needs Triage by Developer to Needs Information from User.Tue, Dec 3, 4:48 PM

If I'm not mistaken, ray_cast now returns the loop_triangle index.
If I'm correct, you can get the poligon index like this:

me.calc_loop_triangles()
polygon_index = me.loop_triangles[0].polygon_index

I don't think this is a bug, can you confirm if this solves your problem?

No, unfortunately that doesn't solve it. :(

This is from the API docs:

Return (result, location, normal, index):
result, Whether the ray successfully hit the geometry, boolean
location, The hit location of this ray cast, float array of 3 items in [-inf, inf]
normal, The face normal at the ray cast hit location, float array of 3 items in [-inf, inf]
index, The face index, -1 when original data isn’t available, int in [-inf, inf]

And this is from the template:

success, location, normal, face_index = obj.ray_cast(ray_origin_obj, ray_direction_obj)

No mention of loop triangles.

And it doesn't always behave wrong. A demonstration with my addon which has face highlighting:

As we can see, only when I move the Wireframe modifier before the Solidify modifier, ray_cast starts to return wrong indices and the highlighting stops working.

Germano Cavalcante (mano-wii) raised the priority of this task from Needs Information from User to Confirmed, Medium.Tue, Dec 3, 6:58 PM

Upon further investigation, I realized that this method always tries to return the original mesh index. (The index of the mesh without modifiers).
See: https://developer.blender.org/diffusion/B/browse/master/source/blender/makesrna/intern/rna_object_api.c$480

But apparently the CustomData_get_layer(&me_eval->pdata, CD_ORIGINDEX) of the Bevel modifier does not work that way.
@Howard Trickey (howardt), do you know why this happens?

We could return the actual mesh index or the looptri index.
This hasn't been done yet to keep the backward compatibility.

CC @Campbell Barton (campbellbarton)