I am using a raycast (just like in the template "operator_modal_view3d_raycast.py") to get a face index. The object the ray is cast on is a cube with inverted faces (directing inwards). Unfortunately the ray returns the index of the first face it hits although it is a backface. How can i make the raycast ignore backfaces so it returns the index of the first frontface it hits?

  • I think the way to go is: determine if backface or frontface was hit (compare ray vector with normal vector?) and then, if backface was hit, start another ray from hit point. Had the hope there would be a more simple solution for coding greenhorns like me. – CoD May 29 at 9:29
up vote 1 down vote accepted

Did it. I did not even bother if front or backface was hit because the first hit is always backface, so i just added a small offset to the hit vector (depending on view vector) and used it as new origin for a second raycast.

ray_origin_offset = mathutils.Vector((0, 0, 0))
if view_vector[0] < 0:
    ray_origin_offset[0] = -0.0001
else:
    ray_origin_offset[0] = +0.0001
if view_vector[1] < 0:
    ray_origin_offset[1] = -0.0001
else:
    ray_origin_offset[1] = +0.0001
if view_vector[2] < 0:
    ray_origin_offset[2] = -0.0001
else:
    ray_origin_offset[2] = +0.0001          
ray_origin = hit + ray_origin_offset
ray_target = ray_origin + view_vector
hit, normal, face_index = obj_ray_cast(obj, matrix)
  • Not sure why you are adding component-wise to the offset vector. If dir is a unit direction vector then calling ray_cast(origin, dir) and get a hit, then ray_cast(hit + 0.001 * dir, dir), until no hit, would be my take on coding. – batFINGER Jun 7 at 13:40
  • @batFINGER Because i am a very bad coder (this is, still, my first project) so ALL my code is most likely quite verbose and circuitous. That might be a bad thing from the perspective of an experienced programmer but not only am i hardly able to make it better (atm... i will learn it over time maybe) but it also helps me to understand my own code (even when picking it up after an adjournment). Maybe i will make a disclaimer from now on when posting code: "Warning, the code is noob-level"^^ (nevertheless i am quite proud how far my project has progressed so far and how stable and fast it runs). – CoD Jun 7 at 21:38
  • No disclaimers necessary. It's not a matter of code, more a matter of method Is what I was alluding to. BTW it's kudos, and upvotes from me on both q&a because you have nutted it out yourself. – batFINGER Jun 8 at 5:57
  • @batFINGER thx g.. I was not offended (hope i did not sound like i was), just told the truth of why i did it that way. I realy appreciate your answer(s) and learn a lot by doing research on what you write. :) (sadly i cannot upvote comments) – CoD Jun 8 at 11:19

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.