1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
|
/*
* $Id$
*
* ***** BEGIN GPL LICENSE BLOCK *****
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* The Original Code is Copyright (C) 2008, Blender Foundation, Joshua Leung
* This is a new part of Blender
*
* Contributor(s): Joshua Leung
*
* ***** END GPL LICENSE BLOCK *****
*/
/** \file blender/editors/gpencil/gpencil_edit.c
* \ingroup edgpencil
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stddef.h>
#include <math.h>
#include "MEM_guardedalloc.h"
#include "BLI_math.h"
#include "BLI_blenlib.h"
#include "BLI_utildefines.h"
#include "DNA_curve_types.h"
#include "DNA_object_types.h"
#include "DNA_node_types.h"
#include "DNA_scene_types.h"
#include "DNA_screen_types.h"
#include "DNA_space_types.h"
#include "DNA_view3d_types.h"
#include "DNA_gpencil_types.h"
#include "BKE_context.h"
#include "BKE_curve.h"
#include "BKE_gpencil.h"
#include "BKE_library.h"
#include "BKE_object.h"
#include "BKE_report.h"
#include "WM_api.h"
#include "WM_types.h"
#include "RNA_access.h"
#include "RNA_define.h"
#include "UI_view2d.h"
#include "ED_gpencil.h"
#include "ED_view3d.h"
#include "gpencil_intern.h"
/* ************************************************ */
/* Context Wrangling... */
/* Get pointer to active Grease Pencil datablock, and an RNA-pointer to trace back to whatever owns it */
bGPdata **gpencil_data_get_pointers (bContext *C, PointerRNA *ptr)
{
Scene *scene= CTX_data_scene(C);
ScrArea *sa= CTX_wm_area(C);
/* if there's an active area, check if the particular editor may
* have defined any special Grease Pencil context for editing...
*/
if (sa) {
switch (sa->spacetype) {
case SPACE_VIEW3D: /* 3D-View */
{
Object *ob= CTX_data_active_object(C);
// TODO: we can include other data-types such as bones later if need be...
/* just in case no active object */
if (ob) {
/* for now, as long as there's an object, default to using that in 3D-View */
if (ptr) RNA_id_pointer_create(&ob->id, ptr);
return &ob->gpd;
}
}
break;
case SPACE_NODE: /* Nodes Editor */
{
SpaceNode *snode= (SpaceNode *)CTX_wm_space_data(C);
/* return the GP data for the active node block/node */
if (snode && snode->nodetree) {
/* for now, as long as there's an active node tree, default to using that in the Nodes Editor */
if (ptr) RNA_id_pointer_create(&snode->nodetree->id, ptr);
return &snode->nodetree->gpd;
}
else {
/* even when there is no node-tree, don't allow this to flow to scene */
return NULL;
}
}
break;
case SPACE_SEQ: /* Sequencer */
{
//SpaceSeq *sseq= (SpaceSeq *)CTX_wm_space_data(C);
/* return the GP data for the active strips/image/etc. */
}
break;
case SPACE_IMAGE: /* Image/UV Editor */
{
SpaceImage *sima= (SpaceImage *)CTX_wm_space_data(C);
/* for now, Grease Pencil data is associated with the space... */
// XXX our convention for everything else is to link to data though...
if (ptr) RNA_pointer_create((ID *)CTX_wm_screen(C), &RNA_SpaceImageEditor, sima, ptr);
return &sima->gpd;
}
break;
default: /* unsupported space */
return NULL;
}
}
/* just fall back on the scene's GP data */
if (ptr) RNA_id_pointer_create((ID *)scene, ptr);
return (scene) ? &scene->gpd : NULL;
}
/* Get the active Grease Pencil datablock */
bGPdata *gpencil_data_get_active (bContext *C)
{
bGPdata **gpd_ptr= gpencil_data_get_pointers(C, NULL);
return (gpd_ptr) ? *(gpd_ptr) : NULL;
}
/* needed for offscreen rendering */
bGPdata *gpencil_data_get_active_v3d (Scene *scene)
{
bGPdata *gpd= scene->basact ? scene->basact->object->gpd : NULL;
return gpd ? gpd : scene->gpd;
}
/* ************************************************ */
/* Panel Operators */
/* poll callback for adding data/layers - special */
static int gp_add_poll (bContext *C)
{
/* the base line we have is that we have somewhere to add Grease Pencil data */
return gpencil_data_get_pointers(C, NULL) != NULL;
}
/* ******************* Add New Data ************************ */
/* add new datablock - wrapper around API */
static int gp_data_add_exec (bContext *C, wmOperator *op)
{
bGPdata **gpd_ptr= gpencil_data_get_pointers(C, NULL);
if (gpd_ptr == NULL) {
BKE_report(op->reports, RPT_ERROR, "Nowhere for Grease Pencil data to go");
return OPERATOR_CANCELLED;
}
else {
/* just add new datablock now */
*gpd_ptr= gpencil_data_addnew("GPencil");
}
/* notifiers */
WM_event_add_notifier(C, NC_SCREEN|ND_GPENCIL|NA_EDITED, NULL); // XXX need a nicer one that will work
return OPERATOR_FINISHED;
}
void GPENCIL_OT_data_add (wmOperatorType *ot)
{
/* identifiers */
ot->name= "Grease Pencil Add New";
ot->idname= "GPENCIL_OT_data_add";
ot->description= "Add new Grease Pencil datablock";
/* callbacks */
ot->exec= gp_data_add_exec;
ot->poll= gp_add_poll;
}
/* ******************* Unlink Data ************************ */
/* poll callback for adding data/layers - special */
static int gp_data_unlink_poll (bContext *C)
{
bGPdata **gpd_ptr= gpencil_data_get_pointers(C, NULL);
/* if we have access to some active data, make sure there's a datablock before enabling this */
return (gpd_ptr && *gpd_ptr);
}
/* unlink datablock - wrapper around API */
static int gp_data_unlink_exec (bContext *C, wmOperator *op)
{
bGPdata **gpd_ptr= gpencil_data_get_pointers(C, NULL);
if (gpd_ptr == NULL) {
BKE_report(op->reports, RPT_ERROR, "Nowhere for Grease Pencil data to go");
return OPERATOR_CANCELLED;
}
else {
/* just unlink datablock now, decreasing its user count */
bGPdata *gpd= (*gpd_ptr);
gpd->id.us--;
*gpd_ptr= NULL;
}
/* notifiers */
WM_event_add_notifier(C, NC_SCREEN|ND_GPENCIL|NA_EDITED, NULL); // XXX need a nicer one that will work
return OPERATOR_FINISHED;
}
void GPENCIL_OT_data_unlink (wmOperatorType *ot)
{
/* identifiers */
ot->name= "Grease Pencil Unlink";
ot->idname= "GPENCIL_OT_data_unlink";
ot->description= "Unlink active Grease Pencil datablock";
/* callbacks */
ot->exec= gp_data_unlink_exec;
ot->poll= gp_data_unlink_poll;
}
/* ******************* Add New Layer ************************ */
/* add new layer - wrapper around API */
static int gp_layer_add_exec (bContext *C, wmOperator *op)
{
bGPdata **gpd_ptr= gpencil_data_get_pointers(C, NULL);
/* if there's no existing Grease-Pencil data there, add some */
if (gpd_ptr == NULL) {
BKE_report(op->reports, RPT_ERROR, "Nowhere for Grease Pencil data to go");
return OPERATOR_CANCELLED;
}
if (*gpd_ptr == NULL)
*gpd_ptr= gpencil_data_addnew("GPencil");
/* add new layer now */
gpencil_layer_addnew(*gpd_ptr);
/* notifiers */
WM_event_add_notifier(C, NC_SCREEN|ND_GPENCIL|NA_EDITED, NULL); // XXX please work!
return OPERATOR_FINISHED;
}
void GPENCIL_OT_layer_add (wmOperatorType *ot)
{
/* identifiers */
ot->name= "Add New Layer";
ot->idname= "GPENCIL_OT_layer_add";
ot->description= "Add new Grease Pencil layer for the active Grease Pencil datablock";
/* callbacks */
ot->exec= gp_layer_add_exec;
ot->poll= gp_add_poll;
}
/* ******************* Delete Active Frame ************************ */
static int gp_actframe_delete_poll (bContext *C)
{
bGPdata *gpd= gpencil_data_get_active(C);
bGPDlayer *gpl= gpencil_layer_getactive(gpd);
/* only if there's an active layer with an active frame */
return (gpl && gpl->actframe);
}
/* delete active frame - wrapper around API calls */
static int gp_actframe_delete_exec (bContext *C, wmOperator *op)
{
Scene *scene= CTX_data_scene(C);
bGPdata *gpd= gpencil_data_get_active(C);
bGPDlayer *gpl= gpencil_layer_getactive(gpd);
bGPDframe *gpf= gpencil_layer_getframe(gpl, CFRA, 0);
/* if there's no existing Grease-Pencil data there, add some */
if (gpd == NULL) {
BKE_report(op->reports, RPT_ERROR, "No Grease Pencil data");
return OPERATOR_CANCELLED;
}
if ELEM(NULL, gpl, gpf) {
BKE_report(op->reports, RPT_ERROR, "No active frame to delete");
return OPERATOR_CANCELLED;
}
/* delete it... */
gpencil_layer_delframe(gpl, gpf);
/* notifiers */
WM_event_add_notifier(C, NC_SCREEN|ND_GPENCIL|NA_EDITED, NULL); // XXX please work!
return OPERATOR_FINISHED;
}
void GPENCIL_OT_active_frame_delete (wmOperatorType *ot)
{
/* identifiers */
ot->name= "Delete Active Frame";
ot->idname= "GPENCIL_OT_active_frame_delete";
ot->description= "Delete the active frame for the active Grease Pencil datablock";
/* callbacks */
ot->exec= gp_actframe_delete_exec;
ot->poll= gp_actframe_delete_poll;
}
/* ************************************************ */
/* Grease Pencil to Data Operator */
/* defines for possible modes */
enum {
GP_STROKECONVERT_PATH = 1,
GP_STROKECONVERT_CURVE,
};
/* RNA enum define */
static EnumPropertyItem prop_gpencil_convertmodes[] = {
{GP_STROKECONVERT_PATH, "PATH", 0, "Path", ""},
{GP_STROKECONVERT_CURVE, "CURVE", 0, "Bezier Curve", ""},
{0, NULL, 0, NULL, NULL}
};
/* --- */
/* convert the coordinates from the given stroke point into 3d-coordinates
* - assumes that the active space is the 3D-View
*/
static void gp_strokepoint_convertcoords (bContext *C, bGPDstroke *gps, bGPDspoint *pt, float p3d[3])
{
Scene *scene= CTX_data_scene(C);
View3D *v3d= CTX_wm_view3d(C);
ARegion *ar= CTX_wm_region(C);
if (gps->flag & GP_STROKE_3DSPACE) {
/* directly use 3d-coordinates */
copy_v3_v3(p3d, &pt->x);
}
else {
float *fp= give_cursor(scene, v3d);
float dvec[3];
short mval[2];
int mx, my;
/* get screen coordinate */
if (gps->flag & GP_STROKE_2DSPACE) {
View2D *v2d= &ar->v2d;
UI_view2d_view_to_region(v2d, pt->x, pt->y, &mx, &my);
}
else {
mx= (int)(pt->x / 100 * ar->winx);
my= (int)(pt->y / 100 * ar->winy);
}
mval[0]= (short)mx;
mval[1]= (short)my;
/* convert screen coordinate to 3d coordinates
* - method taken from editview.c - mouse_cursor()
*/
project_short_noclip(ar, fp, mval);
window_to_3d(ar, dvec, mval[0]-mx, mval[1]-my);
sub_v3_v3v3(p3d, fp, dvec);
}
}
/* --- */
/* convert stroke to 3d path */
static void gp_stroke_to_path (bContext *C, bGPDlayer *gpl, bGPDstroke *gps, Curve *cu)
{
bGPDspoint *pt;
Nurb *nu;
BPoint *bp;
int i;
/* create new 'nurb' within the curve */
nu = (Nurb *)MEM_callocN(sizeof(Nurb), "gpstroke_to_path(nurb)");
nu->pntsu= gps->totpoints;
nu->pntsv= 1;
nu->orderu= gps->totpoints;
nu->flagu= CU_NURB_ENDPOINT;
nu->resolu= 32;
nu->bp= (BPoint *)MEM_callocN(sizeof(BPoint)*gps->totpoints, "bpoints");
/* add points */
for (i=0, pt=gps->points, bp=nu->bp; i < gps->totpoints; i++, pt++, bp++) {
float p3d[3];
/* get coordinates to add at */
gp_strokepoint_convertcoords(C, gps, pt, p3d);
copy_v3_v3(bp->vec, p3d);
/* set settings */
bp->f1= SELECT;
bp->radius = bp->weight = pt->pressure * gpl->thickness;
}
/* add nurb to curve */
BLI_addtail(&cu->nurb, nu);
}
/* convert stroke to 3d bezier */
static void gp_stroke_to_bezier (bContext *C, bGPDlayer *gpl, bGPDstroke *gps, Curve *cu)
{
bGPDspoint *pt;
Nurb *nu;
BezTriple *bezt;
int i, tot;
float p3d_cur[3], p3d_prev[3], p3d_next[3];
/* create new 'nurb' within the curve */
nu = (Nurb *)MEM_callocN(sizeof(Nurb), "gpstroke_to_bezier(nurb)");
nu->pntsu= gps->totpoints;
nu->resolu= 12;
nu->resolv= 12;
nu->type= CU_BEZIER;
nu->bezt = (BezTriple *)MEM_callocN(gps->totpoints*sizeof(BezTriple), "bezts");
tot= gps->totpoints;
/* get initial coordinates */
pt=gps->points;
if (tot) {
gp_strokepoint_convertcoords(C, gps, pt, p3d_cur);
if (tot > 1) {
gp_strokepoint_convertcoords(C, gps, pt+1, p3d_next);
}
}
/* add points */
for (i=0, bezt=nu->bezt; i < tot; i++, pt++, bezt++) {
float h1[3], h2[3];
if (i) interp_v3_v3v3(h1, p3d_cur, p3d_prev, 0.3);
else interp_v3_v3v3(h1, p3d_cur, p3d_next, -0.3);
if (i < tot-1) interp_v3_v3v3(h2, p3d_cur, p3d_next, 0.3);
else interp_v3_v3v3(h2, p3d_cur, p3d_prev, -0.3);
copy_v3_v3(bezt->vec[0], h1);
copy_v3_v3(bezt->vec[1], p3d_cur);
copy_v3_v3(bezt->vec[2], h2);
/* set settings */
bezt->h1= bezt->h2= HD_FREE;
bezt->f1= bezt->f2= bezt->f3= SELECT;
bezt->radius = bezt->weight = pt->pressure * gpl->thickness * 0.1f;
/* shift coord vects */
copy_v3_v3(p3d_prev, p3d_cur);
copy_v3_v3(p3d_cur, p3d_next);
if (i + 1 < tot) {
gp_strokepoint_convertcoords(C, gps, pt+1, p3d_next);
}
}
/* must calculate handles or else we crash */
calchandlesNurb(nu);
/* add nurb to curve */
BLI_addtail(&cu->nurb, nu);
}
/* convert a given grease-pencil layer to a 3d-curve representation (using current view if appropriate) */
static void gp_layer_to_curve (bContext *C, bGPdata *gpd, bGPDlayer *gpl, short mode)
{
Scene *scene= CTX_data_scene(C);
bGPDframe *gpf= gpencil_layer_getframe(gpl, CFRA, 0);
bGPDstroke *gps;
Object *ob;
Curve *cu;
/* error checking */
if (ELEM3(NULL, gpd, gpl, gpf))
return;
/* only convert if there are any strokes on this layer's frame to convert */
if (gpf->strokes.first == NULL)
return;
/* init the curve object (remove rotation and get curve data from it)
* - must clear transforms set on object, as those skew our results
*/
ob= add_object(scene, OB_CURVE);
ob->loc[0]= ob->loc[1]= ob->loc[2]= 0;
ob->rot[0]= ob->rot[1]= ob->rot[2]= 0;
cu= ob->data;
cu->flag |= CU_3D;
/* rename object and curve to layer name */
rename_id((ID *)ob, gpl->info);
rename_id((ID *)cu, gpl->info);
/* add points to curve */
for (gps= gpf->strokes.first; gps; gps= gps->next) {
switch (mode) {
case GP_STROKECONVERT_PATH:
gp_stroke_to_path(C, gpl, gps, cu);
break;
case GP_STROKECONVERT_CURVE:
gp_stroke_to_bezier(C, gpl, gps, cu);
break;
}
}
}
/* --- */
static int gp_convert_poll (bContext *C)
{
bGPdata *gpd= gpencil_data_get_active(C);
ScrArea *sa= CTX_wm_area(C);
/* only if there's valid data, and the current view is 3D View */
return ((sa && sa->spacetype == SPACE_VIEW3D) && gpencil_layer_getactive(gpd));
}
static int gp_convert_layer_exec (bContext *C, wmOperator *op)
{
bGPdata *gpd= gpencil_data_get_active(C);
bGPDlayer *gpl= gpencil_layer_getactive(gpd);
Scene *scene= CTX_data_scene(C);
View3D *v3d= CTX_wm_view3d(C);
float *fp= give_cursor(scene, v3d);
int mode= RNA_enum_get(op->ptr, "type");
/* check if there's data to work with */
if (gpd == NULL) {
BKE_report(op->reports, RPT_ERROR, "No Grease Pencil data to work on.");
return OPERATOR_CANCELLED;
}
/* initialise 3d-cursor correction globals */
initgrabz(CTX_wm_region_view3d(C), fp[0], fp[1], fp[2]);
/* handle conversion modes */
switch (mode) {
case GP_STROKECONVERT_PATH:
case GP_STROKECONVERT_CURVE:
gp_layer_to_curve(C, gpd, gpl, mode);
break;
default: /* unsupoorted */
BKE_report(op->reports, RPT_ERROR, "Unknown conversion option.");
return OPERATOR_CANCELLED;
}
/* notifiers */
WM_event_add_notifier(C, NC_OBJECT|NA_ADDED, NULL);
WM_event_add_notifier(C, NC_SCENE|ND_OB_ACTIVE, scene);
/* done */
return OPERATOR_FINISHED;
}
void GPENCIL_OT_convert (wmOperatorType *ot)
{
/* identifiers */
ot->name= "Convert Grease Pencil";
ot->idname= "GPENCIL_OT_convert";
ot->description= "Convert the active Grease Pencil layer to a new Object";
/* callbacks */
ot->invoke= WM_menu_invoke;
ot->exec= gp_convert_layer_exec;
ot->poll= gp_convert_poll;
/* flags */
ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
/* properties */
ot->prop= RNA_def_enum(ot->srna, "type", prop_gpencil_convertmodes, 0, "Type", "");
}
/* ************************************************ */
|