First what is a :
- Mesh: mesh is a data-block that contains collection of points ( Vertices ) and their relations ( edges , faces ) and other properties related to them as ( Vertex groups , vertex color, UV maps, material index )
- Object: is more general data-block which could be contains mesh or curve, empty, armature, .... and this is the important part of the object( it is the type of the object ), it also includes other properties like Location, rotation, size, modifiers, constraints, materials,particle systems.... ( what to note is that the mesh is only a part of the object )
let's go through the first code line by line :
me = bpy.data.meshes.new('tim_the_mesh')
this will create an empty mesh ( has no points in it ) with the name tim_the_mesh
and me
will be our reference to this mesh( when we need to call it we use me
)
ob = bpy.data.objects.new('tim_the_object', me)
this will create a new object ( of type mesh ) that contains our mesh me
and our reference to this object is ob
if we stop here our object will not show up in any of the scenes (Viewport, Outliner) and that is because we have not linked this object to any scene yet ( blender doesn't know where does this object belong to ) and for this we use the next line to link the object to the active scene ( the current scene )
bpy.context.scene.objects.link(ob)
Now we have created a mesh put it in an object and linked our object to the scene ( the object will be visible now in the scene ) but the problem is that our object has no geometry ( vertices , edges and faces ) now we will fill our mesh with a prepared data ( generated by other code parts ) in he next line
me.from_pydata(verts, [], faces)
after giving our object a shape we want to give it a good look for that we assign a material to this object since the object is new it has no active material (or any material ) and using the next line we set the material of the object to be purple
( purple is material defined earlier in this code )
ob.active_material = purple
and the next line is simply giving the object a smooth look by enabling the smooth shading
bpy.ops.object.shade_smooth()
if you note in the last line we haven't referenced ob
and that's because the function shade_smooth()
will be applied to the active object which is our object ( because it has been just created )
for the second code :
bpy.ops.mesh.primitive_uv_sphere_add(size=1, location=(3,3,3))
this line will take care of creating a mesh, generate its geometry, put in an object, link it to the current scene and move it to the position ( 3,3,3) so it replaces the 4 lines of the first script
ao = bpy.context.active_object
we want to get a reference to our new object to manipulate it later, and since our object has just been created it is the active object and we simply reference that as ao
ao.name = "tim_the_sphere"
we have just changed the name of our object to tim_the_sphere
ao.location = (x,y,z)
now we changed the location of the object to be at (x, y, z)
and the rest is the same as above :
ao.active_material = purple #assign a material
bpy.ops.object.shade_smooth() # set smooth shading
Now how do I know that this object has a property called location
or anything else this can be done by :
1- Locking through the API DOCs: just search for the type of the object you want know about and you'll get a page containing all properties like this one for the Object type
2- Using the console select your object and type C.object.
and hit Ctrl+Space then you will get a list of all properties avialable for this object
3- From the properties panel hover over any property and you'll get the python reference for this property
purple
andx,y,z
$\endgroup$me
, but to change some things later on (say another frame), the object must be reselected? (e.g throughbpy.context.active_object
andbpy.ops.mesh
) I wonder if there is something deeper I can learn by asking why. $\endgroup$