User Tools

Site Tools


blender:blenderdata

Differences

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

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
blender:blenderdata [2017/11/21 18:08]
dwheele [Object]
blender:blenderdata [2017/11/21 18:26] (current)
dwheele [Data Access]
Line 20: Line 20:
   * ''for ob in bpy.data.objects'' # returns all of the objects iterable   * ''for ob in bpy.data.objects'' # returns all of the objects iterable
  
-To create a new object:+=== To create a new object: ===
  
   * Create a datablock with ''datablock = bpy.data.lamps.new'' (or ''bpy.data.objects.new'')   * Create a datablock with ''datablock = bpy.data.lamps.new'' (or ''bpy.data.objects.new'')
Line 51: Line 51:
 </code> </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.1511287697.txt.gz · Last modified: 2017/11/21 18:08 by dwheele