Blender V5.3
bmesh_mesh_convert.cc File Reference
#include <algorithm>
#include "DNA_key_types.h"
#include "DNA_mesh_types.h"
#include "DNA_meshdata_types.h"
#include "DNA_modifier_types.h"
#include "DNA_object_types.h"
#include "MEM_guardedalloc.h"
#include "BLI_array.hh"
#include "BLI_index_range.hh"
#include "BLI_listbase.hh"
#include "BLI_math_vector_c.hh"
#include "BLI_span.hh"
#include "BLI_string.hh"
#include "BLI_string_ref.hh"
#include "BLI_task.hh"
#include "BLI_vector.hh"
#include "BKE_attribute.h"
#include "BKE_attribute.hh"
#include "BKE_attribute_legacy_convert.hh"
#include "BKE_attribute_math.hh"
#include "BKE_customdata.hh"
#include "BKE_mesh.hh"
#include "BKE_mesh_runtime.hh"
#include "BKE_multires.hh"
#include "BKE_key.hh"
#include "BKE_main.hh"
#include "DEG_depsgraph_query.hh"
#include "bmesh.hh"
#include "CLG_log.h"

Go to the source code of this file.

Classes

struct  blender::MeshToBMeshLayerInfo
struct  blender::BMeshToMeshLayerInfo
class  blender::AttrSingleValueChecker

Namespaces

namespace  blender
 BMesh Iterators.

Enumerations

enum class  blender::AttrDomain

Functions

bool blender::BM_attribute_stored_in_bmesh_builtin (const StringRef name)
static BMFace * blender::bm_face_create_from_mpoly (BMesh &bm, Span< int > face_verts, Span< int > face_edges, Span< BMVert * > vtable, Span< BMEdge * > etable)
static const CustomData & blender::get_bm_custom_data (const BMesh &bm, const bke::AttrDomain domain)
static const CustomData & blender::get_mesh_custom_data (const Mesh &mesh, const bke::AttrDomain domain)
static CustomData & blender::get_mesh_custom_data (Mesh &mesh, const bke::AttrDomain domain)
static Vector< MeshToBMeshLayerInfo > blender::mesh_to_bm_copy_info_calc (const Mesh &mesh, const bke::AttrDomain domain, CustomData &bm_data)
static void blender::mesh_attributes_copy_to_bmesh_block (CustomData &data, const Span< MeshToBMeshLayerInfo > copy_info, const int mesh_index, BMHeader &header)
static CustomData blender::get_mesh_to_bm_custom_data (const Mesh &mesh, const bke::AttrDomain domain, const uint64_t cd_type_mask)
void blender::BM_mesh_bm_from_me (BMesh *bm, const Mesh *mesh, const BMeshFromMeshParams *params) ATTR_NONNULL(1
 Mesh -> BMesh.
static BMVert ** blender::bm_to_mesh_vertex_map (BMesh *bm, const int old_verts_num)
 BMesh -> Mesh.
static void blender::assert_bmesh_has_no_mesh_only_attributes (const BMesh &bm)
static void blender::bmesh_to_mesh_calc_object_remap (Main &bmain, Mesh &mesh, BMesh &bm, const int old_totvert)
static Vector< BMeshToMeshLayerInfo > blender::bm_to_mesh_copy_info_calc (const CustomData &bm_data, const bke::AttrDomain domain, Mesh &mesh)
static void blender::bm_vert_table_build (BMesh &bm, MutableSpan< const BMVert * > table, bool &need_select_vert, bool &need_hide_vert)
static void blender::bm_edge_table_build (BMesh &bm, MutableSpan< const BMEdge * > table, bool &need_select_edge, bool &need_hide_edge, bool &need_sharp_edge, bool &need_uv_seams)
static void blender::bm_face_loop_table_build (BMesh &bm, MutableSpan< const BMFace * > face_table, MutableSpan< const BMLoop * > loop_table, bool &need_select_poly, bool &need_hide_poly, bool &need_sharp_face, bool &need_material_index, Vector< int > &loop_layers_not_to_copy)
static void blender::bmesh_block_copy_to_mesh_attributes (const Span< BMeshToMeshLayerInfo > copy_info, const int mesh_index, const void *block)
static void blender::bm_to_mesh_verts (Mesh &mesh, const Span< const BMVert * > bm_verts, const Span< BMeshToMeshLayerInfo > copy_info, AttrSingleValueChecker &single_checker, MutableSpan< bool > select_vert, MutableSpan< bool > hide_vert)
static void blender::bm_to_mesh_edges (Mesh &mesh, const Span< const BMEdge * > bm_edges, const Span< BMeshToMeshLayerInfo > copy_info, AttrSingleValueChecker &single_checker, MutableSpan< bool > select_edge, MutableSpan< bool > hide_edge, MutableSpan< bool > sharp_edge, MutableSpan< bool > uv_seams)
static void blender::bm_to_mesh_faces (Mesh &mesh, const Span< const BMFace * > bm_faces, const Span< BMeshToMeshLayerInfo > copy_info, AttrSingleValueChecker &single_checker, MutableSpan< bool > select_poly, MutableSpan< bool > hide_poly, MutableSpan< bool > sharp_faces, MutableSpan< bool > uv_select_face, MutableSpan< int > material_indices)
static void blender::add_bm_cd_to_mesh (const BMesh &bm, const bke::AttrDomain domain, const uint64_t cd_type_mask, Mesh &mesh)
static void blender::bm_to_mesh_loops (Mesh &mesh, const Span< const BMLoop * > bm_loops, const Span< BMeshToMeshLayerInfo > copy_info, AttrSingleValueChecker &single_checker, MutableSpan< bool > uv_select_vert, MutableSpan< bool > uv_select_edge)
void blender::BM_mesh_bm_to_me (Main *bmain, BMesh *bm, Mesh *mesh, const BMeshToMeshParams *params)
static void blender::mesh_bm_to_me_compact (BMesh &bm, Mesh &mesh, const CustomData_MeshMasks *mask, const bool only_select_and_hide)
void blender::BM_mesh_bm_to_me_for_eval (BMesh &bm, Mesh &mesh, const CustomData_MeshMasks *cd_mask_extra)
void blender::BM_mesh_bm_to_me_only_select_and_hide (BMesh &bm, Mesh &mesh)
Edit-Mesh to Shape Key Conversion

There are some details relating to using data from shape keys that need to be considered carefully for shape key synchronization logic.

Key Block Usage


Key blocks (data in #Mesh.key must be used carefully).

They can be used to query which key blocks are relative to the basis since it's not possible to add/remove/reorder key blocks while in edit-mode.

Key Block Coordinates

Key blocks locations must not be used. This was done from v2.67 to 3.0, causing bugs #35170 & #44415.

Shape key synchronizing could work under the assumption that the key-block is fixed-in-place when entering edit-mode allowing them to be used as a reference when exiting. It often does work but isn't reliable since for example rendering may flush changes from the edit-mesh to the key-block (there are a handful of other situations where changes may be flushed, see ED_editors_flush_edits and related functions). When using undo, it's not known if the data in key-block is from the past or future, so just don't use this data as it causes pain and suffering for users and developers alike.

Instead, use the shape-key values stored in CD_SHAPEKEY since they are reliably based on the original locations, unless explicitly manipulated. It's important to write the final shape-key values back to the CD_SHAPEKEY so applying the difference between the original-basis and the new coordinates isn't done multiple times. Therefore ED_editors_flush_edits and other flushing calls will update both the #Mesh.key and the edit-mode CD_SHAPEKEY custom-data layers.

WARNING: There is an exception to the rule of ignoring coordinates in the destination: that is when shape-key data in bm can't be found (which is itself an error/exception). In this case our own rule is violated as the alternative is losing the shape-data entirely.

Flushing Coordinates Back to the BMesh

The edit-mesh may be flushed back to the Mesh and Key used to generate it. When this is done, the new values are written back to the BMesh's CD_SHAPEKEY as well. This is necessary when editing basis-shapes so the difference in shape keys is not applied multiple times. If it were important to avoid it could be skipped while exiting edit-mode (as the entire BMesh is freed in that case), however it's just copying back a float[3] so the work to check if it's necessary isn't worth the overhead.

In general updating the BMesh's CD_SHAPEKEY makes shake-key logic easier to reason about since it means flushing data back to the mesh has the same behavior as exiting and entering edit-mode (a more common operation). Meaning there is one less corner-case to have to consider.

Exceptional Cases


There are some situations that should not happen in typical usage but are still handled in this code, since failure to handle them could loose user-data. These could be investigated further since if they never happen in practice, we might consider removing them. However, the possibility of an mesh directly being modified by Python or some other low level logic that changes key-blocks means there is a potential this to happen so keeping code to these cases remain supported.

  • Custom Data & Mesh Key Block Synchronization. Key blocks in mesh->key->block should always have an associated CD_SHAPEKEY layer in bm->vdata. If they don't there are two fall-backs for setting the location,
    • Use the value from the original shape key WARNING: this is technically incorrect! (see note on "Key Block Usage").
    • Use the current vertex location, Also not correct but it's better then having it zeroed for example.
  • Missing key-index layer. In this case the basis key won't apply its deltas to other keys and if a shape-key layer is missing, its coordinates will be initialized from the edit-mesh vertex locations instead of attempting to remap the shape-keys coordinates.
Note
These cases are considered abnormal and shouldn't occur in typical usage. A warning is logged in this case to help troubleshooting bugs with shape-keys.
static int blender::bm_to_mesh_shape_layer_index_from_kb (BMesh *bm, KeyBlock *currkey)
static void blender::bm_to_mesh_shape (BMesh *bm, Key *key, MutableSpan< float3 > positions, const bool active_shapekey_to_mvert)

Variables

static CLG_LogRef blender::LOG = {"geom.bmesh.convert"}

Detailed Description

BM mesh conversion functions.

Converting Shape Keys

When converting to/from a Mesh/BMesh you can optionally pass a shape key to edit. This has the effect of editing the shape key-block rather than the original mesh vertex coords (although additional geometry is still allowed and uses fallback locations on converting).

While this works for any mesh/bmesh this is made use of by entering and exiting edit-mode.

There are comments in code but this should help explain the general intention as to how this works converting from/to bmesh.

User Perspective

  • Editmode operations when a shape key-block is active edits only that key-block.
  • The first Basis key-block always matches the Mesh verts.
  • Changing vertex locations of any Basis will apply offsets to those shape keys using this as their Basis.

Entering EditMode - #BM_mesh_bm_from_me

  • The active key-block is used for BMesh vertex locations on entering edit-mode. So obviously the meshes vertex locations remain unchanged and the shape key itself is not being edited directly. Simply the #BMVert.co is a initialized from active shape key (when its set).
  • All key-blocks are added as CustomData layers (read code for details).

Exiting EditMode - #BM_mesh_bm_to_me

This is where the most confusing code is! Won't attempt to document the details here, for that read the code. But basics are as follows.

  • Vertex locations (possibly modified from initial active key-block) are copied directly into the mesh position attribute. (special confusing note that these may be restored later, when editing the 'Basis', read on).
  • if the 'Key' is relative, and the active key-block is the basis for ANY other key-blocks - get an array of offsets between the new vertex locations and the original shape key (before entering edit-mode), these offsets get applied later on to inactive key-blocks using the active one (which we are editing) as their Basis.

Copying the locations back to the shape keys is quite confusing... One main area of confusion is that when editing a 'Basis' key-block 'mesh->key->refkey' The coords are written into the mesh, from the users perspective the Basis coords are written into the mesh when exiting edit-mode.

When not editing the 'Basis', the original vertex locations (stored in the mesh and unchanged during edit-mode), are copied back into the mesh.

This has the effect from the users POV of leaving the mesh un-touched, and only editing the active shape key-block.

Other Notes

Other details noted here which might not be so obvious:

  • The #CD_SHAPEKEY layer is only used in edit-mode, and the #Mesh.key is only used in object-mode. Although the #CD_SHAPEKEY custom-data layer is converted into #Key data-blocks for each undo-step while in edit-mode.
  • The #CD_SHAPE_KEYINDEX layer is used to check if vertices existed when entering edit-mode. Values of the indices are only used for shape-keys when the #CD_SHAPEKEY layer can't be found, allowing coordinates from the #Key to be used to prevent data-loss. These indices are also used to maintain correct indices for hook modifiers and vertex parents.

Definition in file bmesh_mesh_convert.cc.