Advertisement
Guest User

Untitled

a guest
Feb 18th, 2014
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.53 KB | None | 0 0
  1. import bpy
  2. import mathutils
  3.  
  4. obj = bpy.context.active_object
  5. mesh = obj.data
  6.  
  7.  
  8. def get_layers():
  9.     """
  10.    return: tuple of 20 booleans. True if layer is active layer,False if not
  11.    Allows the mesh to be created on the active layer. There is only 1 active layer
  12.    """
  13.     active_layer = bpy.context.scene.active_layer
  14.     layers = [False] * 20
  15.     layers[active_layer] = True
  16.     return tuple(layers)
  17.  
  18.  
  19. def get_global_poly_pos(polygon):
  20.     """
  21.    polygon: bpy_types.MeshPolygon. A edit mode polygon from the bpy module
  22.    return: Vector. A 3d point. The center of the polygon
  23.    Finds the center of a polygon in world space
  24.    """
  25.     location = mathutils.Vector((
  26.         polygon.center[0],
  27.         polygon.center[1],
  28.         polygon.center[2]
  29.         ))
  30.     location = obj.matrix_world * location
  31.     return location
  32.  
  33.  
  34. def get_normal(polygon):
  35.     """
  36.    polygon: bpy_types.MeshPolygon. A edit mode polygon from the bpy module
  37.    return: Vector. A 3d point that crosses the normal.
  38.    Finds a 3d point that intersect with the normal
  39.    """
  40.     normal_vector = polygon.normal
  41.     vec_x = normal_vector[0]  # not used atm
  42.     vec_y = normal_vector[1]  # not used atm
  43.     vec_z = normal_vector[2]  # not used atm
  44.     return normal_vector
  45.  
  46.  
  47. def create_light_at_face():
  48.     """
  49.    layers: tuple. 20 booleans where exaclty one boolen is True
  50.    return: None
  51.    creates an area light the has the same orientation as the normal at the center of each face
  52.    """
  53.     for poly in mesh.polygons:
  54.         loc = get_global_poly_pos(poly)
  55.         normal = get_normal(poly)  # this is probably needed somehow
  56.  
  57.         # get the rotation needed for each polygon face, idk how
  58.         # default values in RADIANS, actual values will be computed somehow
  59.         rot_x = 0
  60.         rot_y = 0
  61.         rot_z = 0
  62.         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())
  63.         # scale lamp up a little fo we can see the area
  64.         bpy.context.object.data.size = 3
  65.  
  66.  
  67. def create_empty_at_face():
  68.     """
  69.    DEBUG, used for testing
  70.    creates and empty at the center of the face
  71.    Have an object selected in object mode
  72.    """
  73.     # create empty at center of face
  74.     for poly in mesh.polygons:
  75.         loc = get_global_poly_pos(poly)
  76.         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())
  77.  
  78. create_light_at_face()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement