| __author__ = 'thoth'
import bpy
from math import *
from mathutils import *
#
#
def make_cubes(scn):
    r=10
    for x in range(-r,r,2):
        for y in range(-r,r,2):
            for z in range(-r,r,2):
                name = "cube at %d,%d,%d"%(x,y,z)
                obj = bpy.data.objects.get(name)
                if obj is None:
                    mesh = bpy.data.meshes['Cube']
                    obj = bpy.data.objects.new(name, mesh)
                    scn.objects.link(obj)
                    obj.location = (x,y,z)
def cubes(scn):
    return [ obj for obj in scn.objects if obj.name[:8]=='cube at ']
class CameraCone:
    def __init__(self, matrix, sensor_width, lens, resolution_x, resolution_y):
        self.matrix = matrix.inverted()
        self.sensor_width = sensor_width
        self.lens = lens
        w = 0.5* sensor_width / lens
        if resolution_x> resolution_y:
            x = w
            y = w*resolution_y/resolution_x
        else:
            x = w*resolution_x/resolution_y
            y = w
        lr = Vector([x,-y,-1])
        ur = Vector([x,y,-1])
        ll = Vector([-x,-y,-1])
        ul = Vector([-x,y,-1])
        self.half_plane_normals = [
            lr.cross(ll).normalized(),
            ll.cross(ul).normalized(),
            ul.cross(ur).normalized(),
            ur.cross(lr).normalized()
        ]
    def from_camera(cam, scn):
        return CameraCone(cam.matrix_world, cam.data.sensor_width, cam.data.lens, scn.render.resolution_x, scn.render.resolution_y)
    def isVisible(self, loc, fudge=0):
        loc2 = self.matrix * loc
        for norm in self.half_plane_normals:
            z2 = loc2.dot(norm)
            if z2 < -fudge:
                return False
        return True
def adjust_visibility(scn):
    cam_cone = CameraCone.from_camera(scn.camera, scn)
    for obj in cubes(scn):
        obj.hide = cam_cone.isVisible(obj.location, sqrt(3))
#
#
scn = bpy.context.scene
make_cubes(scn)
adjust_visibility(scn)
 | 
Blender python API quick-start
Syntax highlighting by Pygments.