15

« **on:** February 16, 2018, 10:27:33 AM »
Here's an example of how to use a hitting ray to get measurements simply by calculating the distance of two 3D points:

`# Calculate distance between a ray's in- and out-hits`

# at a fixed direction (> ray_dir)

# based on the surface normal

import mmapi

from mmRemote import *

import mm

from mm.mm_math import *

import math

# initialize connection

r = mmRemote()

r.connect()

# get object-space bounding box of selected object

(fMin,fMax) = mm.get_selected_bounding_box(r)

# construct a ray from far-away-pt through center of bbox

center = lerpv3(fMin, fMax, 0.5)

diag = subv3(fMax, fMin)

ray_dir = (1.0,0.0,0.0) # fixed direction (here e.g.: along x-axis)

# find hit "in" with "positive" ray direction:

ray_origin = subv3(center, mulv3s(ray_dir, 25.0*max(diag)))

(bHit, inFrame) = mm.find_ray_hit(r, ray_origin, ray_dir)

#print "in_hit: ", bHit, " world pos: ", mm.to_world(r, inFrame.origin), "world units" # uncomment to print output values

#mm.create_pivot(r, inFrame) # uncomment to visualize: drops a pivot at in-hit

# find hit "out" along ray with "negative" ray direction:

ray_origin = subv3(center, mulv3s(negv3(ray_dir), 25.0*max(diag)))

(cHit, outFrame) = mm.find_ray_hit(r, ray_origin, negv3(ray_dir))

#print "out_hit along ray: ", cHit, " out pos: ", mm.to_world(r, outFrame.origin), "world units" # uncomment to print output values

#mm.create_pivot(r, outFrame) # uncomment to visualize: drops a pivot at out along ray

# calculate distance between in and out hit along ray:

dist = math.sqrt((outFrame.origin[0] - inFrame.origin[0])**2 + (outFrame.origin[1] - inFrame.origin[1])**2 + (outFrame.origin[2] - inFrame.origin[2])**2)

print "distance along ray: ", mm.to_world(r, dist), "world units"

# find hit "out" along in-hit's normal direction:

ray_dir = inFrame.z

ray_origin = subv3(inFrame.origin, mulv3s(ray_dir, 25.0*max(diag)))

(cHit, outFrame) = mm.find_ray_hit(r, ray_origin, ray_dir)

#print "out_hit along normal: ", cHit, " out pos: ", mm.to_world(r, outFrame.origin), "world units" # uncomment to print output values

#mm.create_pivot(r, outFrame) # uncomment to visualize: drops a pivot at out along in-hit's normal

# calculate distance between in and out hit along normal:

dist = math.sqrt((outFrame.origin[0] - inFrame.origin[0])**2 + (outFrame.origin[1] - inFrame.origin[1])**2 + (outFrame.origin[2] - inFrame.origin[2])**2)

print "distance along normal: ", mm.to_world(r, dist), "world units"

#done!

r.shutdown()

The first result follows the ray while the second uses the in-hit's surface normal (see attached image)