0
$\begingroup$

My Blender file has only one object, a sort of quanset hut looking mesh sitting on and above the XY plane, with its local origin at the world origin, (0,0,0). It is centered over the Y axis and extends into positive and negative Y territory.

I'm trying to use ray_cast to measure the distance of points on my mesh from the Y axis, so I have written this test to start at (0,0,0), and shoot a ray up at a 45 degree angle (1,0,1), which should intersect with my mesh.

When I ran this program, I got a hit, but it was way too close to the origin to be my mesh, which has a diameter of about 2". The hit was at (0.0182, 0.0000, 0.0182), which is the right direction all right, but way too close. When I scaled my mesh, the hit distance did not change, which meant that it was not "hitting" my mesh.

So what IS it hitting? There is no other object.

I tried moving up the Y axis to (0,1,0), no hit. Then I tried some other starting positions:

origin at (0,0,0) produced a hit at (0.0182, 0.0000, 0.0182) origin at (0,.1,0) produced a hit at (0.0171, 0.1000, 0.0171) origin at (0,.2,0) produced a hit at (0.0161, 0.2000, 0.0161) origin at (0,.21,0) produced a hit at (0.0160, 0.2100, 0.0160) origin at (0,.22,0) produced a hit at (0.0159, 0.2200, 0.0159) origin at (0,.23,0) produced no hit

Could I be hitting the 3D cursor or something? That would be weird

Here's the code:

import bpy
from mathutils import Vector

ob = bpy.data.objects['Brep.010']
origin = Vector((0,.23,0))
direction = Vector((1,0,1))
hit, loc, norm, face = ob.ray_cast(origin, direction)

if hit:
    print("Hit at ", loc, " (local)")
else:
    print("No HIT")
$\endgroup$
4
  • $\begingroup$ PS - since my only object has its local origin at the world origin, (0,0,0), I assumed I did not need to do any conversion of origin or direction parameters to ray_cast(). $\endgroup$
    – WillDotson
    Aug 12, 2021 at 23:42
  • $\begingroup$ I thought of all those things, so I moved my mesh far away, and got the exact same results. drive.google.com/file/d/1yO5FrcP5nxwxOQxDZE4HEjAcXdjN1hBZ/… $\endgroup$
    – WillDotson
    Aug 13, 2021 at 17:10
  • $\begingroup$ Move it far away in edit mode ie change local coordinates and will get a different result. $\endgroup$
    – batFINGER
    Aug 13, 2021 at 17:41
  • $\begingroup$ You gave me a profound insight by pointing out that moving an object in edit mode is not the same as moving in object mode. Now to figure out how to get my units to agree... THANKS $\endgroup$
    – WillDotson
    Aug 13, 2021 at 19:55

1 Answer 1

1
$\begingroup$

Working correctly.... No Ghosts, an Empty Proof

Object raycast is in local space. Unapplied object transforms do not effect local coordinates. The location of the object origin (aka the orange dot) is the local coordinate (0, 0, 0). The result obtained will be in blender units. If you are using imperial units there will be a conversion between native value and converted result in UI. (Much the same as native unit of rotation is radians, default display is degrees)

enter image description here Scene units changed to None, showing dimensions in blender units, and empty placed at hit result.

Hit at  <Vector (0.0182, 0.0000, 0.0182)>  (local)

And again in imperial (yuck) units. 1 BU is one meter, or just over 3 feet. The result is in blender units, the UI is showing feet.

Blender Python convert units to imperial

enter image description here

Test script with empty parented to object at local hit location, to get a visual on result.

import bpy
from mathutils import Vector
context = bpy.context

ob = bpy.data.objects['Brep.010']
origin = Vector((0, 0, 0))
direction = Vector((1, 0, 1))
hit, loc, norm, face = ob.ray_cast(origin, direction)

if hit:
    print("Hit at ", loc, " (local)")
    bpy.ops.object.empty_add(location=loc)
    context.object.parent = ob
else:
    print("No HIT")
$\endgroup$
4
  • $\begingroup$ But the units are still a bit off, which is one reason I didn't think of it. The hit at .0182 in blender units measures 1.00xx inches on the screen, but it should measure .0254 Inches. $\endgroup$
    – WillDotson
    Aug 13, 2021 at 20:29
  • $\begingroup$ Huh???? you appear to have this A about T. surely it's (as shown in link) 12 * 0.0182 / 0.304 = 0.7165 which is also the empty X & Z coordinate when displayed in inches. The length of vector from origin to empty coordinate in blender units is 0.025 which converted to inches is approx 1. $\endgroup$
    – batFINGER
    Aug 13, 2021 at 20:41
  • $\begingroup$ Oh, sorry, I'm stupid. And thanks a million. Is the preferred thing to do to use "Unit Scale" to make my units match? $\endgroup$
    – WillDotson
    Aug 13, 2021 at 20:43
  • $\begingroup$ Or, will I need to convert to and from meters in every statement in my code (which must now be expanded). $\endgroup$
    – WillDotson
    Aug 13, 2021 at 20:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .