User Tools

Site Tools


blender:blenderdata

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
blender:blenderdata [2017/11/21 17:48]
dwheele created
blender:blenderdata [2017/11/21 18:26] (current)
dwheele [Data Access]
Line 2: Line 2:
  
   * {{http://ivan/blenderapi/contents.html|Blender API, Local Copy}}   * {{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(<parameters>)\\ {{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// 
 + 
 +<code python> 
 +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 
 +</code> 
 + 
 +=== Vertex Groups === 
 + 
 +<code python> 
 +for vertexGroup in ob.vertex.groups: 
 +  print vertexGroup.name 
 +</code> 
 + 
 + 
 + 
 +After establishing a group, you can add vertices to it: 
 + 
 +<code python> 
 +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 
 +</code> 
 + 
 +==== Polygons ==== 
 + 
 +Brief example 
 + 
 +<code python> 
 +for p in ob.data.polygons: 
 +  p.material_index = ms_index 
 +</code> 
 + 
 + 
 +==== 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: === 
 + 
 +<code python> 
 +mat = bpy.data.materials.new(name="Card Paper"
 +</code> 
 + 
 +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("Card Paper"
 +  for p in ob.data.polygons: 
 +    p.material_index = ms_index 
 +</code> 
 +     
blender/blenderdata.1511286504.txt.gz · Last modified: 2017/11/21 17:48 by dwheele