diff options
Diffstat (limited to 'source/blender/editors/gpencil')
-rw-r--r-- | source/blender/editors/gpencil/gpencil_edit.c | 251 | ||||
-rw-r--r-- | source/blender/editors/gpencil/gpencil_interpolate.c | 6 | ||||
-rw-r--r-- | source/blender/editors/gpencil/gpencil_mesh.c | 140 | ||||
-rw-r--r-- | source/blender/editors/gpencil/gpencil_trace.h | 4 | ||||
-rw-r--r-- | source/blender/editors/gpencil/gpencil_trace_ops.c | 311 |
5 files changed, 526 insertions, 186 deletions
diff --git a/source/blender/editors/gpencil/gpencil_edit.c b/source/blender/editors/gpencil/gpencil_edit.c index d54bdf552eb..6258e6e8481 100644 --- a/source/blender/editors/gpencil/gpencil_edit.c +++ b/source/blender/editors/gpencil/gpencil_edit.c @@ -2737,12 +2737,12 @@ static bool gpencil_snap_poll(bContext *C) static int gpencil_snap_to_grid(bContext *C, wmOperator *UNUSED(op)) { bGPdata *gpd = ED_gpencil_data_get_active(C); - RegionView3D *rv3d = CTX_wm_region_data(C); + ARegion *region = CTX_wm_region(C); View3D *v3d = CTX_wm_view3d(C); Scene *scene = CTX_data_scene(C); Depsgraph *depsgraph = CTX_data_ensure_evaluated_depsgraph(C); Object *obact = CTX_data_active_object(C); - const float gridf = ED_view3d_grid_view_scale(scene, v3d, rv3d, NULL); + const float gridf = ED_view3d_grid_view_scale(scene, v3d, region, NULL); LISTBASE_FOREACH (bGPDlayer *, gpl, &gpd->layers) { /* only editable and visible layers are considered */ @@ -3152,8 +3152,8 @@ void GPENCIL_OT_stroke_cyclical_set(wmOperatorType *ot) PropertyRNA *prop; static const EnumPropertyItem cyclic_type[] = { - {GP_STROKE_CYCLIC_CLOSE, "CLOSE", 0, "Close all", ""}, - {GP_STROKE_CYCLIC_OPEN, "OPEN", 0, "Open all", ""}, + {GP_STROKE_CYCLIC_CLOSE, "CLOSE", 0, "Close All", ""}, + {GP_STROKE_CYCLIC_OPEN, "OPEN", 0, "Open All", ""}, {GP_STROKE_CYCLIC_TOGGLE, "TOGGLE", 0, "Toggle", ""}, {0, NULL, 0, NULL, NULL}, }; @@ -3398,24 +3398,50 @@ static void gpencil_stroke_join_strokes(bGPDstroke *gps_a, } /* define start and end points of each stroke */ - float area[3], sb[3], ea[3], eb[3]; + float start_a[3], start_b[3], end_a[3], end_b[3]; pt = &gps_a->points[0]; - copy_v3_v3(area, &pt->x); + copy_v3_v3(start_a, &pt->x); pt = &gps_a->points[gps_a->totpoints - 1]; - copy_v3_v3(ea, &pt->x); + copy_v3_v3(end_a, &pt->x); pt = &gps_b->points[0]; - copy_v3_v3(sb, &pt->x); + copy_v3_v3(start_b, &pt->x); pt = &gps_b->points[gps_b->totpoints - 1]; - copy_v3_v3(eb, &pt->x); + copy_v3_v3(end_b, &pt->x); + + /* Check if need flip strokes. */ + float dist = len_squared_v3v3(end_a, start_b); + bool flip_a = false; + bool flip_b = false; + float lowest = dist; + + dist = len_squared_v3v3(end_a, end_b); + if (dist < lowest) { + lowest = dist; + flip_a = false; + flip_b = true; + } + + dist = len_squared_v3v3(start_a, start_b); + if (dist < lowest) { + lowest = dist; + flip_a = true; + flip_b = false; + } + + dist = len_squared_v3v3(start_a, end_b); + if (dist < lowest) { + lowest = dist; + flip_a = true; + flip_b = true; + } - /* review if need flip stroke B */ - float ea_sb = len_squared_v3v3(ea, sb); - float ea_eb = len_squared_v3v3(ea, eb); - /* flip if distance to end point is shorter */ - if (ea_eb < ea_sb) { + if (flip_a) { + gpencil_flip_stroke(gps_a); + } + if (flip_b) { gpencil_flip_stroke(gps_b); } @@ -3439,16 +3465,71 @@ static void gpencil_stroke_join_strokes(bGPDstroke *gps_a, } } +typedef struct tJoinStrokes { + bGPDframe *gpf; + bGPDstroke *gps; + bool used; +} tJoinStrokes; + +static int gpencil_get_nearest_stroke_index(tJoinStrokes *strokes_list, + const bGPDstroke *gps, + const int totstrokes) +{ + int index = -1; + float min_dist = FLT_MAX; + float dist, start_a[3], end_a[3], start_b[3], end_b[3]; + + bGPDspoint *pt = &gps->points[0]; + copy_v3_v3(start_a, &pt->x); + + pt = &gps->points[gps->totpoints - 1]; + copy_v3_v3(end_a, &pt->x); + + for (int i = 0; i < totstrokes; i++) { + tJoinStrokes *elem = &strokes_list[i]; + if (elem->used) { + continue; + } + pt = &elem->gps->points[0]; + copy_v3_v3(start_b, &pt->x); + + pt = &elem->gps->points[elem->gps->totpoints - 1]; + copy_v3_v3(end_b, &pt->x); + + dist = len_squared_v3v3(start_a, start_b); + if (dist < min_dist) { + min_dist = dist; + index = i; + } + dist = len_squared_v3v3(start_a, end_b); + if (dist < min_dist) { + min_dist = dist; + index = i; + } + dist = len_squared_v3v3(end_a, start_b); + if (dist < min_dist) { + min_dist = dist; + index = i; + } + dist = len_squared_v3v3(end_a, end_b); + if (dist < min_dist) { + min_dist = dist; + index = i; + } + } + + return index; +} + static int gpencil_stroke_join_exec(bContext *C, wmOperator *op) { bGPdata *gpd = ED_gpencil_data_get_active(C); bGPDlayer *activegpl = BKE_gpencil_layer_active_get(gpd); Object *ob = CTX_data_active_object(C); - - bGPDframe *gpf_a = NULL; - bGPDstroke *stroke_a = NULL; - bGPDstroke *stroke_b = NULL; - bGPDstroke *new_stroke = NULL; + /* Limit the number of strokes to join. It makes no sense to allow an very high number of strokes + * for CPU time and because to have a stroke with thousands of points is unpractical, so limit + * this number avoid to joining a full frame scene in one single stroke. */ + const int max_join_strokes = 128; const int type = RNA_enum_get(op->ptr, "type"); const bool leave_gaps = RNA_boolean_get(op->ptr, "leave_gaps"); @@ -3464,87 +3545,89 @@ static int gpencil_stroke_join_exec(bContext *C, wmOperator *op) BLI_assert(ELEM(type, GP_STROKE_JOIN, GP_STROKE_JOINCOPY)); - /* read all selected strokes */ - bool first = false; + int tot_strokes = 0; + /** Alloc memory */ + tJoinStrokes *strokes_list = MEM_malloc_arrayN(sizeof(tJoinStrokes), max_join_strokes, __func__); + tJoinStrokes *elem = NULL; + /* Read all selected strokes to create a list. */ CTX_DATA_BEGIN (C, bGPDlayer *, gpl, editable_gpencil_layers) { bGPDframe *gpf = gpl->actframe; if (gpf == NULL) { continue; } - LISTBASE_FOREACH_MUTABLE (bGPDstroke *, gps, &gpf->strokes) { + /* Add all stroke selected of the frame. */ + LISTBASE_FOREACH (bGPDstroke *, gps, &gpf->strokes) { if (gps->flag & GP_STROKE_SELECT) { /* skip strokes that are invalid for current view */ if (ED_gpencil_stroke_can_use(C, gps) == false) { continue; } - /* check if the color is editable */ + /* check if the color is editable. */ if (ED_gpencil_stroke_color_use(ob, gpl, gps) == false) { continue; } - - /* to join strokes, cyclic must be disabled */ - gps->flag &= ~GP_STROKE_CYCLIC; - - /* saves first frame and stroke */ - if (!first) { - first = true; - gpf_a = gpf; - stroke_a = gps; + elem = &strokes_list[tot_strokes]; + elem->gpf = gpf; + elem->gps = gps; + elem->used = false; + + tot_strokes++; + /* Limit the number of strokes. */ + if (tot_strokes == max_join_strokes) { + BKE_reportf(op->reports, + RPT_WARNING, + "Too many strokes selected. Only joined first %d strokes.", + max_join_strokes); + break; } - else { - stroke_b = gps; - - /* create a new stroke if was not created before (only created if something to join) */ - if (new_stroke == NULL) { - new_stroke = BKE_gpencil_stroke_duplicate(stroke_a, true); + } + } + } + CTX_DATA_END; - /* if new, set current color */ - if (type == GP_STROKE_JOINCOPY) { - new_stroke->mat_nr = stroke_a->mat_nr; - } - } + /* Nothing to join. */ + if (tot_strokes < 2) { + MEM_SAFE_FREE(strokes_list); + return OPERATOR_CANCELLED; + } - /* join new_stroke and stroke B. New stroke will contain all the previous data */ - gpencil_stroke_join_strokes(new_stroke, stroke_b, leave_gaps); + /* Take first stroke. */ + elem = &strokes_list[0]; + elem->used = true; - /* if join only, delete old strokes */ - if (type == GP_STROKE_JOIN) { - if (stroke_a) { - /* Calc geometry data. */ - BKE_gpencil_stroke_geometry_update(new_stroke); + /* Create a new stroke. */ + bGPDstroke *gps_new = BKE_gpencil_stroke_duplicate(elem->gps, true); + gps_new->flag &= ~GP_STROKE_CYCLIC; + BLI_insertlinkbefore(&elem->gpf->strokes, elem->gps, gps_new); - BLI_insertlinkbefore(&gpf_a->strokes, stroke_a, new_stroke); - BLI_remlink(&gpf->strokes, stroke_a); - BKE_gpencil_free_stroke(stroke_a); - stroke_a = NULL; - } - if (stroke_b) { - BLI_remlink(&gpf->strokes, stroke_b); - BKE_gpencil_free_stroke(stroke_b); - stroke_b = NULL; - } - } - } - } + /* Join all strokes until the list is completed. */ + while (true) { + int i = gpencil_get_nearest_stroke_index(strokes_list, gps_new, tot_strokes); + if (i < 0) { + break; } + elem = &strokes_list[i]; + /* Join new_stroke and stroke B. */ + gpencil_stroke_join_strokes(gps_new, elem->gps, leave_gaps); + elem->used = true; } - CTX_DATA_END; - /* add new stroke if was not added before */ - if (type == GP_STROKE_JOINCOPY) { - if (new_stroke) { - /* Add a new frame if needed */ - if (activegpl->actframe == NULL) { - activegpl->actframe = BKE_gpencil_frame_addnew(activegpl, gpf_a->framenum); - } - /* Calc geometry data. */ - BKE_gpencil_stroke_geometry_update(new_stroke); + /* Calc geometry data for new stroke. */ + BKE_gpencil_stroke_geometry_update(gps_new); - BLI_addtail(&activegpl->actframe->strokes, new_stroke); + /* If join only, delete old strokes. */ + if (type == GP_STROKE_JOIN) { + for (int i = 0; i < tot_strokes; i++) { + elem = &strokes_list[i]; + BLI_remlink(&elem->gpf->strokes, elem->gps); + BKE_gpencil_free_stroke(elem->gps); } } + /* Free memory. */ + MEM_SAFE_FREE(strokes_list); + /* notifiers */ DEG_id_tag_update(&gpd->id, ID_RECALC_TRANSFORM | ID_RECALC_GEOMETRY); WM_event_add_notifier(C, NC_GPENCIL | ND_DATA | NA_EDITED, NULL); @@ -4678,7 +4761,9 @@ typedef bool (*GPencilTestFn)(bGPDstroke *gps, const float diff_mat[4][4], void *user_data); -static void gpencil_cutter_dissolve(bGPDlayer *hit_layer, bGPDstroke *hit_stroke) +static void gpencil_cutter_dissolve(bGPDlayer *hit_layer, + bGPDstroke *hit_stroke, + const bool flat_caps) { bGPDspoint *pt = NULL; bGPDspoint *pt1 = NULL; @@ -4722,6 +4807,17 @@ static void gpencil_cutter_dissolve(bGPDlayer *hit_layer, bGPDstroke *hit_stroke pt->flag &= ~GP_SPOINT_TAG; } } + /* If flat caps mode check extremes. */ + if (flat_caps) { + if (hit_stroke->points[0].flag & GP_SPOINT_TAG) { + hit_stroke->caps[0] = GP_STROKE_CAP_FLAT; + } + + if (hit_stroke->points[hit_stroke->totpoints - 1].flag & GP_SPOINT_TAG) { + hit_stroke->caps[1] = GP_STROKE_CAP_FLAT; + } + } + gpencil_stroke_delete_tagged_points( hit_layer->actframe, hit_stroke, gpsn, GP_SPOINT_TAG, false, 1); } @@ -4736,6 +4832,7 @@ static int gpencil_cutter_lasso_select(bContext *C, ScrArea *area = CTX_wm_area(C); ToolSettings *ts = CTX_data_tool_settings(C); const float scale = ts->gp_sculpt.isect_threshold; + const bool flat_caps = RNA_boolean_get(op->ptr, "flat_caps"); bGPDspoint *pt; GP_SpaceConversion gsc = {NULL}; @@ -4810,7 +4907,7 @@ static int gpencil_cutter_lasso_select(bContext *C, } LISTBASE_FOREACH_MUTABLE (bGPDstroke *, gps, &gpf->strokes) { if (gps->flag & GP_STROKE_SELECT) { - gpencil_cutter_dissolve(gpl, gps); + gpencil_cutter_dissolve(gpl, gps, flat_caps); } } } @@ -4884,6 +4981,8 @@ void GPENCIL_OT_stroke_cutter(wmOperatorType *ot) /* properties */ WM_operator_properties_gesture_lasso(ot); + + RNA_def_boolean(ot->srna, "flat_caps", 0, "Flat Caps", ""); } bool ED_object_gpencil_exit(struct Main *bmain, Object *ob) diff --git a/source/blender/editors/gpencil/gpencil_interpolate.c b/source/blender/editors/gpencil/gpencil_interpolate.c index 2e13566402b..3a3a9bde38b 100644 --- a/source/blender/editors/gpencil/gpencil_interpolate.c +++ b/source/blender/editors/gpencil/gpencil_interpolate.c @@ -220,7 +220,7 @@ static bool gpencil_interpolate_check_todo(bContext *C, bGPdata *gpd) int fFrame; /* only selected */ - if ((flag & GP_TOOLFLAG_INTERPOLATE_ONLY_SELECTED) && + if ((GPENCIL_EDIT_MODE(gpd)) && (flag & GP_TOOLFLAG_INTERPOLATE_ONLY_SELECTED) && ((gps_from->flag & GP_STROKE_SELECT) == 0)) { continue; } @@ -305,7 +305,7 @@ static void gpencil_interpolate_set_points(bContext *C, tGPDinterpolate *tgpi) bool valid = true; /* only selected */ - if ((tgpi->flag & GP_TOOLFLAG_INTERPOLATE_ONLY_SELECTED) && + if ((GPENCIL_EDIT_MODE(gpd)) && (tgpi->flag & GP_TOOLFLAG_INTERPOLATE_ONLY_SELECTED) && ((gps_from->flag & GP_STROKE_SELECT) == 0)) { valid = false; } @@ -1023,7 +1023,7 @@ static int gpencil_interpolate_seq_exec(bContext *C, wmOperator *op) for (gps_from = prevFrame->strokes.first; gps_from; gps_from = gps_from->next) { /* only selected */ - if ((flag & GP_TOOLFLAG_INTERPOLATE_ONLY_SELECTED) && + if ((GPENCIL_EDIT_MODE(gpd)) && (flag & GP_TOOLFLAG_INTERPOLATE_ONLY_SELECTED) && ((gps_from->flag & GP_STROKE_SELECT) == 0)) { continue; } diff --git a/source/blender/editors/gpencil/gpencil_mesh.c b/source/blender/editors/gpencil/gpencil_mesh.c index e4862617d12..c136ef2070e 100644 --- a/source/blender/editors/gpencil/gpencil_mesh.c +++ b/source/blender/editors/gpencil/gpencil_mesh.c @@ -25,12 +25,15 @@ #include "MEM_guardedalloc.h" #include "BLI_blenlib.h" +#include "BLI_ghash.h" #include "BLI_math.h" +#include "DNA_anim_types.h" #include "DNA_gpencil_types.h" #include "DNA_scene_types.h" #include "DNA_screen_types.h" +#include "BKE_anim_data.h" #include "BKE_context.h" #include "BKE_duplilist.h" #include "BKE_global.h" @@ -86,6 +89,35 @@ typedef struct GpBakeOb { Object *ob; } GpBakeOb; +/* Get list of keyframes used by selected objects. */ +static void animdata_keyframe_list_get(ListBase *ob_list, + const bool only_selected, + GHash *r_keyframes) +{ + /* Loop all objects to get the list of keyframes used. */ + LISTBASE_FOREACH (GpBakeOb *, elem, ob_list) { + Object *ob = elem->ob; + AnimData *adt = BKE_animdata_from_id(&ob->id); + if ((adt == NULL) || (adt->action == NULL)) { + continue; + } + LISTBASE_FOREACH (FCurve *, fcurve, &adt->action->curves) { + int i; + BezTriple *bezt; + for (i = 0, bezt = fcurve->bezt; i < fcurve->totvert; i++, bezt++) { + /* Keyframe number is x value of point. */ + if ((bezt->f2 & SELECT) || (!only_selected)) { + /* Insert only one key for each keyframe number. */ + int key = (int)bezt->vec[1][0]; + if (!BLI_ghash_haskey(r_keyframes, POINTER_FROM_INT(key))) { + BLI_ghash_insert(r_keyframes, POINTER_FROM_INT(key), POINTER_FROM_INT(key)); + } + } + } + } + } +} + static void gpencil_bake_duplilist(Depsgraph *depsgraph, Scene *scene, Object *ob, ListBase *list) { GpBakeOb *elem = NULL; @@ -161,13 +193,13 @@ static int gpencil_bake_mesh_animation_exec(bContext *C, wmOperator *op) View3D *v3d = CTX_wm_view3d(C); Object *ob_gpencil = NULL; - ListBase list = {NULL, NULL}; - gpencil_bake_ob_list(C, depsgraph, scene, &list); + ListBase ob_selected_list = {NULL, NULL}; + gpencil_bake_ob_list(C, depsgraph, scene, &ob_selected_list); /* Cannot check this in poll because the active object changes. */ - if (list.first == NULL) { + if (ob_selected_list.first == NULL) { BKE_report(op->reports, RPT_INFO, "No valid object selected"); - gpencil_bake_free_ob_list(&list); + gpencil_bake_free_ob_list(&ob_selected_list); return OPERATOR_CANCELLED; } @@ -186,29 +218,20 @@ static int gpencil_bake_mesh_animation_exec(bContext *C, wmOperator *op) const int thickness = RNA_int_get(op->ptr, "thickness"); const bool use_seams = RNA_boolean_get(op->ptr, "seams"); const bool use_faces = RNA_boolean_get(op->ptr, "faces"); + const bool only_selected = RNA_boolean_get(op->ptr, "only_selected"); const float offset = RNA_float_get(op->ptr, "offset"); const int frame_offset = RNA_int_get(op->ptr, "frame_target") - frame_start; - char target[64]; - RNA_string_get(op->ptr, "target", target); const int project_type = RNA_enum_get(op->ptr, "project_type"); + ob_gpencil = (Object *)RNA_pointer_get(op->ptr, "target").data; /* Create a new grease pencil object in origin. */ bool newob = false; - if (STREQ(target, "*NEW")) { + if (ob_gpencil == NULL) { ushort local_view_bits = (v3d && v3d->localvd) ? v3d->local_view_uuid : 0; const float loc[3] = {0.0f, 0.0f, 0.0f}; ob_gpencil = ED_gpencil_add_object(C, loc, local_view_bits); newob = true; } - else { - ob_gpencil = BLI_findstring(&bmain->objects, target, offsetof(ID, name) + 2); - } - - if ((ob_gpencil == NULL) || (ob_gpencil->type != OB_GPENCIL)) { - BKE_report(op->reports, RPT_ERROR, "Target grease pencil object not valid"); - gpencil_bake_free_ob_list(&list); - return OPERATOR_CANCELLED; - } bGPdata *gpd = (bGPdata *)ob_gpencil->data; gpd->draw_mode = (project_type == GP_REPROJECT_KEEP) ? GP_DRAWMODE_3D : GP_DRAWMODE_2D; @@ -237,6 +260,13 @@ static int gpencil_bake_mesh_animation_exec(bContext *C, wmOperator *op) /* Loop all frame range. */ int oldframe = (int)DEG_get_ctime(depsgraph); int key = -1; + + /* Get list of keyframes. */ + GHash *keyframe_list = BLI_ghash_int_new(__func__); + if (only_selected) { + animdata_keyframe_list_get(&ob_selected_list, only_selected, keyframe_list); + } + for (int i = frame_start; i < frame_end + 1; i++) { key++; /* Jump if not step limit but include last frame always. */ @@ -244,12 +274,17 @@ static int gpencil_bake_mesh_animation_exec(bContext *C, wmOperator *op) continue; } + /* Check if frame is in the list of frames to be exported. */ + if ((only_selected) && (!BLI_ghash_haskey(keyframe_list, POINTER_FROM_INT(i)))) { + continue; + } + /* Move scene to new frame. */ CFRA = i; BKE_scene_graph_update_for_newframe(depsgraph); /* Loop all objects in the list. */ - LISTBASE_FOREACH (GpBakeOb *, elem, &list) { + LISTBASE_FOREACH (GpBakeOb *, elem, &ob_selected_list) { Object *ob_eval = (Object *)DEG_get_evaluated_object(depsgraph, elem->ob); /* Generate strokes. */ @@ -270,13 +305,14 @@ static int gpencil_bake_mesh_animation_exec(bContext *C, wmOperator *op) if (project_type != GP_REPROJECT_KEEP) { LISTBASE_FOREACH (bGPDlayer *, gpl, &gpd->layers) { bGPDframe *gpf = gpl->actframe; - if (gpf != NULL) { - LISTBASE_FOREACH (bGPDstroke *, gps, &gpf->strokes) { - if ((gps->flag & GP_STROKE_TAG) == 0) { - ED_gpencil_stroke_reproject( - depsgraph, &gsc, sctx, gpl, gpf, gps, project_type, false); - gps->flag |= GP_STROKE_TAG; - } + if (gpf == NULL) { + continue; + } + LISTBASE_FOREACH (bGPDstroke *, gps, &gpf->strokes) { + if ((gps->flag & GP_STROKE_TAG) == 0) { + ED_gpencil_stroke_reproject( + depsgraph, &gsc, sctx, gpl, gpf, gps, project_type, false); + gps->flag |= GP_STROKE_TAG; } } } @@ -314,10 +350,14 @@ static int gpencil_bake_mesh_animation_exec(bContext *C, wmOperator *op) } /* Free memory. */ - gpencil_bake_free_ob_list(&list); + gpencil_bake_free_ob_list(&ob_selected_list); if (sctx != NULL) { ED_transform_snap_object_context_destroy(sctx); } + /* Free temp hash table. */ + if (keyframe_list != NULL) { + BLI_ghash_free(keyframe_list, NULL, NULL); + } /* notifiers */ if (newob) { @@ -334,6 +374,19 @@ static int gpencil_bake_mesh_animation_exec(bContext *C, wmOperator *op) return OPERATOR_FINISHED; } +static int gpencil_bake_mesh_animation_invoke(bContext *C, + wmOperator *op, + const wmEvent *UNUSED(event)) +{ + /* Show popup dialog to allow editing. */ + /* FIXME: hard-coded dimensions here are just arbitrary. */ + return WM_operator_props_dialog_popup(C, op, 250); +} + +static bool rna_GPencil_object_poll(PointerRNA *UNUSED(ptr), PointerRNA value) +{ + return ((Object *)value.owner_id)->type == OB_GPENCIL; +} void GPENCIL_OT_bake_mesh_animation(wmOperatorType *ot) { static const EnumPropertyItem reproject_type[] = { @@ -363,6 +416,7 @@ void GPENCIL_OT_bake_mesh_animation(wmOperatorType *ot) ot->description = "Bake Mesh Animation to Grease Pencil strokes"; /* callbacks */ + ot->invoke = gpencil_bake_mesh_animation_invoke; ot->exec = gpencil_bake_mesh_animation_exec; ot->poll = gpencil_bake_mesh_animation_poll; @@ -370,7 +424,15 @@ void GPENCIL_OT_bake_mesh_animation(wmOperatorType *ot) ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; /* properties */ - ot->prop = RNA_def_int( + ot->prop = RNA_def_pointer_runtime(ot->srna, + "target", + &RNA_Object, + "Target Object", + "Target grease pencil object. Leave empty for new object"); + RNA_def_property_poll_runtime(ot->prop, rna_GPencil_object_poll); + RNA_def_property_flag(ot->prop, PROP_SKIP_SAVE); + + prop = RNA_def_int( ot->srna, "frame_start", 1, 1, 100000, "Start Frame", "The start frame", 1, 100000); prop = RNA_def_int( @@ -379,6 +441,8 @@ void GPENCIL_OT_bake_mesh_animation(wmOperatorType *ot) prop = RNA_def_int(ot->srna, "step", 1, 1, 100, "Step", "Step between generated frames", 1, 100); + RNA_def_int(ot->srna, "thickness", 1, 1, 100, "Thickness", "", 1, 100); + prop = RNA_def_float_rotation(ot->srna, "angle", 0, @@ -391,18 +455,22 @@ void GPENCIL_OT_bake_mesh_animation(wmOperatorType *ot) DEG2RADF(180.0f)); RNA_def_property_float_default(prop, DEG2RADF(70.0f)); - RNA_def_int(ot->srna, "thickness", 1, 1, 100, "Thickness", "", 1, 100); + RNA_def_float_distance(ot->srna, + "offset", + 0.001f, + 0.0, + 100.0, + "Stroke Offset", + "Offset strokes from fill", + 0.0, + 100.00); + RNA_def_boolean(ot->srna, "seams", 0, "Only Seam Edges", "Convert only seam edges"); RNA_def_boolean(ot->srna, "faces", 1, "Export Faces", "Export faces as filled strokes"); - RNA_def_float_distance( - ot->srna, "offset", 0.001f, 0.0, 100.0, "Offset", "Offset strokes from fill", 0.0, 100.00); - RNA_def_int(ot->srna, "frame_target", 1, 1, 100000, "Frame Target", "", 1, 100000); - RNA_def_string(ot->srna, - "target", - "*NEW", - 64, - "Target Object", - "Target grease pencil object name. Leave empty for new object"); + RNA_def_boolean( + ot->srna, "only_selected", 0, "Only Selected Keyframes", "Convert only selected keyframes"); + RNA_def_int( + ot->srna, "frame_target", 1, 1, 100000, "Target Frame", "Destination frame", 1, 100000); RNA_def_enum(ot->srna, "project_type", reproject_type, GP_REPROJECT_VIEW, "Projection Type", ""); } diff --git a/source/blender/editors/gpencil/gpencil_trace.h b/source/blender/editors/gpencil/gpencil_trace.h index 3adde7651cd..85eb4e0609f 100644 --- a/source/blender/editors/gpencil/gpencil_trace.h +++ b/source/blender/editors/gpencil/gpencil_trace.h @@ -56,6 +56,10 @@ struct bGPDframe; #define BM_INV(bm, x, y) (bm_safe(bm, x, y) ? BM_UINV(bm, x, y) : 0) #define BM_PUT(bm, x, y, b) (bm_safe(bm, x, y) ? BM_UPUT(bm, x, y, b) : 0) +/* Trace modes */ +#define GPENCIL_TRACE_MODE_SINGLE 0 +#define GPENCIL_TRACE_MODE_SEQUENCE 1 + void ED_gpencil_trace_bitmap_print(FILE *f, const potrace_bitmap_t *bm); potrace_bitmap_t *ED_gpencil_trace_bitmap_new(int32_t w, int32_t h); diff --git a/source/blender/editors/gpencil/gpencil_trace_ops.c b/source/blender/editors/gpencil/gpencil_trace_ops.c index 4391abee5a1..2d04c31e60d 100644 --- a/source/blender/editors/gpencil/gpencil_trace_ops.c +++ b/source/blender/editors/gpencil/gpencil_trace_ops.c @@ -35,8 +35,10 @@ #include "BKE_context.h" #include "BKE_duplilist.h" +#include "BKE_global.h" #include "BKE_gpencil.h" #include "BKE_image.h" +#include "BKE_lib_id.h" #include "BKE_main.h" #include "BKE_material.h" #include "BKE_object.h" @@ -45,11 +47,15 @@ #include "DEG_depsgraph.h" #include "DEG_depsgraph_query.h" +#include "UI_interface.h" +#include "UI_resources.h" + #include "WM_api.h" #include "WM_types.h" #include "RNA_access.h" #include "RNA_define.h" +#include "RNA_enum_types.h" #include "IMB_imbuf.h" #include "IMB_imbuf_types.h" @@ -61,34 +67,50 @@ #include "gpencil_trace.h" #include "potracelib.h" +typedef struct TraceJob { + /* from wmJob */ + struct Object *owner; + short *stop, *do_update; + float *progress; + + bContext *C; + wmWindowManager *wm; + Main *bmain; + Scene *scene; + View3D *v3d; + Base *base_active; + Object *ob_active; + Image *image; + Object *ob_gpencil; + bGPdata *gpd; + bGPDlayer *gpl; + + bool was_ob_created; + + int32_t frame_target; + float threshold; + float scale; + float sample; + int32_t resolution; + int32_t thickness; + int32_t turnpolicy; + int32_t mode; + + bool success; + bool was_canceled; +} TraceJob; + /** * Trace a image. - * \param C: Context - * \param op: Operator - * \param ob: Grease pencil object, can be NULL - * \param ima: Image - * \param gpf: Destination frame + * \param ibuf: Image buffer. + * \param gpf: Destination frame. */ -static bool gpencil_trace_image( - bContext *C, wmOperator *op, Object *ob, Image *ima, bGPDframe *gpf) +static bool gpencil_trace_image(TraceJob *trace_job, ImBuf *ibuf, bGPDframe *gpf) { - Main *bmain = CTX_data_main(C); - potrace_bitmap_t *bm = NULL; potrace_param_t *param = NULL; potrace_state_t *st = NULL; - const float threshold = RNA_float_get(op->ptr, "threshold"); - const float scale = RNA_float_get(op->ptr, "scale"); - const float sample = RNA_float_get(op->ptr, "sample"); - const int32_t resolution = RNA_int_get(op->ptr, "resolution"); - const int32_t thickness = RNA_int_get(op->ptr, "thickness"); - const int32_t turnpolicy = RNA_enum_get(op->ptr, "turnpolicy"); - - ImBuf *ibuf; - void *lock; - ibuf = BKE_image_acquire_ibuf(ima, NULL, &lock); - /* Create an empty BW bitmap. */ bm = ED_gpencil_trace_bitmap_new(ibuf->x, ibuf->y); if (!bm) { @@ -101,10 +123,10 @@ static bool gpencil_trace_image( return false; } param->turdsize = 0; - param->turnpolicy = turnpolicy; + param->turnpolicy = trace_job->turnpolicy; /* Load BW bitmap with image. */ - ED_gpencil_trace_image_to_bitmap(ibuf, bm, threshold); + ED_gpencil_trace_image_to_bitmap(ibuf, bm, trace_job->threshold); /* Trace the bitmap. */ st = potrace_trace(param, bm); @@ -128,23 +150,26 @@ static bool gpencil_trace_image( * Really, there isn't documented in Potrace about how the scale is calculated, * but after doing a lot of tests, it looks is using a VGA resolution (640) as a base. * Maybe there are others ways to get the right scale conversion, but this solution works. */ - float scale_potrace = scale * (640.0f / (float)ibuf->x) * ((float)ibuf->x / (float)ibuf->y); + float scale_potrace = trace_job->scale * (640.0f / (float)ibuf->x) * + ((float)ibuf->x / (float)ibuf->y); if (ibuf->x > ibuf->y) { scale_potrace *= (float)ibuf->y / (float)ibuf->x; } - ED_gpencil_trace_data_to_strokes( - bmain, st, ob, gpf, offset, scale_potrace, sample, resolution, thickness); + ED_gpencil_trace_data_to_strokes(trace_job->bmain, + st, + trace_job->ob_gpencil, + gpf, + offset, + scale_potrace, + trace_job->sample, + trace_job->resolution, + trace_job->thickness); /* Free memory. */ potrace_state_free(st); potrace_param_free(param); - /* Release ibuf. */ - if (ibuf) { - BKE_image_release_ibuf(ima, ibuf, lock); - } - return true; } @@ -157,68 +182,192 @@ static bool gpencil_trace_image_poll(bContext *C) return false; } + Image *image = (Image *)ob->data; + if (!ELEM(image->source, IMA_SRC_FILE, IMA_SRC_SEQUENCE, IMA_SRC_MOVIE)) { + CTX_wm_operator_poll_msg_set(C, "No valid image format selected"); + return false; + } + return true; } -static int gpencil_trace_image_exec(bContext *C, wmOperator *op) +static void trace_initialize_job_data(TraceJob *trace_job) { - Main *bmain = CTX_data_main(C); - Scene *scene = CTX_data_scene(C); - View3D *v3d = CTX_wm_view3d(C); - Base *base_active = CTX_data_active_base(C); - Object *ob_active = base_active->object; - Image *image = (Image *)ob_active->data; - bool ob_created = false; - - const int32_t frame_target = CFRA; - Object *ob_gpencil = (Object *)RNA_pointer_get(op->ptr, "target").data; - /* Create a new grease pencil object. */ - if (ob_gpencil == NULL) { - ushort local_view_bits = (v3d && v3d->localvd) ? v3d->local_view_uuid : 0; - ob_gpencil = ED_gpencil_add_object(C, ob_active->loc, local_view_bits); + if (trace_job->ob_gpencil == NULL) { + ushort local_view_bits = (trace_job->v3d && trace_job->v3d->localvd) ? + trace_job->v3d->local_view_uuid : + 0; + trace_job->ob_gpencil = ED_gpencil_add_object( + trace_job->C, trace_job->ob_active->loc, local_view_bits); /* Apply image rotation. */ - copy_v3_v3(ob_gpencil->rot, ob_active->rot); + copy_v3_v3(trace_job->ob_gpencil->rot, trace_job->ob_active->rot); /* Grease pencil is rotated 90 degrees in X axis by default. */ - ob_gpencil->rot[0] -= DEG2RADF(90.0f); - ob_created = true; + trace_job->ob_gpencil->rot[0] -= DEG2RADF(90.0f); + trace_job->was_ob_created = true; /* Apply image Scale. */ - copy_v3_v3(ob_gpencil->scale, ob_active->scale); + copy_v3_v3(trace_job->ob_gpencil->scale, trace_job->ob_active->scale); + /* The default display size of the image is 5.0 and this is used as scale = 1.0. */ + mul_v3_fl(trace_job->ob_gpencil->scale, trace_job->ob_active->empty_drawsize / 5.0f); } - if ((ob_gpencil == NULL) || (ob_gpencil->type != OB_GPENCIL)) { - BKE_report(op->reports, RPT_ERROR, "Target grease pencil object not valid"); - return OPERATOR_CANCELLED; + /* Create Layer. */ + trace_job->gpd = (bGPdata *)trace_job->ob_gpencil->data; + trace_job->gpl = BKE_gpencil_layer_active_get(trace_job->gpd); + if (trace_job->gpl == NULL) { + trace_job->gpl = BKE_gpencil_layer_addnew(trace_job->gpd, DATA_("Trace"), true); } +} - /* Create Layer. */ - bGPdata *gpd = (bGPdata *)ob_gpencil->data; - bGPDlayer *gpl = BKE_gpencil_layer_active_get(gpd); - if (gpl == NULL) { - gpl = BKE_gpencil_layer_addnew(gpd, DATA_("Trace"), true); +static void trace_start_job(void *customdata, short *stop, short *do_update, float *progress) +{ + TraceJob *trace_job = customdata; + + trace_job->stop = stop; + trace_job->do_update = do_update; + trace_job->progress = progress; + trace_job->was_canceled = false; + + G.is_break = false; + + /* Single Image. */ + + if ((trace_job->image->source == IMA_SRC_FILE) || + (trace_job->mode == GPENCIL_TRACE_MODE_SINGLE)) { + void *lock; + ImBuf *ibuf = BKE_image_acquire_ibuf(trace_job->image, NULL, &lock); + if (ibuf) { + /* Create frame. */ + bGPDframe *gpf = BKE_gpencil_layer_frame_get( + trace_job->gpl, trace_job->frame_target, GP_GETFRAME_ADD_NEW); + gpencil_trace_image(trace_job, ibuf, gpf); + BKE_image_release_ibuf(trace_job->image, ibuf, lock); + *(trace_job->progress) = 1.0f; + } + } + /* Image sequence. */ + else if (trace_job->image->type == IMA_TYPE_IMAGE) { + ImageUser *iuser = trace_job->ob_active->iuser; + for (int i = 0; i < iuser->frames; i++) { + if (G.is_break) { + trace_job->was_canceled = true; + break; + } + + *(trace_job->progress) = (float)i / (float)iuser->frames; + *do_update = true; + + iuser->framenr = i + 1; + + void *lock; + ImBuf *ibuf = BKE_image_acquire_ibuf(trace_job->image, iuser, &lock); + if (ibuf) { + /* Create frame. */ + bGPDframe *gpf = BKE_gpencil_layer_frame_get( + trace_job->gpl, trace_job->frame_target + i, GP_GETFRAME_ADD_NEW); + gpencil_trace_image(trace_job, ibuf, gpf); + + BKE_image_release_ibuf(trace_job->image, ibuf, lock); + } + } } - /* Create frame. */ - bGPDframe *gpf = BKE_gpencil_layer_frame_get(gpl, frame_target, GP_GETFRAME_ADD_NEW); - gpencil_trace_image(C, op, ob_gpencil, image, gpf); + trace_job->success = !trace_job->was_canceled; + *do_update = true; + *stop = 0; +} - /* Back to active base. */ - ED_object_base_activate(C, base_active); +static void trace_end_job(void *customdata) +{ + TraceJob *trace_job = customdata; + + /* If canceled, delete all previously created object and data-block. */ + if ((trace_job->was_canceled) && (trace_job->was_ob_created) && (trace_job->ob_gpencil)) { + bGPdata *gpd = trace_job->ob_gpencil->data; + BKE_id_delete(trace_job->bmain, &trace_job->ob_gpencil->id); + BKE_id_delete(trace_job->bmain, &gpd->id); + } - /* notifiers */ - if (ob_created) { - DEG_relations_tag_update(bmain); + if (trace_job->success) { + DEG_relations_tag_update(trace_job->bmain); + + DEG_id_tag_update(&trace_job->scene->id, ID_RECALC_SELECT); + DEG_id_tag_update(&trace_job->gpd->id, ID_RECALC_GEOMETRY | ID_RECALC_COPY_ON_WRITE); + + WM_main_add_notifier(NC_OBJECT | NA_ADDED, NULL); + WM_main_add_notifier(NC_SCENE | ND_OB_ACTIVE, trace_job->scene); } +} - DEG_id_tag_update(&scene->id, ID_RECALC_SELECT); - DEG_id_tag_update(&gpd->id, ID_RECALC_GEOMETRY | ID_RECALC_COPY_ON_WRITE); +static void trace_free_job(void *customdata) +{ + TraceJob *tj = customdata; + MEM_freeN(tj); +} - WM_event_add_notifier(C, NC_OBJECT | NA_ADDED, NULL); - WM_event_add_notifier(C, NC_SCENE | ND_OB_ACTIVE, scene); +static int gpencil_trace_image_exec(bContext *C, wmOperator *op) +{ + TraceJob *job = MEM_mallocN(sizeof(TraceJob), "TraceJob"); + job->C = C; + job->owner = CTX_data_active_object(C); + job->wm = CTX_wm_manager(C); + job->bmain = CTX_data_main(C); + Scene *scene = CTX_data_scene(C); + job->scene = scene; + job->v3d = CTX_wm_view3d(C); + job->base_active = CTX_data_active_base(C); + job->ob_active = job->base_active->object; + job->image = (Image *)job->ob_active->data; + job->frame_target = CFRA; + + job->ob_gpencil = (Object *)RNA_pointer_get(op->ptr, "target").data; + job->was_ob_created = false; + + job->threshold = RNA_float_get(op->ptr, "threshold"); + job->scale = RNA_float_get(op->ptr, "scale"); + job->sample = RNA_float_get(op->ptr, "sample"); + job->resolution = RNA_int_get(op->ptr, "resolution"); + job->thickness = RNA_int_get(op->ptr, "thickness"); + job->turnpolicy = RNA_enum_get(op->ptr, "turnpolicy"); + job->mode = RNA_enum_get(op->ptr, "mode"); + + trace_initialize_job_data(job); + + /* Back to active base. */ + ED_object_base_activate(job->C, job->base_active); + + if (job->image->source == IMA_SRC_FILE) { + short stop = 0, do_update = true; + float progress; + trace_start_job(job, &stop, &do_update, &progress); + trace_end_job(job); + trace_free_job(job); + } + else { + wmJob *wm_job = WM_jobs_get(job->wm, + CTX_wm_window(C), + job->scene, + "Trace Image", + WM_JOB_PROGRESS, + WM_JOB_TYPE_TRACE_IMAGE); + + WM_jobs_customdata_set(wm_job, job, trace_free_job); + WM_jobs_timer(wm_job, 0.1, NC_GEOM | ND_DATA, NC_GEOM | ND_DATA); + WM_jobs_callbacks(wm_job, trace_start_job, NULL, NULL, trace_end_job); + + WM_jobs_start(CTX_wm_manager(C), wm_job); + } return OPERATOR_FINISHED; } +static int gpencil_trace_image_invoke(bContext *C, wmOperator *op, const wmEvent *UNUSED(event)) +{ + /* Show popup dialog to allow editing. */ + /* FIXME: hard-coded dimensions here are just arbitrary. */ + return WM_operator_props_dialog_popup(C, op, 250); +} + static bool rna_GPencil_object_poll(PointerRNA *UNUSED(ptr), PointerRNA value) { return ((Object *)value.owner_id)->type == OB_GPENCIL; @@ -257,12 +406,19 @@ void GPENCIL_OT_trace_image(wmOperatorType *ot) {0, NULL, 0, NULL, NULL}, }; + static const EnumPropertyItem trace_modes[] = { + {GPENCIL_TRACE_MODE_SINGLE, "SINGLE", 0, "Single", "Trace the current frame of the image"}, + {GPENCIL_TRACE_MODE_SEQUENCE, "SEQUENCE", 0, "Sequence", "Trace full sequence"}, + {0, NULL, 0, NULL, NULL}, + }; + /* identifiers */ ot->name = "Trace Image to Grease Pencil"; ot->idname = "GPENCIL_OT_trace_image"; ot->description = "Extract Grease Pencil strokes from image"; /* callbacks */ + ot->invoke = gpencil_trace_image_invoke; ot->exec = gpencil_trace_image_exec; ot->poll = gpencil_trace_image_poll; @@ -270,9 +426,16 @@ void GPENCIL_OT_trace_image(wmOperatorType *ot) ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; /* properties */ - prop = RNA_def_pointer_runtime(ot->srna, "target", &RNA_Object, "Target", ""); + prop = RNA_def_pointer_runtime( + ot->srna, + "target", + &RNA_Object, + "Target Object", + "Target grease pencil object name. Leave empty to create a new object"); RNA_def_property_poll_runtime(prop, rna_GPencil_object_poll); + RNA_def_property_flag(prop, PROP_SKIP_SAVE); + RNA_def_int(ot->srna, "thickness", 10, 1, 1000, "Thickness", "", 1, 1000); RNA_def_int( ot->srna, "resolution", 5, 1, 20, "Resolution", "Resolution of the generated curves", 1, 20); @@ -301,7 +464,7 @@ void GPENCIL_OT_trace_image(wmOperatorType *ot) 0.0f, 1.0f, "Color Threshold", - "Determine what is considered white and what black", + "Determine the lightness threshold above which strokes are generated", 0.0f, 1.0f); RNA_def_enum(ot->srna, @@ -310,4 +473,10 @@ void GPENCIL_OT_trace_image(wmOperatorType *ot) POTRACE_TURNPOLICY_MINORITY, "Turn Policy", "Determines how to resolve ambiguities during decomposition of bitmaps into paths"); + RNA_def_enum(ot->srna, + "mode", + trace_modes, + GPENCIL_TRACE_MODE_SINGLE, + "Mode", + "Determines if trace simple image or full sequence"); } |