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
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
|
/*
* ***** 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) 2005 by the Blender Foundation.
* All rights reserved.
*
* Contributor(s): Daniel Dunbar
* Ton Roosendaal,
* Ben Batt,
* Brecht Van Lommel,
* Campbell Barton
*
* ***** END GPL LICENSE BLOCK *****
*
* Modifier stack implementation.
*
* BKE_modifier.h contains the function prototypes for this file.
*
*/
/** \file blender/blenkernel/intern/modifier.c
* \ingroup bke
*/
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#include <stdarg.h>
#include <math.h>
#include <float.h>
#include "MEM_guardedalloc.h"
#include "DNA_armature_types.h"
#include "DNA_mesh_types.h"
#include "DNA_object_types.h"
#include "BLI_utildefines.h"
#include "BLI_listbase.h"
#include "BLI_linklist.h"
#include "BLI_path_util.h"
#include "BLI_string.h"
#include "BLI_string_utils.h"
#include "BLT_translation.h"
#include "BKE_appdir.h"
#include "BKE_cdderivedmesh.h"
#include "BKE_idcode.h"
#include "BKE_key.h"
#include "BKE_library.h"
#include "BKE_library_query.h"
#include "BKE_mesh.h"
#include "BKE_multires.h"
#include "BKE_DerivedMesh.h"
/* may move these, only for modifier_path_relbase */
#include "BKE_global.h" /* ugh, G.main->name only */
#include "BKE_main.h"
/* end */
#include "DEG_depsgraph.h"
#include "MOD_modifiertypes.h"
static ModifierTypeInfo *modifier_types[NUM_MODIFIER_TYPES] = {NULL};
static VirtualModifierData virtualModifierCommonData;
void BKE_modifier_init(void)
{
ModifierData *md;
/* Initialize modifier types */
modifier_type_init(modifier_types); /* MOD_utils.c */
/* Initialize global cmmon storage used for virtual modifier list */
md = modifier_new(eModifierType_Armature);
virtualModifierCommonData.amd = *((ArmatureModifierData *) md);
modifier_free(md);
md = modifier_new(eModifierType_Curve);
virtualModifierCommonData.cmd = *((CurveModifierData *) md);
modifier_free(md);
md = modifier_new(eModifierType_Lattice);
virtualModifierCommonData.lmd = *((LatticeModifierData *) md);
modifier_free(md);
md = modifier_new(eModifierType_ShapeKey);
virtualModifierCommonData.smd = *((ShapeKeyModifierData *) md);
modifier_free(md);
virtualModifierCommonData.amd.modifier.mode |= eModifierMode_Virtual;
virtualModifierCommonData.cmd.modifier.mode |= eModifierMode_Virtual;
virtualModifierCommonData.lmd.modifier.mode |= eModifierMode_Virtual;
virtualModifierCommonData.smd.modifier.mode |= eModifierMode_Virtual;
}
const ModifierTypeInfo *modifierType_getInfo(ModifierType type)
{
/* type unsigned, no need to check < 0 */
if (type < NUM_MODIFIER_TYPES && modifier_types[type]->name[0] != '\0') {
return modifier_types[type];
}
else {
return NULL;
}
}
/***/
ModifierData *modifier_new(int type)
{
const ModifierTypeInfo *mti = modifierType_getInfo(type);
ModifierData *md = MEM_callocN(mti->structSize, mti->structName);
/* note, this name must be made unique later */
BLI_strncpy(md->name, DATA_(mti->name), sizeof(md->name));
md->type = type;
md->mode = eModifierMode_Realtime | eModifierMode_Render | eModifierMode_Expanded;
md->flag = eModifierFlag_StaticOverride_Local;
if (mti->flags & eModifierTypeFlag_EnableInEditmode)
md->mode |= eModifierMode_Editmode;
if (mti->initData) mti->initData(md);
return md;
}
static void modifier_free_data_id_us_cb(void *UNUSED(userData), Object *UNUSED(ob), ID **idpoin, int cb_flag)
{
ID *id = *idpoin;
if (id != NULL && (cb_flag & IDWALK_CB_USER) != 0) {
id_us_min(id);
}
}
void modifier_free_ex(ModifierData *md, const int flag)
{
const ModifierTypeInfo *mti = modifierType_getInfo(md->type);
if ((flag & LIB_ID_CREATE_NO_USER_REFCOUNT) == 0) {
if (mti->foreachIDLink) {
mti->foreachIDLink(md, NULL, modifier_free_data_id_us_cb, NULL);
}
else if (mti->foreachObjectLink) {
mti->foreachObjectLink(md, NULL, (ObjectWalkFunc)modifier_free_data_id_us_cb, NULL);
}
}
if (mti->freeData) mti->freeData(md);
if (md->error) MEM_freeN(md->error);
MEM_freeN(md);
}
void modifier_free(ModifierData *md)
{
modifier_free_ex(md, 0);
}
bool modifier_unique_name(ListBase *modifiers, ModifierData *md)
{
if (modifiers && md) {
const ModifierTypeInfo *mti = modifierType_getInfo(md->type);
return BLI_uniquename(modifiers, md, DATA_(mti->name), '.', offsetof(ModifierData, name), sizeof(md->name));
}
return false;
}
bool modifier_dependsOnTime(ModifierData *md)
{
const ModifierTypeInfo *mti = modifierType_getInfo(md->type);
return mti->dependsOnTime && mti->dependsOnTime(md);
}
bool modifier_supportsMapping(ModifierData *md)
{
const ModifierTypeInfo *mti = modifierType_getInfo(md->type);
return (mti->type == eModifierTypeType_OnlyDeform ||
(mti->flags & eModifierTypeFlag_SupportsMapping));
}
bool modifier_isPreview(ModifierData *md)
{
const ModifierTypeInfo *mti = modifierType_getInfo(md->type);
/* Constructive modifiers are highly likely to also modify data like vgroups or vcol! */
if (!((mti->flags & eModifierTypeFlag_UsesPreview) || (mti->type == eModifierTypeType_Constructive))) {
return false;
}
if (md->mode & eModifierMode_Realtime) {
return true;
}
return false;
}
ModifierData *modifiers_findByType(Object *ob, ModifierType type)
{
ModifierData *md = ob->modifiers.first;
for (; md; md = md->next)
if (md->type == type)
break;
return md;
}
ModifierData *modifiers_findByName(Object *ob, const char *name)
{
return BLI_findstring(&(ob->modifiers), name, offsetof(ModifierData, name));
}
void modifiers_clearErrors(Object *ob)
{
ModifierData *md = ob->modifiers.first;
/* int qRedraw = 0; */
for (; md; md = md->next) {
if (md->error) {
MEM_freeN(md->error);
md->error = NULL;
/* qRedraw = 1; */
}
}
}
void modifiers_foreachObjectLink(Object *ob, ObjectWalkFunc walk, void *userData)
{
ModifierData *md = ob->modifiers.first;
for (; md; md = md->next) {
const ModifierTypeInfo *mti = modifierType_getInfo(md->type);
if (mti->foreachObjectLink)
mti->foreachObjectLink(md, ob, walk, userData);
}
}
void modifiers_foreachIDLink(Object *ob, IDWalkFunc walk, void *userData)
{
ModifierData *md = ob->modifiers.first;
for (; md; md = md->next) {
const ModifierTypeInfo *mti = modifierType_getInfo(md->type);
if (mti->foreachIDLink) mti->foreachIDLink(md, ob, walk, userData);
else if (mti->foreachObjectLink) {
/* each Object can masquerade as an ID, so this should be OK */
ObjectWalkFunc fp = (ObjectWalkFunc)walk;
mti->foreachObjectLink(md, ob, fp, userData);
}
}
}
void modifiers_foreachTexLink(Object *ob, TexWalkFunc walk, void *userData)
{
ModifierData *md = ob->modifiers.first;
for (; md; md = md->next) {
const ModifierTypeInfo *mti = modifierType_getInfo(md->type);
if (mti->foreachTexLink)
mti->foreachTexLink(md, ob, walk, userData);
}
}
/* callback's can use this
* to avoid copying every member.
*/
void modifier_copyData_generic(const ModifierData *md_src, ModifierData *md_dst)
{
const ModifierTypeInfo *mti = modifierType_getInfo(md_src->type);
/* md_dst may have alredy be fully initialized with some extra allocated data,
* we need to free it now to avoid memleak. */
if (mti->freeData) {
mti->freeData(md_dst);
}
const size_t data_size = sizeof(ModifierData);
const char *md_src_data = ((const char *)md_src) + data_size;
char *md_dst_data = ((char *)md_dst) + data_size;
BLI_assert(data_size <= (size_t)mti->structSize);
memcpy(md_dst_data, md_src_data, (size_t)mti->structSize - data_size);
}
static void modifier_copy_data_id_us_cb(void *UNUSED(userData), Object *UNUSED(ob), ID **idpoin, int cb_flag)
{
ID *id = *idpoin;
if (id != NULL && (cb_flag & IDWALK_CB_USER) != 0) {
id_us_plus(id);
}
}
void modifier_copyData_ex(ModifierData *md, ModifierData *target, const int flag)
{
const ModifierTypeInfo *mti = modifierType_getInfo(md->type);
target->mode = md->mode;
target->flag = md->flag;
if (mti->copyData) {
mti->copyData(md, target);
}
if ((flag & LIB_ID_CREATE_NO_USER_REFCOUNT) == 0) {
if (mti->foreachIDLink) {
mti->foreachIDLink(target, NULL, modifier_copy_data_id_us_cb, NULL);
}
else if (mti->foreachObjectLink) {
mti->foreachObjectLink(target, NULL, (ObjectWalkFunc)modifier_copy_data_id_us_cb, NULL);
}
}
}
void modifier_copyData(ModifierData *md, ModifierData *target)
{
modifier_copyData_ex(md, target, 0);
}
bool modifier_supportsCage(struct Scene *scene, ModifierData *md)
{
const ModifierTypeInfo *mti = modifierType_getInfo(md->type);
md->scene = scene;
return ((!mti->isDisabled || !mti->isDisabled(md, 0)) &&
(mti->flags & eModifierTypeFlag_SupportsEditmode) &&
modifier_supportsMapping(md));
}
bool modifier_couldBeCage(struct Scene *scene, ModifierData *md)
{
const ModifierTypeInfo *mti = modifierType_getInfo(md->type);
md->scene = scene;
return ((md->mode & eModifierMode_Realtime) &&
(md->mode & eModifierMode_Editmode) &&
(!mti->isDisabled || !mti->isDisabled(md, 0)) &&
modifier_supportsMapping(md));
}
bool modifier_isSameTopology(ModifierData *md)
{
const ModifierTypeInfo *mti = modifierType_getInfo(md->type);
return ELEM(mti->type, eModifierTypeType_OnlyDeform, eModifierTypeType_NonGeometrical);
}
bool modifier_isNonGeometrical(ModifierData *md)
{
const ModifierTypeInfo *mti = modifierType_getInfo(md->type);
return (mti->type == eModifierTypeType_NonGeometrical);
}
void modifier_setError(ModifierData *md, const char *_format, ...)
{
char buffer[512];
va_list ap;
const char *format = TIP_(_format);
va_start(ap, _format);
vsnprintf(buffer, sizeof(buffer), format, ap);
va_end(ap);
buffer[sizeof(buffer) - 1] = '\0';
if (md->error)
MEM_freeN(md->error);
md->error = BLI_strdup(buffer);
}
/* used for buttons, to find out if the 'draw deformed in editmode' option is
* there
*
* also used in transform_conversion.c, to detect CrazySpace [tm] (2nd arg
* then is NULL)
* also used for some mesh tools to give warnings
*/
int modifiers_getCageIndex(struct Scene *scene, Object *ob, int *r_lastPossibleCageIndex, bool is_virtual)
{
VirtualModifierData virtualModifierData;
ModifierData *md = (is_virtual) ? modifiers_getVirtualModifierList(ob, &virtualModifierData) : ob->modifiers.first;
int i, cageIndex = -1;
if (r_lastPossibleCageIndex) {
/* ensure the value is initialized */
*r_lastPossibleCageIndex = -1;
}
/* Find the last modifier acting on the cage. */
for (i = 0; md; i++, md = md->next) {
const ModifierTypeInfo *mti = modifierType_getInfo(md->type);
bool supports_mapping;
md->scene = scene;
if (mti->isDisabled && mti->isDisabled(md, 0)) continue;
if (!(mti->flags & eModifierTypeFlag_SupportsEditmode)) continue;
if (md->mode & eModifierMode_DisableTemporary) continue;
supports_mapping = modifier_supportsMapping(md);
if (r_lastPossibleCageIndex && supports_mapping) {
*r_lastPossibleCageIndex = i;
}
if (!(md->mode & eModifierMode_Realtime)) continue;
if (!(md->mode & eModifierMode_Editmode)) continue;
if (!supports_mapping)
break;
if (md->mode & eModifierMode_OnCage)
cageIndex = i;
}
return cageIndex;
}
bool modifiers_isSoftbodyEnabled(Object *ob)
{
ModifierData *md = modifiers_findByType(ob, eModifierType_Softbody);
return (md && md->mode & (eModifierMode_Realtime | eModifierMode_Render));
}
bool modifiers_isClothEnabled(Object *ob)
{
ModifierData *md = modifiers_findByType(ob, eModifierType_Cloth);
return (md && md->mode & (eModifierMode_Realtime | eModifierMode_Render));
}
bool modifiers_isModifierEnabled(Object *ob, int modifierType)
{
ModifierData *md = modifiers_findByType(ob, modifierType);
return (md && md->mode & (eModifierMode_Realtime | eModifierMode_Render));
}
bool modifiers_isParticleEnabled(Object *ob)
{
ModifierData *md = modifiers_findByType(ob, eModifierType_ParticleSystem);
return (md && md->mode & (eModifierMode_Realtime | eModifierMode_Render));
}
/**
* Check whether is enabled.
*
* \param scene Current scene, may be NULL, in which case isDisabled callback of the modifier is never called.
*/
bool modifier_isEnabled(struct Scene *scene, ModifierData *md, int required_mode)
{
const ModifierTypeInfo *mti = modifierType_getInfo(md->type);
md->scene = scene;
if ((md->mode & required_mode) != required_mode) return false;
if (scene != NULL && mti->isDisabled && mti->isDisabled(md, required_mode == eModifierMode_Render)) return false;
if (md->mode & eModifierMode_DisableTemporary) return false;
if ((required_mode & eModifierMode_Editmode) && !(mti->flags & eModifierTypeFlag_SupportsEditmode)) return false;
return true;
}
CDMaskLink *modifiers_calcDataMasks(struct Scene *scene, Object *ob, ModifierData *md,
CustomDataMask dataMask, int required_mode,
ModifierData *previewmd, CustomDataMask previewmask)
{
CDMaskLink *dataMasks = NULL;
CDMaskLink *curr, *prev;
/* build a list of modifier data requirements in reverse order */
for (; md; md = md->next) {
const ModifierTypeInfo *mti = modifierType_getInfo(md->type);
curr = MEM_callocN(sizeof(CDMaskLink), "CDMaskLink");
if (modifier_isEnabled(scene, md, required_mode)) {
if (mti->requiredDataMask)
curr->mask = mti->requiredDataMask(ob, md);
if (previewmd == md) {
curr->mask |= previewmask;
}
}
/* prepend new datamask */
curr->next = dataMasks;
dataMasks = curr;
}
/* build the list of required data masks - each mask in the list must
* include all elements of the masks that follow it
*
* note the list is currently in reverse order, so "masks that follow it"
* actually means "masks that precede it" at the moment
*/
for (curr = dataMasks, prev = NULL; curr; prev = curr, curr = curr->next) {
if (prev) {
CustomDataMask prev_mask = prev->mask;
CustomDataMask curr_mask = curr->mask;
curr->mask = curr_mask | prev_mask;
}
else {
CustomDataMask curr_mask = curr->mask;
curr->mask = curr_mask | dataMask;
}
}
/* reverse the list so it's in the correct order */
BLI_linklist_reverse((LinkNode **)&dataMasks);
return dataMasks;
}
ModifierData *modifiers_getLastPreview(struct Scene *scene, ModifierData *md, int required_mode)
{
ModifierData *tmp_md = NULL;
if ((required_mode & ~eModifierMode_Editmode) != eModifierMode_Realtime)
return tmp_md;
/* Find the latest modifier in stack generating preview. */
for (; md; md = md->next) {
if (modifier_isEnabled(scene, md, required_mode) && modifier_isPreview(md))
tmp_md = md;
}
return tmp_md;
}
/* NOTE: This is to support old files from before Blender supported modifiers,
* in some cases versioning code updates these so for new files this will
* return an empty list. */
ModifierData *modifiers_getVirtualModifierList(Object *ob, VirtualModifierData *virtualModifierData)
{
ModifierData *md;
md = ob->modifiers.first;
*virtualModifierData = virtualModifierCommonData;
if (ob->parent) {
if (ob->parent->type == OB_ARMATURE && ob->partype == PARSKEL) {
virtualModifierData->amd.object = ob->parent;
virtualModifierData->amd.modifier.next = md;
virtualModifierData->amd.deformflag = ((bArmature *)(ob->parent->data))->deformflag;
md = &virtualModifierData->amd.modifier;
}
else if (ob->parent->type == OB_CURVE && ob->partype == PARSKEL) {
virtualModifierData->cmd.object = ob->parent;
virtualModifierData->cmd.defaxis = ob->trackflag + 1;
virtualModifierData->cmd.modifier.next = md;
md = &virtualModifierData->cmd.modifier;
}
else if (ob->parent->type == OB_LATTICE && ob->partype == PARSKEL) {
virtualModifierData->lmd.object = ob->parent;
virtualModifierData->lmd.modifier.next = md;
md = &virtualModifierData->lmd.modifier;
}
}
/* shape key modifier, not yet for curves */
if (ELEM(ob->type, OB_MESH, OB_LATTICE) && BKE_key_from_object(ob)) {
if (ob->type == OB_MESH && (ob->shapeflag & OB_SHAPE_EDIT_MODE))
virtualModifierData->smd.modifier.mode |= eModifierMode_Editmode | eModifierMode_OnCage;
else
virtualModifierData->smd.modifier.mode &= ~eModifierMode_Editmode | eModifierMode_OnCage;
virtualModifierData->smd.modifier.next = md;
md = &virtualModifierData->smd.modifier;
}
return md;
}
/* Takes an object and returns its first selected armature, else just its armature
* This should work for multiple armatures per object
*/
Object *modifiers_isDeformedByArmature(Object *ob)
{
VirtualModifierData virtualModifierData;
ModifierData *md = modifiers_getVirtualModifierList(ob, &virtualModifierData);
ArmatureModifierData *amd = NULL;
/* return the first selected armature, this lets us use multiple armatures */
for (; md; md = md->next) {
if (md->type == eModifierType_Armature) {
amd = (ArmatureModifierData *) md;
if (amd->object && (amd->object->flag & SELECT))
return amd->object;
}
}
if (amd) /* if were still here then return the last armature */
return amd->object;
return NULL;
}
Object *modifiers_isDeformedByMeshDeform(Object *ob)
{
VirtualModifierData virtualModifierData;
ModifierData *md = modifiers_getVirtualModifierList(ob, &virtualModifierData);
MeshDeformModifierData *mdmd = NULL;
/* return the first selected armature, this lets us use multiple armatures */
for (; md; md = md->next) {
if (md->type == eModifierType_MeshDeform) {
mdmd = (MeshDeformModifierData *) md;
if (mdmd->object && (mdmd->object->flag & SELECT))
return mdmd->object;
}
}
if (mdmd) /* if were still here then return the last armature */
return mdmd->object;
return NULL;
}
/* Takes an object and returns its first selected lattice, else just its lattice
* This should work for multiple lattices per object
*/
Object *modifiers_isDeformedByLattice(Object *ob)
{
VirtualModifierData virtualModifierData;
ModifierData *md = modifiers_getVirtualModifierList(ob, &virtualModifierData);
LatticeModifierData *lmd = NULL;
/* return the first selected lattice, this lets us use multiple lattices */
for (; md; md = md->next) {
if (md->type == eModifierType_Lattice) {
lmd = (LatticeModifierData *) md;
if (lmd->object && (lmd->object->flag & SELECT))
return lmd->object;
}
}
if (lmd) /* if were still here then return the last lattice */
return lmd->object;
return NULL;
}
/* Takes an object and returns its first selected curve, else just its curve
* This should work for multiple curves per object
*/
Object *modifiers_isDeformedByCurve(Object *ob)
{
VirtualModifierData virtualModifierData;
ModifierData *md = modifiers_getVirtualModifierList(ob, &virtualModifierData);
CurveModifierData *cmd = NULL;
/* return the first selected curve, this lets us use multiple curves */
for (; md; md = md->next) {
if (md->type == eModifierType_Curve) {
cmd = (CurveModifierData *) md;
if (cmd->object && (cmd->object->flag & SELECT))
return cmd->object;
}
}
if (cmd) /* if were still here then return the last curve */
return cmd->object;
return NULL;
}
bool modifiers_usesArmature(Object *ob, bArmature *arm)
{
VirtualModifierData virtualModifierData;
ModifierData *md = modifiers_getVirtualModifierList(ob, &virtualModifierData);
for (; md; md = md->next) {
if (md->type == eModifierType_Armature) {
ArmatureModifierData *amd = (ArmatureModifierData *) md;
if (amd->object && amd->object->data == arm)
return true;
}
}
return false;
}
bool modifier_isCorrectableDeformed(ModifierData *md)
{
const ModifierTypeInfo *mti = modifierType_getInfo(md->type);
return (mti->deformMatricesEM != NULL) || (mti->deformMatricesEM_DM != NULL);
}
bool modifiers_isCorrectableDeformed(struct Scene *scene, Object *ob)
{
VirtualModifierData virtualModifierData;
ModifierData *md = modifiers_getVirtualModifierList(ob, &virtualModifierData);
int required_mode = eModifierMode_Realtime;
if (ob->mode == OB_MODE_EDIT) {
required_mode |= eModifierMode_Editmode;
}
for (; md; md = md->next) {
if (!modifier_isEnabled(scene, md, required_mode)) {
/* pass */
}
else if (modifier_isCorrectableDeformed(md)) {
return true;
}
}
return false;
}
/* Check whether the given object has a modifier in its stack that uses WEIGHT_MCOL CD layer
* to preview something... Used by DynamicPaint and WeightVG currently. */
bool modifiers_isPreview(Object *ob)
{
ModifierData *md = ob->modifiers.first;
for (; md; md = md->next) {
if (modifier_isPreview(md))
return true;
}
return false;
}
void modifier_freeTemporaryData(ModifierData *md)
{
if (md->type == eModifierType_Armature) {
ArmatureModifierData *amd = (ArmatureModifierData *)md;
if (amd->prevCos) {
MEM_freeN(amd->prevCos);
amd->prevCos = NULL;
}
}
}
/* ensure modifier correctness when changing ob->data */
void test_object_modifiers(Object *ob)
{
ModifierData *md;
/* just multires checked for now, since only multires
* modifies mesh data */
if (ob->type != OB_MESH) return;
for (md = ob->modifiers.first; md; md = md->next) {
if (md->type == eModifierType_Multires) {
MultiresModifierData *mmd = (MultiresModifierData *)md;
multiresModifier_set_levels_from_disps(mmd, ob);
}
}
}
/* where should this go?, it doesnt fit well anywhere :S - campbell */
/* elubie: changed this to default to the same dir as the render output
* to prevent saving to C:\ on Windows */
/* campbell: logic behind this...
*
* - if the ID is from a library, return library path
* - else if the file has been saved return the blend file path.
* - else if the file isn't saved and the ID isn't from a library, return the temp dir.
*/
const char *modifier_path_relbase(Object *ob)
{
if (G.relbase_valid || ID_IS_LINKED(ob)) {
return ID_BLEND_PATH(G.main, &ob->id);
}
else {
/* last resort, better then using "" which resolves to the current
* working directory */
return BKE_tempdir_session();
}
}
/* initializes the path with either */
void modifier_path_init(char *path, int path_maxlen, const char *name)
{
/* elubie: changed this to default to the same dir as the render output
* to prevent saving to C:\ on Windows */
BLI_join_dirfile(path, path_maxlen,
G.relbase_valid ? "//" : BKE_tempdir_session(),
name);
}
/* wrapper around ModifierTypeInfo.applyModifier that ensures valid normals */
struct DerivedMesh *modwrap_applyModifier(
ModifierData *md, const ModifierEvalContext *ctx,
struct DerivedMesh *dm)
{
const ModifierTypeInfo *mti = modifierType_getInfo(md->type);
BLI_assert(CustomData_has_layer(&dm->polyData, CD_NORMAL) == false);
if (mti->dependsOnNormals && mti->dependsOnNormals(md)) {
DM_ensure_normals(dm);
}
return modifier_applyModifier_DM_deprecated(md, ctx, dm);
}
struct DerivedMesh *modwrap_applyModifierEM(
ModifierData *md, const ModifierEvalContext *ctx,
struct BMEditMesh *em, DerivedMesh *dm)
{
const ModifierTypeInfo *mti = modifierType_getInfo(md->type);
BLI_assert(CustomData_has_layer(&dm->polyData, CD_NORMAL) == false);
if (mti->dependsOnNormals && mti->dependsOnNormals(md)) {
DM_ensure_normals(dm);
}
return modifier_applyModifierEM_DM_deprecated(md, ctx, em, dm);
}
void modwrap_deformVerts(
ModifierData *md, const ModifierEvalContext *ctx,
DerivedMesh *dm, float (*vertexCos)[3], int numVerts)
{
const ModifierTypeInfo *mti = modifierType_getInfo(md->type);
BLI_assert(!dm || CustomData_has_layer(&dm->polyData, CD_NORMAL) == false);
if (dm && mti->dependsOnNormals && mti->dependsOnNormals(md)) {
DM_ensure_normals(dm);
}
modifier_deformVerts_DM_deprecated(md, ctx, dm, vertexCos, numVerts);
}
void modwrap_deformVertsEM(
ModifierData *md, const ModifierEvalContext *ctx,
struct BMEditMesh *em, DerivedMesh *dm,
float (*vertexCos)[3], int numVerts)
{
const ModifierTypeInfo *mti = modifierType_getInfo(md->type);
BLI_assert(!dm || CustomData_has_layer(&dm->polyData, CD_NORMAL) == false);
if (dm && mti->dependsOnNormals && mti->dependsOnNormals(md)) {
DM_ensure_normals(dm);
}
modifier_deformVertsEM_DM_deprecated(md, ctx, em, dm, vertexCos, numVerts);
}
/* end modifier callback wrappers */
/* wrappers for modifier callbacks that accept Mesh and select the proper implementation
* depending on if the modifier has been ported to Mesh or is still using DerivedMesh
*/
void modifier_deformVerts(struct ModifierData *md, const ModifierEvalContext *ctx,
struct Mesh *mesh,
float (*vertexCos)[3], int numVerts)
{
const ModifierTypeInfo *mti = modifierType_getInfo(md->type);
if (mti->deformVerts) {
mti->deformVerts(md, ctx, mesh, vertexCos, numVerts);
}
else {
DerivedMesh *dm = NULL;
if (mesh) {
dm = CDDM_from_mesh(mesh);
}
mti->deformVerts_DM(md, ctx, dm, vertexCos, numVerts);
if (dm) {
dm->release(dm);
}
}
}
void modifier_deformMatrices(struct ModifierData *md, const ModifierEvalContext *ctx,
struct Mesh *mesh,
float (*vertexCos)[3], float (*defMats)[3][3], int numVerts)
{
const ModifierTypeInfo *mti = modifierType_getInfo(md->type);
if (mti->deformMatrices) {
mti->deformMatrices(md, ctx, mesh, vertexCos, defMats, numVerts);
}
else {
DerivedMesh *dm = NULL;
if (mesh) {
dm = CDDM_from_mesh(mesh);
}
mti->deformMatrices_DM(md, ctx, dm, vertexCos, defMats, numVerts);
if (dm) {
dm->release(dm);
}
}
}
void modifier_deformVertsEM(struct ModifierData *md, const ModifierEvalContext *ctx,
struct BMEditMesh *editData, struct Mesh *mesh,
float (*vertexCos)[3], int numVerts)
{
const ModifierTypeInfo *mti = modifierType_getInfo(md->type);
if (mti->deformVertsEM) {
mti->deformVertsEM(md, ctx, editData, mesh, vertexCos, numVerts);
}
else {
DerivedMesh *dm = NULL;
if (mesh) {
dm = CDDM_from_mesh(mesh);
}
mti->deformVertsEM_DM(md, ctx, editData, dm, vertexCos, numVerts);
if (dm) {
dm->release(dm);
}
}
}
void modifier_deformMatricesEM(struct ModifierData *md, const ModifierEvalContext *ctx,
struct BMEditMesh *editData, struct Mesh *mesh,
float (*vertexCos)[3], float (*defMats)[3][3], int numVerts)
{
const ModifierTypeInfo *mti = modifierType_getInfo(md->type);
if (mti->deformMatricesEM) {
mti->deformMatricesEM(md, ctx, editData, mesh, vertexCos, defMats, numVerts);
}
else {
DerivedMesh *dm = NULL;
if (mesh) {
dm = CDDM_from_mesh(mesh);
}
mti->deformMatricesEM_DM(md, ctx, editData, dm, vertexCos, defMats, numVerts);
if (dm) {
dm->release(dm);
}
}
}
struct Mesh *modifier_applyModifier(struct ModifierData *md, const ModifierEvalContext *ctx,
struct Mesh *mesh)
{
const ModifierTypeInfo *mti = modifierType_getInfo(md->type);
if (mti->applyModifier) {
return mti->applyModifier(md, ctx, mesh);
}
else {
DerivedMesh *dm = CDDM_from_mesh(mesh);
DerivedMesh *ndm = mti->applyModifier_DM(md, ctx, dm);
if (ndm != dm) {
dm->release(dm);
}
DM_to_mesh(ndm, mesh, ctx->object, CD_MASK_EVERYTHING, true);
return mesh;
}
}
struct Mesh *modifier_applyModifierEM(struct ModifierData *md, const ModifierEvalContext *ctx,
struct BMEditMesh *editData,
struct Mesh *mesh)
{
const ModifierTypeInfo *mti = modifierType_getInfo(md->type);
if (mti->applyModifierEM) {
return mti->applyModifierEM(md, ctx, editData, mesh);
}
else {
DerivedMesh *dm = CDDM_from_mesh(mesh);
DerivedMesh *ndm = mti->applyModifierEM_DM(md, ctx, editData, dm);
if (ndm != dm) {
dm->release(dm);
}
DM_to_mesh(ndm, mesh, ctx->object, CD_MASK_EVERYTHING, true);
return mesh;
}
}
/* depricated variants of above that accept DerivedMesh */
void modifier_deformVerts_DM_deprecated(struct ModifierData *md, const ModifierEvalContext *ctx,
struct DerivedMesh *dm,
float (*vertexCos)[3], int numVerts)
{
const ModifierTypeInfo *mti = modifierType_getInfo(md->type);
if (mti->deformVerts_DM) {
mti->deformVerts_DM(md, ctx, dm, vertexCos, numVerts);
}
else {
/* TODO(sybren): deduplicate all the copies of this code in this file. */
Mesh *mesh = NULL;
if (dm != NULL) {
mesh = BKE_id_new_nomain(ID_ME, NULL);
DM_to_mesh(dm, mesh, ctx->object, CD_MASK_EVERYTHING, false);
}
mti->deformVerts(md, ctx, mesh, vertexCos, numVerts);
if (mesh != NULL) {
BKE_id_free(NULL, mesh);
}
}
}
void modifier_deformMatrices_DM_deprecated(struct ModifierData *md, const ModifierEvalContext *ctx,
struct DerivedMesh *dm,
float (*vertexCos)[3], float (*defMats)[3][3], int numVerts)
{
const ModifierTypeInfo *mti = modifierType_getInfo(md->type);
if (mti->deformMatrices_DM) {
mti->deformMatrices_DM(md, ctx, dm, vertexCos, defMats, numVerts);
}
else {
/* TODO(sybren): deduplicate all the copies of this code in this file. */
Mesh *mesh = NULL;
if (dm != NULL) {
mesh = BKE_id_new_nomain(ID_ME, NULL);
DM_to_mesh(dm, mesh, ctx->object, CD_MASK_EVERYTHING, false);
}
mti->deformMatrices(md, ctx, mesh, vertexCos, defMats, numVerts);
if (mesh != NULL) {
BKE_id_free(NULL, mesh);
}
}
}
void modifier_deformVertsEM_DM_deprecated(struct ModifierData *md, const ModifierEvalContext *ctx,
struct BMEditMesh *editData, struct DerivedMesh *dm,
float (*vertexCos)[3], int numVerts)
{
const ModifierTypeInfo *mti = modifierType_getInfo(md->type);
if (mti->deformVertsEM_DM) {
mti->deformVertsEM_DM(md, ctx, editData, dm, vertexCos, numVerts);
}
else {
/* TODO(sybren): deduplicate all the copies of this code in this file. */
Mesh *mesh = NULL;
if (dm != NULL) {
mesh = BKE_id_new_nomain(ID_ME, NULL);
DM_to_mesh(dm, mesh, ctx->object, CD_MASK_EVERYTHING, false);
}
mti->deformVertsEM(md, ctx, editData, mesh, vertexCos, numVerts);
if (mesh != NULL) {
BKE_id_free(NULL, mesh);
}
}
}
void modifier_deformMatricesEM_DM_deprecated(struct ModifierData *md, const ModifierEvalContext *ctx,
struct BMEditMesh *editData, struct DerivedMesh *dm,
float (*vertexCos)[3], float (*defMats)[3][3], int numVerts)
{
const ModifierTypeInfo *mti = modifierType_getInfo(md->type);
if (mti->deformMatricesEM_DM) {
mti->deformMatricesEM_DM(md, ctx, editData, dm, vertexCos, defMats, numVerts);
}
else {
/* TODO(sybren): deduplicate all the copies of this code in this file. */
Mesh *mesh = NULL;
if (dm != NULL) {
mesh = BKE_id_new_nomain(ID_ME, NULL);
DM_to_mesh(dm, mesh, ctx->object, CD_MASK_EVERYTHING, false);
}
mti->deformMatricesEM(md, ctx, editData, mesh, vertexCos, defMats, numVerts);
if (mesh != NULL) {
BKE_id_free(NULL, mesh);
}
}
}
struct DerivedMesh *modifier_applyModifier_DM_deprecated(struct ModifierData *md, const ModifierEvalContext *ctx,
struct DerivedMesh *dm)
{
const ModifierTypeInfo *mti = modifierType_getInfo(md->type);
if (mti->applyModifier_DM) {
return mti->applyModifier_DM(md, ctx, dm);
}
else {
/* TODO(sybren): deduplicate all the copies of this code in this file. */
Mesh *mesh = NULL;
if (dm != NULL) {
mesh = BKE_id_new_nomain(ID_ME, NULL);
DM_to_mesh(dm, mesh, ctx->object, CD_MASK_EVERYTHING, false);
}
struct Mesh *new_mesh = mti->applyModifier(md, ctx, mesh);
/* Make a DM that doesn't reference new_mesh so we can free the latter. */
DerivedMesh *ndm = CDDM_from_mesh_ex(new_mesh, CD_DUPLICATE);
if (new_mesh != mesh) {
BKE_id_free(NULL, new_mesh);
}
if (mesh != NULL) {
BKE_id_free(NULL, mesh);
}
return ndm;
}
}
struct DerivedMesh *modifier_applyModifierEM_DM_deprecated(struct ModifierData *md, const ModifierEvalContext *ctx,
struct BMEditMesh *editData,
struct DerivedMesh *dm)
{
const ModifierTypeInfo *mti = modifierType_getInfo(md->type);
if (mti->applyModifierEM_DM) {
return mti->applyModifierEM_DM(md, ctx, editData, dm);
}
else {
/* TODO(sybren): deduplicate all the copies of this code in this file. */
Mesh *mesh = NULL;
if (dm != NULL) {
mesh = BKE_id_new_nomain(ID_ME, NULL);
DM_to_mesh(dm, mesh, ctx->object, CD_MASK_EVERYTHING, false);
}
struct Mesh *new_mesh = mti->applyModifierEM(md, ctx, editData, mesh);
/* Make a DM that doesn't reference new_mesh so we can free the latter. */
DerivedMesh *ndm = CDDM_from_mesh_ex(new_mesh, CD_DUPLICATE);
if (new_mesh != mesh) {
BKE_id_free(NULL, new_mesh);
}
if (mesh != NULL) {
BKE_id_free(NULL, mesh);
}
return ndm;
}
}
/** Get evaluated mesh for other object, which is used as an operand for the modifier,
* i.e. second operand for boolean modifier.
*/
Mesh *BKE_modifier_get_evaluated_mesh_from_object(Object *ob, const ModifierApplyFlag flag)
{
if (flag & MOD_APPLY_RENDER) {
/* TODO(sergey): Use proper derived render in the future. */
return ob->mesh_evaluated;
}
else {
return ob->mesh_evaluated;
}
}
|