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 "DNA_anim_types.h"
47 #include "DNA_curve_types.h"
48 #include "DNA_object_types.h"
49 #include "DNA_node_types.h"
50 #include "DNA_scene_types.h"
51 #include "DNA_screen_types.h"
52 #include "DNA_space_types.h"
53 #include "DNA_view3d_types.h"
54 #include "DNA_gpencil_types.h"
56 #include "BKE_animsys.h"
57 #include "BKE_context.h"
58 #include "BKE_curve.h"
59 #include "BKE_depsgraph.h"
60 #include "BKE_fcurve.h"
61 #include "BKE_global.h"
62 #include "BKE_gpencil.h"
63 #include "BKE_library.h"
64 #include "BKE_object.h"
65 #include "BKE_report.h"
66 #include "BKE_tracking.h"
68 #include "UI_interface.h"
73 #include "RNA_access.h"
74 #include "RNA_define.h"
76 #include "UI_view2d.h"
78 #include "ED_gpencil.h"
79 #include "ED_view3d.h"
81 #include "ED_keyframing.h"
83 #include "gpencil_intern.h"
85 /* ************************************************ */
86 /* Context Wrangling... */
88 /* Get pointer to active Grease Pencil datablock, and an RNA-pointer to trace back to whatever owns it */
89 bGPdata **gpencil_data_get_pointers(const bContext *C, PointerRNA *ptr)
91 ID *screen_id = (ID *)CTX_wm_screen(C);
92 Scene *scene = CTX_data_scene(C);
93 ScrArea *sa = CTX_wm_area(C);
95 /* if there's an active area, check if the particular editor may
96 * have defined any special Grease Pencil context for editing...
99 switch (sa->spacetype) {
100 case SPACE_VIEW3D: /* 3D-View */
102 Object *ob = CTX_data_active_object(C);
104 /* TODO: we can include other data-types such as bones later if need be... */
106 /* just in case no active object */
108 /* for now, as long as there's an object, default to using that in 3D-View */
109 if (ptr) RNA_id_pointer_create(&ob->id, ptr);
115 case SPACE_NODE: /* Nodes Editor */
117 SpaceNode *snode = (SpaceNode *)CTX_wm_space_data(C);
119 /* return the GP data for the active node block/node */
120 if (snode && snode->nodetree) {
121 /* for now, as long as there's an active node tree, default to using that in the Nodes Editor */
122 if (ptr) RNA_id_pointer_create(&snode->nodetree->id, ptr);
123 return &snode->nodetree->gpd;
126 /* even when there is no node-tree, don't allow this to flow to scene */
132 case SPACE_SEQ: /* Sequencer */
134 SpaceSeq *sseq = (SpaceSeq *)CTX_wm_space_data(C);
136 /* for now, Grease Pencil data is associated with the space (actually preview region only) */
137 /* XXX our convention for everything else is to link to data though... */
138 if (ptr) RNA_pointer_create(screen_id, &RNA_SpaceSequenceEditor, sseq, ptr);
143 case SPACE_IMAGE: /* Image/UV Editor */
145 SpaceImage *sima = (SpaceImage *)CTX_wm_space_data(C);
147 /* for now, Grease Pencil data is associated with the space... */
148 /* XXX our convention for everything else is to link to data though... */
149 if (ptr) RNA_pointer_create(screen_id, &RNA_SpaceImageEditor, sima, ptr);
154 case SPACE_CLIP: /* Nodes Editor */
156 SpaceClip *sc = (SpaceClip *)CTX_wm_space_data(C);
157 MovieClip *clip = ED_space_clip_get_clip(sc);
160 if (sc->gpencil_src == SC_GPENCIL_SRC_TRACK) {
161 MovieTrackingTrack *track = BKE_tracking_track_get_active(&clip->tracking);
167 RNA_pointer_create(&clip->id, &RNA_MovieTrackingTrack, track, ptr);
173 RNA_id_pointer_create(&clip->id, ptr);
181 default: /* unsupported space */
186 /* just fall back on the scene's GP data */
187 if (ptr) RNA_id_pointer_create((ID *)scene, ptr);
188 return (scene) ? &scene->gpd : NULL;
191 /* Get the active Grease Pencil datablock */
192 bGPdata *gpencil_data_get_active(const bContext *C)
194 bGPdata **gpd_ptr = gpencil_data_get_pointers(C, NULL);
195 return (gpd_ptr) ? *(gpd_ptr) : NULL;
198 /* needed for offscreen rendering */
199 bGPdata *gpencil_data_get_active_v3d(Scene *scene)
201 bGPdata *gpd = scene->basact ? scene->basact->object->gpd : NULL;
202 return gpd ? gpd : scene->gpd;
205 /* ************************************************ */
206 /* Panel Operators */
208 /* poll callback for adding data/layers - special */
209 static int gp_add_poll(bContext *C)
211 /* the base line we have is that we have somewhere to add Grease Pencil data */
212 return gpencil_data_get_pointers(C, NULL) != NULL;
215 /* ******************* Add New Data ************************ */
217 /* add new datablock - wrapper around API */
218 static int gp_data_add_exec(bContext *C, wmOperator *op)
220 bGPdata **gpd_ptr = gpencil_data_get_pointers(C, NULL);
222 if (gpd_ptr == NULL) {
223 BKE_report(op->reports, RPT_ERROR, "Nowhere for grease pencil data to go");
224 return OPERATOR_CANCELLED;
227 /* decrement user count and add new datablock */
228 bGPdata *gpd = (*gpd_ptr);
231 *gpd_ptr = gpencil_data_addnew("GPencil");
235 WM_event_add_notifier(C, NC_GPENCIL | ND_DATA | NA_EDITED, NULL);
237 return OPERATOR_FINISHED;
240 void GPENCIL_OT_data_add(wmOperatorType *ot)
243 ot->name = "Grease Pencil Add New";
244 ot->idname = "GPENCIL_OT_data_add";
245 ot->description = "Add new Grease Pencil datablock";
246 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
249 ot->exec = gp_data_add_exec;
250 ot->poll = gp_add_poll;
253 /* ******************* Unlink Data ************************ */
255 /* poll callback for adding data/layers - special */
256 static int gp_data_unlink_poll(bContext *C)
258 bGPdata **gpd_ptr = gpencil_data_get_pointers(C, NULL);
260 /* if we have access to some active data, make sure there's a datablock before enabling this */
261 return (gpd_ptr && *gpd_ptr);
265 /* unlink datablock - wrapper around API */
266 static int gp_data_unlink_exec(bContext *C, wmOperator *op)
268 bGPdata **gpd_ptr = gpencil_data_get_pointers(C, NULL);
270 if (gpd_ptr == NULL) {
271 BKE_report(op->reports, RPT_ERROR, "Nowhere for grease pencil data to go");
272 return OPERATOR_CANCELLED;
275 /* just unlink datablock now, decreasing its user count */
276 bGPdata *gpd = (*gpd_ptr);
283 WM_event_add_notifier(C, NC_GPENCIL | ND_DATA | NA_EDITED, NULL);
285 return OPERATOR_FINISHED;
288 void GPENCIL_OT_data_unlink(wmOperatorType *ot)
291 ot->name = "Grease Pencil Unlink";
292 ot->idname = "GPENCIL_OT_data_unlink";
293 ot->description = "Unlink active Grease Pencil datablock";
294 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
297 ot->exec = gp_data_unlink_exec;
298 ot->poll = gp_data_unlink_poll;
301 /* ******************* Add New Layer ************************ */
303 /* add new layer - wrapper around API */
304 static int gp_layer_add_exec(bContext *C, wmOperator *op)
306 bGPdata **gpd_ptr = gpencil_data_get_pointers(C, NULL);
308 /* if there's no existing Grease-Pencil data there, add some */
309 if (gpd_ptr == NULL) {
310 BKE_report(op->reports, RPT_ERROR, "Nowhere for grease pencil data to go");
311 return OPERATOR_CANCELLED;
313 if (*gpd_ptr == NULL)
314 *gpd_ptr = gpencil_data_addnew("GPencil");
316 /* add new layer now */
317 gpencil_layer_addnew(*gpd_ptr, "GP_Layer", 1);
320 WM_event_add_notifier(C, NC_GPENCIL | ND_DATA | NA_EDITED, NULL);
322 return OPERATOR_FINISHED;
325 void GPENCIL_OT_layer_add(wmOperatorType *ot)
328 ot->name = "Add New Layer";
329 ot->idname = "GPENCIL_OT_layer_add";
330 ot->description = "Add new Grease Pencil layer for the active Grease Pencil datablock";
331 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
334 ot->exec = gp_layer_add_exec;
335 ot->poll = gp_add_poll;
338 /* ******************* Delete Active Frame ************************ */
340 static int gp_actframe_delete_poll(bContext *C)
342 bGPdata *gpd = gpencil_data_get_active(C);
343 bGPDlayer *gpl = gpencil_layer_getactive(gpd);
345 /* only if there's an active layer with an active frame */
346 return (gpl && gpl->actframe);
349 /* delete active frame - wrapper around API calls */
350 static int gp_actframe_delete_exec(bContext *C, wmOperator *op)
352 Scene *scene = CTX_data_scene(C);
353 bGPdata *gpd = gpencil_data_get_active(C);
354 bGPDlayer *gpl = gpencil_layer_getactive(gpd);
355 bGPDframe *gpf = gpencil_layer_getframe(gpl, CFRA, 0);
357 /* if there's no existing Grease-Pencil data there, add some */
359 BKE_report(op->reports, RPT_ERROR, "No grease pencil data");
360 return OPERATOR_CANCELLED;
362 if (ELEM(NULL, gpl, gpf)) {
363 BKE_report(op->reports, RPT_ERROR, "No active frame to delete");
364 return OPERATOR_CANCELLED;
368 gpencil_layer_delframe(gpl, gpf);
371 WM_event_add_notifier(C, NC_GPENCIL | ND_DATA | NA_EDITED, NULL);
373 return OPERATOR_FINISHED;
376 void GPENCIL_OT_active_frame_delete(wmOperatorType *ot)
379 ot->name = "Delete Active Frame";
380 ot->idname = "GPENCIL_OT_active_frame_delete";
381 ot->description = "Delete the active frame for the active Grease Pencil datablock";
382 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
385 ot->exec = gp_actframe_delete_exec;
386 ot->poll = gp_actframe_delete_poll;
389 /* ************************************************ */
390 /* Grease Pencil to Data Operator */
392 /* defines for possible modes */
394 GP_STROKECONVERT_PATH = 1,
395 GP_STROKECONVERT_CURVE,
398 /* Defines for possible timing modes */
400 GP_STROKECONVERT_TIMING_NONE = 1,
401 GP_STROKECONVERT_TIMING_LINEAR = 2,
402 GP_STROKECONVERT_TIMING_FULL = 3,
403 GP_STROKECONVERT_TIMING_CUSTOMGAP = 4,
406 /* RNA enum define */
407 static EnumPropertyItem prop_gpencil_convertmodes[] = {
408 {GP_STROKECONVERT_PATH, "PATH", 0, "Path", ""},
409 {GP_STROKECONVERT_CURVE, "CURVE", 0, "Bezier Curve", ""},
410 {0, NULL, 0, NULL, NULL}
413 static EnumPropertyItem prop_gpencil_convert_timingmodes_restricted[] = {
414 {GP_STROKECONVERT_TIMING_NONE, "NONE", 0, "No Timing", "Ignore timing"},
415 {GP_STROKECONVERT_TIMING_LINEAR, "LINEAR", 0, "Linear", "Simple linear timing"},
416 {0, NULL, 0, NULL, NULL},
419 static EnumPropertyItem prop_gpencil_convert_timingmodes[] = {
420 {GP_STROKECONVERT_TIMING_NONE, "NONE", 0, "No Timing", "Ignore timing"},
421 {GP_STROKECONVERT_TIMING_LINEAR, "LINEAR", 0, "Linear", "Simple linear timing"},
422 {GP_STROKECONVERT_TIMING_FULL, "FULL", 0, "Original", "Use the original timing, gaps included"},
423 {GP_STROKECONVERT_TIMING_CUSTOMGAP, "CUSTOMGAP", 0, "Custom Gaps",
424 "Use the original timing, but with custom gap lengths (in frames)"},
425 {0, NULL, 0, NULL, NULL},
428 static EnumPropertyItem *rna_GPConvert_mode_items(bContext *UNUSED(C), PointerRNA *ptr, PropertyRNA *UNUSED(prop),
432 if (RNA_boolean_get(ptr, "use_timing_data")) {
433 return prop_gpencil_convert_timingmodes;
435 return prop_gpencil_convert_timingmodes_restricted;
440 /* convert the coordinates from the given stroke point into 3d-coordinates
441 * - assumes that the active space is the 3D-View
443 static void gp_strokepoint_convertcoords(bContext *C, bGPDstroke *gps, bGPDspoint *pt, float p3d[3], rctf *subrect)
445 Scene *scene = CTX_data_scene(C);
446 View3D *v3d = CTX_wm_view3d(C);
447 ARegion *ar = CTX_wm_region(C);
449 if (gps->flag & GP_STROKE_3DSPACE) {
450 /* directly use 3d-coordinates */
451 copy_v3_v3(p3d, &pt->x);
454 float *fp = give_cursor(scene, v3d);
457 /* get screen coordinate */
458 if (gps->flag & GP_STROKE_2DSPACE) {
460 View2D *v2d = &ar->v2d;
461 UI_view2d_view_to_region(v2d, pt->x, pt->y, mvali, mvali + 1);
462 VECCOPY2D(mvalf, mvali);
466 mvalf[0] = (((float)pt->x / 100.0f) * BLI_rctf_size_x(subrect)) + subrect->xmin;
467 mvalf[1] = (((float)pt->y / 100.0f) * BLI_rctf_size_y(subrect)) + subrect->ymin;
470 mvalf[0] = (float)pt->x / 100.0f * ar->winx;
471 mvalf[1] = (float)pt->y / 100.0f * ar->winy;
475 /* convert screen coordinate to 3d coordinates
476 * - method taken from editview.c - mouse_cursor()
478 ED_view3d_win_to_3d(ar, fp, mvalf, p3d);
484 /* temp struct for gp_stroke_path_animation() */
485 typedef struct tGpTimingData {
486 /* Data set from operator settings */
488 int frame_range; /* Number of frames evaluated for path animation */
489 int start_frame, end_frame;
490 int realtime; /* A bool, actually, will overwrite end_frame in case of Original or CustomGap timing... */
491 float gap_duration, gap_randomness; /* To be used with CustomGap mode*/
494 /* Data set from points, used to compute final timing FCurve */
495 int num_points, cur_point;
502 float *times; /* Note: Gap times will be negative! */
503 float tot_time, gap_tot_time;
507 /* init point buffers for timing data */
508 static void _gp_timing_data_set_nbr(tGpTimingData *gtd, int nbr)
512 BLI_assert(nbr > gtd->num_points);
516 gtd->dists = MEM_callocN(sizeof(float) * nbr, __func__);
518 memcpy(gtd->dists, tmp, sizeof(float) * gtd->num_points);
524 gtd->times = MEM_callocN(sizeof(float) * nbr, __func__);
526 memcpy(gtd->times, tmp, sizeof(float) * gtd->num_points);
530 gtd->num_points = nbr;
533 /* add stroke point to timing buffers */
534 static void gp_timing_data_add_point(tGpTimingData *gtd, double stroke_inittime, float time, float delta_dist)
537 /* This is a gap, negative value! */
538 gtd->tot_time = -(gtd->times[gtd->cur_point] = -(((float)(stroke_inittime - gtd->inittime)) + time));
539 gtd->gap_tot_time += gtd->times[gtd->cur_point] - gtd->times[gtd->cur_point - 1];
542 gtd->tot_time = (gtd->times[gtd->cur_point] = (((float)(stroke_inittime - gtd->inittime)) + time));
545 gtd->tot_dist += delta_dist;
546 gtd->dists[gtd->cur_point] = gtd->tot_dist;
550 /* In frames! Binary search for FCurve keys have a threshold of 0.01, so we can't set
551 * arbitrarily close points - this is esp. important with NoGaps mode!
553 #define MIN_TIME_DELTA 0.02f
555 /* Loop over next points to find the end of the stroke, and compute */
556 static int gp_find_end_of_stroke_idx(tGpTimingData *gtd, int idx, int nbr_gaps, int *nbr_done_gaps,
557 float tot_gaps_time, float delta_time, float *next_delta_time)
561 for (j = idx + 1; j < gtd->num_points; j++) {
562 if (gtd->times[j] < 0) {
563 gtd->times[j] = -gtd->times[j];
564 if (gtd->mode == GP_STROKECONVERT_TIMING_CUSTOMGAP) {
565 /* In this mode, gap time between this stroke and the next should be 0 currently...
566 * So we have to compute its final duration!
568 if (gtd->gap_randomness > 0.0f) {
569 /* We want gaps that are in gtd->gap_duration +/- gtd->gap_randomness range,
570 * and which sum to exactly tot_gaps_time...
572 int rem_gaps = nbr_gaps - *nbr_done_gaps;
574 /* Last gap, just give remaining time! */
575 *next_delta_time = tot_gaps_time;
578 float delta, min, max;
579 /* This code ensures that if the first gaps have been shorter than average gap_duration,
580 * next gaps will tend to be longer (i.e. try to recover the lateness), and vice-versa!
582 delta = delta_time - (gtd->gap_duration * (*nbr_done_gaps));
584 /* Clamp min between [-gap_randomness, 0.0], with lower delta giving higher min */
585 min = -gtd->gap_randomness - delta;
586 CLAMP(min, -gtd->gap_randomness, 0.0f);
588 /* Clamp max between [0.0, gap_randomness], with lower delta giving higher max */
589 max = gtd->gap_randomness - delta;
590 CLAMP(max, 0.0f, gtd->gap_randomness);
591 *next_delta_time += gtd->gap_duration + (BLI_frand() * (max - min)) + min;
595 *next_delta_time += gtd->gap_duration;
606 static void gp_stroke_path_animation_preprocess_gaps(tGpTimingData *gtd, int *nbr_gaps, float *tot_gaps_time)
609 float delta_time = 0.0f;
611 for (i = 0; i < gtd->num_points; i++) {
612 if (gtd->times[i] < 0 && i) {
614 gtd->times[i] = -gtd->times[i] - delta_time;
615 delta_time += gtd->times[i] - gtd->times[i - 1];
616 gtd->times[i] = -gtd->times[i - 1]; /* Temp marker, values *have* to be different! */
619 gtd->times[i] -= delta_time;
622 gtd->tot_time -= delta_time;
624 *tot_gaps_time = (float)(*nbr_gaps) * gtd->gap_duration;
625 gtd->tot_time += *tot_gaps_time;
626 if (G.debug & G_DEBUG) {
627 printf("%f, %f, %f, %d\n", gtd->tot_time, delta_time, *tot_gaps_time, *nbr_gaps);
629 if (gtd->gap_randomness > 0.0f) {
630 BLI_srandom(gtd->seed);
634 static void gp_stroke_path_animation_add_keyframes(ReportList *reports, PointerRNA ptr, PropertyRNA *prop, FCurve *fcu,
635 Curve *cu, tGpTimingData *gtd, float time_range,
636 int nbr_gaps, float tot_gaps_time)
638 /* Use actual recorded timing! */
639 float time_start = (float)gtd->start_frame;
641 float last_valid_time = 0.0f;
642 int end_stroke_idx = -1, start_stroke_idx = 0;
643 float end_stroke_time = 0.0f;
645 /* CustomGaps specific */
646 float delta_time = 0.0f, next_delta_time = 0.0f;
647 int nbr_done_gaps = 0;
652 /* This is a bit tricky, as:
653 * - We can't add arbitrarily close points on FCurve (in time).
654 * - We *must* have all "caps" points of all strokes in FCurve, as much as possible!
656 for (i = 0; i < gtd->num_points; i++) {
657 /* If new stroke... */
658 if (i > end_stroke_idx) {
659 start_stroke_idx = i;
660 delta_time = next_delta_time;
661 /* find end of that new stroke */
662 end_stroke_idx = gp_find_end_of_stroke_idx(gtd, i, nbr_gaps, &nbr_done_gaps,
663 tot_gaps_time, delta_time, &next_delta_time);
664 /* This one should *never* be negative! */
665 end_stroke_time = time_start + ((gtd->times[end_stroke_idx] + delta_time) / gtd->tot_time * time_range);
668 /* Simple proportional stuff... */
669 cu->ctime = gtd->dists[i] / gtd->tot_dist * cu->pathlen;
670 cfra = time_start + ((gtd->times[i] + delta_time) / gtd->tot_time * time_range);
672 /* And now, the checks about timing... */
673 if (i == start_stroke_idx) {
674 /* If first point of a stroke, be sure it's enough ahead of last valid keyframe, and
675 * that the end point of the stroke is far enough!
676 * In case it is not, we keep the end point...
677 * Note that with CustomGaps mode, this is here we set the actual gap timing!
679 if ((end_stroke_time - last_valid_time) > MIN_TIME_DELTA * 2) {
680 if ((cfra - last_valid_time) < MIN_TIME_DELTA) {
681 cfra = last_valid_time + MIN_TIME_DELTA;
683 insert_keyframe_direct(reports, ptr, prop, fcu, cfra, INSERTKEY_FAST);
684 last_valid_time = cfra;
686 else if (G.debug & G_DEBUG) {
687 printf("\t Skipping start point %d, too close from end point %d\n", i, end_stroke_idx);
690 else if (i == end_stroke_idx) {
691 /* Always try to insert end point of a curve (should be safe enough, anyway...) */
692 if ((cfra - last_valid_time) < MIN_TIME_DELTA) {
693 cfra = last_valid_time + MIN_TIME_DELTA;
695 insert_keyframe_direct(reports, ptr, prop, fcu, cfra, INSERTKEY_FAST);
696 last_valid_time = cfra;
699 /* Else ("middle" point), we only insert it if it's far enough from last keyframe,
700 * and also far enough from (not yet added!) end_stroke keyframe!
702 if ((cfra - last_valid_time) > MIN_TIME_DELTA && (end_stroke_time - cfra) > MIN_TIME_DELTA) {
703 insert_keyframe_direct(reports, ptr, prop, fcu, cfra, INSERTKEY_FAST);
704 last_valid_time = cfra;
706 else if (G.debug & G_DEBUG) {
707 printf("\t Skipping \"middle\" point %d, too close from last added point or end point %d\n",
714 static void gp_stroke_path_animation(bContext *C, ReportList *reports, Curve *cu, tGpTimingData *gtd)
716 Scene *scene = CTX_data_scene(C);
720 PropertyRNA *prop = NULL;
723 if (gtd->mode == GP_STROKECONVERT_TIMING_NONE)
726 /* gap_duration and gap_randomness are in frames, but we need seconds!!! */
727 gtd->gap_duration = FRA2TIME(gtd->gap_duration);
728 gtd->gap_randomness = FRA2TIME(gtd->gap_randomness);
732 cu->pathlen = gtd->frame_range;
734 /* Get RNA pointer to read/write path time values */
735 RNA_id_pointer_create((ID *)cu, &ptr);
736 prop = RNA_struct_find_property(&ptr, "eval_time");
738 /* Ensure we have an F-Curve to add keyframes to */
739 act = verify_adt_action((ID *)cu, TRUE);
740 fcu = verify_fcurve(act, NULL, &ptr, "eval_time", 0, TRUE);
742 if (G.debug & G_DEBUG) {
743 printf("%s: tot len: %f\t\ttot time: %f\n", __func__, gtd->tot_dist, gtd->tot_time);
744 for (i = 0; i < gtd->num_points; i++) {
745 printf("\tpoint %d:\t\tlen: %f\t\ttime: %f\n", i, gtd->dists[i], gtd->times[i]);
749 if (gtd->mode == GP_STROKECONVERT_TIMING_LINEAR) {
752 /* Linear extrapolation! */
753 fcu->extend = FCURVE_EXTRAPOLATE_LINEAR;
756 cfra = (float)gtd->start_frame;
757 insert_keyframe_direct(reports, ptr, prop, fcu, cfra, INSERTKEY_FAST);
759 cu->ctime = cu->pathlen;
761 cfra += (float)TIME2FRA(gtd->tot_time); /* Seconds to frames */
764 cfra = (float)gtd->end_frame;
766 insert_keyframe_direct(reports, ptr, prop, fcu, cfra, INSERTKEY_FAST);
769 /* Use actual recorded timing! */
772 /* CustomGaps specific */
773 float tot_gaps_time = 0.0f;
775 /* Pre-process gaps, in case we don't want to keep their original timing */
776 if (gtd->mode == GP_STROKECONVERT_TIMING_CUSTOMGAP) {
777 gp_stroke_path_animation_preprocess_gaps(gtd, &nbr_gaps, &tot_gaps_time);
781 time_range = (float)TIME2FRA(gtd->tot_time); /* Seconds to frames */
784 time_range = (float)(gtd->end_frame - gtd->start_frame);
787 if (G.debug & G_DEBUG) {
788 printf("GP Stroke Path Conversion: Starting keying!\n");
791 gp_stroke_path_animation_add_keyframes(reports, ptr, prop, fcu, cu, gtd, time_range,
792 nbr_gaps, tot_gaps_time);
795 /* As we used INSERTKEY_FAST mode, we need to recompute all curve's handles now */
796 calchandles_fcurve(fcu);
798 if (G.debug & G_DEBUG) {
799 printf("%s: \ntot len: %f\t\ttot time: %f\n", __func__, gtd->tot_dist, gtd->tot_time);
800 for (i = 0; i < gtd->num_points; i++) {
801 printf("\tpoint %d:\t\tlen: %f\t\ttime: %f\n", i, gtd->dists[i], gtd->times[i]);
806 WM_event_add_notifier(C, NC_ANIMATION | ND_KEYFRAME | NA_EDITED, NULL);
809 DAG_id_tag_update(&cu->id, 0);
812 #undef MIN_TIME_DELTA
814 #define GAP_DFAC 0.05f
815 #define WIDTH_CORR_FAC 0.1f
816 #define BEZT_HANDLE_FAC 0.3f
818 /* convert stroke to 3d path */
819 static void gp_stroke_to_path(bContext *C, bGPDlayer *gpl, bGPDstroke *gps, Curve *cu, rctf *subrect, Nurb **curnu,
820 float minmax_weights[2], float rad_fac, int stitch, tGpTimingData *gtd)
823 Nurb *nu = (curnu) ? *curnu : NULL;
824 BPoint *bp, *prev_bp = NULL;
825 const int do_gtd = (gtd->mode != GP_STROKECONVERT_TIMING_NONE);
828 /* create new 'nurb' or extend current one within the curve */
832 /* If stitch, the first point of this stroke is already present in current nu.
833 * Else, we have to add to additional points to make the zero-radius link between strokes.
835 BKE_nurb_points_add(nu, gps->totpoints + (stitch ? -1 : 2));
838 nu = (Nurb *)MEM_callocN(sizeof(Nurb), "gpstroke_to_path(nurb)");
840 nu->pntsu = gps->totpoints;
842 nu->orderu = 2; /* point-to-point! */
844 nu->flagu = CU_NURB_ENDPOINT;
845 nu->resolu = cu->resolu;
846 nu->resolv = cu->resolv;
849 nu->bp = (BPoint *)MEM_callocN(sizeof(BPoint) * nu->pntsu, "bpoints");
851 stitch = FALSE; /* Security! */
855 _gp_timing_data_set_nbr(gtd, nu->pntsu);
858 /* If needed, make the link between both strokes with two zero-radius additional points */
859 /* About "zero-radius" point interpolations:
860 * - If we have at least two points in current curve (most common case), we linearly extrapolate
861 * the last segment to get the first point (p1) position and timing.
862 * - If we do not have those (quite odd, but may happen), we linearly interpolate the last point
863 * with the first point of the current stroke.
864 * The same goes for the second point, first segment of the current stroke is "negatively" extrapolated
865 * if it exists, else (if the stroke is a single point), linear interpolation with last curve point...
867 if (curnu && !stitch && old_nbp) {
868 float p1[3], p2[3], p[3], next_p[3];
872 if ((old_nbp > 1) && gps->prev && (gps->prev->totpoints > 1)) {
873 /* Only use last curve segment if previous stroke was not a single-point one! */
874 prev_bp = nu->bp + old_nbp - 2;
876 bp = nu->bp + old_nbp - 1;
878 /* XXX We do this twice... Not sure it's worth to bother about this! */
879 gp_strokepoint_convertcoords(C, gps, gps->points, p, subrect);
881 interp_v3_v3v3(p1, prev_bp->vec, bp->vec, 1.0f + GAP_DFAC);
884 interp_v3_v3v3(p1, bp->vec, p, GAP_DFAC);
887 if (gps->totpoints > 1) {
888 /* XXX We do this twice... Not sure it's worth to bother about this! */
889 gp_strokepoint_convertcoords(C, gps, gps->points + 1, next_p, subrect);
890 interp_v3_v3v3(p2, p, next_p, -GAP_DFAC);
893 interp_v3_v3v3(p2, p, bp->vec, GAP_DFAC);
898 copy_v3_v3(bp->vec, p1);
901 minmax_weights[0] = bp->radius = bp->weight = 0.0f;
904 delta_time = gtd->tot_time + (gtd->tot_time - gtd->times[gtd->cur_point - 1]) * GAP_DFAC;
907 delta_time = gtd->tot_time + (((float)(gps->inittime - gtd->inittime)) - gtd->tot_time) * GAP_DFAC;
909 gp_timing_data_add_point(gtd, gtd->inittime, delta_time, len_v3v3((bp - 1)->vec, p1));
914 copy_v3_v3(bp->vec, p2);
917 minmax_weights[0] = bp->radius = bp->weight = 0.0f;
919 /* This negative delta_time marks the gap! */
920 if (gps->totpoints > 1) {
921 delta_time = ((gps->points + 1)->time - gps->points->time) * -GAP_DFAC;
924 delta_time = -(((float)(gps->inittime - gtd->inittime)) - gtd->tot_time) * GAP_DFAC;
926 gp_timing_data_add_point(gtd, gps->inittime, delta_time, len_v3v3(p1, p2));
931 if (old_nbp && do_gtd) {
932 prev_bp = nu->bp + old_nbp - 1;
936 for (i = (stitch) ? 1 : 0, pt = gps->points + ((stitch) ? 1 : 0), bp = nu->bp + old_nbp;
941 float width = pt->pressure * gpl->thickness * WIDTH_CORR_FAC;
943 /* get coordinates to add at */
944 gp_strokepoint_convertcoords(C, gps, pt, p3d, subrect);
945 copy_v3_v3(bp->vec, p3d);
950 bp->radius = width * rad_fac;
952 CLAMP(bp->weight, 0.0f, 1.0f);
953 if (bp->weight < minmax_weights[0]) {
954 minmax_weights[0] = bp->weight;
956 else if (bp->weight > minmax_weights[1]) {
957 minmax_weights[1] = bp->weight;
960 /* Update timing data */
962 gp_timing_data_add_point(gtd, gps->inittime, pt->time, (prev_bp) ? len_v3v3(prev_bp->vec, p3d) : 0.0f);
967 /* add nurb to curve */
968 if (!curnu || !*curnu) {
969 BLI_addtail(&cu->nurb, nu);
975 BKE_nurb_knot_calc_u(nu);
978 static int gp_camera_view_subrect(bContext *C, rctf *subrect)
980 View3D *v3d = CTX_wm_view3d(C);
981 ARegion *ar = CTX_wm_region(C);
984 RegionView3D *rv3d = ar->regiondata;
986 /* for camera view set the subrect */
987 if (rv3d->persp == RV3D_CAMOB) {
988 Scene *scene = CTX_data_scene(C);
989 ED_view3d_calc_camera_border(scene, ar, v3d, rv3d, subrect, TRUE); /* no shift */
997 /* convert stroke to 3d bezier */
998 static void gp_stroke_to_bezier(bContext *C, bGPDlayer *gpl, bGPDstroke *gps, Curve *cu, rctf *subrect, Nurb **curnu,
999 float minmax_weights[2], float rad_fac, int stitch, tGpTimingData *gtd)
1002 Nurb *nu = (curnu) ? *curnu : NULL;
1003 BezTriple *bezt, *prev_bezt = NULL;
1004 int i, tot, old_nbezt = 0;
1005 float p3d_cur[3], p3d_prev[3], p3d_next[3];
1006 const int do_gtd = (gtd->mode != GP_STROKECONVERT_TIMING_NONE);
1008 /* create new 'nurb' or extend current one within the curve */
1010 old_nbezt = nu->pntsu;
1011 /* If we do stitch, first point of current stroke is assumed the same as last point of previous stroke,
1012 * so no need to add it.
1013 * If no stitch, we want to add two additional points to make a "zero-radius" link between both strokes.
1015 BKE_nurb_bezierPoints_add(nu, gps->totpoints + ((stitch) ? -1 : 2));
1018 nu = (Nurb *)MEM_callocN(sizeof(Nurb), "gpstroke_to_bezier(nurb)");
1020 nu->pntsu = gps->totpoints;
1023 nu->type = CU_BEZIER;
1024 nu->bezt = (BezTriple *)MEM_callocN(gps->totpoints * sizeof(BezTriple), "bezts");
1026 stitch = FALSE; /* Security! */
1030 _gp_timing_data_set_nbr(gtd, nu->pntsu);
1033 tot = gps->totpoints;
1035 /* get initial coordinates */
1038 gp_strokepoint_convertcoords(C, gps, pt, (stitch) ? p3d_prev : p3d_cur, subrect);
1040 gp_strokepoint_convertcoords(C, gps, pt + 1, (stitch) ? p3d_cur : p3d_next, subrect);
1042 if (stitch && tot > 2) {
1043 gp_strokepoint_convertcoords(C, gps, pt + 2, p3d_next, subrect);
1047 /* If needed, make the link between both strokes with two zero-radius additional points */
1048 if (curnu && old_nbezt) {
1049 /* Update last point's second handle */
1052 bezt = nu->bezt + old_nbezt - 1;
1053 interp_v3_v3v3(h2, bezt->vec[1], p3d_cur, BEZT_HANDLE_FAC);
1054 copy_v3_v3(bezt->vec[2], h2);
1058 /* Create "link points" */
1059 /* About "zero-radius" point interpolations:
1060 * - If we have at least two points in current curve (most common case), we linearly extrapolate
1061 * the last segment to get the first point (p1) position and timing.
1062 * - If we do not have those (quite odd, but may happen), we linearly interpolate the last point
1063 * with the first point of the current stroke.
1064 * The same goes for the second point, first segment of the current stroke is "negatively" extrapolated
1065 * if it exists, else (if the stroke is a single point), linear interpolation with last curve point...
1068 float h1[3], h2[3], p1[3], p2[3];
1072 if (old_nbezt > 1 && gps->prev && gps->prev->totpoints > 1) {
1073 /* Only use last curve segment if previous stroke was not a single-point one! */
1074 prev_bezt = nu->bezt + old_nbezt - 2;
1076 bezt = nu->bezt + old_nbezt - 1;
1078 interp_v3_v3v3(p1, prev_bezt->vec[1], bezt->vec[1], 1.0f + GAP_DFAC);
1081 interp_v3_v3v3(p1, bezt->vec[1], p3d_cur, GAP_DFAC);
1084 interp_v3_v3v3(p2, p3d_cur, p3d_next, -GAP_DFAC);
1087 interp_v3_v3v3(p2, p3d_cur, bezt->vec[1], GAP_DFAC);
1090 /* Second handle of last point */
1091 interp_v3_v3v3(h2, bezt->vec[1], p1, BEZT_HANDLE_FAC);
1092 copy_v3_v3(bezt->vec[2], h2);
1095 interp_v3_v3v3(h1, p1, bezt->vec[1], BEZT_HANDLE_FAC);
1096 interp_v3_v3v3(h2, p1, p2, BEZT_HANDLE_FAC);
1099 copy_v3_v3(bezt->vec[0], h1);
1100 copy_v3_v3(bezt->vec[1], p1);
1101 copy_v3_v3(bezt->vec[2], h2);
1102 bezt->h1 = bezt->h2 = HD_FREE;
1103 bezt->f1 = bezt->f2 = bezt->f3 = SELECT;
1104 minmax_weights[0] = bezt->radius = bezt->weight = 0.0f;
1108 delta_time = gtd->tot_time + (gtd->tot_time - gtd->times[gtd->cur_point - 1]) * GAP_DFAC;
1111 delta_time = gtd->tot_time + (((float)(gps->inittime - gtd->inittime)) - gtd->tot_time) * GAP_DFAC;
1113 gp_timing_data_add_point(gtd, gtd->inittime, delta_time, len_v3v3((bezt - 1)->vec[1], p1));
1117 interp_v3_v3v3(h1, p2, p1, BEZT_HANDLE_FAC);
1118 interp_v3_v3v3(h2, p2, p3d_cur, BEZT_HANDLE_FAC);
1121 copy_v3_v3(bezt->vec[0], h1);
1122 copy_v3_v3(bezt->vec[1], p2);
1123 copy_v3_v3(bezt->vec[2], h2);
1124 bezt->h1 = bezt->h2 = HD_FREE;
1125 bezt->f1 = bezt->f2 = bezt->f3 = SELECT;
1126 minmax_weights[0] = bezt->radius = bezt->weight = 0.0f;
1129 /* This negative delta_time marks the gap! */
1131 delta_time = ((gps->points + 1)->time - gps->points->time) * -GAP_DFAC;
1134 delta_time = -(((float)(gps->inittime - gtd->inittime)) - gtd->tot_time) * GAP_DFAC;
1136 gp_timing_data_add_point(gtd, gps->inittime, delta_time, len_v3v3(p1, p2));
1140 copy_v3_v3(p3d_prev, p2);
1143 if (old_nbezt && do_gtd) {
1144 prev_bezt = nu->bezt + old_nbezt - 1;
1148 for (i = stitch ? 1 : 0, bezt = nu->bezt + old_nbezt; i < tot; i++, pt++, bezt++) {
1150 float width = pt->pressure * gpl->thickness * WIDTH_CORR_FAC;
1152 if (i || old_nbezt) {
1153 interp_v3_v3v3(h1, p3d_cur, p3d_prev, BEZT_HANDLE_FAC);
1156 interp_v3_v3v3(h1, p3d_cur, p3d_next, -BEZT_HANDLE_FAC);
1160 interp_v3_v3v3(h2, p3d_cur, p3d_next, BEZT_HANDLE_FAC);
1163 interp_v3_v3v3(h2, p3d_cur, p3d_prev, -BEZT_HANDLE_FAC);
1166 copy_v3_v3(bezt->vec[0], h1);
1167 copy_v3_v3(bezt->vec[1], p3d_cur);
1168 copy_v3_v3(bezt->vec[2], h2);
1171 bezt->h1 = bezt->h2 = HD_FREE;
1172 bezt->f1 = bezt->f2 = bezt->f3 = SELECT;
1173 bezt->radius = width * rad_fac;
1174 bezt->weight = width;
1175 CLAMP(bezt->weight, 0.0f, 1.0f);
1176 if (bezt->weight < minmax_weights[0]) {
1177 minmax_weights[0] = bezt->weight;
1179 else if (bezt->weight > minmax_weights[1]) {
1180 minmax_weights[1] = bezt->weight;
1183 /* Update timing data */
1185 gp_timing_data_add_point(gtd, gps->inittime, pt->time, prev_bezt ? len_v3v3(prev_bezt->vec[1], p3d_cur) : 0.0f);
1188 /* shift coord vects */
1189 copy_v3_v3(p3d_prev, p3d_cur);
1190 copy_v3_v3(p3d_cur, p3d_next);
1193 gp_strokepoint_convertcoords(C, gps, pt + 2, p3d_next, subrect);
1199 /* must calculate handles or else we crash */
1200 BKE_nurb_handles_calc(nu);
1202 if (!curnu || !*curnu) {
1203 BLI_addtail(&cu->nurb, nu);
1211 #undef WIDTH_CORR_FAC
1212 #undef BEZT_HANDLE_FAC
1214 static void gp_stroke_finalize_curve_endpoints(Curve *cu)
1217 Nurb *nu = cu->nurb.first;
1220 BezTriple *bezt = nu->bezt;
1222 bezt[i].weight = bezt[i].radius = 0.0f;
1226 BPoint *bp = nu->bp;
1228 bp[i].weight = bp[i].radius = 0.0f;
1236 BezTriple *bezt = nu->bezt;
1238 bezt[i].weight = bezt[i].radius = 0.0f;
1242 BPoint *bp = nu->bp;
1244 bp[i].weight = bp[i].radius = 0.0f;
1249 static void gp_stroke_norm_curve_weights(Curve *cu, float minmax_weights[2])
1252 const float delta = minmax_weights[0];
1253 const float fac = 1.0f / (minmax_weights[1] - delta);
1256 for (nu = cu->nurb.first; nu; nu = nu->next) {
1258 BezTriple *bezt = nu->bezt;
1259 for (i = 0; i < nu->pntsu; i++, bezt++) {
1260 bezt->weight = (bezt->weight - delta) * fac;
1264 BPoint *bp = nu->bp;
1265 for (i = 0; i < nu->pntsu; i++, bp++) {
1266 bp->weight = (bp->weight - delta) * fac;
1272 /* convert a given grease-pencil layer to a 3d-curve representation (using current view if appropriate) */
1273 static void gp_layer_to_curve(bContext *C, ReportList *reports, bGPdata *gpd, bGPDlayer *gpl, int mode,
1274 int norm_weights, float rad_fac, int link_strokes, tGpTimingData *gtd)
1276 Scene *scene = CTX_data_scene(C);
1277 bGPDframe *gpf = gpencil_layer_getframe(gpl, CFRA, 0);
1278 bGPDstroke *gps, *prev_gps = NULL;
1282 Base *base = BASACT, *newbase = NULL;
1283 float minmax_weights[2] = {1.0f, 0.0f};
1285 /* camera framing */
1286 rctf subrect, *subrect_ptr = NULL;
1288 /* error checking */
1289 if (ELEM3(NULL, gpd, gpl, gpf))
1292 /* only convert if there are any strokes on this layer's frame to convert */
1293 if (gpf->strokes.first == NULL)
1296 /* initialize camera framing */
1297 if (gp_camera_view_subrect(C, &subrect)) {
1298 subrect_ptr = &subrect;
1301 /* init the curve object (remove rotation and get curve data from it)
1302 * - must clear transforms set on object, as those skew our results
1304 ob = BKE_object_add(scene, OB_CURVE);
1310 /* rename object and curve to layer name */
1311 rename_id((ID *)ob, gpl->info);
1312 rename_id((ID *)cu, gpl->info);
1314 gtd->inittime = ((bGPDstroke *)gpf->strokes.first)->inittime;
1316 /* add points to curve */
1317 for (gps = gpf->strokes.first; gps; gps = gps->next) {
1318 /* Detect new strokes created because of GP_STROKE_BUFFER_MAX reached,
1319 * and stitch them to previous one.
1324 bGPDspoint *pt1 = prev_gps->points + prev_gps->totpoints - 1;
1325 bGPDspoint *pt2 = gps->points;
1327 if ((pt1->x == pt2->x) && (pt1->y == pt2->y)) {
1332 /* Decide whether we connect this stroke to previous one */
1333 if (!(stitch || link_strokes)) {
1338 case GP_STROKECONVERT_PATH:
1339 gp_stroke_to_path(C, gpl, gps, cu, subrect_ptr, &nu, minmax_weights, rad_fac, stitch, gtd);
1341 case GP_STROKECONVERT_CURVE:
1342 gp_stroke_to_bezier(C, gpl, gps, cu, subrect_ptr, &nu, minmax_weights, rad_fac, stitch, gtd);
1345 BLI_assert(!"invalid mode");
1351 /* If link_strokes, be sure first and last points have a zero weight/size! */
1353 gp_stroke_finalize_curve_endpoints(cu);
1355 /* Update curve's weights, if needed */
1356 if (norm_weights && ((minmax_weights[0] > 0.0f) || (minmax_weights[1] < 1.0f)))
1357 gp_stroke_norm_curve_weights(cu, minmax_weights);
1359 /* Create the path animation, if needed */
1360 gp_stroke_path_animation(C, reports, cu, gtd);
1362 /* Reset original object as active, else we can't edit operator's settings!!! */
1366 newbase->lay = base->lay;
1367 ob->lay = newbase->lay;
1370 /* restore, BKE_object_add sets active */
1373 base->flag |= SELECT;
1379 /* Check a GP layer has valid timing data! Else, most timing options are hidden in the operator.
1382 static int gp_convert_check_has_valid_timing(bContext *C, bGPDlayer *gpl, wmOperator *op)
1384 Scene *scene = CTX_data_scene(C);
1385 bGPDframe *gpf = gpencil_layer_getframe(gpl, CFRA, 0);
1386 bGPDstroke *gps = gpf->strokes.first;
1388 double base_time, cur_time, prev_time = -1.0;
1389 int i, valid = TRUE;
1392 base_time = cur_time = gps->inittime;
1393 if (cur_time <= prev_time) {
1398 prev_time = cur_time;
1399 for (i = 0, pt = gps->points; i < gps->totpoints; i++, pt++) {
1400 cur_time = base_time + (double)pt->time;
1401 /* First point of a stroke should have the same time as stroke's inittime,
1402 * so it's the only case where equality is allowed!
1404 if ((i && cur_time <= prev_time) || (cur_time < prev_time)) {
1408 prev_time = cur_time;
1414 } while ((gps = gps->next));
1417 RNA_boolean_set(op->ptr, "use_timing_data", valid);
1422 /* Check end_frame is always > start frame! */
1423 static void gp_convert_set_end_frame(struct Main *UNUSED(main), struct Scene *UNUSED(scene), struct PointerRNA *ptr)
1425 int start_frame = RNA_int_get(ptr, "start_frame");
1426 int end_frame = RNA_int_get(ptr, "end_frame");
1428 if (end_frame <= start_frame) {
1429 RNA_int_set(ptr, "end_frame", start_frame + 1);
1433 static int gp_convert_poll(bContext *C)
1435 bGPdata *gpd = gpencil_data_get_active(C);
1436 ScrArea *sa = CTX_wm_area(C);
1437 Scene *scene = CTX_data_scene(C);
1439 /* only if there's valid data, and the current view is 3D View */
1440 return ((sa && sa->spacetype == SPACE_VIEW3D) && gpencil_layer_getactive(gpd) && (scene->obedit == NULL));
1443 static int gp_convert_layer_exec(bContext *C, wmOperator *op)
1445 PropertyRNA *prop = RNA_struct_find_property(op->ptr, "use_timing_data");
1446 bGPdata *gpd = gpencil_data_get_active(C);
1447 bGPDlayer *gpl = gpencil_layer_getactive(gpd);
1448 Scene *scene = CTX_data_scene(C);
1449 int mode = RNA_enum_get(op->ptr, "type");
1450 int norm_weights = RNA_boolean_get(op->ptr, "use_normalize_weights");
1451 float rad_fac = RNA_float_get(op->ptr, "radius_multiplier");
1452 int link_strokes = RNA_boolean_get(op->ptr, "use_link_strokes");
1456 /* check if there's data to work with */
1458 BKE_report(op->reports, RPT_ERROR, "No Grease Pencil data to work on");
1459 return OPERATOR_CANCELLED;
1462 if (!RNA_property_is_set(op->ptr, prop) && !gp_convert_check_has_valid_timing(C, gpl, op)) {
1463 BKE_report(op->reports, RPT_WARNING,
1464 "Current Grease Pencil strokes have no valid timing data, most timing options will be hidden!");
1466 valid_timing = RNA_property_boolean_get(op->ptr, prop);
1468 gtd.mode = RNA_enum_get(op->ptr, "timing_mode");
1469 /* Check for illegal timing mode! */
1470 if (!valid_timing && !ELEM(gtd.mode, GP_STROKECONVERT_TIMING_NONE, GP_STROKECONVERT_TIMING_LINEAR)) {
1471 gtd.mode = GP_STROKECONVERT_TIMING_LINEAR;
1472 RNA_enum_set(op->ptr, "timing_mode", gtd.mode);
1474 if (!link_strokes) {
1475 gtd.mode = GP_STROKECONVERT_TIMING_NONE;
1478 /* grab all relevant settings */
1479 gtd.frame_range = RNA_int_get(op->ptr, "frame_range");
1480 gtd.start_frame = RNA_int_get(op->ptr, "start_frame");
1481 gtd.realtime = valid_timing ? RNA_boolean_get(op->ptr, "use_realtime") : FALSE;
1482 gtd.end_frame = RNA_int_get(op->ptr, "end_frame");
1483 gtd.gap_duration = RNA_float_get(op->ptr, "gap_duration");
1484 gtd.gap_randomness = RNA_float_get(op->ptr, "gap_randomness");
1485 gtd.gap_randomness = min_ff(gtd.gap_randomness, gtd.gap_duration);
1486 gtd.seed = RNA_int_get(op->ptr, "seed");
1487 gtd.num_points = gtd.cur_point = 0;
1488 gtd.dists = gtd.times = NULL;
1489 gtd.tot_dist = gtd.tot_time = gtd.gap_tot_time = 0.0f;
1492 /* perform conversion */
1493 gp_layer_to_curve(C, op->reports, gpd, gpl, mode, norm_weights, rad_fac, link_strokes, >d);
1495 /* free temp memory */
1497 MEM_freeN(gtd.dists);
1501 MEM_freeN(gtd.times);
1506 WM_event_add_notifier(C, NC_OBJECT | NA_ADDED, NULL);
1507 WM_event_add_notifier(C, NC_SCENE | ND_OB_ACTIVE, scene);
1510 return OPERATOR_FINISHED;
1513 static int gp_convert_draw_check_prop(PointerRNA *ptr, PropertyRNA *prop)
1515 const char *prop_id = RNA_property_identifier(prop);
1516 int link_strokes = RNA_boolean_get(ptr, "use_link_strokes");
1517 int timing_mode = RNA_enum_get(ptr, "timing_mode");
1518 int realtime = RNA_boolean_get(ptr, "use_realtime");
1519 float gap_duration = RNA_float_get(ptr, "gap_duration");
1520 float gap_randomness = RNA_float_get(ptr, "gap_randomness");
1521 int valid_timing = RNA_boolean_get(ptr, "use_timing_data");
1523 /* Always show those props */
1524 if (strcmp(prop_id, "type") == 0 ||
1525 strcmp(prop_id, "use_normalize_weights") == 0 ||
1526 strcmp(prop_id, "radius_multiplier") == 0 ||
1527 strcmp(prop_id, "use_link_strokes") == 0)
1532 /* Never show this prop */
1533 if (strcmp(prop_id, "use_timing_data") == 0)
1537 /* Only show when link_stroke is TRUE */
1538 if (strcmp(prop_id, "timing_mode") == 0)
1541 if (timing_mode != GP_STROKECONVERT_TIMING_NONE) {
1542 /* Only show when link_stroke is TRUE and stroke timing is enabled */
1543 if (strcmp(prop_id, "frame_range") == 0 ||
1544 strcmp(prop_id, "start_frame") == 0)
1549 /* Only show if we have valid timing data! */
1550 if (valid_timing && strcmp(prop_id, "use_realtime") == 0)
1553 /* Only show if realtime or valid_timing is FALSE! */
1554 if ((!realtime || !valid_timing) && strcmp(prop_id, "end_frame") == 0)
1557 if (valid_timing && timing_mode == GP_STROKECONVERT_TIMING_CUSTOMGAP) {
1558 /* Only show for custom gaps! */
1559 if (strcmp(prop_id, "gap_duration") == 0)
1562 /* Only show randomness for non-null custom gaps! */
1563 if (strcmp(prop_id, "gap_randomness") == 0 && (gap_duration > 0.0f))
1566 /* Only show seed for randomize action! */
1567 if (strcmp(prop_id, "seed") == 0 && (gap_duration > 0.0f) && (gap_randomness > 0.0f))
1577 static void gp_convert_ui(bContext *C, wmOperator *op)
1579 uiLayout *layout = op->layout;
1580 wmWindowManager *wm = CTX_wm_manager(C);
1583 RNA_pointer_create(&wm->id, op->type->srna, op->properties, &ptr);
1585 /* Main auto-draw call */
1586 uiDefAutoButsRNA(layout, &ptr, gp_convert_draw_check_prop, '\0');
1589 void GPENCIL_OT_convert(wmOperatorType *ot)
1594 ot->name = "Convert Grease Pencil";
1595 ot->idname = "GPENCIL_OT_convert";
1596 ot->description = "Convert the active Grease Pencil layer to a new Curve Object";
1599 ot->invoke = WM_menu_invoke;
1600 ot->exec = gp_convert_layer_exec;
1601 ot->poll = gp_convert_poll;
1602 ot->ui = gp_convert_ui;
1605 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1608 ot->prop = RNA_def_enum(ot->srna, "type", prop_gpencil_convertmodes, 0, "Type", "Which type of curve to convert to");
1610 RNA_def_boolean(ot->srna, "use_normalize_weights", TRUE, "Normalize Weight",
1611 "Normalize weight (set from stroke width)");
1612 RNA_def_float(ot->srna, "radius_multiplier", 1.0f, 0.0f, 1000.0f, "Radius Fac",
1613 "Multiplier for the points' radii (set from stroke width)", 0.0f, 10.0f);
1614 RNA_def_boolean(ot->srna, "use_link_strokes", TRUE, "Link Strokes",
1615 "Whether to link strokes with zero-radius sections of curves");
1617 prop = RNA_def_enum(ot->srna, "timing_mode", prop_gpencil_convert_timingmodes, GP_STROKECONVERT_TIMING_FULL,
1618 "Timing Mode", "How to use timing data stored in strokes");
1619 RNA_def_enum_funcs(prop, rna_GPConvert_mode_items);
1621 RNA_def_int(ot->srna, "frame_range", 100, 1, 10000, "Frame Range",
1622 "The duration of evaluation of the path control curve", 1, 1000);
1623 RNA_def_int(ot->srna, "start_frame", 1, 1, 100000, "Start Frame",
1624 "The start frame of the path control curve", 1, 100000);
1625 RNA_def_boolean(ot->srna, "use_realtime", FALSE, "Realtime",
1626 "Whether the path control curve reproduces the drawing in realtime, starting from Start Frame");
1627 prop = RNA_def_int(ot->srna, "end_frame", 250, 1, 100000, "End Frame",
1628 "The end frame of the path control curve (if Realtime is not set)", 1, 100000);
1629 RNA_def_property_update_runtime(prop, gp_convert_set_end_frame);
1631 RNA_def_float(ot->srna, "gap_duration", 0.0f, 0.0f, 10000.0f, "Gap Duration",
1632 "Custom Gap mode: (Average) length of gaps, in frames "
1633 "(Note: Realtime value, will be scaled if Realtime is not set)", 0.0f, 1000.0f);
1634 RNA_def_float(ot->srna, "gap_randomness", 0.0f, 0.0f, 10000.0f, "Gap Randomness",
1635 "Custom Gap mode: Number of frames that gap lengths can vary", 0.0f, 1000.0f);
1636 RNA_def_int(ot->srna, "seed", 0, 0, 1000, "Random Seed",
1637 "Custom Gap mode: Random generator seed", 0, 100);
1639 /* Note: Internal use, this one will always be hidden by UI code... */
1640 prop = RNA_def_boolean(ot->srna, "use_timing_data", FALSE, "Has Valid Timing",
1641 "Whether the converted Grease Pencil layer has valid timing data (internal use)");
1642 RNA_def_property_flag(prop, PROP_SKIP_SAVE);
1645 /* ************************************************ */