Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import bpy
- import mathutils
- obj = bpy.context.active_object
- mesh = obj.data
- def get_layers():
- """
- return: tuple of 20 booleans. True if layer is active layer,False if not
- Allows the mesh to be created on the active layer. There is only 1 active layer
- """
- active_layer = bpy.context.scene.active_layer
- layers = [False] * 20
- layers[active_layer] = True
- return tuple(layers)
- def get_global_poly_pos(polygon):
- """
- polygon: bpy_types.MeshPolygon. A edit mode polygon from the bpy module
- return: Vector. A 3d point. The center of the polygon
- Finds the center of a polygon in world space
- """
- location = mathutils.Vector((
- polygon.center[0],
- polygon.center[1],
- polygon.center[2]
- ))
- location = obj.matrix_world * location
- return location
- def get_normal(polygon):
- """
- polygon: bpy_types.MeshPolygon. A edit mode polygon from the bpy module
- return: Vector. A 3d point that crosses the normal.
- Finds a 3d point that intersect with the normal
- """
- normal_vector = polygon.normal
- vec_x = normal_vector[0] # not used atm
- vec_y = normal_vector[1] # not used atm
- vec_z = normal_vector[2] # not used atm
- return normal_vector
- def create_light_at_face():
- """
- layers: tuple. 20 booleans where exaclty one boolen is True
- return: None
- creates an area light the has the same orientation as the normal at the center of each face
- """
- for poly in mesh.polygons:
- loc = get_global_poly_pos(poly)
- normal = get_normal(poly) # this is probably needed somehow
- # get the rotation needed for each polygon face, idk how
- # default values in RADIANS, actual values will be computed somehow
- rot_x = 0
- rot_y = 0
- rot_z = 0
- bpy.ops.object.lamp_add(type='AREA', view_align=False, location=(loc[0], loc[1], loc[2]), rotation=(rot_x, rot_y, rot_z), layers=get_layers())
- # scale lamp up a little fo we can see the area
- bpy.context.object.data.size = 3
- def create_empty_at_face():
- """
- DEBUG, used for testing
- creates and empty at the center of the face
- Have an object selected in object mode
- """
- # create empty at center of face
- for poly in mesh.polygons:
- loc = get_global_poly_pos(poly)
- bpy.ops.object.empty_add(type='PLAIN_AXES', view_align=False, location=(loc[0], loc[1], loc[2]), rotation=(0.0, 0.0, 0.0), layers=get_layers())
- create_light_at_face()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement