Blender V5.3
editmesh_fit_edge_loops_to_annotation.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2026 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "DNA_mesh_types.h"
10#include "DNA_object_types.h"
11#include "DNA_view3d_types.h"
12
13#include "BKE_context.hh"
14#include "BKE_editmesh.hh"
15#include "BKE_layer.hh"
16
17#include "BLI_listbase.hh"
18#include "BLI_math_matrix.hh"
19#include "BLI_math_vector_c.hh"
20
21#include "BLT_translation.hh"
22
24
25#include "ED_mesh.hh"
26#include "ED_screen.hh"
27
28#include "RNA_access.hh"
29#include "RNA_define.hh"
30
31#include "UI_interface.hh"
33
34#include "BKE_annotations.h"
36#include "ED_gpencil_legacy.hh"
37
38#include "mesh_intern.hh" /* own include */
39
40namespace blender {
41
44 "SPREAD",
45 0,
46 "Spread",
47 "Distribute vertices along the full stroke, retaining relative distances between the "
48 "vertices"},
50 "SPREAD_EVENLY",
51 0,
52 "Spread Evenly",
53 "Distribute vertices at regular distances along the full stroke "},
55 "PROJECT",
56 0,
57 "Project",
58 "Project vertices onto the stroke using vertex normals and connected edges. "
59 "Requires more exact annotation placement, otherwise it may do nothing"},
60 {0, nullptr},
61};
62
64{
65 const Main *bmain = CTX_data_main(C);
66 const Scene *scene = CTX_data_scene(C);
67 ViewLayer *view_layer = CTX_data_view_layer(C);
69 *bmain, scene, view_layer, CTX_wm_view3d(C));
70
71 const float factor = RNA_float_get(op->ptr, "factor");
72 const int method = RNA_enum_get(op->ptr, "method");
73 const bool delete_strokes = RNA_boolean_get(op->ptr, "delete_strokes");
74 bool lock[3];
75 RNA_boolean_get_array(op->ptr, "lock", lock);
76 bool changed_multi = false;
77
79 bGPDlayer *gpl = gpd ? BKE_annotations_layer_active_get(gpd) : nullptr;
80 bGPDframe *gpf = gpl ? gpl->actframe : nullptr;
81 Depsgraph *depsgraph = CTX_data_ensure_evaluated_depsgraph(C);
82
83 for (Object *obedit : objects) {
85
86 Vector<Vector<float3>> all_points;
87 if (gpf) {
88 for (bGPDstroke *gps = gpf->strokes.first(); gps; gps = gps->next) {
89 if (gps->totpoints < 2) {
90 continue;
91 }
92
93 if (!(gps->flag & GP_STROKE_3DSPACE)) {
94 continue;
95 }
96
97 Vector<float3> points;
98 points.reserve(gps->totpoints);
99 Object *obedit_eval = DEG_get_evaluated(depsgraph, obedit);
100 float4x4 mat = obedit_eval->world_to_object();
101
102 for (const int i : IndexRange(gps->totpoints)) {
103 bGPDspoint *stroke_point = &gps->points[i];
104 float3 local_co = math::transform_point(
105 mat, float3(stroke_point->x, stroke_point->y, stroke_point->z));
106 points.append(local_co);
107 }
108
109 all_points.append(std::move(points));
110 }
111 }
112
113 Vector<Span<float3>> stroke_data;
114 stroke_data.reserve(all_points.size());
115 for (const Vector<float3> &points : all_points) {
116 stroke_data.append(points);
117 }
118 if (!EDBM_op_callf(
119 bm,
120 op,
121 "fit_edge_loops_to_annotation geom=%he factor=%f method=%i strokes=%p lock_x=%b "
122 "lock_y=%b lock_z=%b",
124 factor,
125 method,
126 &stroke_data,
127 lock[0],
128 lock[1],
129 lock[2]))
130 {
131 continue;
132 }
133
134 changed_multi = true;
135 EDBMUpdate_Params params{};
136 params.calc_looptris = true;
137 params.calc_normals = true;
138 EDBM_update(id_cast<Mesh *>(obedit->data), &params);
139 }
140
141 if (changed_multi && delete_strokes && gpf) {
142 bool deleted = false;
143 bGPDstroke *gps_next;
144 for (bGPDstroke *gps = gpf->strokes.first(); gps; gps = gps_next) {
145 gps_next = gps->next;
146 if (gps->totpoints >= 2 && (gps->flag & GP_STROKE_3DSPACE)) {
147 BLI_remlink(&gpf->strokes, gps);
149 deleted = true;
150 }
151 }
152 if (deleted) {
154 }
155 }
156
157 return changed_multi ? OPERATOR_FINISHED : OPERATOR_CANCELLED;
158}
159
161{
162 /* A custom UI function is needed to draw the axis locks (X/Y/Z) as a row of toggle buttons. */
163 ui::Layout &layout = *op->layout;
164 layout.use_property_split_set(true);
165
166 layout.prop(op->ptr, "factor", UI_ITEM_NONE, std::nullopt, ICON_NONE);
167 layout.prop(op->ptr, "method", UI_ITEM_NONE, std::nullopt, ICON_NONE);
168 layout.prop(op->ptr, "delete_strokes", UI_ITEM_NONE, std::nullopt, ICON_NONE);
169
170 ui::Layout &lock_row = layout.row(true, IFACE_("Lock"));
171 PropertyRNA *lock_prop = RNA_struct_find_property(op->ptr, "lock");
172 lock_row.prop(op->ptr, lock_prop, 0, 0, ui::ITEM_R_TOGGLE, "X", ICON_NONE);
173 lock_row.prop(op->ptr, lock_prop, 1, 0, ui::ITEM_R_TOGGLE, "Y", ICON_NONE);
174 lock_row.prop(op->ptr, lock_prop, 2, 0, ui::ITEM_R_TOGGLE, "Z", ICON_NONE);
175}
176
178{
179 ot->name = "Fit Edge Loops To Annotation";
180 ot->description = "Stretch selected vertices to active stroke";
181 ot->idname = "MESH_OT_fit_edge_loops_to_annotation";
182
186
188
190 ot->srna, "factor", 1.0f, 0.0f, 1.0f, "Factor", "Annotation factor", 0.0f, 1.0f);
192 "delete_strokes",
193 false,
194 "Delete Strokes",
195 "Remove strokes if they have been used");
196 RNA_def_enum(ot->srna,
197 "method",
199 0,
200 "Method",
201 "Method of distributing the vertices over the stroke");
202 RNA_def_boolean_array(ot->srna, "lock", 3, nullptr, "Lock", "Lock editing of the axis");
203}
204
205} // namespace blender
#define IFACE_(msgid)
Object is a sort of wrapper for general info.
#define C
Definition RandGen.cpp:29
#define UI_ITEM_NONE
volatile int lock
constexpr IndexRange()=default
int64_t size() const
void append(const T &value)
void reserve(const int64_t min_capacity)
VecBase< T, 3 > transform_point(const CartesianBasis &basis, const VecBase< T, 3 > &v)
BMesh Iterators.
void MESH_OT_fit_edge_loops_to_annotation(wmOperatorType *ot)
int RNA_enum_get(PointerRNA *ptr, const char *name)
PropertyRNA * RNA_struct_find_property(PointerRNA *ptr, const char *identifier)
BMesh * BKE_editmesh_bmesh_get_for_write(Mesh *mesh)
Definition editmesh.cc:79
void EDBM_update(Mesh *mesh, const EDBMUpdate_Params *params)
bool EDBM_op_callf(BMesh *bm, wmOperator *op, const char *fmt,...)
PropertyRNA * RNA_def_enum(StructOrFunctionRNA *cont_, const char *identifier, const EnumPropertyItem *items, const int default_value, const char *ui_name, const char *ui_description)
static void edbm_fit_edge_loops_to_annotation_ui(bContext *, wmOperator *op)
PropertyRNA * RNA_def_boolean_array(StructOrFunctionRNA *cont_, const char *identifier, const int len, const bool *default_value, const char *ui_name, const char *ui_description)
void BKE_annotations_free_stroke(struct bGPDstroke *gps)
void RNA_boolean_get_array(PointerRNA *ptr, const char *name, bool *values)
MatBase< float, 4, 4 > float4x4
static const EnumPropertyItem prop_method_items[]
ViewLayer * CTX_data_view_layer(const bContext *C)
PropertyRNA * RNA_def_float_factor(StructOrFunctionRNA *cont_, const char *identifier, const float default_value, const float hardmin, const float hardmax, const char *ui_name, const char *ui_description, const float softmin, const float softmax)
bool ED_operator_editmesh(bContext *C)
void BLI_remlink(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:138
void DEG_id_tag_update(ID *id, unsigned int flags)
T * DEG_get_evaluated(const Depsgraph *depsgraph, T *id)
Depsgraph * CTX_data_ensure_evaluated_depsgraph(const bContext *C, bool rna_write_check=false)
bool RNA_boolean_get(PointerRNA *ptr, const char *name)
bGPdata * ED_annotation_data_get_active(const bContext *C)
struct bGPDlayer * BKE_annotations_layer_active_get(struct bGPdata *gpd)
static wmOperatorStatus edbm_fit_edge_loops_to_annotation_exec(bContext *C, wmOperator *op)
@ TO_ANNOTATION_SPREAD_EVENLY
Main * CTX_data_main(const bContext *C)
@ ID_RECALC_GEOMETRY
Definition DNA_ID.h:1074
Vector< Object * > BKE_view_layer_array_from_objects_in_edit_mode_unique_data(const Main &bmain, const Scene *scene, ViewLayer *view_layer, const View3D *v3d)
Scene * CTX_data_scene(const bContext *C)
@ OPTYPE_REGISTER
Definition WM_types.hh:188
@ OPTYPE_UNDO
Definition WM_types.hh:190
VecBase< float, 3 > float3
PropertyRNA * RNA_def_boolean(StructOrFunctionRNA *cont_, const char *identifier, const bool default_value, const char *ui_name, const char *ui_description)
float RNA_float_get(PointerRNA *ptr, const char *name)
View3D * CTX_wm_view3d(const bContext *C)
ListBaseT< bGPDstroke > strokes
void use_property_split_set(bool is_sep)
void prop(PointerRNA *ptr, PropertyRNA *prop, int index, int value, eUI_Item_Flag flag, std::optional< StringRef > name_opt, int icon, std::optional< StringRef > placeholder=std::nullopt)
Layout & row(bool align)
wmOperatorStatus(*) exec(bContext *C, wmOperator *op) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1125
const char * description
Definition WM_types.hh:1115
void(*) ui(bContext *C, wmOperator *op)
Definition WM_types.hh:1176
bool(*) poll(bContext *C) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1165