I wanted to make a script to shrinkwrap one mesh around another … but I need to try to do it another way, because this method causes some of the vertices to not move.

Basically, a ray is cast from a vertices in the inverse direction of the normal, and then the vertices is moved to the closest point where the ray intersects one of the triangles. A better way would be to find the shortest distance from a point to a triangle, and then move the vertice onto the triangle. I don’t think I am explaining it very well … oh well.

To use this, first select the mesh to shrink around, then also select the mesh that would be doing the shrinking. Then run the script. A Better version may come about later.

The idea was to allow people to focus on shapes and proportions, without having to worry about vertice placement, then afterwards work on better verice placement. I found that this was rather hard to do with this script, because rays cast from some of the vertices do not intersect with any triangles (the inverse of the normal points slightly off). Maybe somebody might find it interesting though.

```
# shrink-wrap - Nov. 7, 2004
# Created by: Nathan Moore
# Ray Triangle Intersection found at: http://www.cs.virginia.edu/~gfx/Courses/2003/ImageSynthesis/papers/Acceleration/Fast%20MinimumStorage%20RayTriangle%20Intersection.pdf
import Blender
print "
########## Shrink-Wrap ##########"
shrinkObj = Blender.Object.GetSelected()[0]
wrapObj = Blender.Object.GetSelected()[1]
shrinkMesh = Blender.NMesh.GetRaw(shrinkObj.data.name)
epsilon = 0.000001
def cross(v1,v2):
dest = []
dest.append(v1[1]*v2[2]-v1[2]*v2[1])
dest.append(v1[2]*v2[0]-v1[0]*v2[2])
dest.append(v1[0]*v2[1]-v1[1]*v2[0])
return dest
def dot(v1,v2):
return v1[0]*v2[0]+v1[1]*v2[1]+v1[2]*v2[2]
def sub(v1,v2):
dest = []
dest.append(v1[0]-v2[0])
dest.append(v1[1]-v2[1])
dest.append(v1[2]-v2[2])
return dest
def intersectTriangle(orig, dir, v0, v1, v2):
edge1 = []
edge2 = []
tvec = []
qvec = []
edge1 = sub(v1,v0)
edge2 = sub(v2,v0)
pvec = cross(dir, edge2)
det = dot(edge1, pvec)
if det<epsilon:
return 0
tvec = sub(orig, v0)
u = dot(tvec, pvec)
if (u<0.0 or u>det):
return 0
qvec = cross(tvec, edge1)
v = dot(dir, qvec)
if(v<0.0 or u+v>det):
return 0
t = dot(edge2, qvec)
inv_det = 1.0/det
t = t * inv_det
u = u * inv_det
v = v * inv_det
r = []
r.append(t)
r.append(u)
r.append(v)
return r
def shrinkwrap():
global shrinkMesh, wrapObj, shrinkObj
c=0
for v in shrinkMesh.verts:
d = 500000.0
ntmp = [-v.no[0], -v.no[1], -v.no[2]]
for f in wrapObj.getData().faces:
tmp = intersectTriangle(v.co, ntmp, f.v[0], f.v[1], f.v[2])
if tmp != 0:
if tmp[0]<d:
d = tmp[0]
if len(f.v) > 3:
tmp = intersectTriangle(v.co, ntmp, f.v[0], f.v[2], f.v[3])
if tmp != 0:
if tmp[0]<d:
d = tmp[0]
if d != 500000.0:
shrinkMesh.verts[v.index].co[0] = v.co[0]+ntmp[0]*d
shrinkMesh.verts[v.index].co[1] = v.co[1]+ntmp[1]*d
shrinkMesh.verts[v.index].co[2] = v.co[2]+ntmp[2]*d
c = c+1
print c,"/",len(shrinkMesh.verts)
shrinkMesh.update(1)
shrinkwrap()
print "done"
```