2
$\begingroup$

I need to reproduce what the shrinkwrap modifier does but without creating geometry.

I have an xml containing a list of locations "Seat" (x,y,z) in world space and I need to loop through them projecting each one, (only in z axis) onto a mesh given by an object. Once they are projected I need to rewrite the xml with the new locations.

So, I'm struggling with the raycast, I manage to make it work but I really don't understand what I'm doing, let me show you what I have right now:

def conformHeight(tree, filename):

    for venue in tree:

        for Sector in venue.findall('Sector'):

            for Seat in Sector:

                # Get Position Data
                posX, posY, posZ = float(Seat.get('px')), float(Seat.get('py')), float(Seat.get('pz'))


                obj = bpy.context.object

                def ray_cast():
                    location = obj.ray_cast(posZ, (0.0, 0.0, 1.0))

                    return location

                loc = ray_cast()

                print(loc)

In the ray_cast() I'm not sure where to add the selected object over which I want to project the locations neither to get in return the hit locations.

I have searched all over the internet and all the info I find is very confusing as mostly refers to camera ray tracing.

How should the function be written?

$\endgroup$
1
$\begingroup$

Object.ray_cast takes three parameters: origin, direction and an optional maximum distance.

The parameters origin and direction should both be lists/tuples of length 3 or mathutils.Vector, e.g.

sol = obj.ray_cast((posX, posY, posZ), (0, 0, 1))
location = sol[1]

If you're not sure about whether the original surface is below or above the target, you might want to offset the origin in the function, e.g.

origin = Vector((posX, posY, posZ))
direction = Vector((0, 0, 1))
sol = obj.ray_cast(origin - direction * 100000, direction)
location = sol[1]

Additionally, you have to keep in mind that ray_cast uses local space coordinates. If your target mesh is translated or rotated, you have to convert origin and direction from world space to the target's coordinate system first, and maybe convert the result vector back to world space afterwards. Or you temporarily create a copy of the object with transformations applied so that world space and local space are the same and then cast with the temporary object.

|improve this answer|||||
$\endgroup$
  • $\begingroup$ Ok, after some tests and following your guides I'm getting some results, just correct me if I misunderstood when I say; in obj.ray_cast obj would be the object onto which all origins are projected? $\endgroup$ – Juan Manuel Lynch Sep 20 '18 at 18:36
  • $\begingroup$ Yes, that's correct. obj is the object that you cast onto. The mesh that the rays originate from isn't given to the function as an object. Instead you have to loop over the origin object's vertices (or other points) that you put into the ray_cast function as origin vectors. $\endgroup$ – binweg Sep 20 '18 at 18:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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