2 * ***** BEGIN GPL LICENSE BLOCK *****
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * The Original Code is Copyright (C) 2008, Blender Foundation, Joshua Leung
19 * This is a new part of Blender
21 * Contributor(s): Joshua Leung
23 * ***** END GPL LICENSE BLOCK *****
26 /** \file blender/editors/gpencil/gpencil_edit.c
38 #include "MEM_guardedalloc.h"
42 #include "BLI_blenlib.h"
44 #include "BLI_utildefines.h"
46 #include "BLF_translation.h"
48 #include "DNA_anim_types.h"
49 #include "DNA_curve_types.h"
50 #include "DNA_object_types.h"
51 #include "DNA_node_types.h"
52 #include "DNA_scene_types.h"
53 #include "DNA_screen_types.h"
54 #include "DNA_space_types.h"
55 #include "DNA_view3d_types.h"
56 #include "DNA_gpencil_types.h"
58 #include "BKE_animsys.h"
59 #include "BKE_context.h"
60 #include "BKE_curve.h"
61 #include "BKE_depsgraph.h"
62 #include "BKE_fcurve.h"
63 #include "BKE_global.h"
64 #include "BKE_gpencil.h"
65 #include "BKE_library.h"
66 #include "BKE_object.h"
67 #include "BKE_report.h"
68 #include "BKE_scene.h"
69 #include "BKE_tracking.h"
71 #include "UI_interface.h"
76 #include "RNA_access.h"
77 #include "RNA_define.h"
79 #include "UI_view2d.h"
81 #include "ED_gpencil.h"
82 #include "ED_view3d.h"
84 #include "ED_keyframing.h"
86 #include "gpencil_intern.h"
88 /* ************************************************ */
89 /* Context Wrangling... */
91 /* Get pointer to active Grease Pencil datablock, and an RNA-pointer to trace back to whatever owns it */
92 bGPdata **gpencil_data_get_pointers(const bContext *C, PointerRNA *ptr)
94 ID *screen_id = (ID *)CTX_wm_screen(C);
95 Scene *scene = CTX_data_scene(C);
96 ScrArea *sa = CTX_wm_area(C);
98 /* if there's an active area, check if the particular editor may
99 * have defined any special Grease Pencil context for editing...
102 switch (sa->spacetype) {
103 case SPACE_VIEW3D: /* 3D-View */
105 Object *ob = CTX_data_active_object(C);
107 /* TODO: we can include other data-types such as bones later if need be... */
109 /* just in case no active object */
111 /* for now, as long as there's an object, default to using that in 3D-View */
112 if (ptr) RNA_id_pointer_create(&ob->id, ptr);
118 case SPACE_NODE: /* Nodes Editor */
120 SpaceNode *snode = (SpaceNode *)CTX_wm_space_data(C);
122 /* return the GP data for the active node block/node */
123 if (snode && snode->nodetree) {
124 /* for now, as long as there's an active node tree, default to using that in the Nodes Editor */
125 if (ptr) RNA_id_pointer_create(&snode->nodetree->id, ptr);
126 return &snode->nodetree->gpd;
129 /* even when there is no node-tree, don't allow this to flow to scene */
135 case SPACE_SEQ: /* Sequencer */
137 SpaceSeq *sseq = (SpaceSeq *)CTX_wm_space_data(C);
139 /* for now, Grease Pencil data is associated with the space (actually preview region only) */
140 /* XXX our convention for everything else is to link to data though... */
141 if (ptr) RNA_pointer_create(screen_id, &RNA_SpaceSequenceEditor, sseq, ptr);
146 case SPACE_IMAGE: /* Image/UV Editor */
148 SpaceImage *sima = (SpaceImage *)CTX_wm_space_data(C);
150 /* for now, Grease Pencil data is associated with the space... */
151 /* XXX our convention for everything else is to link to data though... */
152 if (ptr) RNA_pointer_create(screen_id, &RNA_SpaceImageEditor, sima, ptr);
157 case SPACE_CLIP: /* Nodes Editor */
159 SpaceClip *sc = (SpaceClip *)CTX_wm_space_data(C);
160 MovieClip *clip = ED_space_clip_get_clip(sc);
163 if (sc->gpencil_src == SC_GPENCIL_SRC_TRACK) {
164 MovieTrackingTrack *track = BKE_tracking_track_get_active(&clip->tracking);
170 RNA_pointer_create(&clip->id, &RNA_MovieTrackingTrack, track, ptr);
176 RNA_id_pointer_create(&clip->id, ptr);
184 default: /* unsupported space */
189 /* just fall back on the scene's GP data */
190 if (ptr) RNA_id_pointer_create((ID *)scene, ptr);
191 return (scene) ? &scene->gpd : NULL;
194 /* Get the active Grease Pencil datablock */
195 bGPdata *gpencil_data_get_active(const bContext *C)
197 bGPdata **gpd_ptr = gpencil_data_get_pointers(C, NULL);
198 return (gpd_ptr) ? *(gpd_ptr) : NULL;
201 /* needed for offscreen rendering */
202 bGPdata *gpencil_data_get_active_v3d(Scene *scene)
204 bGPdata *gpd = scene->basact ? scene->basact->object->gpd : NULL;
205 return gpd ? gpd : scene->gpd;
208 /* ************************************************ */
209 /* Panel Operators */
211 /* poll callback for adding data/layers - special */
212 static int gp_add_poll(bContext *C)
214 /* the base line we have is that we have somewhere to add Grease Pencil data */
215 return gpencil_data_get_pointers(C, NULL) != NULL;
218 /* ******************* Add New Data ************************ */
220 /* add new datablock - wrapper around API */
221 static int gp_data_add_exec(bContext *C, wmOperator *op)
223 bGPdata **gpd_ptr = gpencil_data_get_pointers(C, NULL);
225 if (gpd_ptr == NULL) {
226 BKE_report(op->reports, RPT_ERROR, "Nowhere for grease pencil data to go");
227 return OPERATOR_CANCELLED;
230 /* decrement user count and add new datablock */
231 bGPdata *gpd = (*gpd_ptr);
234 *gpd_ptr = gpencil_data_addnew(DATA_("GPencil"));
238 WM_event_add_notifier(C, NC_GPENCIL | ND_DATA | NA_EDITED, NULL);
240 return OPERATOR_FINISHED;
243 void GPENCIL_OT_data_add(wmOperatorType *ot)
246 ot->name = "Grease Pencil Add New";
247 ot->idname = "GPENCIL_OT_data_add";
248 ot->description = "Add new Grease Pencil datablock";
249 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
252 ot->exec = gp_data_add_exec;
253 ot->poll = gp_add_poll;
256 /* ******************* Unlink Data ************************ */
258 /* poll callback for adding data/layers - special */
259 static int gp_data_unlink_poll(bContext *C)
261 bGPdata **gpd_ptr = gpencil_data_get_pointers(C, NULL);
263 /* if we have access to some active data, make sure there's a datablock before enabling this */
264 return (gpd_ptr && *gpd_ptr);
268 /* unlink datablock - wrapper around API */
269 static int gp_data_unlink_exec(bContext *C, wmOperator *op)
271 bGPdata **gpd_ptr = gpencil_data_get_pointers(C, NULL);
273 if (gpd_ptr == NULL) {
274 BKE_report(op->reports, RPT_ERROR, "Nowhere for grease pencil data to go");
275 return OPERATOR_CANCELLED;
278 /* just unlink datablock now, decreasing its user count */
279 bGPdata *gpd = (*gpd_ptr);
286 WM_event_add_notifier(C, NC_GPENCIL | ND_DATA | NA_EDITED, NULL);
288 return OPERATOR_FINISHED;
291 void GPENCIL_OT_data_unlink(wmOperatorType *ot)
294 ot->name = "Grease Pencil Unlink";
295 ot->idname = "GPENCIL_OT_data_unlink";
296 ot->description = "Unlink active Grease Pencil datablock";
297 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
300 ot->exec = gp_data_unlink_exec;
301 ot->poll = gp_data_unlink_poll;
304 /* ******************* Add New Layer ************************ */
306 /* add new layer - wrapper around API */
307 static int gp_layer_add_exec(bContext *C, wmOperator *op)
309 bGPdata **gpd_ptr = gpencil_data_get_pointers(C, NULL);
311 /* if there's no existing Grease-Pencil data there, add some */
312 if (gpd_ptr == NULL) {
313 BKE_report(op->reports, RPT_ERROR, "Nowhere for grease pencil data to go");
314 return OPERATOR_CANCELLED;
316 if (*gpd_ptr == NULL)
317 *gpd_ptr = gpencil_data_addnew(DATA_("GPencil"));
319 /* add new layer now */
320 gpencil_layer_addnew(*gpd_ptr, DATA_("GP_Layer"), 1);
323 WM_event_add_notifier(C, NC_GPENCIL | ND_DATA | NA_EDITED, NULL);
325 return OPERATOR_FINISHED;
328 void GPENCIL_OT_layer_add(wmOperatorType *ot)
331 ot->name = "Add New Layer";
332 ot->idname = "GPENCIL_OT_layer_add";
333 ot->description = "Add new Grease Pencil layer for the active Grease Pencil datablock";
334 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
337 ot->exec = gp_layer_add_exec;
338 ot->poll = gp_add_poll;
341 /* ******************* Delete Active Frame ************************ */
343 static int gp_actframe_delete_poll(bContext *C)
345 bGPdata *gpd = gpencil_data_get_active(C);
346 bGPDlayer *gpl = gpencil_layer_getactive(gpd);
348 /* only if there's an active layer with an active frame */
349 return (gpl && gpl->actframe);
352 /* delete active frame - wrapper around API calls */
353 static int gp_actframe_delete_exec(bContext *C, wmOperator *op)
355 Scene *scene = CTX_data_scene(C);
356 bGPdata *gpd = gpencil_data_get_active(C);
357 bGPDlayer *gpl = gpencil_layer_getactive(gpd);
358 bGPDframe *gpf = gpencil_layer_getframe(gpl, CFRA, 0);
360 /* if there's no existing Grease-Pencil data there, add some */
362 BKE_report(op->reports, RPT_ERROR, "No grease pencil data");
363 return OPERATOR_CANCELLED;
365 if (ELEM(NULL, gpl, gpf)) {
366 BKE_report(op->reports, RPT_ERROR, "No active frame to delete");
367 return OPERATOR_CANCELLED;
371 gpencil_layer_delframe(gpl, gpf);
374 WM_event_add_notifier(C, NC_GPENCIL | ND_DATA | NA_EDITED, NULL);
376 return OPERATOR_FINISHED;
379 void GPENCIL_OT_active_frame_delete(wmOperatorType *ot)
382 ot->name = "Delete Active Frame";
383 ot->idname = "GPENCIL_OT_active_frame_delete";
384 ot->description = "Delete the active frame for the active Grease Pencil datablock";
385 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
388 ot->exec = gp_actframe_delete_exec;
389 ot->poll = gp_actframe_delete_poll;
392 /* ************************************************ */
393 /* Grease Pencil to Data Operator */
395 /* defines for possible modes */
397 GP_STROKECONVERT_PATH = 1,
398 GP_STROKECONVERT_CURVE,
399 GP_STROKECONVERT_POLY,
402 /* Defines for possible timing modes */
404 GP_STROKECONVERT_TIMING_NONE = 1,
405 GP_STROKECONVERT_TIMING_LINEAR = 2,
406 GP_STROKECONVERT_TIMING_FULL = 3,
407 GP_STROKECONVERT_TIMING_CUSTOMGAP = 4,
410 /* RNA enum define */
411 static EnumPropertyItem prop_gpencil_convertmodes[] = {
412 {GP_STROKECONVERT_PATH, "PATH", 0, "Path", ""},
413 {GP_STROKECONVERT_CURVE, "CURVE", 0, "Bezier Curve", ""},
414 {GP_STROKECONVERT_POLY, "POLY", 0, "Polygon Curve", ""},
415 {0, NULL, 0, NULL, NULL}
418 static EnumPropertyItem prop_gpencil_convert_timingmodes_restricted[] = {
419 {GP_STROKECONVERT_TIMING_NONE, "NONE", 0, "No Timing", "Ignore timing"},
420 {GP_STROKECONVERT_TIMING_LINEAR, "LINEAR", 0, "Linear", "Simple linear timing"},
421 {0, NULL, 0, NULL, NULL},
424 static EnumPropertyItem prop_gpencil_convert_timingmodes[] = {
425 {GP_STROKECONVERT_TIMING_NONE, "NONE", 0, "No Timing", "Ignore timing"},
426 {GP_STROKECONVERT_TIMING_LINEAR, "LINEAR", 0, "Linear", "Simple linear timing"},
427 {GP_STROKECONVERT_TIMING_FULL, "FULL", 0, "Original", "Use the original timing, gaps included"},
428 {GP_STROKECONVERT_TIMING_CUSTOMGAP, "CUSTOMGAP", 0, "Custom Gaps",
429 "Use the original timing, but with custom gap lengths (in frames)"},
430 {0, NULL, 0, NULL, NULL},
433 static EnumPropertyItem *rna_GPConvert_mode_items(bContext *UNUSED(C), PointerRNA *ptr, PropertyRNA *UNUSED(prop),
437 if (RNA_boolean_get(ptr, "use_timing_data")) {
438 return prop_gpencil_convert_timingmodes;
440 return prop_gpencil_convert_timingmodes_restricted;
445 /* convert the coordinates from the given stroke point into 3d-coordinates
446 * - assumes that the active space is the 3D-View
448 static void gp_strokepoint_convertcoords(bContext *C, bGPDstroke *gps, bGPDspoint *pt, float p3d[3], rctf *subrect)
450 Scene *scene = CTX_data_scene(C);
451 View3D *v3d = CTX_wm_view3d(C);
452 ARegion *ar = CTX_wm_region(C);
454 if (gps->flag & GP_STROKE_3DSPACE) {
455 /* directly use 3d-coordinates */
456 copy_v3_v3(p3d, &pt->x);
459 const float *fp = give_cursor(scene, v3d);
462 /* get screen coordinate */
463 if (gps->flag & GP_STROKE_2DSPACE) {
465 View2D *v2d = &ar->v2d;
466 UI_view2d_view_to_region(v2d, pt->x, pt->y, mvali, mvali + 1);
467 VECCOPY2D(mvalf, mvali);
471 mvalf[0] = (((float)pt->x / 100.0f) * BLI_rctf_size_x(subrect)) + subrect->xmin;
472 mvalf[1] = (((float)pt->y / 100.0f) * BLI_rctf_size_y(subrect)) + subrect->ymin;
475 mvalf[0] = (float)pt->x / 100.0f * ar->winx;
476 mvalf[1] = (float)pt->y / 100.0f * ar->winy;
480 /* convert screen coordinate to 3d coordinates
481 * - method taken from editview.c - mouse_cursor()
483 ED_view3d_win_to_3d(ar, fp, mvalf, p3d);
489 /* temp struct for gp_stroke_path_animation() */
490 typedef struct tGpTimingData {
491 /* Data set from operator settings */
493 int frame_range; /* Number of frames evaluated for path animation */
494 int start_frame, end_frame;
495 int realtime; /* A bool, actually, will overwrite end_frame in case of Original or CustomGap timing... */
496 float gap_duration, gap_randomness; /* To be used with CustomGap mode*/
499 /* Data set from points, used to compute final timing FCurve */
500 int num_points, cur_point;
507 float *times; /* Note: Gap times will be negative! */
508 float tot_time, gap_tot_time;
512 /* init point buffers for timing data */
513 static void _gp_timing_data_set_nbr(tGpTimingData *gtd, int nbr)
517 BLI_assert(nbr > gtd->num_points);
521 gtd->dists = MEM_callocN(sizeof(float) * nbr, __func__);
523 memcpy(gtd->dists, tmp, sizeof(float) * gtd->num_points);
529 gtd->times = MEM_callocN(sizeof(float) * nbr, __func__);
531 memcpy(gtd->times, tmp, sizeof(float) * gtd->num_points);
535 gtd->num_points = nbr;
538 /* add stroke point to timing buffers */
539 static void gp_timing_data_add_point(tGpTimingData *gtd, double stroke_inittime, float time, float delta_dist)
542 /* This is a gap, negative value! */
543 gtd->times[gtd->cur_point] = -(((float)(stroke_inittime - gtd->inittime)) + time);
544 gtd->tot_time = -gtd->times[gtd->cur_point];
546 gtd->gap_tot_time += gtd->times[gtd->cur_point] - gtd->times[gtd->cur_point - 1];
549 gtd->times[gtd->cur_point] = (((float)(stroke_inittime - gtd->inittime)) + time);
550 gtd->tot_time = (gtd->times[gtd->cur_point]);
553 gtd->tot_dist += delta_dist;
554 gtd->dists[gtd->cur_point] = gtd->tot_dist;
559 /* In frames! Binary search for FCurve keys have a threshold of 0.01, so we can't set
560 * arbitrarily close points - this is esp. important with NoGaps mode!
562 #define MIN_TIME_DELTA 0.02f
564 /* Loop over next points to find the end of the stroke, and compute */
565 static int gp_find_end_of_stroke_idx(tGpTimingData *gtd, RNG *rng, int idx, int nbr_gaps, int *nbr_done_gaps,
566 float tot_gaps_time, float delta_time, float *next_delta_time)
570 for (j = idx + 1; j < gtd->num_points; j++) {
571 if (gtd->times[j] < 0) {
572 gtd->times[j] = -gtd->times[j];
573 if (gtd->mode == GP_STROKECONVERT_TIMING_CUSTOMGAP) {
574 /* In this mode, gap time between this stroke and the next should be 0 currently...
575 * So we have to compute its final duration!
577 if (gtd->gap_randomness > 0.0f) {
578 /* We want gaps that are in gtd->gap_duration +/- gtd->gap_randomness range,
579 * and which sum to exactly tot_gaps_time...
581 int rem_gaps = nbr_gaps - (*nbr_done_gaps);
583 /* Last gap, just give remaining time! */
584 *next_delta_time = tot_gaps_time;
587 float delta, min, max;
589 /* This code ensures that if the first gaps have been shorter than average gap_duration,
590 * next gaps will tend to be longer (i.e. try to recover the lateness), and vice-versa!
592 delta = delta_time - (gtd->gap_duration * (*nbr_done_gaps));
594 /* Clamp min between [-gap_randomness, 0.0], with lower delta giving higher min */
595 min = -gtd->gap_randomness - delta;
596 CLAMP(min, -gtd->gap_randomness, 0.0f);
598 /* Clamp max between [0.0, gap_randomness], with lower delta giving higher max */
599 max = gtd->gap_randomness - delta;
600 CLAMP(max, 0.0f, gtd->gap_randomness);
601 *next_delta_time += gtd->gap_duration + (BLI_rng_get_float(rng) * (max - min)) + min;
605 *next_delta_time += gtd->gap_duration;
616 static void gp_stroke_path_animation_preprocess_gaps(tGpTimingData *gtd, RNG *rng, int *nbr_gaps, float *tot_gaps_time)
619 float delta_time = 0.0f;
621 for (i = 0; i < gtd->num_points; i++) {
622 if (gtd->times[i] < 0 && i) {
624 gtd->times[i] = -gtd->times[i] - delta_time;
625 delta_time += gtd->times[i] - gtd->times[i - 1];
626 gtd->times[i] = -gtd->times[i - 1]; /* Temp marker, values *have* to be different! */
629 gtd->times[i] -= delta_time;
632 gtd->tot_time -= delta_time;
634 *tot_gaps_time = (float)(*nbr_gaps) * gtd->gap_duration;
635 gtd->tot_time += *tot_gaps_time;
636 if (G.debug & G_DEBUG) {
637 printf("%f, %f, %f, %d\n", gtd->tot_time, delta_time, *tot_gaps_time, *nbr_gaps);
639 if (gtd->gap_randomness > 0.0f) {
640 BLI_rng_srandom(rng, gtd->seed);
644 static void gp_stroke_path_animation_add_keyframes(ReportList *reports, PointerRNA ptr, PropertyRNA *prop, FCurve *fcu,
645 Curve *cu, tGpTimingData *gtd, RNG *rng, float time_range,
646 int nbr_gaps, float tot_gaps_time)
648 /* Use actual recorded timing! */
649 float time_start = (float)gtd->start_frame;
651 float last_valid_time = 0.0f;
652 int end_stroke_idx = -1, start_stroke_idx = 0;
653 float end_stroke_time = 0.0f;
655 /* CustomGaps specific */
656 float delta_time = 0.0f, next_delta_time = 0.0f;
657 int nbr_done_gaps = 0;
662 /* This is a bit tricky, as:
663 * - We can't add arbitrarily close points on FCurve (in time).
664 * - We *must* have all "caps" points of all strokes in FCurve, as much as possible!
666 for (i = 0; i < gtd->num_points; i++) {
667 /* If new stroke... */
668 if (i > end_stroke_idx) {
669 start_stroke_idx = i;
670 delta_time = next_delta_time;
671 /* find end of that new stroke */
672 end_stroke_idx = gp_find_end_of_stroke_idx(gtd, rng, i, nbr_gaps, &nbr_done_gaps,
673 tot_gaps_time, delta_time, &next_delta_time);
674 /* This one should *never* be negative! */
675 end_stroke_time = time_start + ((gtd->times[end_stroke_idx] + delta_time) / gtd->tot_time * time_range);
678 /* Simple proportional stuff... */
679 cu->ctime = gtd->dists[i] / gtd->tot_dist * cu->pathlen;
680 cfra = time_start + ((gtd->times[i] + delta_time) / gtd->tot_time * time_range);
682 /* And now, the checks about timing... */
683 if (i == start_stroke_idx) {
684 /* If first point of a stroke, be sure it's enough ahead of last valid keyframe, and
685 * that the end point of the stroke is far enough!
686 * In case it is not, we keep the end point...
687 * Note that with CustomGaps mode, this is here we set the actual gap timing!
689 if ((end_stroke_time - last_valid_time) > MIN_TIME_DELTA * 2) {
690 if ((cfra - last_valid_time) < MIN_TIME_DELTA) {
691 cfra = last_valid_time + MIN_TIME_DELTA;
693 insert_keyframe_direct(reports, ptr, prop, fcu, cfra, INSERTKEY_FAST);
694 last_valid_time = cfra;
696 else if (G.debug & G_DEBUG) {
697 printf("\t Skipping start point %d, too close from end point %d\n", i, end_stroke_idx);
700 else if (i == end_stroke_idx) {
701 /* Always try to insert end point of a curve (should be safe enough, anyway...) */
702 if ((cfra - last_valid_time) < MIN_TIME_DELTA) {
703 cfra = last_valid_time + MIN_TIME_DELTA;
705 insert_keyframe_direct(reports, ptr, prop, fcu, cfra, INSERTKEY_FAST);
706 last_valid_time = cfra;
709 /* Else ("middle" point), we only insert it if it's far enough from last keyframe,
710 * and also far enough from (not yet added!) end_stroke keyframe!
712 if ((cfra - last_valid_time) > MIN_TIME_DELTA && (end_stroke_time - cfra) > MIN_TIME_DELTA) {
713 insert_keyframe_direct(reports, ptr, prop, fcu, cfra, INSERTKEY_FAST);
714 last_valid_time = cfra;
716 else if (G.debug & G_DEBUG) {
717 printf("\t Skipping \"middle\" point %d, too close from last added point or end point %d\n",
724 static void gp_stroke_path_animation(bContext *C, ReportList *reports, Curve *cu, tGpTimingData *gtd)
726 Scene *scene = CTX_data_scene(C);
730 PropertyRNA *prop = NULL;
733 if (gtd->mode == GP_STROKECONVERT_TIMING_NONE)
736 /* gap_duration and gap_randomness are in frames, but we need seconds!!! */
737 gtd->gap_duration = FRA2TIME(gtd->gap_duration);
738 gtd->gap_randomness = FRA2TIME(gtd->gap_randomness);
742 cu->pathlen = gtd->frame_range;
744 /* Get RNA pointer to read/write path time values */
745 RNA_id_pointer_create((ID *)cu, &ptr);
746 prop = RNA_struct_find_property(&ptr, "eval_time");
748 /* Ensure we have an F-Curve to add keyframes to */
749 act = verify_adt_action((ID *)cu, TRUE);
750 fcu = verify_fcurve(act, NULL, &ptr, "eval_time", 0, TRUE);
752 if (G.debug & G_DEBUG) {
753 printf("%s: tot len: %f\t\ttot time: %f\n", __func__, gtd->tot_dist, gtd->tot_time);
754 for (i = 0; i < gtd->num_points; i++) {
755 printf("\tpoint %d:\t\tlen: %f\t\ttime: %f\n", i, gtd->dists[i], gtd->times[i]);
759 if (gtd->mode == GP_STROKECONVERT_TIMING_LINEAR) {
762 /* Linear extrapolation! */
763 fcu->extend = FCURVE_EXTRAPOLATE_LINEAR;
766 cfra = (float)gtd->start_frame;
767 insert_keyframe_direct(reports, ptr, prop, fcu, cfra, INSERTKEY_FAST);
769 cu->ctime = cu->pathlen;
771 cfra += (float)TIME2FRA(gtd->tot_time); /* Seconds to frames */
774 cfra = (float)gtd->end_frame;
776 insert_keyframe_direct(reports, ptr, prop, fcu, cfra, INSERTKEY_FAST);
779 /* Use actual recorded timing! */
780 RNG *rng = BLI_rng_new(0);
783 /* CustomGaps specific */
784 float tot_gaps_time = 0.0f;
786 /* Pre-process gaps, in case we don't want to keep their original timing */
787 if (gtd->mode == GP_STROKECONVERT_TIMING_CUSTOMGAP) {
788 gp_stroke_path_animation_preprocess_gaps(gtd, rng, &nbr_gaps, &tot_gaps_time);
792 time_range = (float)TIME2FRA(gtd->tot_time); /* Seconds to frames */
795 time_range = (float)(gtd->end_frame - gtd->start_frame);
798 if (G.debug & G_DEBUG) {
799 printf("GP Stroke Path Conversion: Starting keying!\n");
802 gp_stroke_path_animation_add_keyframes(reports, ptr, prop, fcu, cu, gtd,
804 nbr_gaps, tot_gaps_time);
809 /* As we used INSERTKEY_FAST mode, we need to recompute all curve's handles now */
810 calchandles_fcurve(fcu);
812 if (G.debug & G_DEBUG) {
813 printf("%s: \ntot len: %f\t\ttot time: %f\n", __func__, gtd->tot_dist, gtd->tot_time);
814 for (i = 0; i < gtd->num_points; i++) {
815 printf("\tpoint %d:\t\tlen: %f\t\ttime: %f\n", i, gtd->dists[i], gtd->times[i]);
820 WM_event_add_notifier(C, NC_ANIMATION | ND_KEYFRAME | NA_EDITED, NULL);
823 DAG_id_tag_update(&cu->id, 0);
826 #undef MIN_TIME_DELTA
828 #define GAP_DFAC 0.05f
829 #define WIDTH_CORR_FAC 0.1f
830 #define BEZT_HANDLE_FAC 0.3f
832 /* convert stroke to 3d path */
833 static void gp_stroke_to_path(bContext *C, bGPDlayer *gpl, bGPDstroke *gps, Curve *cu, rctf *subrect, Nurb **curnu,
834 float minmax_weights[2], float rad_fac, int stitch, tGpTimingData *gtd)
837 Nurb *nu = (curnu) ? *curnu : NULL;
838 BPoint *bp, *prev_bp = NULL;
839 const int do_gtd = (gtd->mode != GP_STROKECONVERT_TIMING_NONE);
842 /* create new 'nurb' or extend current one within the curve */
846 /* If stitch, the first point of this stroke is already present in current nu.
847 * Else, we have to add to additional points to make the zero-radius link between strokes.
849 BKE_nurb_points_add(nu, gps->totpoints + (stitch ? -1 : 2));
852 nu = (Nurb *)MEM_callocN(sizeof(Nurb), "gpstroke_to_path(nurb)");
854 nu->pntsu = gps->totpoints;
856 nu->orderu = 2; /* point-to-point! */
858 nu->flagu = CU_NURB_ENDPOINT;
859 nu->resolu = cu->resolu;
860 nu->resolv = cu->resolv;
863 nu->bp = (BPoint *)MEM_callocN(sizeof(BPoint) * nu->pntsu, "bpoints");
865 stitch = FALSE; /* Security! */
869 _gp_timing_data_set_nbr(gtd, nu->pntsu);
872 /* If needed, make the link between both strokes with two zero-radius additional points */
873 /* About "zero-radius" point interpolations:
874 * - If we have at least two points in current curve (most common case), we linearly extrapolate
875 * the last segment to get the first point (p1) position and timing.
876 * - If we do not have those (quite odd, but may happen), we linearly interpolate the last point
877 * with the first point of the current stroke.
878 * The same goes for the second point, first segment of the current stroke is "negatively" extrapolated
879 * if it exists, else (if the stroke is a single point), linear interpolation with last curve point...
881 if (curnu && !stitch && old_nbp) {
882 float p1[3], p2[3], p[3], next_p[3];
886 if ((old_nbp > 1) && gps->prev && (gps->prev->totpoints > 1)) {
887 /* Only use last curve segment if previous stroke was not a single-point one! */
888 prev_bp = nu->bp + old_nbp - 2;
890 bp = nu->bp + old_nbp - 1;
892 /* XXX We do this twice... Not sure it's worth to bother about this! */
893 gp_strokepoint_convertcoords(C, gps, gps->points, p, subrect);
895 interp_v3_v3v3(p1, prev_bp->vec, bp->vec, 1.0f + GAP_DFAC);
898 interp_v3_v3v3(p1, bp->vec, p, GAP_DFAC);
901 if (gps->totpoints > 1) {
902 /* XXX We do this twice... Not sure it's worth to bother about this! */
903 gp_strokepoint_convertcoords(C, gps, gps->points + 1, next_p, subrect);
904 interp_v3_v3v3(p2, p, next_p, -GAP_DFAC);
907 interp_v3_v3v3(p2, p, bp->vec, GAP_DFAC);
912 copy_v3_v3(bp->vec, p1);
915 minmax_weights[0] = bp->radius = bp->weight = 0.0f;
918 delta_time = gtd->tot_time + (gtd->tot_time - gtd->times[gtd->cur_point - 1]) * GAP_DFAC;
921 delta_time = gtd->tot_time + (((float)(gps->inittime - gtd->inittime)) - gtd->tot_time) * GAP_DFAC;
923 gp_timing_data_add_point(gtd, gtd->inittime, delta_time, len_v3v3((bp - 1)->vec, p1));
928 copy_v3_v3(bp->vec, p2);
931 minmax_weights[0] = bp->radius = bp->weight = 0.0f;
933 /* This negative delta_time marks the gap! */
934 if (gps->totpoints > 1) {
935 delta_time = ((gps->points + 1)->time - gps->points->time) * -GAP_DFAC;
938 delta_time = -(((float)(gps->inittime - gtd->inittime)) - gtd->tot_time) * GAP_DFAC;
940 gp_timing_data_add_point(gtd, gps->inittime, delta_time, len_v3v3(p1, p2));
945 if (old_nbp && do_gtd) {
946 prev_bp = nu->bp + old_nbp - 1;
950 for (i = (stitch) ? 1 : 0, pt = gps->points + ((stitch) ? 1 : 0), bp = nu->bp + old_nbp;
955 float width = pt->pressure * gpl->thickness * WIDTH_CORR_FAC;
957 /* get coordinates to add at */
958 gp_strokepoint_convertcoords(C, gps, pt, p3d, subrect);
959 copy_v3_v3(bp->vec, p3d);
964 bp->radius = width * rad_fac;
966 CLAMP(bp->weight, 0.0f, 1.0f);
967 if (bp->weight < minmax_weights[0]) {
968 minmax_weights[0] = bp->weight;
970 else if (bp->weight > minmax_weights[1]) {
971 minmax_weights[1] = bp->weight;
974 /* Update timing data */
976 gp_timing_data_add_point(gtd, gps->inittime, pt->time, (prev_bp) ? len_v3v3(prev_bp->vec, p3d) : 0.0f);
981 /* add nurb to curve */
982 if (!curnu || !*curnu) {
983 BLI_addtail(&cu->nurb, nu);
989 BKE_nurb_knot_calc_u(nu);
992 static int gp_camera_view_subrect(bContext *C, rctf *subrect)
994 View3D *v3d = CTX_wm_view3d(C);
995 ARegion *ar = CTX_wm_region(C);
998 RegionView3D *rv3d = ar->regiondata;
1000 /* for camera view set the subrect */
1001 if (rv3d->persp == RV3D_CAMOB) {
1002 Scene *scene = CTX_data_scene(C);
1003 ED_view3d_calc_camera_border(scene, ar, v3d, rv3d, subrect, TRUE); /* no shift */
1011 /* convert stroke to 3d bezier */
1012 static void gp_stroke_to_bezier(bContext *C, bGPDlayer *gpl, bGPDstroke *gps, Curve *cu, rctf *subrect, Nurb **curnu,
1013 float minmax_weights[2], float rad_fac, int stitch, tGpTimingData *gtd)
1016 Nurb *nu = (curnu) ? *curnu : NULL;
1017 BezTriple *bezt, *prev_bezt = NULL;
1018 int i, tot, old_nbezt = 0;
1019 float p3d_cur[3], p3d_prev[3], p3d_next[3];
1020 const int do_gtd = (gtd->mode != GP_STROKECONVERT_TIMING_NONE);
1022 /* create new 'nurb' or extend current one within the curve */
1024 old_nbezt = nu->pntsu;
1025 /* If we do stitch, first point of current stroke is assumed the same as last point of previous stroke,
1026 * so no need to add it.
1027 * If no stitch, we want to add two additional points to make a "zero-radius" link between both strokes.
1029 BKE_nurb_bezierPoints_add(nu, gps->totpoints + ((stitch) ? -1 : 2));
1032 nu = (Nurb *)MEM_callocN(sizeof(Nurb), "gpstroke_to_bezier(nurb)");
1034 nu->pntsu = gps->totpoints;
1037 nu->type = CU_BEZIER;
1038 nu->bezt = (BezTriple *)MEM_callocN(gps->totpoints * sizeof(BezTriple), "bezts");
1040 stitch = FALSE; /* Security! */
1044 _gp_timing_data_set_nbr(gtd, nu->pntsu);
1047 tot = gps->totpoints;
1049 /* get initial coordinates */
1052 gp_strokepoint_convertcoords(C, gps, pt, (stitch) ? p3d_prev : p3d_cur, subrect);
1054 gp_strokepoint_convertcoords(C, gps, pt + 1, (stitch) ? p3d_cur : p3d_next, subrect);
1056 if (stitch && tot > 2) {
1057 gp_strokepoint_convertcoords(C, gps, pt + 2, p3d_next, subrect);
1061 /* If needed, make the link between both strokes with two zero-radius additional points */
1062 if (curnu && old_nbezt) {
1063 /* Update last point's second handle */
1066 bezt = nu->bezt + old_nbezt - 1;
1067 interp_v3_v3v3(h2, bezt->vec[1], p3d_cur, BEZT_HANDLE_FAC);
1068 copy_v3_v3(bezt->vec[2], h2);
1072 /* Create "link points" */
1073 /* About "zero-radius" point interpolations:
1074 * - If we have at least two points in current curve (most common case), we linearly extrapolate
1075 * the last segment to get the first point (p1) position and timing.
1076 * - If we do not have those (quite odd, but may happen), we linearly interpolate the last point
1077 * with the first point of the current stroke.
1078 * The same goes for the second point, first segment of the current stroke is "negatively" extrapolated
1079 * if it exists, else (if the stroke is a single point), linear interpolation with last curve point...
1082 float h1[3], h2[3], p1[3], p2[3];
1086 if (old_nbezt > 1 && gps->prev && gps->prev->totpoints > 1) {
1087 /* Only use last curve segment if previous stroke was not a single-point one! */
1088 prev_bezt = nu->bezt + old_nbezt - 2;
1090 bezt = nu->bezt + old_nbezt - 1;
1092 interp_v3_v3v3(p1, prev_bezt->vec[1], bezt->vec[1], 1.0f + GAP_DFAC);
1095 interp_v3_v3v3(p1, bezt->vec[1], p3d_cur, GAP_DFAC);
1098 interp_v3_v3v3(p2, p3d_cur, p3d_next, -GAP_DFAC);
1101 interp_v3_v3v3(p2, p3d_cur, bezt->vec[1], GAP_DFAC);
1104 /* Second handle of last point */
1105 interp_v3_v3v3(h2, bezt->vec[1], p1, BEZT_HANDLE_FAC);
1106 copy_v3_v3(bezt->vec[2], h2);
1109 interp_v3_v3v3(h1, p1, bezt->vec[1], BEZT_HANDLE_FAC);
1110 interp_v3_v3v3(h2, p1, p2, BEZT_HANDLE_FAC);
1113 copy_v3_v3(bezt->vec[0], h1);
1114 copy_v3_v3(bezt->vec[1], p1);
1115 copy_v3_v3(bezt->vec[2], h2);
1116 bezt->h1 = bezt->h2 = HD_FREE;
1117 bezt->f1 = bezt->f2 = bezt->f3 = SELECT;
1118 minmax_weights[0] = bezt->radius = bezt->weight = 0.0f;
1122 delta_time = gtd->tot_time + (gtd->tot_time - gtd->times[gtd->cur_point - 1]) * GAP_DFAC;
1125 delta_time = gtd->tot_time + (((float)(gps->inittime - gtd->inittime)) - gtd->tot_time) * GAP_DFAC;
1127 gp_timing_data_add_point(gtd, gtd->inittime, delta_time, len_v3v3((bezt - 1)->vec[1], p1));
1131 interp_v3_v3v3(h1, p2, p1, BEZT_HANDLE_FAC);
1132 interp_v3_v3v3(h2, p2, p3d_cur, BEZT_HANDLE_FAC);
1135 copy_v3_v3(bezt->vec[0], h1);
1136 copy_v3_v3(bezt->vec[1], p2);
1137 copy_v3_v3(bezt->vec[2], h2);
1138 bezt->h1 = bezt->h2 = HD_FREE;
1139 bezt->f1 = bezt->f2 = bezt->f3 = SELECT;
1140 minmax_weights[0] = bezt->radius = bezt->weight = 0.0f;
1143 /* This negative delta_time marks the gap! */
1145 delta_time = ((gps->points + 1)->time - gps->points->time) * -GAP_DFAC;
1148 delta_time = -(((float)(gps->inittime - gtd->inittime)) - gtd->tot_time) * GAP_DFAC;
1150 gp_timing_data_add_point(gtd, gps->inittime, delta_time, len_v3v3(p1, p2));
1154 copy_v3_v3(p3d_prev, p2);
1157 if (old_nbezt && do_gtd) {
1158 prev_bezt = nu->bezt + old_nbezt - 1;
1162 for (i = stitch ? 1 : 0, bezt = nu->bezt + old_nbezt; i < tot; i++, pt++, bezt++) {
1164 float width = pt->pressure * gpl->thickness * WIDTH_CORR_FAC;
1166 if (i || old_nbezt) {
1167 interp_v3_v3v3(h1, p3d_cur, p3d_prev, BEZT_HANDLE_FAC);
1170 interp_v3_v3v3(h1, p3d_cur, p3d_next, -BEZT_HANDLE_FAC);
1174 interp_v3_v3v3(h2, p3d_cur, p3d_next, BEZT_HANDLE_FAC);
1177 interp_v3_v3v3(h2, p3d_cur, p3d_prev, -BEZT_HANDLE_FAC);
1180 copy_v3_v3(bezt->vec[0], h1);
1181 copy_v3_v3(bezt->vec[1], p3d_cur);
1182 copy_v3_v3(bezt->vec[2], h2);
1185 bezt->h1 = bezt->h2 = HD_FREE;
1186 bezt->f1 = bezt->f2 = bezt->f3 = SELECT;
1187 bezt->radius = width * rad_fac;
1188 bezt->weight = width;
1189 CLAMP(bezt->weight, 0.0f, 1.0f);
1190 if (bezt->weight < minmax_weights[0]) {
1191 minmax_weights[0] = bezt->weight;
1193 else if (bezt->weight > minmax_weights[1]) {
1194 minmax_weights[1] = bezt->weight;
1197 /* Update timing data */
1199 gp_timing_data_add_point(gtd, gps->inittime, pt->time, prev_bezt ? len_v3v3(prev_bezt->vec[1], p3d_cur) : 0.0f);
1202 /* shift coord vects */
1203 copy_v3_v3(p3d_prev, p3d_cur);
1204 copy_v3_v3(p3d_cur, p3d_next);
1207 gp_strokepoint_convertcoords(C, gps, pt + 2, p3d_next, subrect);
1213 /* must calculate handles or else we crash */
1214 BKE_nurb_handles_calc(nu);
1216 if (!curnu || !*curnu) {
1217 BLI_addtail(&cu->nurb, nu);
1225 #undef WIDTH_CORR_FAC
1226 #undef BEZT_HANDLE_FAC
1228 static void gp_stroke_finalize_curve_endpoints(Curve *cu)
1231 Nurb *nu = cu->nurb.first;
1234 BezTriple *bezt = nu->bezt;
1236 bezt[i].weight = bezt[i].radius = 0.0f;
1240 BPoint *bp = nu->bp;
1242 bp[i].weight = bp[i].radius = 0.0f;
1250 BezTriple *bezt = nu->bezt;
1252 bezt[i].weight = bezt[i].radius = 0.0f;
1256 BPoint *bp = nu->bp;
1258 bp[i].weight = bp[i].radius = 0.0f;
1263 static void gp_stroke_norm_curve_weights(Curve *cu, const float minmax_weights[2])
1266 const float delta = minmax_weights[0];
1267 const float fac = 1.0f / (minmax_weights[1] - delta);
1270 for (nu = cu->nurb.first; nu; nu = nu->next) {
1272 BezTriple *bezt = nu->bezt;
1273 for (i = 0; i < nu->pntsu; i++, bezt++) {
1274 bezt->weight = (bezt->weight - delta) * fac;
1278 BPoint *bp = nu->bp;
1279 for (i = 0; i < nu->pntsu; i++, bp++) {
1280 bp->weight = (bp->weight - delta) * fac;
1286 /* convert a given grease-pencil layer to a 3d-curve representation (using current view if appropriate) */
1287 static void gp_layer_to_curve(bContext *C, ReportList *reports, bGPdata *gpd, bGPDlayer *gpl, int mode,
1288 int norm_weights, float rad_fac, int link_strokes, tGpTimingData *gtd)
1290 struct Main *bmain = CTX_data_main(C);
1291 Scene *scene = CTX_data_scene(C);
1292 bGPDframe *gpf = gpencil_layer_getframe(gpl, CFRA, 0);
1293 bGPDstroke *gps, *prev_gps = NULL;
1297 Base *base_orig = BASACT, *base_new = NULL;
1298 float minmax_weights[2] = {1.0f, 0.0f};
1300 /* camera framing */
1301 rctf subrect, *subrect_ptr = NULL;
1303 /* error checking */
1304 if (ELEM3(NULL, gpd, gpl, gpf))
1307 /* only convert if there are any strokes on this layer's frame to convert */
1308 if (gpf->strokes.first == NULL)
1311 /* initialize camera framing */
1312 if (gp_camera_view_subrect(C, &subrect)) {
1313 subrect_ptr = &subrect;
1316 /* init the curve object (remove rotation and get curve data from it)
1317 * - must clear transforms set on object, as those skew our results
1319 ob = BKE_object_add_only_object(bmain, OB_CURVE, gpl->info);
1320 cu = ob->data = BKE_curve_add(bmain, gpl->info, OB_CURVE);
1321 base_new = BKE_scene_base_add(scene, ob);
1325 gtd->inittime = ((bGPDstroke *)gpf->strokes.first)->inittime;
1327 /* add points to curve */
1328 for (gps = gpf->strokes.first; gps; gps = gps->next) {
1329 /* Detect new strokes created because of GP_STROKE_BUFFER_MAX reached,
1330 * and stitch them to previous one.
1335 bGPDspoint *pt1 = prev_gps->points + prev_gps->totpoints - 1;
1336 bGPDspoint *pt2 = gps->points;
1338 if ((pt1->x == pt2->x) && (pt1->y == pt2->y)) {
1343 /* Decide whether we connect this stroke to previous one */
1344 if (!(stitch || link_strokes)) {
1349 case GP_STROKECONVERT_PATH:
1350 gp_stroke_to_path(C, gpl, gps, cu, subrect_ptr, &nu, minmax_weights, rad_fac, stitch, gtd);
1352 case GP_STROKECONVERT_CURVE:
1353 case GP_STROKECONVERT_POLY: /* convert after */
1354 gp_stroke_to_bezier(C, gpl, gps, cu, subrect_ptr, &nu, minmax_weights, rad_fac, stitch, gtd);
1357 BLI_assert(!"invalid mode");
1363 /* If link_strokes, be sure first and last points have a zero weight/size! */
1365 gp_stroke_finalize_curve_endpoints(cu);
1367 /* Update curve's weights, if needed */
1368 if (norm_weights && ((minmax_weights[0] > 0.0f) || (minmax_weights[1] < 1.0f)))
1369 gp_stroke_norm_curve_weights(cu, minmax_weights);
1371 /* Create the path animation, if needed */
1372 gp_stroke_path_animation(C, reports, cu, gtd);
1374 if (mode == GP_STROKECONVERT_POLY) {
1375 for (nu = cu->nurb.first; nu; nu = nu->next) {
1376 BKE_nurb_type_convert(nu, CU_POLY, false);
1380 /* set the layer and select */
1381 base_new->lay = ob->lay = base_orig ? base_orig->lay : scene->lay;
1382 base_new->flag = ob->flag = base_new->flag | SELECT;
1387 /* Check a GP layer has valid timing data! Else, most timing options are hidden in the operator.
1390 static int gp_convert_check_has_valid_timing(bContext *C, bGPDlayer *gpl, wmOperator *op)
1392 Scene *scene = CTX_data_scene(C);
1393 bGPDframe *gpf = NULL;
1394 bGPDstroke *gps = NULL;
1396 double base_time, cur_time, prev_time = -1.0;
1397 int i, valid = TRUE;
1399 if (!gpl || !(gpf = gpencil_layer_getframe(gpl, CFRA, 0)) || !(gps = gpf->strokes.first))
1403 base_time = cur_time = gps->inittime;
1404 if (cur_time <= prev_time) {
1409 prev_time = cur_time;
1410 for (i = 0, pt = gps->points; i < gps->totpoints; i++, pt++) {
1411 cur_time = base_time + (double)pt->time;
1412 /* First point of a stroke should have the same time as stroke's inittime,
1413 * so it's the only case where equality is allowed!
1415 if ((i && cur_time <= prev_time) || (cur_time < prev_time)) {
1419 prev_time = cur_time;
1425 } while ((gps = gps->next));
1428 RNA_boolean_set(op->ptr, "use_timing_data", valid);
1433 /* Check end_frame is always > start frame! */
1434 static void gp_convert_set_end_frame(struct Main *UNUSED(main), struct Scene *UNUSED(scene), struct PointerRNA *ptr)
1436 int start_frame = RNA_int_get(ptr, "start_frame");
1437 int end_frame = RNA_int_get(ptr, "end_frame");
1439 if (end_frame <= start_frame) {
1440 RNA_int_set(ptr, "end_frame", start_frame + 1);
1444 static int gp_convert_poll(bContext *C)
1446 bGPdata *gpd = gpencil_data_get_active(C);
1447 bGPDlayer *gpl = NULL;
1448 bGPDframe *gpf = NULL;
1449 ScrArea *sa = CTX_wm_area(C);
1450 Scene *scene = CTX_data_scene(C);
1452 /* only if the current view is 3D View, if there's valid data (i.e. at least one stroke!),
1453 * and if we are not in edit mode!
1455 return ((sa && sa->spacetype == SPACE_VIEW3D) &&
1456 (gpl = gpencil_layer_getactive(gpd)) &&
1457 (gpf = gpencil_layer_getframe(gpl, CFRA, 0)) &&
1458 (gpf->strokes.first) &&
1459 (scene->obedit == NULL));
1462 static int gp_convert_layer_exec(bContext *C, wmOperator *op)
1464 PropertyRNA *prop = RNA_struct_find_property(op->ptr, "use_timing_data");
1465 bGPdata *gpd = gpencil_data_get_active(C);
1466 bGPDlayer *gpl = gpencil_layer_getactive(gpd);
1467 Scene *scene = CTX_data_scene(C);
1468 int mode = RNA_enum_get(op->ptr, "type");
1469 int norm_weights = RNA_boolean_get(op->ptr, "use_normalize_weights");
1470 float rad_fac = RNA_float_get(op->ptr, "radius_multiplier");
1471 int link_strokes = RNA_boolean_get(op->ptr, "use_link_strokes");
1475 /* check if there's data to work with */
1477 BKE_report(op->reports, RPT_ERROR, "No Grease Pencil data to work on");
1478 return OPERATOR_CANCELLED;
1481 if (!RNA_property_is_set(op->ptr, prop) && !gp_convert_check_has_valid_timing(C, gpl, op)) {
1482 BKE_report(op->reports, RPT_WARNING,
1483 "Current Grease Pencil strokes have no valid timing data, most timing options will be hidden!");
1485 valid_timing = RNA_property_boolean_get(op->ptr, prop);
1487 gtd.mode = RNA_enum_get(op->ptr, "timing_mode");
1488 /* Check for illegal timing mode! */
1489 if (!valid_timing && !ELEM(gtd.mode, GP_STROKECONVERT_TIMING_NONE, GP_STROKECONVERT_TIMING_LINEAR)) {
1490 gtd.mode = GP_STROKECONVERT_TIMING_LINEAR;
1491 RNA_enum_set(op->ptr, "timing_mode", gtd.mode);
1493 if (!link_strokes) {
1494 gtd.mode = GP_STROKECONVERT_TIMING_NONE;
1497 /* grab all relevant settings */
1498 gtd.frame_range = RNA_int_get(op->ptr, "frame_range");
1499 gtd.start_frame = RNA_int_get(op->ptr, "start_frame");
1500 gtd.realtime = valid_timing ? RNA_boolean_get(op->ptr, "use_realtime") : FALSE;
1501 gtd.end_frame = RNA_int_get(op->ptr, "end_frame");
1502 gtd.gap_duration = RNA_float_get(op->ptr, "gap_duration");
1503 gtd.gap_randomness = RNA_float_get(op->ptr, "gap_randomness");
1504 gtd.gap_randomness = min_ff(gtd.gap_randomness, gtd.gap_duration);
1505 gtd.seed = RNA_int_get(op->ptr, "seed");
1506 gtd.num_points = gtd.cur_point = 0;
1507 gtd.dists = gtd.times = NULL;
1508 gtd.tot_dist = gtd.tot_time = gtd.gap_tot_time = 0.0f;
1511 /* perform conversion */
1512 gp_layer_to_curve(C, op->reports, gpd, gpl, mode, norm_weights, rad_fac, link_strokes, >d);
1514 /* free temp memory */
1516 MEM_freeN(gtd.dists);
1520 MEM_freeN(gtd.times);
1525 WM_event_add_notifier(C, NC_OBJECT | NA_ADDED, NULL);
1526 WM_event_add_notifier(C, NC_SCENE | ND_OB_ACTIVE, scene);
1529 return OPERATOR_FINISHED;
1532 static bool gp_convert_draw_check_prop(PointerRNA *ptr, PropertyRNA *prop)
1534 const char *prop_id = RNA_property_identifier(prop);
1535 int link_strokes = RNA_boolean_get(ptr, "use_link_strokes");
1536 int timing_mode = RNA_enum_get(ptr, "timing_mode");
1537 int realtime = RNA_boolean_get(ptr, "use_realtime");
1538 float gap_duration = RNA_float_get(ptr, "gap_duration");
1539 float gap_randomness = RNA_float_get(ptr, "gap_randomness");
1540 int valid_timing = RNA_boolean_get(ptr, "use_timing_data");
1542 /* Always show those props */
1543 if (strcmp(prop_id, "type") == 0 ||
1544 strcmp(prop_id, "use_normalize_weights") == 0 ||
1545 strcmp(prop_id, "radius_multiplier") == 0 ||
1546 strcmp(prop_id, "use_link_strokes") == 0)
1551 /* Never show this prop */
1552 if (strcmp(prop_id, "use_timing_data") == 0)
1556 /* Only show when link_stroke is true */
1557 if (strcmp(prop_id, "timing_mode") == 0)
1560 if (timing_mode != GP_STROKECONVERT_TIMING_NONE) {
1561 /* Only show when link_stroke is true and stroke timing is enabled */
1562 if (strcmp(prop_id, "frame_range") == 0 ||
1563 strcmp(prop_id, "start_frame") == 0)
1568 /* Only show if we have valid timing data! */
1569 if (valid_timing && strcmp(prop_id, "use_realtime") == 0)
1572 /* Only show if realtime or valid_timing is FALSE! */
1573 if ((!realtime || !valid_timing) && strcmp(prop_id, "end_frame") == 0)
1576 if (valid_timing && timing_mode == GP_STROKECONVERT_TIMING_CUSTOMGAP) {
1577 /* Only show for custom gaps! */
1578 if (strcmp(prop_id, "gap_duration") == 0)
1581 /* Only show randomness for non-null custom gaps! */
1582 if (strcmp(prop_id, "gap_randomness") == 0 && (gap_duration > 0.0f))
1585 /* Only show seed for randomize action! */
1586 if (strcmp(prop_id, "seed") == 0 && (gap_duration > 0.0f) && (gap_randomness > 0.0f))
1596 static void gp_convert_ui(bContext *C, wmOperator *op)
1598 uiLayout *layout = op->layout;
1599 wmWindowManager *wm = CTX_wm_manager(C);
1602 RNA_pointer_create(&wm->id, op->type->srna, op->properties, &ptr);
1604 /* Main auto-draw call */
1605 uiDefAutoButsRNA(layout, &ptr, gp_convert_draw_check_prop, '\0');
1608 void GPENCIL_OT_convert(wmOperatorType *ot)
1613 ot->name = "Convert Grease Pencil";
1614 ot->idname = "GPENCIL_OT_convert";
1615 ot->description = "Convert the active Grease Pencil layer to a new Curve Object";
1618 ot->invoke = WM_menu_invoke;
1619 ot->exec = gp_convert_layer_exec;
1620 ot->poll = gp_convert_poll;
1621 ot->ui = gp_convert_ui;
1624 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1627 ot->prop = RNA_def_enum(ot->srna, "type", prop_gpencil_convertmodes, 0, "Type", "Which type of curve to convert to");
1629 RNA_def_boolean(ot->srna, "use_normalize_weights", TRUE, "Normalize Weight",
1630 "Normalize weight (set from stroke width)");
1631 RNA_def_float(ot->srna, "radius_multiplier", 1.0f, 0.0f, 1000.0f, "Radius Fac",
1632 "Multiplier for the points' radii (set from stroke width)", 0.0f, 10.0f);
1633 RNA_def_boolean(ot->srna, "use_link_strokes", TRUE, "Link Strokes",
1634 "Whether to link strokes with zero-radius sections of curves");
1636 prop = RNA_def_enum(ot->srna, "timing_mode", prop_gpencil_convert_timingmodes, GP_STROKECONVERT_TIMING_FULL,
1637 "Timing Mode", "How to use timing data stored in strokes");
1638 RNA_def_enum_funcs(prop, rna_GPConvert_mode_items);
1640 RNA_def_int(ot->srna, "frame_range", 100, 1, 10000, "Frame Range",
1641 "The duration of evaluation of the path control curve", 1, 1000);
1642 RNA_def_int(ot->srna, "start_frame", 1, 1, 100000, "Start Frame",
1643 "The start frame of the path control curve", 1, 100000);
1644 RNA_def_boolean(ot->srna, "use_realtime", FALSE, "Realtime",
1645 "Whether the path control curve reproduces the drawing in realtime, starting from Start Frame");
1646 prop = RNA_def_int(ot->srna, "end_frame", 250, 1, 100000, "End Frame",
1647 "The end frame of the path control curve (if Realtime is not set)", 1, 100000);
1648 RNA_def_property_update_runtime(prop, gp_convert_set_end_frame);
1650 RNA_def_float(ot->srna, "gap_duration", 0.0f, 0.0f, 10000.0f, "Gap Duration",
1651 "Custom Gap mode: (Average) length of gaps, in frames "
1652 "(Note: Realtime value, will be scaled if Realtime is not set)", 0.0f, 1000.0f);
1653 RNA_def_float(ot->srna, "gap_randomness", 0.0f, 0.0f, 10000.0f, "Gap Randomness",
1654 "Custom Gap mode: Number of frames that gap lengths can vary", 0.0f, 1000.0f);
1655 RNA_def_int(ot->srna, "seed", 0, 0, 1000, "Random Seed",
1656 "Custom Gap mode: Random generator seed", 0, 100);
1658 /* Note: Internal use, this one will always be hidden by UI code... */
1659 prop = RNA_def_boolean(ot->srna, "use_timing_data", FALSE, "Has Valid Timing",
1660 "Whether the converted Grease Pencil layer has valid timing data (internal use)");
1661 RNA_def_property_flag(prop, PROP_SKIP_SAVE);
1664 /* ************************************************ */