76 return StringRef(object_name).drop_suffix(2) +
".R";
79 return StringRef(object_name).drop_suffix(2) +
".L";
85 const bool ensure_keys_exist,
92 Mesh &active_mesh = *id_cast<Mesh *>(active_object.
data);
99 auto topology_count_matches = [](
const Mesh &a,
const Mesh &
b) {
103 bool found_object =
false;
104 bool found_non_equal_count =
false;
107 if (ob_iter == &active_object) {
110 if (ob_iter->type !=
OB_MESH) {
119 if (topology_count_matches(*
mesh, active_mesh)) {
125 const Mesh &mesh_orig = *id_cast<const Mesh *>(ob_iter->data);
126 if (topology_count_matches(mesh_orig, active_mesh)) {
130 found_non_equal_count =
true;
139 if (found_non_equal_count) {
144 if (compatible_objects.
is_empty()) {
146 reports,
RPT_WARNING,
"No additional selected meshes with equal vertex count to join");
151 for (
const ObjectInfo &info : compatible_objects) {
152 if (!info.name.endswith(
".L") && !info.name.endswith(
".R")) {
159 if (!active_mesh.
key) {
167 int mirror_count = 0;
168 int mirror_fail_count = 0;
169 int keys_changed = 0;
170 bool any_keys_added =
false;
171 for (
const ObjectInfo &info : compatible_objects) {
173 if (ensure_keys_exist) {
176 any_keys_added =
true;
190 if (!ensure_keys_exist) {
191 if (keys_changed == 0) {
205 if (any_keys_added && bmain) {
223 Mesh **r_mesh_mirror,
226 Mesh *mesh_mirror =
nullptr;
227 BMesh *bm_mirror =
nullptr;
230 if (mesh_eval !=
nullptr) {
231 mesh_mirror = mesh_eval;
233 else if (
mesh->runtime->edit_mesh) {
240 *r_mesh_mirror = mesh_mirror;
241 *r_bm_mirror = bm_mirror;
277 const Span<float3> positions = mesh_eval ? mesh_eval->vert_positions() :
mesh->vert_positions();
281 vec[0] = -positions[
index][0];
282 vec[1] = positions[
index][1];
283 vec[2] = positions[
index][2];
311 if ((isfinite(co[0]) ==
false) || (isfinite(co[1]) ==
false) || (isfinite(co[2]) ==
false)) {
353 return reinterpret_cast<BMVert *
>(poinval);
388static float *editmesh_get_mirror_uv(
389 BMesh *
bm,
int axis,
float *uv,
float *mirrCent,
float *face_cent)
396 if (
isnan(uv[0]) || !isfinite(uv[0]) ||
isnan(uv[1]) || !isfinite(uv[1])) {
402 vec[1] = -((uv[1]) - mirrCent[1]) + mirrCent[1];
404 cent_vec[0] = face_cent[0];
405 cent_vec[1] = -((face_cent[1]) - mirrCent[1]) + mirrCent[1];
408 vec[0] = -((uv[0]) - mirrCent[0]) + mirrCent[0];
411 cent_vec[0] = -((face_cent[0]) - mirrCent[0]) + mirrCent[0];
412 cent_vec[1] = face_cent[1];
423 if ((
fabsf(cent[0] - cent_vec[0]) < 0.001f) && (
fabsf(cent[1] - cent_vec[1]) < 0.001f)) {
429 if ((
fabsf(luv[0] - vec[0]) < 0.001f) && (
fabsf(luv[1] - vec[1]) < 0.001f)) {
444 const MFace *mf =
static_cast<const MFace *
>(ptr);
448 v0 = std::min({mf->
v1, mf->
v2, mf->
v3, mf->
v4});
449 v1 = std::max({mf->
v1, mf->
v2, mf->
v3, mf->
v4});
452 v0 = std::min({mf->
v1, mf->
v2, mf->
v3});
453 v1 = std::min({mf->
v1, mf->
v2, mf->
v3});
456 return ((v0 * 39) ^ (v1 * 31));
462 if (a->
v1 ==
b->v1 && a->
v2 ==
b->v2 && a->
v3 ==
b->v3 && a->
v4 ==
b->v4) {
465 if (a->
v4 ==
b->v1 && a->
v1 ==
b->v2 && a->
v2 ==
b->v3 && a->
v3 ==
b->v4) {
468 if (a->
v3 ==
b->v1 && a->
v4 ==
b->v2 && a->
v1 ==
b->v3 && a->
v2 ==
b->v4) {
471 if (a->
v2 ==
b->v1 && a->
v3 ==
b->v2 && a->
v4 ==
b->v3 && a->
v1 ==
b->v4) {
476 if (a->
v1 ==
b->v1 && a->
v2 ==
b->v2 && a->
v3 ==
b->v3) {
479 if (a->
v3 ==
b->v1 && a->
v1 ==
b->v2 && a->
v2 ==
b->v3) {
482 if (a->
v2 ==
b->v1 && a->
v3 ==
b->v2 && a->
v1 ==
b->v3) {
493 static_cast<MFace *
>(
const_cast<void *
>(
b))) == -1);
500 const MFace *mf, *hashmf;
502 int *mirrorverts, *mirrorfaces;
505 const int totvert = mesh_eval ? mesh_eval->
verts_num :
mesh->verts_num;
512 const Span<float3> vert_positions = mesh_eval ? mesh_eval->vert_positions() :
513 mesh->vert_positions();
514 const MFace *mface =
static_cast<const MFace *
>(
527 for (a = 0, mf = mface; a < totface; a++, mf++) {
531 for (a = 0, mf = mface; a < totface; a++, mf++) {
532 mirrormf.
v1 = mirrorverts[mf->
v3];
533 mirrormf.
v2 = mirrorverts[mf->
v2];
534 mirrormf.
v3 = mirrorverts[mf->
v1];
535 mirrormf.
v4 = (mf->
v4) ? mirrorverts[mf->
v4] : 0;
538 if (mf->
v4 && mirrormf.
v4 == 0) {
539 std::swap(mirrormf.
v1, mirrormf.
v3);
540 std::swap(mirrormf.
v2, mirrormf.
v4);
545 mirrorfaces[a * 2] = hashmf - mface;
549 mirrorfaces[a * 2] = -1;
554 MEM_delete(mirrorverts);
567 if (!
mesh ||
mesh->faces_num == 0) {
586 if ((*r_index) == 0 || (*r_index) >
uint(
mesh->faces_num)) {
602 const int *corner_verts,
607 for (
int j = face.
size(); j--;) {
609 const int v_idx = corner_verts[face[j]];
614 if (len_test < *r_len_best) {
615 *r_len_best = len_test;
616 *r_v_idx_best = v_idx;
641 const float mval_f[2] = {
float(mval[0]),
float(mval[1])};
644 const Span<float3> vert_positions = mesh_eval->vert_positions();
646 const Span<int> corner_verts = mesh_eval->corner_verts();
648 const int *index_mp_to_orig =
static_cast<const int *
>(
652 if (index_mp_to_orig) {
654 if (index_mp_to_orig[i] == face_index) {
666 if (face_index < faces.
size()) {
679 const int *index_mv_to_orig =
static_cast<const int *
>(
681 if (index_mv_to_orig) {
682 v_idx_best = index_mv_to_orig[v_idx_best];
686 if ((v_idx_best !=
ORIGINDEX_NONE) && (v_idx_best < mesh->verts_num)) {
687 *r_index = v_idx_best;
702 if (!
mesh ||
mesh->edges_num == 0) {
724 if (edge_idx_best == 0 || edge_idx_best >
uint(
mesh->edges_num)) {
731 *r_index = edge_idx_best;
781 if (!
mesh ||
mesh->verts_num == 0) {
801 if ((*r_index) == 0 || (*r_index) >
uint(
mesh->verts_num)) {
814 const float mval_f[2] = {
float(mval[0]),
float(mval[1])};
820 if (mesh_eval ==
nullptr) {
827 data.region = region;
828 data.mval_f = mval_f;
830 data.v_idx_best = -1;
835 if (
data.v_idx_best == -1) {
839 *r_index =
data.v_idx_best;
849 if (!
mesh->vertex_group_names.is_empty()) {
853 if (cd_dvert_offset != -1) {
879 if (
index == -1 ||
mesh->deform_verts().is_empty()) {
883 return &dverts[
index];
910 for (
Object *obedit : objects) {
927 int elem_offset[4] = {0, 0, 0, 0};
928 for (
Object *obedit : objects) {
946 if (
mesh->faces_num == 0 ||
mesh->verts_num == 0) {
980 ot->
name =
"Reorder Mesh Spatially";
981 ot->
idname =
"MESH_OT_reorder_vertices_spatial";
983 "Reorder mesh faces and vertices based on their spatial position for better BVH building "
984 "and sculpting performance.";
#define CTX_DATA_BEGIN(C, Type, instance, member)
CustomData interface, see also DNA_customdata_types.h.
General operations, lookup, etc. for materials.
General operations, lookup, etc. for blender objects.
A BVH for high poly meshes.
Object is a sort of wrapper for general info.
#define V3D_PROJ_TEST_CLIP_DEFAULT
Read Blender memory allocation and freeing.
#define BM_ELEM_CD_GET_FLOAT_P(ele, offset)
#define BM_ELEM_CD_GET_VOID_P(ele, offset)
#define BM_elem_index_get(ele)
#define BM_ITER_ELEM(ele, iter, data, itype)
#define BM_ITER_MESH(ele, iter, bm, itype)
constexpr int64_t size() const
constexpr const T * data() const
constexpr IndexRange index_range() const
void append(const T &value)
GAttributeReader lookup(const StringRef name) const
constexpr bool endswith(StringRef suffix) const
IndexRange index_range() const
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
void mesh_apply_spatial_organization(Mesh &mesh)
Object * context_active_object(const bContext *C)
void shape_key_mirror(Object *ob, KeyBlock *kb, bool use_topology, int &totmirr, int &totfail)
void geometry_begin(const Scene &scene, Object &ob, const wmOperator *op)
void geometry_end(Object &ob)
void WM_main_add_notifier(uint type, void *reference)
int CustomData_get_offset(const CustomData *data, eCustomDataType type)
void EDBM_mesh_elem_index_ensure_multi(Span< Object * > objects, char htype)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
static int mesh_get_x_mirror_vert_spatial(Object *ob, Mesh *mesh_eval, int index)
void ED_mesh_mirror_topo_table_end(Object *ob)
bool ED_mesh_pick_face_vert(bContext *C, Object *ob, const int mval[2], uint dist_px, uint *r_index)
uint DRW_select_buffer_find_nearest_to_point(Depsgraph *depsgraph, ARegion *region, View3D *v3d, const int center[2], uint id_min, uint id_max, uint *dist)
void ED_mesh_mirrtopo_init(BMesh *bm, Mesh *mesh, MirrTopoStore_t *mesh_topo_store, bool skip_bm_vert_array_init)
GHash * BLI_ghash_new_ex(GHashHashFP hashfp, GHashCmpFP cmpfp, const char *info, unsigned int nentries_reserve) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT
MDeformVert * ED_mesh_active_dvert_get_em(Object *ob, BMVert **r_eve)
BMesh * BKE_editmesh_bmesh_get_for_write(Mesh *mesh)
ViewContext ED_view3d_viewcontext_init(bContext *C, Depsgraph *depsgraph)
int ED_mesh_mirror_spatial_table_lookup(Object *ob, Mesh *mesh_eval, const float co[3])
ATTR_WARN_UNUSED_RESULT const BMLoop * l
eV3DProjStatus ED_view3d_project_float_object(const ARegion *region, const float co[3], float r_co[2], eV3DProjTest flag)
void * BLI_ghash_lookup(const GHash *gh, const void *key) ATTR_WARN_UNUSED_RESULT
static int mirror_facerotation(const MFace *a, const MFace *b)
static int mesh_get_x_mirror_vert_topo(Object *ob, Mesh *mesh, int index)
void BKE_keyblock_update_from_mesh(const Mesh *mesh, KeyBlock *kb)
void ED_mesh_mirror_spatial_table_end(Object *ob)
MDeformVert * ED_mesh_active_dvert_get_ob(Object *ob, int *r_index)
int mesh_get_x_mirror_vert(Object *ob, Mesh *mesh_eval, int index, bool use_topology)
KeyBlock * BKE_keyblock_find_name(Key *key, const char name[])
bool ED_mesh_mirrtopo_recalc_check(BMesh *bm, Mesh *mesh, MirrTopoStore_t *mesh_topo_store)
ATTR_WARN_UNUSED_RESULT const BMVert * v
void MESH_OT_reorder_vertices_spatial(wmOperatorType *ot)
int ED_mesh_mirror_get_vert(Object *ob, int index)
const char * BKE_id_name(const ID &id)
static void ed_mesh_pick_face_vert__mpoly_find(ARegion *region, const float mval[2], const IndexRange face, const Span< float3 > vert_positions, const int *corner_verts, float *r_len_best, int *r_v_idx_best)
uint DRW_select_buffer_sample_point(Depsgraph *depsgraph, ARegion *region, View3D *v3d, const int center[2])
static BMVert * editbmesh_get_x_mirror_vert_spatial(Object *ob, BMesh *bm, const float co[3])
static BMVert * editbmesh_get_x_mirror_vert_topo(Object *ob, BMesh *bm, BMVert *eve, int index)
void BLI_ghash_insert(GHash *gh, void *key, void *val)
int * mesh_get_x_mirror_faces(Object *ob, Mesh *mesh_eval)
static wmOperatorStatus mesh_reorder_vertices_spatial_exec(bContext *C, wmOperator *op)
BLI_INLINE void mesh_mirror_topo_table_get_meshes(Object *ob, Mesh *mesh_eval, Mesh **r_mesh_mirror, BMesh **r_bm_mirror)
bool ED_mesh_pick_face(bContext *C, Object *ob, const int mval[2], uint dist_px, uint *r_index)
void DRW_select_buffer_context_create(Depsgraph *depsgraph, Span< Base * > bases, short select_mode)
void BLI_ghash_free(GHash *gh, GHashKeyFreeFP keyfreefp, GHashValFreeFP valfreefp)
static void ed_mesh_pick_vert__mapFunc(void *user_data, int index, const float co[3], const float[3])
Key * BKE_key_add(Main *bmain, ID *id)
void DEG_id_tag_update(ID *id, unsigned int flags)
void BKE_report(ReportList *reports, eReportType type, const char *message)
BMesh const char void * data
T * DEG_get_evaluated(const Depsgraph *depsgraph, T *id)
Depsgraph * CTX_data_ensure_evaluated_depsgraph(const bContext *C, bool rna_write_check=false)
void ED_mesh_mirrtopo_free(MirrTopoStore_t *mesh_topo_store)
void BM_face_uv_calc_center_median(const BMFace *f, const int cd_loop_uv_offset, float r_cent[2])
int BKE_mesh_mselect_active_get(const Mesh *mesh, eMSelect_Type type)
bool ED_mesh_pick_edge(bContext *C, Object *ob, const int mval[2], uint dist_px, uint *r_index)
const void * CustomData_get_layer(const CustomData *data, eCustomDataType type)
static bool mirror_facecmp(const void *a, const void *b)
void ED_view3d_init_mats_rv3d(const Object *ob, RegionView3D *rv3d)
void ED_mesh_report_mirror_ex(ReportList &reports, int totmirr, int totfail, char selectmode)
@ ME_SCULPT_DYNAMIC_TOPOLOGY
BLI_INLINE BMVert * BM_vert_at_index(BMesh *bm, const int index)
void WM_event_add_notifier(const bContext *C, uint type, void *reference)
void ED_view3d_select_id_validate(const ViewContext *vc)
MINLINE float len_manhattan_v2v2(const float a[2], const float b[2]) ATTR_WARN_UNUSED_RESULT
void EDBM_mesh_stats_multi(Span< Object * > objects, int totelem[3], int totelem_sel[3])
static bool mesh_reorder_vertices_spatial_poll(bContext *C)
void BKE_reportf(ReportList *reports, eReportType type, const char *format,...) ATTR_PRINTF_FORMAT(3
static MirrTopoStore_t mesh_topo_store
Main * CTX_data_main(const bContext *C)
static bool ed_mesh_mirror_topo_table_update(Object *ob, Mesh *mesh_eval)
KeyBlock * BKE_keyblock_add(Key *key, const char *name)
ID_Type GS(const char *name)
wmOperatorStatus ED_mesh_shapes_join_objects_exec(bContext *C, bool ensure_keys_exist, bool mirror, ReportList *reports)
Base * BKE_view_layer_base_find(ViewLayer *view_layer, Object *ob)
void BKE_mesh_foreach_mapped_vert(const Mesh *mesh, void(*func)(void *user_data, int index, const float co[3], const float no[3]), void *user_data, MeshForeachFlag flag)
const BMesh * BKE_editmesh_bmesh_get(const Object *ob)
void ED_mesh_mirror_spatial_table_begin(Object *ob, Mesh *mesh_eval)
void ED_mesh_mirror_topo_table_begin(Object *ob, Mesh *mesh_eval)
BMVert * editbmesh_get_x_mirror_vert(Object *ob, BMesh *bm, BMVert *eve, const float co[3], int index, bool use_topology)
void DEG_relations_tag_update(Main *bmain)
Scene * CTX_data_scene(const bContext *C)
MDeformVert * ED_mesh_active_dvert_get_only(Object *ob)
bool ED_mesh_pick_vert(bContext *C, Object *ob, const int mval[2], uint dist_px, bool use_zbuf, uint *r_index)
static uint mirror_facehash(const void *ptr)
void BKE_keyblock_convert_from_mesh(const Mesh *mesh, const Key *key, KeyBlock *kb)
void BM_mesh_elem_index_ensure_ex(BMesh *bm, const char htype, int elem_offset[4])
Object * CTX_data_active_object(const bContext *C)
ARegion * CTX_wm_region(const bContext *C)
BMVert * BM_mesh_active_vert_get(BMesh *bm)
void * MEM_new_array_zeroed(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
Mesh * BKE_object_get_evaluated_mesh(const Object *object_eval)
static std::string create_mirrored_name(const StringRefNull object_name, const bool mirror)
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
ShapekeyContainerType type
VArraySpan< bool > hide_vert
wmOperatorStatus(*) exec(bContext *C, wmOperator *op) ATTR_WARN_UNUSED_RESULT
bool(*) poll(bContext *C) ATTR_WARN_UNUSED_RESULT
struct ReportList * reports