Changeset View
Changeset View
Standalone View
Standalone View
doc/python_api/examples/bpy.types.Depsgraph.4.py
| """ | """ | ||||
| Dependency graph: Object.to_mesh() | Dependency graph: Object.to_mesh() | ||||
| +++++++++++++++++++++++++++++++++++ | +++++++++++++++++++++++++++++++++++ | ||||
| Object.to_mesh() (and bpy.data.meshes.new_from_object()) are closely interacting with dependency | Object.to_mesh() is closely interacting with dependency graph: its behavior depends on whether it | ||||
| graph: their behavior depends on whether they are used on original or evaluated object. | is used on original or evaluated object. | ||||
| When is used on original object, the result mesh is calculated from the object without taking | When is used on original object, the result mesh is calculated from the object without taking | ||||
| animation or modifiers into account: | animation or modifiers into account: | ||||
| - For meshes this is similar to duplicating the source mesh. | - For meshes this is similar to duplicating the source mesh. | ||||
| - For curves this disables own modifiers, and modifiers of objects used as bevel and taper. | - For curves this disables own modifiers, and modifiers of objects used as bevel and taper. | ||||
| - For metaballs this produces an empty mesh since polygonization is done as a modifier evaluation. | - For metaballs this produces an empty mesh since polygonization is done as a modifier evaluation. | ||||
| When is used on evaluated object all modifiers are taken into account. | When is used on evaluated object all modifiers are taken into account. | ||||
| .. note:: The result mesh is added to the main database. | .. note:: The result mesh is owned by the object. It can be freed by calling `object.to_mesh_clear()`. | ||||
| .. note:: If object does not have geometry (i.e. camera) the functions returns None. | .. note:: If object does not have geometry (i.e. camera) the functions returns None. | ||||
| """ | """ | ||||
| import bpy | import bpy | ||||
| class OBJECT_OT_object_to_mesh(bpy.types.Operator): | class OBJECT_OT_object_to_mesh(bpy.types.Operator): | ||||
| """Convert selected object to mesh and show number of vertices""" | """Convert selected object to mesh and show number of vertices""" | ||||
| bl_label = "DEG Object to Mesh" | bl_label = "DEG Object to Mesh" | ||||
| Show All 9 Lines | def execute(self, context): | ||||
| if object.type not in {'MESH', 'CURVE', 'SURFACE', 'FONT', 'META'}: | if object.type not in {'MESH', 'CURVE', 'SURFACE', 'FONT', 'META'}: | ||||
| self.report({'INFO'}, "Object can not be converted to mesh") | self.report({'INFO'}, "Object can not be converted to mesh") | ||||
| return {'CANCELLED'} | return {'CANCELLED'} | ||||
| depsgraph = context.evaluated_depsgraph_get() | depsgraph = context.evaluated_depsgraph_get() | ||||
| # Invoke to_mesh() for original object. | # Invoke to_mesh() for original object. | ||||
| mesh_from_orig = object.to_mesh() | mesh_from_orig = object.to_mesh() | ||||
| self.report({'INFO'}, f"{len(mesh_from_orig.vertices)} in new mesh without modifiers.") | self.report({'INFO'}, f"{len(mesh_from_orig.vertices)} in new mesh without modifiers.") | ||||
| # Remove temporary mesh. | # Remove temporary mesh. | ||||
| bpy.data.meshes.remove(mesh_from_orig) | object.to_mesh_clear() | ||||
| # Invoke to_mesh() for evaluated object. | # Invoke to_mesh() for evaluated object. | ||||
| object_eval = object.evaluated_get(depsgraph) | object_eval = object.evaluated_get(depsgraph) | ||||
| mesh_from_eval = object_eval.to_mesh() | mesh_from_eval = object_eval.to_mesh() | ||||
| self.report({'INFO'}, f"{len(mesh_from_eval.vertices)} in new mesh with modifiers.") | self.report({'INFO'}, f"{len(mesh_from_eval.vertices)} in new mesh with modifiers.") | ||||
| # Remove temporary mesh. | # Remove temporary mesh. | ||||
| bpy.data.meshes.remove(mesh_from_eval) | object_eval.to_mesh_clear() | ||||
| return {'FINISHED'} | return {'FINISHED'} | ||||
| def register(): | def register(): | ||||
| bpy.utils.register_class(OBJECT_OT_object_to_mesh) | bpy.utils.register_class(OBJECT_OT_object_to_mesh) | ||||
| def unregister(): | def unregister(): | ||||
| bpy.utils.unregister_class(OBJECT_OT_object_to_mesh) | bpy.utils.unregister_class(OBJECT_OT_object_to_mesh) | ||||
| if __name__ == "__main__": | if __name__ == "__main__": | ||||
| register() | register() | ||||