Cleanup: Adhere to our naming convention for BKE_linestyle.h API
[blender.git] / source / blender / blenkernel / intern / linestyle.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
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.
8  *
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.
13  *
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.
17  *
18  * The Original Code is Copyright (C) 2010 Blender Foundation.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): none yet.
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file blender/blenkernel/intern/linestyle.c
29  *  \ingroup bke
30  */
31
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35
36 #include "MEM_guardedalloc.h"
37
38 #include "DNA_object_types.h"
39 #include "DNA_material_types.h" /* for ramp blend */
40 #include "DNA_texture_types.h"
41
42 #include "BLI_blenlib.h"
43 #include "BLI_math.h"
44 #include "BLI_utildefines.h"
45
46 #include "BKE_freestyle.h"
47 #include "BKE_global.h"
48 #include "BKE_library.h"
49 #include "BKE_linestyle.h"
50 #include "BKE_node.h"
51 #include "BKE_texture.h"
52 #include "BKE_colortools.h"
53 #include "BKE_animsys.h"
54
55 static const char *modifier_name[LS_MODIFIER_NUM] = {
56         NULL,
57         "Along Stroke",
58         "Distance from Camera",
59         "Distance from Object",
60         "Material",
61         "Sampling",
62         "Bezier Curve",
63         "Sinus Displacement",
64         "Spatial Noise",
65         "Perlin Noise 1D",
66         "Perlin Noise 2D",
67         "Backbone Stretcher",
68         "Tip Remover",
69         "Calligraphy",
70         "Polygonalization",
71         "Guiding Lines",
72         "Blueprint",
73         "2D Offset",
74         "2D Transform",
75 };
76
77 static void default_linestyle_settings(FreestyleLineStyle *linestyle)
78 {
79         linestyle->panel = LS_PANEL_STROKES;
80         linestyle->r = linestyle->g = linestyle->b = 0.0f;
81         linestyle->alpha = 1.0f;
82         linestyle->thickness = 3.0f;
83         linestyle->thickness_position = LS_THICKNESS_CENTER;
84         linestyle->thickness_ratio = 0.5f;
85         linestyle->flag = LS_SAME_OBJECT | LS_NO_SORTING | LS_TEXTURE;
86         linestyle->chaining = LS_CHAINING_PLAIN;
87         linestyle->rounds = 3;
88         linestyle->min_angle = DEG2RADF(0.0f);
89         linestyle->max_angle = DEG2RADF(0.0f);
90         linestyle->min_length = 0.0f;
91         linestyle->max_length = 10000.0f;
92         linestyle->split_length = 100;
93         linestyle->sort_key = LS_SORT_KEY_DISTANCE_FROM_CAMERA;
94         linestyle->integration_type = LS_INTEGRATION_MEAN;
95         linestyle->texstep = 1.0f;
96         linestyle->pr_texture = TEX_PR_TEXTURE;
97
98         BLI_listbase_clear(&linestyle->color_modifiers);
99         BLI_listbase_clear(&linestyle->alpha_modifiers);
100         BLI_listbase_clear(&linestyle->thickness_modifiers);
101         BLI_listbase_clear(&linestyle->geometry_modifiers);
102
103         BKE_linestyle_modifier_add_geometry(linestyle, NULL, LS_MODIFIER_SAMPLING);
104
105         linestyle->caps = LS_CAPS_BUTT;
106 }
107
108 FreestyleLineStyle *BKE_linestyle_new(const char *name, struct Main *main)
109 {
110         FreestyleLineStyle *linestyle;
111
112         if (!main)
113                 main = G.main;
114
115         linestyle = (FreestyleLineStyle *)BKE_libblock_alloc(main, ID_LS, name);
116
117         default_linestyle_settings(linestyle);
118
119         return linestyle;
120 }
121
122 void BKE_linestyle_free(FreestyleLineStyle *linestyle)
123 {
124         LineStyleModifier *m;
125
126         MTex *mtex;
127         int a;
128
129         for (a = 0; a < MAX_MTEX; a++) {
130                 mtex = linestyle->mtex[a];
131                 if (mtex && mtex->tex) mtex->tex->id.us--;
132                 if (mtex) MEM_freeN(mtex);
133         }
134         if (linestyle->nodetree) {
135                 ntreeFreeTree(linestyle->nodetree);
136                 MEM_freeN(linestyle->nodetree);
137         }
138
139         BKE_free_animdata(&linestyle->id);
140         while ((m = (LineStyleModifier *)linestyle->color_modifiers.first))
141                 BKE_linestyle_modifier_remove_color(linestyle, m);
142         while ((m = (LineStyleModifier *)linestyle->alpha_modifiers.first))
143                 BKE_linestyle_modifier_remove_alpha(linestyle, m);
144         while ((m = (LineStyleModifier *)linestyle->thickness_modifiers.first))
145                 BKE_linestyle_modifier_remove_thickness(linestyle, m);
146         while ((m = (LineStyleModifier *)linestyle->geometry_modifiers.first))
147                 BKE_linestyle_modifier_remove_geometry(linestyle, m);
148 }
149
150 FreestyleLineStyle *BKE_linestyle_copy(FreestyleLineStyle *linestyle)
151 {
152         FreestyleLineStyle *new_linestyle;
153         LineStyleModifier *m;
154         int a;
155
156         new_linestyle = BKE_linestyle_new(linestyle->id.name + 2, NULL);
157         BKE_linestyle_free(new_linestyle);
158
159         for (a = 0; a < MAX_MTEX; a++) {
160                 if (linestyle->mtex[a]) {
161                         new_linestyle->mtex[a] = MEM_mallocN(sizeof(MTex), "BKE_linestyle_copy");
162                         memcpy(new_linestyle->mtex[a], linestyle->mtex[a], sizeof(MTex));
163                         id_us_plus((ID *)new_linestyle->mtex[a]->tex);
164                 }
165         }
166         if (linestyle->nodetree) {
167                 linestyle->nodetree = ntreeCopyTree(linestyle->nodetree);
168         }
169
170         new_linestyle->r = linestyle->r;
171         new_linestyle->g = linestyle->g;
172         new_linestyle->b = linestyle->b;
173         new_linestyle->alpha = linestyle->alpha;
174         new_linestyle->thickness = linestyle->thickness;
175         new_linestyle->thickness_position = linestyle->thickness_position;
176         new_linestyle->thickness_ratio = linestyle->thickness_ratio;
177         new_linestyle->flag = linestyle->flag;
178         new_linestyle->caps = linestyle->caps;
179         new_linestyle->chaining = linestyle->chaining;
180         new_linestyle->rounds = linestyle->rounds;
181         new_linestyle->split_length = linestyle->split_length;
182         new_linestyle->min_angle = linestyle->min_angle;
183         new_linestyle->max_angle = linestyle->max_angle;
184         new_linestyle->min_length = linestyle->min_length;
185         new_linestyle->max_length = linestyle->max_length;
186         new_linestyle->split_dash1 = linestyle->split_dash1;
187         new_linestyle->split_gap1 = linestyle->split_gap1;
188         new_linestyle->split_dash2 = linestyle->split_dash2;
189         new_linestyle->split_gap2 = linestyle->split_gap2;
190         new_linestyle->split_dash3 = linestyle->split_dash3;
191         new_linestyle->split_gap3 = linestyle->split_gap3;
192         new_linestyle->dash1 = linestyle->dash1;
193         new_linestyle->gap1 = linestyle->gap1;
194         new_linestyle->dash2 = linestyle->dash2;
195         new_linestyle->gap2 = linestyle->gap2;
196         new_linestyle->dash3 = linestyle->dash3;
197         new_linestyle->gap3 = linestyle->gap3;
198         new_linestyle->panel = linestyle->panel;
199         new_linestyle->texstep = linestyle->texstep;
200         new_linestyle->pr_texture = linestyle->pr_texture;
201         for (m = (LineStyleModifier *)linestyle->color_modifiers.first; m; m = m->next)
202                 BKE_linestyle_modifier_copy_color(new_linestyle, m);
203         for (m = (LineStyleModifier *)linestyle->alpha_modifiers.first; m; m = m->next)
204                 BKE_linestyle_modifier_copy_alpha(new_linestyle, m);
205         for (m = (LineStyleModifier *)linestyle->thickness_modifiers.first; m; m = m->next)
206                 BKE_linestyle_modifier_copy_thickness(new_linestyle, m);
207         for (m = (LineStyleModifier *)linestyle->geometry_modifiers.first; m; m = m->next)
208                 BKE_linestyle_modifier_copy_geometry(new_linestyle, m);
209
210         return new_linestyle;
211 }
212
213 FreestyleLineStyle *BKE_linestyle_active_from_scene(Scene *scene)
214 {
215         SceneRenderLayer *actsrl = BLI_findlink(&scene->r.layers, scene->r.actlay);
216         FreestyleConfig *config = &actsrl->freestyleConfig;
217         FreestyleLineSet *lineset = BKE_freestyle_lineset_get_active(config);
218
219         if (lineset) {
220                 return lineset->linestyle;
221         }
222         return NULL;
223 }
224
225 static LineStyleModifier *new_modifier(const char *name, int type, size_t size)
226 {
227         LineStyleModifier *m;
228
229         if (!name) {
230                 name = modifier_name[type];
231         }
232         m = (LineStyleModifier *)MEM_callocN(size, "line style modifier");
233         m->type = type;
234         BLI_strncpy(m->name, name, sizeof(m->name));
235         m->influence = 1.0f;
236         m->flags = LS_MODIFIER_ENABLED | LS_MODIFIER_EXPANDED;
237
238         return m;
239 }
240
241 static void add_to_modifier_list(ListBase *lb, LineStyleModifier *m)
242 {
243         BLI_addtail(lb, (void *)m);
244         BLI_uniquename(lb, m, modifier_name[m->type], '.', offsetof(LineStyleModifier, name), sizeof(m->name));
245 }
246
247 static LineStyleModifier *alloc_color_modifier(const char *name, int type)
248 {
249         size_t size;
250
251         switch (type) {
252                 case LS_MODIFIER_ALONG_STROKE:
253                         size = sizeof(LineStyleColorModifier_AlongStroke);
254                         break;
255                 case LS_MODIFIER_DISTANCE_FROM_CAMERA:
256                         size = sizeof(LineStyleColorModifier_DistanceFromCamera);
257                         break;
258                 case LS_MODIFIER_DISTANCE_FROM_OBJECT:
259                         size = sizeof(LineStyleColorModifier_DistanceFromObject);
260                         break;
261                 case LS_MODIFIER_MATERIAL:
262                         size = sizeof(LineStyleColorModifier_Material);
263                         break;
264                 default:
265                         return NULL; /* unknown modifier type */
266         }
267
268         return new_modifier(name, type, size);
269 }
270
271 LineStyleModifier *BKE_linestyle_modifier_add_color(FreestyleLineStyle *linestyle, const char *name, int type)
272 {
273         LineStyleModifier *m;
274
275         m = alloc_color_modifier(name, type);
276         m->blend = MA_RAMP_BLEND;
277
278         switch (type) {
279                 case LS_MODIFIER_ALONG_STROKE:
280                         ((LineStyleColorModifier_AlongStroke *)m)->color_ramp = add_colorband(1);
281                         break;
282                 case LS_MODIFIER_DISTANCE_FROM_CAMERA:
283                         ((LineStyleColorModifier_DistanceFromCamera *)m)->color_ramp = add_colorband(1);
284                         ((LineStyleColorModifier_DistanceFromCamera *)m)->range_min = 0.0f;
285                         ((LineStyleColorModifier_DistanceFromCamera *)m)->range_max = 10000.0f;
286                         break;
287                 case LS_MODIFIER_DISTANCE_FROM_OBJECT:
288                         ((LineStyleColorModifier_DistanceFromObject *)m)->target = NULL;
289                         ((LineStyleColorModifier_DistanceFromObject *)m)->color_ramp = add_colorband(1);
290                         ((LineStyleColorModifier_DistanceFromObject *)m)->range_min = 0.0f;
291                         ((LineStyleColorModifier_DistanceFromObject *)m)->range_max = 10000.0f;
292                         break;
293                 case LS_MODIFIER_MATERIAL:
294                         ((LineStyleColorModifier_Material *)m)->color_ramp = add_colorband(1);
295                         ((LineStyleColorModifier_Material *)m)->mat_attr = LS_MODIFIER_MATERIAL_LINE;
296                         break;
297                 default:
298                         return NULL; /* unknown modifier type */
299         }
300         add_to_modifier_list(&linestyle->color_modifiers, m);
301
302         return m;
303 }
304
305 LineStyleModifier *BKE_linestyle_modifier_copy_color(FreestyleLineStyle *linestyle, LineStyleModifier *m)
306 {
307         LineStyleModifier *new_m;
308
309         new_m = alloc_color_modifier(m->name, m->type);
310         new_m->influence = m->influence;
311         new_m->flags = m->flags;
312         new_m->blend = m->blend;
313
314         switch (m->type) {
315                 case LS_MODIFIER_ALONG_STROKE:
316                 {
317                         LineStyleColorModifier_AlongStroke *p = (LineStyleColorModifier_AlongStroke *)m;
318                         LineStyleColorModifier_AlongStroke *q = (LineStyleColorModifier_AlongStroke *)new_m;
319                         q->color_ramp = MEM_dupallocN(p->color_ramp);
320                         break;
321                 }
322                 case LS_MODIFIER_DISTANCE_FROM_CAMERA:
323                 {
324                         LineStyleColorModifier_DistanceFromCamera *p = (LineStyleColorModifier_DistanceFromCamera *)m;
325                         LineStyleColorModifier_DistanceFromCamera *q = (LineStyleColorModifier_DistanceFromCamera *)new_m;
326                         q->color_ramp = MEM_dupallocN(p->color_ramp);
327                         q->range_min = p->range_min;
328                         q->range_max = p->range_max;
329                         break;
330                 }
331                 case LS_MODIFIER_DISTANCE_FROM_OBJECT:
332                 {
333                         LineStyleColorModifier_DistanceFromObject *p = (LineStyleColorModifier_DistanceFromObject *)m;
334                         LineStyleColorModifier_DistanceFromObject *q = (LineStyleColorModifier_DistanceFromObject *)new_m;
335                         if (p->target)
336                                 p->target->id.us++;
337                         q->target = p->target;
338                         q->color_ramp = MEM_dupallocN(p->color_ramp);
339                         q->range_min = p->range_min;
340                         q->range_max = p->range_max;
341                         break;
342                 }
343                 case LS_MODIFIER_MATERIAL:
344                 {
345                         LineStyleColorModifier_Material *p = (LineStyleColorModifier_Material *)m;
346                         LineStyleColorModifier_Material *q = (LineStyleColorModifier_Material *)new_m;
347                         q->color_ramp = MEM_dupallocN(p->color_ramp);
348                         q->flags = p->flags;
349                         q->mat_attr = p->mat_attr;
350                         break;
351                 }
352                 default:
353                         return NULL; /* unknown modifier type */
354         }
355         add_to_modifier_list(&linestyle->color_modifiers, new_m);
356
357         return new_m;
358 }
359
360 int BKE_linestyle_modifier_remove_color(FreestyleLineStyle *linestyle, LineStyleModifier *m)
361 {
362         if (BLI_findindex(&linestyle->color_modifiers, m) == -1)
363                 return -1;
364         switch (m->type) {
365                 case LS_MODIFIER_ALONG_STROKE:
366                         MEM_freeN(((LineStyleColorModifier_AlongStroke *)m)->color_ramp);
367                         break;
368                 case LS_MODIFIER_DISTANCE_FROM_CAMERA:
369                         MEM_freeN(((LineStyleColorModifier_DistanceFromCamera *)m)->color_ramp);
370                         break;
371                 case LS_MODIFIER_DISTANCE_FROM_OBJECT:
372                         MEM_freeN(((LineStyleColorModifier_DistanceFromObject *)m)->color_ramp);
373                         break;
374                 case LS_MODIFIER_MATERIAL:
375                         MEM_freeN(((LineStyleColorModifier_Material *)m)->color_ramp);
376                         break;
377         }
378         BLI_freelinkN(&linestyle->color_modifiers, m);
379         return 0;
380 }
381
382 static LineStyleModifier *alloc_alpha_modifier(const char *name, int type)
383 {
384         size_t size;
385
386         switch (type) {
387                 case LS_MODIFIER_ALONG_STROKE:
388                         size = sizeof(LineStyleAlphaModifier_AlongStroke);
389                         break;
390                 case LS_MODIFIER_DISTANCE_FROM_CAMERA:
391                         size = sizeof(LineStyleAlphaModifier_DistanceFromCamera);
392                         break;
393                 case LS_MODIFIER_DISTANCE_FROM_OBJECT:
394                         size = sizeof(LineStyleAlphaModifier_DistanceFromObject);
395                         break;
396                 case LS_MODIFIER_MATERIAL:
397                         size = sizeof(LineStyleAlphaModifier_Material);
398                         break;
399                 default:
400                         return NULL; /* unknown modifier type */
401         }
402         return new_modifier(name, type, size);
403 }
404
405 LineStyleModifier *BKE_linestyle_modifier_add_alpha(FreestyleLineStyle *linestyle, const char *name, int type)
406 {
407         LineStyleModifier *m;
408
409         m = alloc_alpha_modifier(name, type);
410         m->blend = LS_VALUE_BLEND;
411
412         switch (type) {
413                 case LS_MODIFIER_ALONG_STROKE:
414                 {
415                         LineStyleAlphaModifier_AlongStroke *p = (LineStyleAlphaModifier_AlongStroke *)m;
416                         p->curve = curvemapping_add(1, 0.0f, 0.0f, 1.0f, 1.0f);
417                         break;
418                 }
419                 case LS_MODIFIER_DISTANCE_FROM_CAMERA:
420                 {
421                         LineStyleAlphaModifier_DistanceFromCamera *p = (LineStyleAlphaModifier_DistanceFromCamera *)m;
422                         p->curve = curvemapping_add(1, 0.0f, 0.0f, 1.0f, 1.0f);
423                         p->range_min = 0.0f;
424                         p->range_max = 10000.0f;
425                         break;
426                 }
427                 case LS_MODIFIER_DISTANCE_FROM_OBJECT:
428                 {
429                         LineStyleAlphaModifier_DistanceFromObject *p = (LineStyleAlphaModifier_DistanceFromObject *)m;
430                         p->target = NULL;
431                         p->curve = curvemapping_add(1, 0.0f, 0.0f, 1.0f, 1.0f);
432                         p->range_min = 0.0f;
433                         p->range_max = 10000.0f;
434                         break;
435                 }
436                 case LS_MODIFIER_MATERIAL:
437                 {
438                         LineStyleAlphaModifier_Material *p = (LineStyleAlphaModifier_Material *)m;
439                         p->curve = curvemapping_add(1, 0.0f, 0.0f, 1.0f, 1.0f);
440                         p->mat_attr = LS_MODIFIER_MATERIAL_ALPHA;
441                         break;
442                 }
443                 default:
444                         return NULL; /* unknown modifier type */
445         }
446         add_to_modifier_list(&linestyle->alpha_modifiers, m);
447
448         return m;
449 }
450
451 LineStyleModifier *BKE_linestyle_modifier_copy_alpha(FreestyleLineStyle *linestyle, LineStyleModifier *m)
452 {
453         LineStyleModifier *new_m;
454
455         new_m = alloc_alpha_modifier(m->name, m->type);
456         new_m->influence = m->influence;
457         new_m->flags = m->flags;
458         new_m->blend = m->blend;
459
460         switch (m->type) {
461                 case LS_MODIFIER_ALONG_STROKE:
462                 {
463                         LineStyleAlphaModifier_AlongStroke *p = (LineStyleAlphaModifier_AlongStroke *)m;
464                         LineStyleAlphaModifier_AlongStroke *q = (LineStyleAlphaModifier_AlongStroke *)new_m;
465                         q->curve = curvemapping_copy(p->curve);
466                         q->flags = p->flags;
467                         break;
468                 }
469                 case LS_MODIFIER_DISTANCE_FROM_CAMERA:
470                 {
471                         LineStyleAlphaModifier_DistanceFromCamera *p = (LineStyleAlphaModifier_DistanceFromCamera *)m;
472                         LineStyleAlphaModifier_DistanceFromCamera *q = (LineStyleAlphaModifier_DistanceFromCamera *)new_m;
473                         q->curve = curvemapping_copy(p->curve);
474                         q->flags = p->flags;
475                         q->range_min = p->range_min;
476                         q->range_max = p->range_max;
477                         break;
478                 }
479                 case LS_MODIFIER_DISTANCE_FROM_OBJECT:
480                 {
481                         LineStyleAlphaModifier_DistanceFromObject *p = (LineStyleAlphaModifier_DistanceFromObject *)m;
482                         LineStyleAlphaModifier_DistanceFromObject *q = (LineStyleAlphaModifier_DistanceFromObject *)new_m;
483                         if (p->target)
484                                 p->target->id.us++;
485                         q->target = p->target;
486                         q->curve = curvemapping_copy(p->curve);
487                         q->flags = p->flags;
488                         q->range_min = p->range_min;
489                         q->range_max = p->range_max;
490                         break;
491                 }
492                 case LS_MODIFIER_MATERIAL:
493                 {
494                         LineStyleAlphaModifier_Material *p = (LineStyleAlphaModifier_Material *)m;
495                         LineStyleAlphaModifier_Material *q = (LineStyleAlphaModifier_Material *)new_m;
496                         q->curve = curvemapping_copy(p->curve);
497                         q->flags = p->flags;
498                         q->mat_attr = p->mat_attr;
499                         break;
500                 }
501                 default:
502                         return NULL; /* unknown modifier type */
503         }
504         add_to_modifier_list(&linestyle->alpha_modifiers, new_m);
505
506         return new_m;
507 }
508
509 int BKE_linestyle_modifier_remove_alpha(FreestyleLineStyle *linestyle, LineStyleModifier *m)
510 {
511         if (BLI_findindex(&linestyle->alpha_modifiers, m) == -1)
512                 return -1;
513         switch (m->type) {
514                 case LS_MODIFIER_ALONG_STROKE:
515                         curvemapping_free(((LineStyleAlphaModifier_AlongStroke *)m)->curve);
516                         break;
517                 case LS_MODIFIER_DISTANCE_FROM_CAMERA:
518                         curvemapping_free(((LineStyleAlphaModifier_DistanceFromCamera *)m)->curve);
519                         break;
520                 case LS_MODIFIER_DISTANCE_FROM_OBJECT:
521                         curvemapping_free(((LineStyleAlphaModifier_DistanceFromObject *)m)->curve);
522                         break;
523                 case LS_MODIFIER_MATERIAL:
524                         curvemapping_free(((LineStyleAlphaModifier_Material *)m)->curve);
525                         break;
526         }
527         BLI_freelinkN(&linestyle->alpha_modifiers, m);
528         return 0;
529 }
530
531 static LineStyleModifier *alloc_thickness_modifier(const char *name, int type)
532 {
533         size_t size;
534
535         switch (type) {
536                 case LS_MODIFIER_ALONG_STROKE:
537                         size = sizeof(LineStyleThicknessModifier_AlongStroke);
538                         break;
539                 case LS_MODIFIER_DISTANCE_FROM_CAMERA:
540                         size = sizeof(LineStyleThicknessModifier_DistanceFromCamera);
541                         break;
542                 case LS_MODIFIER_DISTANCE_FROM_OBJECT:
543                         size = sizeof(LineStyleThicknessModifier_DistanceFromObject);
544                         break;
545                 case LS_MODIFIER_MATERIAL:
546                         size = sizeof(LineStyleThicknessModifier_Material);
547                         break;
548                 case LS_MODIFIER_CALLIGRAPHY:
549                         size = sizeof(LineStyleThicknessModifier_Calligraphy);
550                         break;
551                 default:
552                         return NULL; /* unknown modifier type */
553         }
554
555         return new_modifier(name, type, size);
556 }
557
558 LineStyleModifier *BKE_linestyle_modifier_add_thickness(FreestyleLineStyle *linestyle, const char *name, int type)
559 {
560         LineStyleModifier *m;
561
562         m = alloc_thickness_modifier(name, type);
563         m->blend = LS_VALUE_BLEND;
564
565         switch (type) {
566                 case LS_MODIFIER_ALONG_STROKE:
567                 {
568                         LineStyleThicknessModifier_AlongStroke *p = (LineStyleThicknessModifier_AlongStroke *)m;
569                         p->curve = curvemapping_add(1, 0.0f, 0.0f, 1.0f, 1.0f);
570                         p->value_min = 0.0f;
571                         p->value_max = 1.0f;
572                         break;
573                 }
574                 case LS_MODIFIER_DISTANCE_FROM_CAMERA:
575                 {
576                         LineStyleThicknessModifier_DistanceFromCamera *p = (LineStyleThicknessModifier_DistanceFromCamera *)m;
577                         p->curve = curvemapping_add(1, 0.0f, 0.0f, 1.0f, 1.0f);
578                         p->range_min = 0.0f;
579                         p->range_max = 1000.0f;
580                         p->value_min = 0.0f;
581                         p->value_max = 1.0f;
582                         break;
583                 }
584                 case LS_MODIFIER_DISTANCE_FROM_OBJECT:
585                 {
586                         LineStyleThicknessModifier_DistanceFromObject *p = (LineStyleThicknessModifier_DistanceFromObject *)m;
587                         p->target = NULL;
588                         p->curve = curvemapping_add(1, 0.0f, 0.0f, 1.0f, 1.0f);
589                         p->range_min = 0.0f;
590                         p->range_max = 1000.0f;
591                         p->value_min = 0.0f;
592                         p->value_max = 1.0f;
593                         break;
594                 }
595                 case LS_MODIFIER_MATERIAL:
596                 {
597                         LineStyleThicknessModifier_Material *p = (LineStyleThicknessModifier_Material *)m;
598                         p->curve = curvemapping_add(1, 0.0f, 0.0f, 1.0f, 1.0f);
599                         p->mat_attr = LS_MODIFIER_MATERIAL_LINE;
600                         p->value_min = 0.0f;
601                         p->value_max = 1.0f;
602                         break;
603                 }
604                 case LS_MODIFIER_CALLIGRAPHY:
605                 {
606                         LineStyleThicknessModifier_Calligraphy *p = (LineStyleThicknessModifier_Calligraphy *)m;
607                         p->min_thickness = 1.0f;
608                         p->max_thickness = 10.0f;
609                         p->orientation = DEG2RADF(60.0f);
610                         break;
611                 }
612                 default:
613                         return NULL; /* unknown modifier type */
614         }
615         add_to_modifier_list(&linestyle->thickness_modifiers, m);
616
617         return m;
618 }
619
620 LineStyleModifier *BKE_linestyle_modifier_copy_thickness(FreestyleLineStyle *linestyle, LineStyleModifier *m)
621 {
622         LineStyleModifier *new_m;
623
624         new_m = alloc_thickness_modifier(m->name, m->type);
625         if (!new_m)
626                 return NULL;
627         new_m->influence = m->influence;
628         new_m->flags = m->flags;
629         new_m->blend = m->blend;
630
631         switch (m->type) {
632                 case LS_MODIFIER_ALONG_STROKE:
633                 {
634                         LineStyleThicknessModifier_AlongStroke *p = (LineStyleThicknessModifier_AlongStroke *)m;
635                         LineStyleThicknessModifier_AlongStroke *q = (LineStyleThicknessModifier_AlongStroke *)new_m;
636                         q->curve = curvemapping_copy(p->curve);
637                         q->flags = p->flags;
638                         q->value_min = p->value_min;
639                         q->value_max = p->value_max;
640                         break;
641                 }
642                 case LS_MODIFIER_DISTANCE_FROM_CAMERA:
643                 {
644                         LineStyleThicknessModifier_DistanceFromCamera *p = (LineStyleThicknessModifier_DistanceFromCamera *)m;
645                         LineStyleThicknessModifier_DistanceFromCamera *q = (LineStyleThicknessModifier_DistanceFromCamera *)new_m;
646                         q->curve = curvemapping_copy(p->curve);
647                         q->flags = p->flags;
648                         q->range_min = p->range_min;
649                         q->range_max = p->range_max;
650                         q->value_min = p->value_min;
651                         q->value_max = p->value_max;
652                         break;
653                 }
654                 case LS_MODIFIER_DISTANCE_FROM_OBJECT:
655                 {
656                         LineStyleThicknessModifier_DistanceFromObject *p = (LineStyleThicknessModifier_DistanceFromObject *)m;
657                         LineStyleThicknessModifier_DistanceFromObject *q = (LineStyleThicknessModifier_DistanceFromObject *)new_m;
658                         if (p->target)
659                                 p->target->id.us++;
660                         q->target = p->target;
661                         q->curve = curvemapping_copy(p->curve);
662                         q->flags = p->flags;
663                         q->range_min = p->range_min;
664                         q->range_max = p->range_max;
665                         q->value_min = p->value_min;
666                         q->value_max = p->value_max;
667                         break;
668                 }
669                 case LS_MODIFIER_MATERIAL:
670                 {
671                         LineStyleThicknessModifier_Material *p = (LineStyleThicknessModifier_Material *)m;
672                         LineStyleThicknessModifier_Material *q = (LineStyleThicknessModifier_Material *)new_m;
673                         q->curve = curvemapping_copy(p->curve);
674                         q->flags = p->flags;
675                         q->mat_attr = p->mat_attr;
676                         q->value_min = p->value_min;
677                         q->value_max = p->value_max;
678                         break;
679                 }
680                 case LS_MODIFIER_CALLIGRAPHY:
681                 {
682                         LineStyleThicknessModifier_Calligraphy *p = (LineStyleThicknessModifier_Calligraphy *)m;
683                         LineStyleThicknessModifier_Calligraphy *q = (LineStyleThicknessModifier_Calligraphy *)new_m;
684                         q->min_thickness = p->min_thickness;
685                         q->max_thickness = p->max_thickness;
686                         q->orientation = p->orientation;
687                         break;
688                 }
689                 default:
690                         return NULL; /* unknown modifier type */
691         }
692         add_to_modifier_list(&linestyle->thickness_modifiers, new_m);
693
694         return new_m;
695 }
696
697 int BKE_linestyle_modifier_remove_thickness(FreestyleLineStyle *linestyle, LineStyleModifier *m)
698 {
699         if (BLI_findindex(&linestyle->thickness_modifiers, m) == -1)
700                 return -1;
701         switch (m->type) {
702                 case LS_MODIFIER_ALONG_STROKE:
703                         curvemapping_free(((LineStyleThicknessModifier_AlongStroke *)m)->curve);
704                         break;
705                 case LS_MODIFIER_DISTANCE_FROM_CAMERA:
706                         curvemapping_free(((LineStyleThicknessModifier_DistanceFromCamera *)m)->curve);
707                         break;
708                 case LS_MODIFIER_DISTANCE_FROM_OBJECT:
709                         curvemapping_free(((LineStyleThicknessModifier_DistanceFromObject *)m)->curve);
710                         break;
711                 case LS_MODIFIER_MATERIAL:
712                         curvemapping_free(((LineStyleThicknessModifier_Material *)m)->curve);
713                         break;
714                 case LS_MODIFIER_CALLIGRAPHY:
715                         break;
716         }
717         BLI_freelinkN(&linestyle->thickness_modifiers, m);
718         return 0;
719 }
720
721 static LineStyleModifier *alloc_geometry_modifier(const char *name, int type)
722 {
723         size_t size;
724
725         switch (type) {
726                 case LS_MODIFIER_SAMPLING:
727                         size = sizeof(LineStyleGeometryModifier_Sampling);
728                         break;
729                 case LS_MODIFIER_BEZIER_CURVE:
730                         size = sizeof(LineStyleGeometryModifier_BezierCurve);
731                         break;
732                 case LS_MODIFIER_SINUS_DISPLACEMENT:
733                         size = sizeof(LineStyleGeometryModifier_SinusDisplacement);
734                         break;
735                 case LS_MODIFIER_SPATIAL_NOISE:
736                         size = sizeof(LineStyleGeometryModifier_SpatialNoise);
737                         break;
738                 case LS_MODIFIER_PERLIN_NOISE_1D:
739                         size = sizeof(LineStyleGeometryModifier_PerlinNoise1D);
740                         break;
741                 case LS_MODIFIER_PERLIN_NOISE_2D:
742                         size = sizeof(LineStyleGeometryModifier_PerlinNoise2D);
743                         break;
744                 case LS_MODIFIER_BACKBONE_STRETCHER:
745                         size = sizeof(LineStyleGeometryModifier_BackboneStretcher);
746                         break;
747                 case LS_MODIFIER_TIP_REMOVER:
748                         size = sizeof(LineStyleGeometryModifier_TipRemover);
749                         break;
750                 case LS_MODIFIER_POLYGONIZATION:
751                         size = sizeof(LineStyleGeometryModifier_Polygonalization);
752                         break;
753                 case LS_MODIFIER_GUIDING_LINES:
754                         size = sizeof(LineStyleGeometryModifier_GuidingLines);
755                         break;
756                 case LS_MODIFIER_BLUEPRINT:
757                         size = sizeof(LineStyleGeometryModifier_Blueprint);
758                         break;
759                 case LS_MODIFIER_2D_OFFSET:
760                         size = sizeof(LineStyleGeometryModifier_2DOffset);
761                         break;
762                 case LS_MODIFIER_2D_TRANSFORM:
763                         size = sizeof(LineStyleGeometryModifier_2DTransform);
764                         break;
765                 default:
766                         return NULL; /* unknown modifier type */
767         }
768
769         return new_modifier(name, type, size);
770 }
771
772 LineStyleModifier *BKE_linestyle_modifier_add_geometry(FreestyleLineStyle *linestyle, const char *name, int type)
773 {
774         LineStyleModifier *m;
775
776         m = alloc_geometry_modifier(name, type);
777
778         switch (type) {
779                 case LS_MODIFIER_SAMPLING:
780                 {
781                         LineStyleGeometryModifier_Sampling *p = (LineStyleGeometryModifier_Sampling *)m;
782                         p->sampling = 10.0f;
783                         break;
784                 }
785                 case LS_MODIFIER_BEZIER_CURVE:
786                 {
787                         LineStyleGeometryModifier_BezierCurve *p = (LineStyleGeometryModifier_BezierCurve *)m;
788                         p->error = 10.0f;
789                         break;
790                 }
791                 case LS_MODIFIER_SINUS_DISPLACEMENT:
792                 {
793                         LineStyleGeometryModifier_SinusDisplacement *p = (LineStyleGeometryModifier_SinusDisplacement *)m;
794                         p->wavelength = 20.0f;
795                         p->amplitude = 5.0f;
796                         p->phase = 0.0f;
797                         break;
798                 }
799                 case LS_MODIFIER_SPATIAL_NOISE:
800                 {
801                         LineStyleGeometryModifier_SpatialNoise *p = (LineStyleGeometryModifier_SpatialNoise *)m;
802                         p->amplitude = 5.0f;
803                         p->scale = 20.0f;
804                         p->octaves = 4;
805                         p->flags = LS_MODIFIER_SPATIAL_NOISE_SMOOTH | LS_MODIFIER_SPATIAL_NOISE_PURERANDOM;
806                         break;
807                 }
808                 case LS_MODIFIER_PERLIN_NOISE_1D:
809                 {
810                         LineStyleGeometryModifier_PerlinNoise1D *p = (LineStyleGeometryModifier_PerlinNoise1D *)m;
811                         p->frequency = 10.0f;
812                         p->amplitude = 10.0f;
813                         p->octaves = 4;
814                         p->angle = DEG2RADF(45.0f);
815                         break;
816                 }
817                 case LS_MODIFIER_PERLIN_NOISE_2D:
818                 {
819                         LineStyleGeometryModifier_PerlinNoise2D *p = (LineStyleGeometryModifier_PerlinNoise2D *)m;
820                         p->frequency = 10.0f;
821                         p->amplitude = 10.0f;
822                         p->octaves = 4;
823                         p->angle = DEG2RADF(45.0f);
824                         break;
825                 }
826                 case LS_MODIFIER_BACKBONE_STRETCHER:
827                 {
828                         LineStyleGeometryModifier_BackboneStretcher *p = (LineStyleGeometryModifier_BackboneStretcher *)m;
829                         p->backbone_length = 10.0f;
830                         break;
831                 }
832                 case LS_MODIFIER_TIP_REMOVER:
833                 {
834                         LineStyleGeometryModifier_TipRemover *p = (LineStyleGeometryModifier_TipRemover *)m;
835                         p->tip_length = 10.0f;
836                         break;
837                 }
838                 case LS_MODIFIER_POLYGONIZATION:
839                 {
840                         LineStyleGeometryModifier_Polygonalization *p = (LineStyleGeometryModifier_Polygonalization *)m;
841                         p->error = 10.0f;
842                         break;
843                 }
844                 case LS_MODIFIER_GUIDING_LINES:
845                 {
846                         LineStyleGeometryModifier_GuidingLines *p = (LineStyleGeometryModifier_GuidingLines *)m;
847                         p->offset = 0.0f;
848                         break;
849                 }
850                 case LS_MODIFIER_BLUEPRINT:
851                 {
852                         LineStyleGeometryModifier_Blueprint *p = (LineStyleGeometryModifier_Blueprint *)m;
853                         p->flags = LS_MODIFIER_BLUEPRINT_CIRCLES;
854                         p->rounds = 1;
855                         p->backbone_length = 10.0f;
856                         p->random_radius = 3;
857                         p->random_center = 5;
858                         p->random_backbone = 5;
859                         break;
860                 }
861                 case LS_MODIFIER_2D_OFFSET:
862                 {
863                         LineStyleGeometryModifier_2DOffset *p = (LineStyleGeometryModifier_2DOffset *)m;
864                         p->start = 0.0f;
865                         p->end = 0.0f;
866                         p->x = 0.0f;
867                         p->y = 0.0f;
868                         break;
869                 }
870                 case LS_MODIFIER_2D_TRANSFORM:
871                 {
872                         LineStyleGeometryModifier_2DTransform *p = (LineStyleGeometryModifier_2DTransform *)m;
873                         p->pivot = LS_MODIFIER_2D_TRANSFORM_PIVOT_CENTER;
874                         p->scale_x = 1.0f;
875                         p->scale_y = 1.0f;
876                         p->angle = DEG2RADF(0.0f);
877                         p->pivot_u = 0.5f;
878                         p->pivot_x = 0.0f;
879                         p->pivot_y = 0.0f;
880                         break;
881                 }
882                 default:
883                         return NULL; /* unknown modifier type */
884         }
885         add_to_modifier_list(&linestyle->geometry_modifiers, m);
886
887         return m;
888 }
889
890 LineStyleModifier *BKE_linestyle_modifier_copy_geometry(FreestyleLineStyle *linestyle, LineStyleModifier *m)
891 {
892         LineStyleModifier *new_m;
893
894         new_m = alloc_geometry_modifier(m->name, m->type);
895         new_m->flags = m->flags;
896
897         switch (m->type) {
898                 case LS_MODIFIER_SAMPLING:
899                 {
900                         LineStyleGeometryModifier_Sampling *p = (LineStyleGeometryModifier_Sampling *)m;
901                         LineStyleGeometryModifier_Sampling *q = (LineStyleGeometryModifier_Sampling *)new_m;
902                         q->sampling = p->sampling;
903                         break;
904                 }
905                 case LS_MODIFIER_BEZIER_CURVE:
906                 {
907                         LineStyleGeometryModifier_BezierCurve *p = (LineStyleGeometryModifier_BezierCurve *)m;
908                         LineStyleGeometryModifier_BezierCurve *q = (LineStyleGeometryModifier_BezierCurve *)new_m;
909                         q->error = p->error;
910                         break;
911                 }
912                 case LS_MODIFIER_SINUS_DISPLACEMENT:
913                 {
914                         LineStyleGeometryModifier_SinusDisplacement *p = (LineStyleGeometryModifier_SinusDisplacement *)m;
915                         LineStyleGeometryModifier_SinusDisplacement *q = (LineStyleGeometryModifier_SinusDisplacement *)new_m;
916                         q->wavelength = p->wavelength;
917                         q->amplitude = p->amplitude;
918                         q->phase = p->phase;
919                         break;
920                 }
921                 case LS_MODIFIER_SPATIAL_NOISE:
922                 {
923                         LineStyleGeometryModifier_SpatialNoise *p = (LineStyleGeometryModifier_SpatialNoise *)m;
924                         LineStyleGeometryModifier_SpatialNoise *q = (LineStyleGeometryModifier_SpatialNoise *)new_m;
925                         q->amplitude = p->amplitude;
926                         q->scale = p->scale;
927                         q->octaves = p->octaves;
928                         q->flags = p->flags;
929                         break;
930                 }
931                 case LS_MODIFIER_PERLIN_NOISE_1D:
932                 {
933                         LineStyleGeometryModifier_PerlinNoise1D *p = (LineStyleGeometryModifier_PerlinNoise1D *)m;
934                         LineStyleGeometryModifier_PerlinNoise1D *q = (LineStyleGeometryModifier_PerlinNoise1D *)new_m;
935                         q->frequency = p->frequency;
936                         q->amplitude = p->amplitude;
937                         q->angle = p->angle;
938                         q->octaves = p->octaves;
939                         q->seed = p->seed;
940                         break;
941                 }
942                 case LS_MODIFIER_PERLIN_NOISE_2D:
943                 {
944                         LineStyleGeometryModifier_PerlinNoise2D *p = (LineStyleGeometryModifier_PerlinNoise2D *)m;
945                         LineStyleGeometryModifier_PerlinNoise2D *q = (LineStyleGeometryModifier_PerlinNoise2D *)new_m;
946                         q->frequency = p->frequency;
947                         q->amplitude = p->amplitude;
948                         q->angle = p->angle;
949                         q->octaves = p->octaves;
950                         q->seed = p->seed;
951                         break;
952                 }
953                 case LS_MODIFIER_BACKBONE_STRETCHER:
954                 {
955                         LineStyleGeometryModifier_BackboneStretcher *p = (LineStyleGeometryModifier_BackboneStretcher *)m;
956                         LineStyleGeometryModifier_BackboneStretcher *q = (LineStyleGeometryModifier_BackboneStretcher *)new_m;
957                         q->backbone_length = p->backbone_length;
958                         break;
959                 }
960                 case LS_MODIFIER_TIP_REMOVER:
961                 {
962                         LineStyleGeometryModifier_TipRemover *p = (LineStyleGeometryModifier_TipRemover *)m;
963                         LineStyleGeometryModifier_TipRemover *q = (LineStyleGeometryModifier_TipRemover *)new_m;
964                         q->tip_length = p->tip_length;
965                         break;
966                 }
967                 case LS_MODIFIER_POLYGONIZATION:
968                 {
969                         LineStyleGeometryModifier_Polygonalization *p = (LineStyleGeometryModifier_Polygonalization *)m;
970                         LineStyleGeometryModifier_Polygonalization *q = (LineStyleGeometryModifier_Polygonalization *)new_m;
971                         q->error = p->error;
972                         break;
973                 }
974                 case LS_MODIFIER_GUIDING_LINES:
975                 {
976                         LineStyleGeometryModifier_GuidingLines *p = (LineStyleGeometryModifier_GuidingLines *)m;
977                         LineStyleGeometryModifier_GuidingLines *q = (LineStyleGeometryModifier_GuidingLines *)new_m;
978                         q->offset = p->offset;
979                         break;
980                 }
981                 case LS_MODIFIER_BLUEPRINT:
982                 {
983                         LineStyleGeometryModifier_Blueprint *p = (LineStyleGeometryModifier_Blueprint *)m;
984                         LineStyleGeometryModifier_Blueprint *q = (LineStyleGeometryModifier_Blueprint *)new_m;
985                         q->flags = p->flags;
986                         q->rounds = p->rounds;
987                         q->backbone_length = p->backbone_length;
988                         q->random_radius = p->random_radius;
989                         q->random_center = p->random_center;
990                         q->random_backbone = p->random_backbone;
991                         break;
992                 }
993                 case LS_MODIFIER_2D_OFFSET:
994                 {
995                         LineStyleGeometryModifier_2DOffset *p = (LineStyleGeometryModifier_2DOffset *)m;
996                         LineStyleGeometryModifier_2DOffset *q = (LineStyleGeometryModifier_2DOffset *)new_m;
997                         q->start = p->start;
998                         q->end = p->end;
999                         q->x = p->x;
1000                         q->y = p->y;
1001                         break;
1002                 }
1003                 case LS_MODIFIER_2D_TRANSFORM:
1004                 {
1005                         LineStyleGeometryModifier_2DTransform *p = (LineStyleGeometryModifier_2DTransform *)m;
1006                         LineStyleGeometryModifier_2DTransform *q = (LineStyleGeometryModifier_2DTransform *)new_m;
1007                         q->pivot = p->pivot;
1008                         q->scale_x = p->scale_x;
1009                         q->scale_y = p->scale_y;
1010                         q->angle = p->angle;
1011                         q->pivot_u = p->pivot_u;
1012                         q->pivot_x = p->pivot_x;
1013                         q->pivot_y = p->pivot_y;
1014                         break;
1015                 }
1016                 default:
1017                         return NULL; /* unknown modifier type */
1018         }
1019         add_to_modifier_list(&linestyle->geometry_modifiers, new_m);
1020
1021         return new_m;
1022 }
1023
1024 int BKE_linestyle_modifier_remove_geometry(FreestyleLineStyle *linestyle, LineStyleModifier *m)
1025 {
1026         if (BLI_findindex(&linestyle->geometry_modifiers, m) == -1)
1027                 return -1;
1028         BLI_freelinkN(&linestyle->geometry_modifiers, m);
1029         return 0;
1030 }
1031
1032 static void move_modifier(ListBase *lb, LineStyleModifier *modifier, int direction)
1033 {
1034         BLI_remlink(lb, modifier);
1035         if (direction > 0)
1036                 BLI_insertlinkbefore(lb, modifier->prev, modifier);
1037         else
1038                 BLI_insertlinkafter(lb, modifier->next, modifier);
1039 }
1040
1041 void BKE_linestyle_modifier_move_color(FreestyleLineStyle *linestyle, LineStyleModifier *modifier, int direction)
1042 {
1043         move_modifier(&linestyle->color_modifiers, modifier, direction);
1044 }
1045
1046 void BKE_linestyle_modifier_move_alpha(FreestyleLineStyle *linestyle, LineStyleModifier *modifier, int direction)
1047 {
1048         move_modifier(&linestyle->alpha_modifiers, modifier, direction);
1049 }
1050
1051 void BKE_linestyle_modifier_move_thickness(FreestyleLineStyle *linestyle, LineStyleModifier *modifier, int direction)
1052 {
1053         move_modifier(&linestyle->thickness_modifiers, modifier, direction);
1054 }
1055
1056 void BKE_linestyle_modifier_move_geometry(FreestyleLineStyle *linestyle, LineStyleModifier *modifier, int direction)
1057 {
1058         move_modifier(&linestyle->geometry_modifiers, modifier, direction);
1059 }
1060
1061 void BKE_linestyle_modifier_list_color_ramps(FreestyleLineStyle *linestyle, ListBase *listbase)
1062 {
1063         LineStyleModifier *m;
1064         ColorBand *color_ramp;
1065         LinkData *link;
1066
1067         BLI_listbase_clear(listbase);
1068
1069         for (m = (LineStyleModifier *)linestyle->color_modifiers.first; m; m = m->next) {
1070                 switch (m->type) {
1071                         case LS_MODIFIER_ALONG_STROKE:
1072                                 color_ramp = ((LineStyleColorModifier_AlongStroke *)m)->color_ramp;
1073                                 break;
1074                         case LS_MODIFIER_DISTANCE_FROM_CAMERA:
1075                                 color_ramp = ((LineStyleColorModifier_DistanceFromCamera *)m)->color_ramp;
1076                                 break;
1077                         case LS_MODIFIER_DISTANCE_FROM_OBJECT:
1078                                 color_ramp = ((LineStyleColorModifier_DistanceFromObject *)m)->color_ramp;
1079                                 break;
1080                         case LS_MODIFIER_MATERIAL:
1081                                 color_ramp = ((LineStyleColorModifier_Material *)m)->color_ramp;
1082                                 break;
1083                         default:
1084                                 continue;
1085                 }
1086                 link = (LinkData *) MEM_callocN(sizeof(LinkData), "link to color ramp");
1087                 link->data = color_ramp;
1088                 BLI_addtail(listbase, link);
1089         }
1090 }
1091
1092 char *BKE_linestyle_path_to_color_ramp(FreestyleLineStyle *linestyle, ColorBand *color_ramp)
1093 {
1094         LineStyleModifier *m;
1095         bool found = false;
1096
1097         for (m = (LineStyleModifier *)linestyle->color_modifiers.first; m; m = m->next) {
1098                 switch (m->type) {
1099                         case LS_MODIFIER_ALONG_STROKE:
1100                                 if (color_ramp == ((LineStyleColorModifier_AlongStroke *)m)->color_ramp)
1101                                         found = true;
1102                                 break;
1103                         case LS_MODIFIER_DISTANCE_FROM_CAMERA:
1104                                 if (color_ramp == ((LineStyleColorModifier_DistanceFromCamera *)m)->color_ramp)
1105                                         found = true;
1106                                 break;
1107                         case LS_MODIFIER_DISTANCE_FROM_OBJECT:
1108                                 if (color_ramp == ((LineStyleColorModifier_DistanceFromObject *)m)->color_ramp)
1109                                         found = true;
1110                                 break;
1111                         case LS_MODIFIER_MATERIAL:
1112                                 if (color_ramp == ((LineStyleColorModifier_Material *)m)->color_ramp)
1113                                         found = true;
1114                                 break;
1115                 }
1116
1117                 if (found) {
1118                         char name_esc[sizeof(m->name) * 2];
1119                         BLI_strescape(name_esc, m->name, sizeof(name_esc));
1120                         return BLI_sprintfN("color_modifiers[\"%s\"].color_ramp", name_esc);
1121                 }
1122         }
1123         printf("BKE_linestyle_path_to_color_ramp: No color ramps correspond to the given pointer.\n");
1124         return NULL;
1125 }
1126
1127 void BKE_linestyle_target_object_unlink(FreestyleLineStyle *linestyle, struct Object *ob)
1128 {
1129         LineStyleModifier *m;
1130
1131         for (m = (LineStyleModifier *)linestyle->color_modifiers.first; m; m = m->next) {
1132                 if (m->type == LS_MODIFIER_DISTANCE_FROM_OBJECT) {
1133                         if (((LineStyleColorModifier_DistanceFromObject *)m)->target == ob) {
1134                                 ((LineStyleColorModifier_DistanceFromObject *)m)->target = NULL;
1135                         }
1136                 }
1137         }
1138         for (m = (LineStyleModifier *)linestyle->alpha_modifiers.first; m; m = m->next) {
1139                 if (m->type == LS_MODIFIER_DISTANCE_FROM_OBJECT) {
1140                         if (((LineStyleAlphaModifier_DistanceFromObject *)m)->target == ob) {
1141                                 ((LineStyleAlphaModifier_DistanceFromObject *)m)->target = NULL;
1142                         }
1143                 }
1144         }
1145         for (m = (LineStyleModifier *)linestyle->thickness_modifiers.first; m; m = m->next) {
1146                 if (m->type == LS_MODIFIER_DISTANCE_FROM_OBJECT) {
1147                         if (((LineStyleThicknessModifier_DistanceFromObject *)m)->target == ob) {
1148                                 ((LineStyleThicknessModifier_DistanceFromObject *)m)->target = NULL;
1149                         }
1150                 }
1151         }
1152 }