Skip to content

Instantly share code, notes, and snippets.

@enzyme69
Created March 24, 2013 01:57
Show Gist options
  • Save enzyme69/5230124 to your computer and use it in GitHub Desktop.
Save enzyme69/5230124 to your computer and use it in GitHub Desktop.
Blender Sushi Script to select some meshes within specific distance to an Empty in 3D scene. User can adjust the Distance value.
import bpy
def selectMeshBasedOnDistance(distance, empty):
"""
A simple script to calculate the distance of all
mesh in the scene relative to the selected camera
"""
# Firstly, deselect all objects in the scene
bpy.ops.object.select_all(action='DESELECT')
# Get all objects in the scene of type 'MESH'
meshes = []
for item in bpy.data.objects:
if item.type == 'MESH':
meshes.append(item)
# Check if empty argument given is a type of empty
if empty.type != 'EMPTY':
print("Please specify an empty!")
# Create and clear an array to store mesh within the specified distance
meshToSelect = []
del(meshToSelect[:])
print(meshToSelect)
# Get the camera positions in world space
#emptyXform = empty.matrix_world * empty.location
emptyXform = empty.location
# Get the mesh xform and distance from camera
for mesh in meshes:
#meshXform = mesh.matrix_world * mesh.location
meshXform = mesh.location
meshDistanceFromEmpty=(meshXform-emptyXform).length
print(mesh, meshDistanceFromEmpty)
if meshDistanceFromEmpty < distance:
meshToAdd = []
meshToAdd = mesh
meshToSelect.append(meshToAdd)
print(meshToSelect)
# adding if for error checking, Python does not like selecting empty list
if(len(meshToSelect))>0:
for item in meshToSelect:
item.select=True
# My Test 1:
# selectMeshBasedOnDistance(20, bpy.context.selected_objects[0])
# My Test 2:
selectMeshBasedOnDistance(5, bpy.data.objects['Empty'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment