If you’re interested in taking the first step into Python for 3D software, Or simply would like to browse some script examples, your welcome to visit my Gist page, It contains a useful library of Python code example for Blender, Maya, 3ds max and Unreal engine: https://gist.github.com/CGLion
Note that accessing an model’s animated vertex locations requires reading the model’s evaluated (deformed) mesh state per frame. For that reason a new bmesh object is initiated per each frame with the the model’s updated dependency graph.
import bpy
import bmesh
obj = bpy.context.active_object
frames = range(0,10)
get the object's evaluated dependency graph:
depgraph = bpy.context.evaluated_depsgraph_get()
iterate animation frames:
for f in frames:
bpy.context.scene.frame_set(f)
# define new bmesh object:
bm = bmesh.new()
bm.verts.ensure_lookup_table()
# read the evaluated mesh data into the bmesh object:
bm.from_object( obj, depgraph )
# iterate the bmesh verts:
for i, v in enumerate(bm.verts):
print("frame: {}, vert: {}, location: {}".format(f, i, v.co))
Some useful short Python snippets for batch re-naming objects in Blender:
Remove last 4 characters from selected object names:
import bpy
objects = bpy.context.selected_objects
for o in objects:
   o.name = o.name[:-4]
Rename all selected objects to a set base name followed by a 3 digit numeric suffix:
import bpy
objects = bpy.context.selected_objects
for (i,o) in enumerate(objects):
o.name = "some_base_name_{:03d}".format(i)
Prefix all selected objects name with their Blender data type like renaming “SomeModel” to “MESH_SomeModel” or for example:
import bpy
objects = bpy.context.selected_objects
for (i,o) in enumerate(objects):
o.name = "{}_{}".format(o.type,o.name)
* note that when copying and pasting a script from this example, the indentation may not be pasted correctly.
Note:
Blender 2.8 has a robust batch renaming utility that is invoked by pressing Ctrl + F2 so we don’t have to write scripts to do batch renaming of objects:
By default, mesh triangles are not accessible to Python in Blender.
When accessing the mesh triangles is needed, they must be calculated first using the ‘calc_loop_triangles’Mesh object method.
Before the calc_loop_triangle method has been called, the loop_triangles property of the Mesh object will reference a empty collection.
After calling the calc_loop_triangles method, the loop_triangles property will reference a collection of MeshLoopTriangle objects, in which the vertices property will hold an array of 3 integers that are the indices of the triangle vertices.
The following example script creates and places sphere objects at the centers of the cube’s triangles:
import bpy
mesh = bpy.data.objects['Cube'].data
mesh.calc_loop_triangles()
for tri in mesh.loop_triangles:
tri_center = (mesh.vertices[tri.vertices[0]].co * 0.333) +\
(mesh.vertices[tri.vertices[1]].co * 0.333) +\
(mesh.vertices[tri.vertices[2]].co * 0.333)
bpy.ops.mesh.primitive_uv_sphere_add(radius=0.1,
enter_editmode=False,
location=tri_center)
* note that when copying and pasting a script from this example, the indentation may not be pasted correctly.
If you happen to link an external object that was made unselectable in the original blend file, You will have no interactive way to make it selectable again, or local, or even delete it from the current blend file.
The good news is that it can easily be done by a very short Python script: