This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision | ||
|
blender:blenderdata [2017/11/21 17:53] dwheele [Top Level] |
blender:blenderdata [2017/11/21 18:26] (current) dwheele [Data Access] |
||
|---|---|---|---|
| Line 4: | Line 4: | ||
| - | ===== Top Level ====== | + | ====== Top Level ====== |
| ^bpy.context|Context Access\\ e.g., bpy.context.current_object\\ {{http:// | ^bpy.context|Context Access\\ e.g., bpy.context.current_object\\ {{http:// | ||
| - | ^bpy.data|Data Access\\ e.g., bpy.data.objects[" | + | ^bpy.data|Data Access\\ e.g., bpy.data.objects[" |
| ^bpy.ops|Operators\\ e.g., bpy.ops.object.add(< | ^bpy.ops|Operators\\ e.g., bpy.ops.object.add(< | ||
| + | ===== Data Access ===== | ||
| + | |||
| + | ==== Object ==== | ||
| + | |||
| + | === To get an existing object: === | ||
| + | |||
| + | * '' | ||
| + | * '' | ||
| + | * '' | ||
| + | |||
| + | === To create a new object: === | ||
| + | |||
| + | * Create a datablock with '' | ||
| + | * 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// | ||
| + | |||
| + | <code python> | ||
| + | import bpy | ||
| + | from mathutils import Matrix | ||
| + | |||
| + | scene = bpy.context.scene | ||
| + | |||
| + | # Create new lamp datablock | ||
| + | lamp_data = bpy.data.lamps.new(name=" | ||
| + | |||
| + | # Create new object with our lamp datablock | ||
| + | lamp_object = bpy.data.objects.new(name=" | ||
| + | |||
| + | # 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 === | ||
| + | |||
| + | <code python> | ||
| + | for vertexGroup in ob.vertex.groups: | ||
| + | print vertexGroup.name | ||
| + | </ | ||
| + | |||
| + | |||
| + | |||
| + | After establishing a group, you can add vertices to it: | ||
| + | |||
| + | <code python> | ||
| + | verts = ((x,y,z), (x+w, y,z), (x+w, | ||
| + | (x,y+t,z), (x+w, | ||
| + | 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+' | ||
| + | |||
| + | # Create mesh from given verts, faces. | ||
| + | me.from_pydata(verts, | ||
| + | # Update mesh with new data | ||
| + | me.update() | ||
| + | | ||
| + | vg = ob.vertex_groups.new(" | ||
| + | vg.add([0, | ||
| + | # The indexes 0,1,2,3 are referring to the vertices | ||
| + | # in the order they are added to the object | ||
| + | </ | ||
| + | |||
| + | ==== Polygons ==== | ||
| + | |||
| + | Brief example | ||
| + | |||
| + | <code python> | ||
| + | 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: === | ||
| + | |||
| + | * '' | ||
| + | * '' | ||
| + | |||
| + | === To create a new material: === | ||
| + | |||
| + | <code python> | ||
| + | mat = bpy.data.materials.new(name=" | ||
| + | </ | ||
| + | |||
| + | Assign material to an object **ob** | ||
| + | |||
| + | <code python> | ||
| + | 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(" | ||
| + | for p in ob.data.polygons: | ||
| + | p.material_index = ms_index | ||
| + | </ | ||
| + | | ||