Drop2Ground

I tried to use the script to drop some objects to a BWF-made mesh but it didn’t work. Does anyone know why?


#!BPY

""" Registration info for Blender menus:
Name:		'Drop To Ground'
Blender:	233
Group:		'Misc'
Tooltip:	'Drop objects from current selection on mesh object *Ground*'
"""

import Blender
from Blender import NMesh, Object, Draw
from math import *

def sideofline(PA, PB, PP):
	return ((PA[0]-PP[0])*(PB[1]-PP[1]))-((PB[0]-PP[0])*(PA[1]-PP[1]))

def InsideQuad(p0, p1, p2, p3, p4):
	if (sideofline(p1, p3, p0) >= 0):
		return (sideofline(p1, p4, p0) <= 0) and (sideofline(p4, p3, p0) <= 0)
	else:
		return (sideofline(p1, p2, p0) >= 0) and (sideofline(p2, p3, p0) >= 0)

def InsideTri(p0, p1, p2, p3):
	return (sideofline(p1, p2, p0) >= 0) and (sideofline(p2, p3, p0) >= 0) and (sideofline(p3, p1, p0) >= 0)

def FindZ(TriangleFace, tx, ty, tz, px, py):
	[A, B, C]=TriangleFace.no
	D=-((A*tx)+(B*ty)+(C*tz))
	return -((A*px)+(B*py)+D)/C

def InverseTranslate(InCoords, DeltaVec):
	return [InCoords[0]+DeltaVec[0], InCoords[1]+DeltaVec[1], InCoords[2]+DeltaVec[2]]

try:
	FloatingObjects	= Object.GetSelected()
	Ground		= Object.Get('Ground')
	GroundMesh	= NMesh.GetRawFromObject('Ground')

	InvVec		= Ground.getLocation()
	GroundBounds	= Ground.getBoundBox()
	GBminX = 0
	GBminY = 0
	GBmaxX = 0
	GBmaxY = 0

	name = "OK? %t|drop objects %x1|cancel %x0"
	result = Draw.PupMenu(name)
	if result == 1:
		for [gbx, gby, gbz] in GroundBounds:
			gbx = gbx + InvVec[0]
			gby = gby + InvVec[1]
			gbz = gbz + InvVec[2]
			if GBminX > gbx:
				GBminX = gbx
			else:
				if GBmaxX < gbx: GBmaxX = gbx
			if GBminY > gby:
				GBminY = gby
			else:
				if GBmaxY < gby: GBmaxY = gby

		for DropObject in FloatingObjects:
			[DropX, DropY, DropZ] = DropObject.getLocation()
			if ((DropX <= GBmaxX) and (DropX >= GBminX) and (DropY >= GBminY) and (DropY <= GBmaxY)):
				GroundFaces = GroundMesh.faces
				for GroundFace in GroundFaces:
					GroundVerts = GroundFace.v
					if len(GroundVerts) == 4:
						[x1, y1, z1] = InverseTranslate(GroundVerts[0].co, InvVec)
						[x2, y2, z2] = InverseTranslate(GroundVerts[1].co, InvVec)
						[x3, y3, z3] = InverseTranslate(GroundVerts[2].co, InvVec)
						[x4, y4, z4] = InverseTranslate(GroundVerts[3].co, InvVec)
						p0 = [DropX, DropY, 0]
						p1 = [x1, y1, 0]
						p2 = [x2, y2, 0]
						p3 = [x3, y3, 0]
						p4 = [x4, y4, 0]
						if InsideQuad(p0, p1, p2, p3, p4):
							print 'Moved ', DropObject, ' to a Quad face.'
							DropObject.setLocation(DropX, DropY, FindZ(GroundFace, x1, y1, z1, DropX, DropY))
					else:
						[x1, y1, z1] = GroundVerts[0].co
						[x2, y2, z2] = GroundVerts[1].co
						[x3, y3, z3] = GroundVerts[2].co
						p0 = [DropX, DropY, 0]
						p1 = [x1, y1, 0]
						p2 = [x2, y2, 0]
						p3 = [x3, y3, 0]
						if InsideTri(p0, p1, p2, p3):
							print 'Moved ', DropObject, ' to a Tri face.'
							DropObject.setLocation(DropX, DropY, FindZ(GroundFace, x1, y1, z1, DropX, DropY))

except:
	name = "No Ground object in Scene %t|cancel"
	result = Draw.PupMenu(name)
	if result:
		pass

Blender.Redraw()

i’ve edited this script to work here fine, but … you never know :stuck_out_tongue:
however i ain’t the author and i forgot who that noble person was …

aw forgot … the ground object has to be named * Ground * for script ot work as expected

Thanks for writing that error checking code. I hate doing that stuff!

I have tried your rewrite, but it still doesn’t work. I had name the mesh “Ground” too.

ja, known thing - you’ll have to manually unwrap the lines that forum wrapped (some code should be on one line only) - or else I can optionally send you the*.py thing via email.

Ground should have normals pointing outwards

btw - you had selected all to-drop objects?

I tried what you suggested on the normals thing and it works. It seemed the mesh had normals pointing downwards.

sparrow, nice you’ve got it doing what it’s supposed to do :stuck_out_tongue:

@ harkyman - you are the author?

Yes. Glad people are using it.

harkyman, blender people project looks deadly serious. any chance of a lite version that wouldn’t require mysql?

i have cca 45 py things which i have either edited/upg’ded or just cleaned up comments etc. so i’ve lost track of respective authors.

for some reason ever since 2.3 i think when you add a grid the normals are pointing down. kind of confusing the first couple of times. also don’t forget to do ‘ctrl A’ on all objects.