How can I find all the vertices/faces inside a 3D sphere in script
Thanks for clarify

Computing the distance requires a square root. However, both sides of the inequality can be squared so you lose the square root and end up with basically this:
r = radius of sphere
distance = (x - x0)² + (y - y0)² + (z - z0)² 

where x, y, z are the coordinates of one vertex IN the sphere
and x0,y0,z0 are the coordinates of the vertex that you are looking for.

distance = radius  -> vertex in the edge of sphere
distance &lt; radius  -> vertex inside sphere
distance > radius  -> vertex outside sphere



please use this code as example

import bpy
import bmesh
import random
from math import sqrt

def create_uv_sphere(name, u, v, d):

    #https://github.com/zeffii/BlenderPythonRecipes/wiki/bmesh_ops_primitives 
    # bmesh.ops.create_uvsphere also accepts a matrix keyword argument, 
    # which i've dropped from the example.
    bm = bmesh.new()
    bmesh.ops.create_uvsphere(bm, u_segments=u, v_segments=v, diameter=d)

    # create new empty mesh and fill with uvsphere
    mesh = bpy.data.meshes.new(name + "_mesh")
    bm.to_mesh(mesh)
    bm.free()

    # create object and link to scene
    obj = bpy.data.objects.new(name, mesh)
    bpy.context.scene.objects.link(obj)
    return obj

def calc_distance(v1, v2, radius):
    distance = sqrt( (v2.x - v1.x)**2 + (v2.y - v1.y)**2 + (v2.z - v2.z)**2)

    if distance &lt; radius:
        inside =True
    else:
        inside =False

    return inside


#############################3

name = "my_uvsphere"
u=30
v=30
d=1
create_uv_sphere(name, u, v, d)
bpy.data.objects[name].select = True
bpy.context.scene.objects.active = bpy.data.objects[name]
bpy.ops.object.mode_set(mode='EDIT')

obj = bpy.context.edit_object
me = obj.data
bm = bmesh.from_edit_mesh(me)

vs =bm.verts

if hasattr(bm.verts, "ensure_lookup_table"): 
    bm.verts.ensure_lookup_table()
v1 =vs[0]

number= 500
counter = 0
a=-d
b=d
while counter &lt;=number:

    v2 = bm.verts.new((random.uniform(a, b), 
                       random.uniform(a, b), 
                       random.uniform(a, b)))

    inside  =calc_distance(v1.co, v2.co, d/2)
    if inside:
        v2.select = True

    counter+=1


bmesh.update_edit_mesh(me, True)