Archive for December, 2016

Fri, Dec 23rd, 2016
posted by jjburton 09:12 PM

First of all, what is ray casting? Ray casting in maya is when one of several api functions is called which when given a vector, start point and shapes to hit – returns points of intersection.

Turns out you can use that information for all kinds of things. For several years now, we’ve been using it to place follicles, cast curves and shapes on other meshes and other functions. A few months ago, I took a quick pass at adding a snap to function to our implementation where a user selects objects to snap, activates the tool and then casts a ray in scene to get a point in space to snap to. It worked but penetrations were rampant and I planned on revisiting it when I had some time.

Recently I found I had small chunks of time and this was one of the things that seemed useful to use one of those chunks for.

The solution we ended up with is as follows:

  1. Objects are selected
  2. The tool is activated
  3. The user left clicks the screen to cast a ray given the options they’ve provided via the marking menu
  4. A locator is generated and continuously updated while the key is held down
  5. When the left click is released, the snap targets:
    1. Cast another ray either along their ‘down’ axis or casting back to the hit point depending or orient mode
    2. The first mesh hit is assumed to be the driven shape of the control or object and provides the offset distance to use
    3. The targets are snapped to a new point in space from the hit point out along the normal of the mesh or nurbs surface of that hit the offset distance detected or provided via the marking menu for fixed amount
    4. The objects are oriented (if required

The core of our functionality for this work on this pass is found:

  • cgm.core.lib.rayCaster — I simplified our call to a more generic rayCaster.cast rather than breaking down multi hit and other modes via separate calls. Also added normal returns from hit points as it was necessary for the offseting
  • cgm.core.classes.DraggerContextFactory.clickMesh — oh so much…
    • Added offsetting
    • Cast plane mode. Can create objects on a function generated cast plane of x,y,z
    • vectorLine — new create type for visualizing vectors and normals
    • data — new create type to just get data
    • object axis args — for orient stuff
    • Duplication — Selected objects are duplicated and snapped with each left click until the tool is dropped.
  • cgm.core.lib.math_utils.get_vector_of_two_points — Self evident.
  • cgm.core.lib.distance_utils.get_pos_by_vec_dist —  Get a point along a ray given a point, ray and distance along that ray

Lessons learned:

  • Not 100% satisfied on current orient mode and I think Bokser may take a stab at that
  • Maybe I was the only one still using it but zoo’s baseMel UI has some serious slowdown in 2016. Normal mc. calls are much much faster. I’m culling out our usage as I can for speedier ui’s.
  • Initially I was using a vector from the hit point to the snap object as the offset vector but it proved to be inconsistent – For example, if you cast to a far side of a mesh with a ‘far’ cast, the offset put it inside the shape that was hit. Ended up finding the normal of the mesh/nurbs shape hit point to be a much better offset vector to use.
  • There are some issues with Maya api 2.0 folks should be aware of if you should want to mess with this stuff yourselves. These were all found to be True in Maya 2016.
    • meshFn.allIntersections — When casting at poly edges, 2.0 fails. 1.0 does not
    • surfaceFn.intersect — Nurbs surface UV returns a different rawUV than 1.0’s. 1.0’s normalizes as expected, 2.0’s does not
    • surfaceFn.normal — Nurbs surface normal return is junk and broken with 2.0. 1.0’s is just fine.

More on all of this, a vid or two and a new tool to play with in a few weeks.