3
Name: 'Drop Onto Ground'
6
Tooltip: 'Drop the selected objects onto "ground" objects'
8
__author__= "Campbell Barton"
9
__url__= ["blender.org", "blenderartists.org"]
15
# --------------------------------------------------------------------------
16
# Drop Objects v1.0 by Campbell Barton (AKA Ideasman42)
17
# --------------------------------------------------------------------------
18
# ***** BEGIN GPL LICENSE BLOCK *****
20
# This program is free software; you can redistribute it and/or
21
# modify it under the terms of the GNU General Public License
22
# as published by the Free Software Foundation; either version 2
23
# of the License, or (at your option) any later version.
25
# This program is distributed in the hope that it will be useful,
26
# but WITHOUT ANY WARRANTY; without even the implied warranty of
27
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28
# GNU General Public License for more details.
30
# You should have received a copy of the GNU General Public License
31
# along with this program; if not, write to the Free Software Foundation,
32
# Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
34
# ***** END GPL LICENCE BLOCK *****
35
# --------------------------------------------------------------------------
38
from Blender import Draw, Geometry, Mathutils, Window
43
GLOBALS['GROUND_SOURCE'] = [Draw.Create(1), Draw.Create(0)]
44
GLOBALS['GROUND_GROUP_NAME'] = Draw.Create('terrain')
45
GLOBALS['DROP_AXIS'] = [Draw.Create(1), Draw.Create(0)] # on what axis will we drop?
46
GLOBALS['DROP_OVERLAP_CHECK'] = Draw.Create(1) # is the terrain a single skin?
48
GLOBALS['MOUSE'] = None
50
def collect_terrain_triangles(obs_terrain):
52
me = bpy.data.meshes.new()
54
for ob in obs_terrain:
55
# this matrix takes the object and drop matrix into account
56
ob_mat = ob.matrixWorld # * drop_matrix
58
def blend_face_to_terrain_tris(f):
59
cos = [v.co*ob_mat for v in f]
60
if len(cos) == 4: return [(cos[0], cos[1], cos[2]), (cos[0], cos[2], cos[3])]
61
else: return [(cos[0], cos[1], cos[2]), ]
65
try: me.getFromObject(ob)
68
for f in me.faces: # may be [], thats ok
69
terrain_tris.extend( blend_face_to_terrain_tris(f) )
71
me.verts = None # clear to save ram
74
def calc_drop_loc(ob, terrain_tris, axis):
75
pt = Mathutils.Vector(ob.loc)
81
for t1,t2,t3 in terrain_tris:
82
#if Geometry.PointInTriangle2D(pt, t1,t2,t3):
83
isect = Mathutils.Intersect(t1, t2, t3, axis, pt, 1) # 1==clip
85
if not GLOBALS['DROP_OVERLAP_CHECK'].val:
86
# Find the first location
90
isect_len = (pt-isect).length
91
if isect_len < isect_best_len:
92
isect_best_len = isect_len
96
isect_best_len = (pt-isect).length
102
Draw.PupMenu('The Group name does not exist')
104
Draw.PupMenu('There is no active object')
105
def error_noground():
106
Draw.PupMenu('No triangles could be found to drop the objects onto')
108
Draw.PupMenu('No objects selected to drop')
110
# event and value arnt used
111
def terrain_clamp(event, value):
113
sce = bpy.data.scenes.active
114
if GLOBALS['GROUND_SOURCE'][0].val:
115
obs_terrain = [sce.objects.active]
116
if not obs_terrain[0]:
120
try: obs_terrain = bpy.data.groups[ GLOBALS['GROUND_GROUP_NAME'].val ].objects
125
obs_clamp = [ob for ob in sce.objects.context if ob not in obs_terrain and not ob.lib]
130
terrain_tris = collect_terrain_triangles(obs_terrain)
137
if GLOBALS['DROP_AXIS'][0].val:
138
axis = Mathutils.Vector(0,0,-1)
140
axis = Mathutils.Vector(Window.GetViewVector())
143
loc = calc_drop_loc(ob, terrain_tris, axis)
147
# to make the while loop exist
148
GLOBALS['EVENT'] = EVENT_EXIT
151
# UI STUFF ------------------------
153
GLOBALS['DROP_AXIS'][0].val = 1
154
GLOBALS['DROP_AXIS'][1].val = 0
157
def do_axis_view(e,v):
158
GLOBALS['DROP_AXIS'][0].val = 0
159
GLOBALS['DROP_AXIS'][1].val = 1
162
def do_ground_source_act(e,v):
163
GLOBALS['GROUND_SOURCE'][0].val = 1
164
GLOBALS['GROUND_SOURCE'][1].val = 0
167
def do_ground_source_group(e,v):
168
GLOBALS['GROUND_SOURCE'][0].val = 0
169
GLOBALS['GROUND_SOURCE'][1].val = 1
172
def do_ground_group_name(e,v):
173
try: g = bpy.data.groups[v]
175
if not g: error_nogroup()
183
def terain_clamp_ui():
185
# Only to center the UI
186
x,y = GLOBALS['MOUSE']
190
Draw.Label('Drop Axis', x-70,y+120, 60, 20)
192
GLOBALS['DROP_AXIS'][0] = Draw.Toggle('Z', EVENT_REDRAW, x+20, y+120, 30, 20, GLOBALS['DROP_AXIS'][0].val, 'Drop down on the global Z axis', do_axis_z)
193
GLOBALS['DROP_AXIS'][1] = Draw.Toggle('View Z', EVENT_REDRAW, x+50, y+120, 70, 20, GLOBALS['DROP_AXIS'][1].val, 'Drop allong the view vector', do_axis_view)
197
Draw.Label('Drop on to...', x-70,y+90, 120, 20)
199
GLOBALS['GROUND_SOURCE'][0] = Draw.Toggle('Active Object', EVENT_REDRAW, x-70, y+70, 110, 20, GLOBALS['GROUND_SOURCE'][0].val, '', do_ground_source_act)
200
GLOBALS['GROUND_SOURCE'][1] = Draw.Toggle('Group', EVENT_REDRAW, x+40, y+70, 80, 20, GLOBALS['GROUND_SOURCE'][1].val, '', do_ground_source_group)
201
if GLOBALS['GROUND_SOURCE'][1].val:
202
GLOBALS['GROUND_GROUP_NAME'] = Draw.String('GR:', EVENT_REDRAW+1001, x-70, y+50, 190, 20, GLOBALS['GROUND_GROUP_NAME'].val, 21, '', do_ground_group_name)
205
GLOBALS['DROP_OVERLAP_CHECK'] = Draw.Toggle('Overlapping Terrain', EVENT_NONE, x-70, y+20, 190, 20, GLOBALS['DROP_OVERLAP_CHECK'].val, "Check all terrain triangles and use the top most (slow)")
207
Draw.PushButton('Drop Objects', EVENT_EXIT, x+20, y-10, 100, 20, 'Drop the selected objects', terrain_clamp)
209
# So moving the mouse outside the popup exits the while loop
210
GLOBALS['EVENT'] = EVENT_EXIT
214
# This is to set the position if the popup
215
GLOBALS['MOUSE'] = Window.GetMouseCoords()
217
# hack so the toggle buttons redraw. this is not nice at all
218
while GLOBALS['EVENT'] == EVENT_REDRAW:
219
Draw.UIBlock(terain_clamp_ui)
221
if __name__ == '__main__':
b'\\ No newline at end of file'