Refactor: GPv3: Arguments to ensure_active_keyframe function

This does some changes to the ed::greasepencil::ensure_active_keyframe` function.
* The context was only needed to get the scene, pass the scene directly
* Don't use the active layer by default but pass a reference to a
  layer instead. This makes it also possible to create keyframes
  on multiple layers using a loop.
This commit is contained in:
Falk David
2024-09-24 11:58:26 +02:00
parent 6e5e01e630
commit 37458329e2
5 changed files with 18 additions and 19 deletions

View File

@@ -2221,7 +2221,7 @@ static int grease_pencil_paste_strokes_exec(bContext *C, wmOperator *op)
BKE_report(op->reports, RPT_ERROR, "No active Grease Pencil layer");
return OPERATOR_CANCELLED;
}
const bke::greasepencil::Layer &active_layer = *grease_pencil.get_active_layer();
bke::greasepencil::Layer &active_layer = *grease_pencil.get_active_layer();
if (!active_layer.is_editable()) {
BKE_report(op->reports, RPT_ERROR, "Active layer is locked or hidden");
return OPERATOR_CANCELLED;
@@ -2229,7 +2229,7 @@ static int grease_pencil_paste_strokes_exec(bContext *C, wmOperator *op)
/* Ensure active keyframe. */
bool inserted_keyframe = false;
if (!ensure_active_keyframe(C, grease_pencil, false, inserted_keyframe)) {
if (!ensure_active_keyframe(scene, grease_pencil, active_layer, false, inserted_keyframe)) {
BKE_report(op->reports, RPT_ERROR, "No Grease Pencil frame to draw on");
return OPERATOR_CANCELLED;
}

View File

@@ -337,24 +337,21 @@ void create_keyframe_edit_data_selected_frames_list(KeyframeEditData *ked,
}
}
bool ensure_active_keyframe(bContext *C,
bool ensure_active_keyframe(const Scene &scene,
GreasePencil &grease_pencil,
bke::greasepencil::Layer &layer,
const bool duplicate_previous_key,
bool &r_inserted_keyframe)
{
Scene &scene = *CTX_data_scene(C);
const int current_frame = scene.r.cfra;
bke::greasepencil::Layer &active_layer = *grease_pencil.get_active_layer();
if (!active_layer.has_drawing_at(current_frame) && !blender::animrig::is_autokey_on(&scene)) {
if (!layer.has_drawing_at(current_frame) && !blender::animrig::is_autokey_on(&scene)) {
return false;
}
/* If auto-key is on and the drawing at the current frame starts before the current frame a new
* keyframe needs to be inserted. */
const bool is_first = active_layer.is_empty() ||
(active_layer.sorted_keys().first() > current_frame);
const std::optional<int> previous_key_frame_start = active_layer.start_frame_at(current_frame);
const bool is_first = layer.is_empty() || (layer.sorted_keys().first() > current_frame);
const std::optional<int> previous_key_frame_start = layer.start_frame_at(current_frame);
const bool has_previous_key = previous_key_frame_start.has_value();
const bool needs_new_drawing = is_first || !has_previous_key ||
(previous_key_frame_start < current_frame);
@@ -363,18 +360,16 @@ bool ensure_active_keyframe(bContext *C,
GP_TOOL_FLAG_RETAIN_LAST) != 0;
if (has_previous_key && (use_additive_drawing || duplicate_previous_key)) {
/* We duplicate the frame that's currently visible and insert it at the current frame. */
grease_pencil.insert_duplicate_frame(
active_layer, *previous_key_frame_start, current_frame, false);
grease_pencil.insert_duplicate_frame(layer, *previous_key_frame_start, current_frame, false);
}
else {
/* Otherwise we just insert a blank keyframe at the current frame. */
grease_pencil.insert_frame(active_layer, current_frame);
grease_pencil.insert_frame(layer, current_frame);
}
r_inserted_keyframe = true;
}
/* There should now always be a drawing at the current frame. */
BLI_assert(active_layer.has_drawing_at(current_frame));
BLI_assert(layer.has_drawing_at(current_frame));
return true;
}

View File

@@ -1501,6 +1501,7 @@ int grease_pencil_draw_operator_invoke(bContext *C,
wmOperator *op,
const bool use_duplicate_previous_key)
{
const Scene *scene = CTX_data_scene(C);
const Object *object = CTX_data_active_object(C);
if (!object || object->type != OB_GREASE_PENCIL) {
return OPERATOR_CANCELLED;
@@ -1528,7 +1529,7 @@ int grease_pencil_draw_operator_invoke(bContext *C,
/* Ensure a drawing at the current keyframe. */
bool inserted_keyframe = false;
if (!ed::greasepencil::ensure_active_keyframe(
C, grease_pencil, use_duplicate_previous_key, inserted_keyframe))
*scene, grease_pencil, active_layer, use_duplicate_previous_key, inserted_keyframe))
{
BKE_report(op->reports, RPT_ERROR, "No Grease Pencil frame to draw on");
return OPERATOR_CANCELLED;

View File

@@ -258,8 +258,9 @@ bool has_any_frame_selected(const bke::greasepencil::Layer &layer);
* create one when auto-key is on (taking additive drawing setting into account).
* \return false when no keyframe could be found or created.
*/
bool ensure_active_keyframe(bContext *C,
bool ensure_active_keyframe(const Scene &scene,
GreasePencil &grease_pencil,
bke::greasepencil::Layer &layer,
bool duplicate_previous_key,
bool &r_inserted_keyframe);

View File

@@ -330,6 +330,7 @@ static bool grease_pencil_sculpt_paint_poll(bContext *C)
static int grease_pencil_sculpt_paint_invoke(bContext *C, wmOperator *op, const wmEvent *event)
{
const Scene *scene = CTX_data_scene(C);
const Object *object = CTX_data_active_object(C);
if (!object || object->type != OB_GREASE_PENCIL) {
return OPERATOR_CANCELLED;
@@ -359,7 +360,7 @@ static int grease_pencil_sculpt_paint_invoke(bContext *C, wmOperator *op, const
* the previous key. */
const bool use_duplicate_previous_key = true;
if (!ed::greasepencil::ensure_active_keyframe(
C, grease_pencil, use_duplicate_previous_key, inserted_keyframe))
*scene, grease_pencil, active_layer, use_duplicate_previous_key, inserted_keyframe))
{
BKE_report(op->reports, RPT_ERROR, "No Grease Pencil frame to draw on");
return OPERATOR_CANCELLED;
@@ -526,6 +527,7 @@ static int grease_pencil_vertex_brush_stroke_invoke(bContext *C,
wmOperator *op,
const wmEvent *event)
{
const Scene *scene = CTX_data_scene(C);
const Object *object = CTX_data_active_object(C);
if (!object || object->type != OB_GREASE_PENCIL) {
return OPERATOR_CANCELLED;
@@ -555,7 +557,7 @@ static int grease_pencil_vertex_brush_stroke_invoke(bContext *C,
* duplicate the previous key. */
const bool use_duplicate_previous_key = true;
if (!ed::greasepencil::ensure_active_keyframe(
C, grease_pencil, use_duplicate_previous_key, inserted_keyframe))
*scene, grease_pencil, active_layer, use_duplicate_previous_key, inserted_keyframe))
{
BKE_report(op->reports, RPT_ERROR, "No Grease Pencil frame to draw on");
return OPERATOR_CANCELLED;