When referring to the mesh geometry – its vertices, edges, and polygons, Blender operates with the original data without the usage of modifiers added to the object.
For example, the number of vertices of the active object, we can get with the following command:
1 |
len(bpy.context.active_object.data.vertices) |
And it will be returned without considering the object’s Subdivision modifier, which modifies the mesh’s geometry, increasing it.
So for a cube that has the Subdivision modifier added:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import bpy cube = bpy.context.active_object print('vertices: ', len(cube.data.vertices)) print('edges: ', len(cube.data.edges)) print('faces: ', len(cube.data.polygons)) print('all vertices ', cube.data.vertices) # vertices: 8 # edges: 12 # faces: 6 # all vertices <bpy_collection[8], MeshVertices> |
To get the object’s geometry, considering the modifiers that change it, we need to refer to the object through the “depsgraph” structure, which makes it possible to access the real state of the scene.
First, we need to get the “depsgraph” object for the current context:
1 |
depsgraph = bpy.context.evaluated_depsgraph_get() |
Then, to get the “evaluated” version of the desired object, in which the effect of all modifiers has already been taken:
1 |
cube_eval = cube.evaluated_get(depsgraph) |
And through the evaluated object, get the geometry data.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import bpy cube = bpy.context.active_object depsgraph = bpy.context.evaluated_depsgraph_get() cube_eval = cube.evaluated_get(depsgraph) print('vertices: ', len(cube_eval.data.vertices)) print('edges: ', len(cube_eval.data.edges)) print('faces: ', len(cube_eval.data.polygons)) print('all_vertices ', cube_eval.data.vertices) # vertices: 26 # edges: 48 # faces: 24 # all_vertices <bpy_collection[26], MeshVertices> |
We obtained the real data of the cube’s geometry like the Subdivision modifier was applied.
Great. Now how do we apply that data back to the objects?
If you need to apply all modifiers to the object in that way, please see https://b3d.interplanety.org/en/quickly-apply-all-modifiers-to-the-object/