Page MenuHome

Added BKE_mesh_clear_geometry() function
ClosedPublic

Authored by Sybren A. Stüvel (sybren) on Jul 30 2019, 6:38 PM.

Details

Summary

This function makes it possible to clear/remove/nuke all the geometry in a mesh, allowing functions like Mesh.from_python() to construct a new mesh in its place. Without this function, code like in T67627 have to allocate a new Mesh datablock, fill that, and swap out the old Mesh for the new one. This introduces issues when exporting, as the new mesh could be seen by an exporter as unrelated to the old one.

Diff Detail

Repository
rB Blender

Event Timeline

LGTM, would rather have our mesh expert check on it too though, @Campbell Barton (campbellbarton) what do you think?

This revision is now accepted and ready to land.Jul 30 2019, 6:56 PM
Brecht Van Lommel (brecht) requested changes to this revision.Jul 31 2019, 1:06 AM
Brecht Van Lommel (brecht) added inline comments.
source/blender/blenkernel/intern/mesh.c
552–555

Don't do this kind of hacking, instead create a shared function that both BKE_mesh_free and this can call.

Otherwise it's too easy to make a change in BKE_mesh_free that breaks BKE_mesh_clear_geometry. It also helps a little to put these functions next to each other.

569

Most BKE functions don't have depsgraph tagging, better to put it next to the notifier I think since these usually go together.

570

This function misses freeing shape keys.

This revision now requires changes to proceed.Jul 31 2019, 1:06 AM
Sybren A. Stüvel (sybren) marked 2 inline comments as done.
  • Add the fact that shape keys and materials are not freed to the RNA function description
Sybren A. Stüvel (sybren) added inline comments.
source/blender/blenkernel/intern/mesh.c
552–555

Done, although I'm not really happy with the name mesh_free_most_data(). It does what it says, but it's not really descriptive either. I could name it mesh_free_data_except_materials_and_shapekeys(), but that's opening the door to code rot (add something to the mesh structure but not this function and the name has already gone bad).

570

I had a little discussion with @Sergey Sharybin (sergey) and he pointed out that freeing the shapekeys requires tagging the depsgraph for rebuilding relations. Since this is an expensive operation, I'd rather not have it here. The use case for this function is to make it cheaper for Python code to replace one generated mesh with another. I don't think it'll be common to have shape keys in this scenario. I'd rather keep such an expensive operation outside this function, and let people deal with shape keys explicitly when necessary.

I added a comment to the code to indicate that shapekeys aren't freed. I also added this to the RNA function description (and also included that materials aren't freed either).

source/blender/blenkernel/intern/mesh.c
552–555

You could make BKE_mesh_free just call BKE_mesh_clear_geometry, the overhead will be negligible.

Sybren A. Stüvel (sybren) marked an inline comment as done.Jul 31 2019, 12:14 PM
This revision is now accepted and ready to land.Jul 31 2019, 12:14 PM