Blender V5.3
transform_convert_grease_pencil.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "ANIM_keyframing.hh"
10
11#include "BKE_context.hh"
12#include "BKE_curves_utils.hh"
13
15
17
18#include "ED_curves.hh"
19#include "ED_grease_pencil.hh"
20
21#include "transform.hh"
22#include "transform_convert.hh"
23#include "transform_snap.hh"
24
25/* -------------------------------------------------------------------- */
28
30
32{
33 Depsgraph *depsgraph = CTX_data_depsgraph_pointer(C);
34 Scene *scene = CTX_data_scene(C);
37 const bool use_proportional_edit = (t->flag & T_PROP_EDIT_ALL) != 0;
38 const bool use_connected_only = (t->flag & T_PROP_CONNECTED) != 0;
39 const bool use_individual_origins = (t->around == V3D_AROUND_LOCAL_ORIGINS);
40 ToolSettings *ts = scene->toolsettings;
41 const bool is_scale_thickness = ((t->mode == TFM_CURVE_SHRINKFATTEN) ||
43
44 int total_number_of_drawings = 0;
45 Vector<CurvesTransformData *> all_curves_transform_data;
46 /* Count the number layers in all objects. */
47 for (const int i : trans_data_contrainers.index_range()) {
48 TransDataContainer &tc = trans_data_contrainers[i];
49 GreasePencil &grease_pencil = *id_cast<GreasePencil *>(tc.obedit->data);
50
52 tc.custom.type);
53
55 *scene, grease_pencil);
56
57 if (animrig::is_autokey_on(scene)) {
58 for (const int info_i : curves_transform_data->drawings.index_range()) {
59 bke::greasepencil::Layer &target_layer = grease_pencil.layer(
60 curves_transform_data->drawings[info_i].layer_index);
61 const int current_frame = scene->r.cfra;
62 std::optional<int> start_frame = target_layer.start_frame_at(current_frame);
63 if (start_frame.has_value() && (start_frame.value() != current_frame)) {
64 if (grease_pencil.insert_duplicate_frame(
65 target_layer, *target_layer.start_frame_at(current_frame), current_frame, false))
66 {
68 curves_transform_data->duplicate_layer_keyframes.add(&target_layer, current_frame);
69 }
70 }
71 }
73 *scene, grease_pencil);
74 }
75
76 all_curves_transform_data.append(curves_transform_data);
77 total_number_of_drawings += curves_transform_data->drawings.size();
78 }
79
80 Array<Vector<IndexMask>> points_to_transform_per_attribute(total_number_of_drawings);
81 Array<IndexMask> bezier_curves(total_number_of_drawings);
82 int layer_offset = 0;
83
84 /* Count selected elements per layer per object and create TransData structs. */
85 for (const int i : trans_data_contrainers.index_range()) {
86 TransDataContainer &tc = trans_data_contrainers[i];
87 CurvesTransformData &curves_transform_data = *all_curves_transform_data[i];
88 tc.data_len = 0;
89
91 curves_transform_data.drawings.as_span();
92 curves_transform_data.grease_pencil_falloffs.reinitialize(drawings.size());
93
94 for (ed::greasepencil::MutableDrawingInfo info : drawings) {
95 bke::CurvesGeometry &curves = info.drawing.strokes_for_write();
97 curves);
98 std::array<IndexMask, 3> selection_per_attribute;
99
101 *object, info.drawing, info.layer_index, curves_transform_data.memory);
103 *object, info.drawing, info.layer_index, curves_transform_data.memory);
104
105 bezier_curves[layer_offset] = bke::curves::indices_for_type(curves.curve_types(),
106 curves.curve_type_counts(),
108 editable_strokes,
109 curves_transform_data.memory);
110 const OffsetIndices<int> points_by_curve = curves.points_by_curve();
111 const IndexMask bezier_points = IndexMask::from_ranges(
112 points_by_curve, bezier_curves[layer_offset], curves_transform_data.memory);
113
114 for (const int attribute_i : selection_attribute_names.index_range()) {
115 const StringRef &selection_name = selection_attribute_names[attribute_i];
116 selection_per_attribute[attribute_i] = ed::curves::retrieve_selected_points(
117 curves, selection_name, bezier_points, curves_transform_data.memory);
118
119 /* Make sure only editable points are used. */
120 selection_per_attribute[attribute_i] = IndexMask::from_intersection(
121 selection_per_attribute[attribute_i], editable_points, curves_transform_data.memory);
122 }
123
124 /* Alter selection as in legacy curves bezt_select_to_transform_triple_flag(). */
125 if (!bezier_points.is_empty()) {
127 t->mode, selection_per_attribute, bezier_points, curves))
128 {
129 info.drawing.tag_topology_changed();
130 }
131
133 const index_mask::Expr &selected_bezier_points = builder.intersect(
134 {&bezier_points, selection_per_attribute.data()});
135
136 /* Select bezier handles that must be transformed because the control point is
137 * selected. */
138 selection_per_attribute[1] = evaluate_expression(
139 builder.merge({&selection_per_attribute[1], &selected_bezier_points}),
140 curves_transform_data.memory);
141 selection_per_attribute[2] = evaluate_expression(
142 builder.merge({&selection_per_attribute[2], &selected_bezier_points}),
143 curves_transform_data.memory);
144 }
145
146 if (use_proportional_edit) {
147 const IndexMask editable_bezier_points = IndexMask::from_intersection(
148 editable_points, bezier_points, curves_transform_data.memory);
149 tc.data_len += editable_points.size() + 2 * editable_bezier_points.size();
150 points_to_transform_per_attribute[layer_offset].append(editable_points);
151
152 if (selection_attribute_names.size() > 1) {
153 points_to_transform_per_attribute[layer_offset].append(editable_bezier_points);
154 points_to_transform_per_attribute[layer_offset].append(editable_bezier_points);
155 }
156 }
157 else {
158 for (const int selection_i : selection_attribute_names.index_range()) {
159 points_to_transform_per_attribute[layer_offset].append(
160 selection_per_attribute[selection_i]);
161 tc.data_len += points_to_transform_per_attribute[layer_offset][selection_i].size();
162 }
163 }
164
165 layer_offset++;
166 }
167
168 if (tc.data_len > 0) {
170 curves_transform_data.positions.reinitialize(tc.data_len);
171 }
172 else {
173 tc.custom.type.free_cb(t, &tc, &tc.custom.type);
174 }
175 }
176
177 /* Reuse the variable `layer_offset`. */
178 layer_offset = 0;
179 IndexMaskMemory memory;
180
181 /* Populate TransData structs. */
182 for (const int i : trans_data_contrainers.index_range()) {
183 TransDataContainer &tc = trans_data_contrainers[i];
184 if (tc.data_len == 0) {
185 continue;
186 }
187 Object *object_eval = DEG_get_evaluated(depsgraph, tc.obedit);
188 GreasePencil &grease_pencil = *id_cast<GreasePencil *>(tc.obedit->data);
190
191 CurvesTransformData &transform_data = *static_cast<CurvesTransformData *>(tc.custom.type.data);
192 const Span<ed::greasepencil::MutableDrawingInfo> drawings = transform_data.drawings.as_span();
193
194 transform_data.aligned_with_left.reinitialize(drawings.size());
195 transform_data.aligned_with_right.reinitialize(drawings.size());
196
197 for (const int drawing : drawings.index_range()) {
198 ed::greasepencil::MutableDrawingInfo info = drawings[drawing];
199 const bke::greasepencil::Layer &layer = *layers[info.layer_index];
200 const float4x4 layer_space_to_world_space = layer.to_world_space(*object_eval);
202 const bke::crazyspace::GeometryDeformation deformation =
204 *CTX_data_depsgraph_pointer(C), *object, info.drawing);
205
206 std::optional<MutableSpan<float>> value_attribute;
207 if (t->mode == TFM_GPENCIL_OPACITY) {
209 value_attribute = opacities;
210 }
211 else if (is_scale_thickness) {
213 value_attribute = radii;
214 }
215
216 const IndexMask affected_strokes = use_proportional_edit || use_individual_origins ?
218 *object, info.drawing, info.layer_index, memory) :
219 IndexMask();
220
221 CurvesTransformData &curves_transform_data = *static_cast<CurvesTransformData *>(
222 tc.custom.type.data);
223 curves_transform_data.grease_pencil_falloffs[drawing] = info.multi_frame_falloff;
224 float &drawing_falloff = curves_transform_data.grease_pencil_falloffs[drawing];
226 tc,
227 curves,
228 layer_space_to_world_space,
229 deformation,
230 value_attribute,
231 points_to_transform_per_attribute[layer_offset],
232 affected_strokes,
233 use_connected_only,
234 bezier_curves[layer_offset],
235 &drawing_falloff);
237 curves, points_to_transform_per_attribute[layer_offset], drawing, tc.custom.type);
238
239 layer_offset++;
240 }
241 }
242}
243
245{
246 if (t->state != TRANS_CANCEL) {
248 }
249
250 const Span<TransDataContainer> trans_data_contrainers(t->data_container, t->data_container_len);
251 for (const TransDataContainer &tc : trans_data_contrainers) {
252 GreasePencil &grease_pencil = *id_cast<GreasePencil *>(tc.obedit->data);
253 const CurvesTransformData &transform_data = *static_cast<CurvesTransformData *>(
254 tc.custom.type.data);
255
256 const Vector<ed::greasepencil::MutableDrawingInfo> drawings = transform_data.drawings;
257
258 int layer_i = 0;
259 for (const int64_t i : drawings.index_range()) {
260 ed::greasepencil::MutableDrawingInfo info = drawings[i];
262
263 if (t->mode == TFM_CURVE_SHRINKFATTEN) {
264 curves.tag_radii_changed();
265 }
266 else if (t->mode == TFM_TILT) {
267 curves.tag_normals_changed();
268 }
269 else {
270 const Vector<MutableSpan<float3>> positions_per_selection_attr =
272 for (MutableSpan<float3> positions : positions_per_selection_attr) {
274 tc.custom.type, layer_i++, positions);
275 }
276 curves.tag_positions_changed();
277 curves.calculate_bezier_auto_handles();
280 }
281 }
282
284 }
285}
286
288{
290
291 /* If the transform operation was cancelled and new keyframes got created, remove them. */
292 if ((t->state != TRANS_CANCEL) || ((t->flag & T_DUPLICATED_KEYFRAMES) == 0)) {
293 return;
294 }
295
296 for (TransDataContainer &tc : trans_data_contrainers) {
297 GreasePencil &grease_pencil = *id_cast<GreasePencil *>(tc.obedit->data);
298 CurvesTransformData &transform_data = *static_cast<CurvesTransformData *>(tc.custom.type.data);
299
300 for (auto item : transform_data.duplicate_layer_keyframes.items()) {
301 bke::greasepencil::Layer &layer = *item.key;
302 grease_pencil.remove_frames(layer, {item.value});
303 }
304
305 transform_data.duplicate_layer_keyframes.clear();
306 }
307}
308
310
312 /*flags*/ (T_EDIT | T_POINTS),
313 /*create_trans_data*/ createTransGreasePencilVerts,
314 /*recalc_data*/ recalcData_grease_pencil,
315 /*special_aftertrans_update*/ special_aftertrans_update__grease_pencil,
316};
317
318} // namespace blender::ed::transform::greasepencil
Functions to insert, delete or modify keyframes.
Low-level operations for curves.
#define C
Definition RandGen.cpp:29
int64_t size() const
Definition BLI_array.hh:267
void reinitialize(const int64_t new_size)
Definition BLI_array.hh:430
static IndexMask from_ranges(OffsetIndices< T > offsets, const IndexMask &mask, LinearAllocator<> &memory)
static IndexMask from_intersection(const IndexMask &mask_a, const IndexMask &mask_b, LinearAllocator<> &memory)
constexpr IndexRange index_range() const
Definition BLI_span.hh:676
constexpr int64_t size() const
Definition BLI_span.hh:256
constexpr IndexRange index_range() const
Definition BLI_span.hh:405
void append(const T &value)
IndexRange index_range() const
MutableSpan< float > opacities_for_write()
MutableSpan< float > radii_for_write()
bke::CurvesGeometry & strokes_for_write()
std::optional< int > start_frame_at(int frame_number) const
const IntersectionExpr & intersect(const Span< Term > terms)
const UnionExpr & merge(const Span< Term > terms)
#define layer
bool is_autokey_on(const Scene *scene)
GeometryDeformation get_evaluated_grease_pencil_drawing_deformation(const Object *ob_eval, const Object &ob_orig, const bke::greasepencil::Drawing &drawing_orig)
IndexMask indices_for_type(const VArray< int8_t > &types, const std::array< int, CURVE_TYPES_NUM > &type_counts, const CurveType type, const IndexMask &selection, LinearAllocator<> &memory)
IndexMask retrieve_selected_points(const bke::CurvesGeometry &curves, LinearAllocator<> &memory)
Vector< MutableSpan< float3 > > get_curves_positions_for_write(bke::CurvesGeometry &curves)
Span< StringRef > get_curves_selection_attribute_names(const bke::CurvesGeometry &curves)
IndexMask retrieve_editable_points(Object &object, const bke::greasepencil::Drawing &drawing, int layer_index, IndexMaskMemory &memory)
Vector< MutableDrawingInfo > retrieve_editable_drawings_with_falloff(const Scene &scene, GreasePencil &grease_pencil)
IndexMask retrieve_editable_strokes(Object &object, const bke::greasepencil::Drawing &drawing, int layer_index, IndexMaskMemory &memory)
void create_aligned_handles_masks(const bke::CurvesGeometry &curves, Span< IndexMask > points_to_transform_per_attr, int curve_index, TransCustomData &custom_data)
CurvesTransformData * create_curves_transform_custom_data(TransCustomData &custom_data)
void calculate_single_aligned_handles(const TransCustomData &custom_data, bke::CurvesGeometry &curves, int curve_index)
bool update_handle_types_for_transform(eTfmMode mode, const std::array< IndexMask, 3 > &selection_per_attribute, const IndexMask &bezier_points, bke::CurvesGeometry &curves)
void curve_populate_trans_data_structs(const TransInfo &t, TransDataContainer &tc, bke::CurvesGeometry &curves, const float4x4 &transform, const bke::crazyspace::GeometryDeformation &deformation, std::optional< MutableSpan< float > > value_attribute, Span< IndexMask > points_to_transform_per_attr, const IndexMask &affected_curves, bool use_connected_only, const IndexMask &bezier_curves, void *extra=nullptr)
void copy_positions_from_curves_transform_custom_data(const TransCustomData &custom_data, int layer, MutableSpan< float3 > positions_dst)
static void createTransGreasePencilVerts(bContext *C, TransInfo *t)
static void special_aftertrans_update__grease_pencil(bContext *, TransInfo *t)
void transform_snap_project_individual_apply(TransInfo *t)
@ GP_SCULPT_SETT_FLAG_SCALE_THICKNESS
__int64 int64_t
Definition BKE_text.h:90
Depsgraph * CTX_data_depsgraph_pointer(const bContext *C)
@ V3D_AROUND_LOCAL_ORIGINS
MatBase< float, 4, 4 > float4x4
void DEG_id_tag_update(ID *id, unsigned int flags)
T * DEG_get_evaluated(const Depsgraph *depsgraph, T *id)
@ ID_RECALC_GEOMETRY
Definition DNA_ID.h:1074
Scene * CTX_data_scene(const bContext *C)
Object * CTX_data_active_object(const bContext *C)
void * MEM_new_array_zeroed(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
eGP_Sculpt_SettingsFlag flag
struct RenderData r
struct ToolSettings * toolsettings
struct GP_Sculpt_Settings gp_sculpt
Map< bke::greasepencil::Layer *, int > duplicate_layer_keyframes
Vector< ed::greasepencil::MutableDrawingInfo > drawings
void(*) free_cb(TransInfo *, TransDataContainer *tc, TransCustomData *custom_data)
Definition transform.hh:655
TransDataContainer * data_container
Definition transform.hh:828
#define T_PROP_EDIT_ALL
Definition transform.hh:34
conversion and adaptation of different datablocks to a common struct.