====== Blender Data Stucture ====== * {{http://ivan/blenderapi/contents.html|Blender API, Local Copy}} ====== Top Level ====== ^bpy.context|Context Access\\ e.g., bpy.context.current_object\\ {{http://ivan/blenderapi/bpy.context.html|API Docs}}| ^bpy.data|Data Access\\ e.g., bpy.data.objects["Cube"]\\ {{http://ivan/blenderapi/bpy.data.html|API Docs}}| ^bpy.ops|Operators\\ e.g., bpy.ops.object.add()\\ {{http://ivan/blenderapi/bpy.ops.object.html|API Docs}}| ===== Data Access ===== ==== Object ==== === To get an existing object: === * ''ob = bpy.data.object["Cube"]'' * ''ob = bpy.data.object.get("Cube")'' # If not found, returns None gracefully * ''for ob in bpy.data.objects'' # returns all of the objects iterable === To create a new object: === * Create a datablock with ''datablock = bpy.data.lamps.new'' (or ''bpy.data.objects.new'') * Create an object, linking datablock to it * Can also use operators to create a new object, e.g., bpy.ops.object.add //From docs, example is to create a lamp// import bpy from mathutils import Matrix scene = bpy.context.scene # Create new lamp datablock lamp_data = bpy.data.lamps.new(name="New Lamp", type='POINT') # Create new object with our lamp datablock lamp_object = bpy.data.objects.new(name="New Lamp", object_data=lamp_data) # Link lamp object to the scene so it'll appear in this scene scene.objects.link(lamp_object) # Place lamp to a specified location lamp_object.location = (5.0, 5.0, 5.0) # And finally select it make active lamp_object.select = True scene.objects.active = lamp_object === Vertex Groups === for vertexGroup in ob.vertex.groups: print vertexGroup.name After establishing a group, you can add vertices to it: verts = ((x,y,z), (x+w, y,z), (x+w,y,z+h), (x,y,z+h), \ (x,y+t,z), (x+w,y+t,z), (x+w,y+t,z+h), (x,y+t,z+h)) faces = ((0,1,2,3), (5,4,7,6), (5,6,2,1), (6,7,3,2), (7,4,0,3), (4,5,1,0)) me = ob.data me.name = name+'Mesh' # Create mesh from given verts, faces. me.from_pydata(verts, [], faces) # Update mesh with new data me.update() vg = ob.vertex_groups.new("Card Face") vg.add([0,1,2,3],1,"ADD") # The indexes 0,1,2,3 are referring to the vertices # in the order they are added to the object ==== Polygons ==== Brief example for p in ob.data.polygons: p.material_index = ms_index ==== Material ==== Materials are established separate from Objects, then links are made from Objects to Materials. === To get an existing material: === * ''mat = bpy.data.materials["Material"]'' * ''mat= bpy.data.materials.get("Material")'' # If not found, returns None gracefully === To create a new material: === mat = bpy.data.materials.new(name="Card Paper") Assign material to an object **ob** if not(ob.data.materials.get(mat.name)): ob.data.materials.append(mat) # this example adds material to all polygons in ob ms_index = ob.material_slots.find("Card Paper") for p in ob.data.polygons: p.material_index = ms_index