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
|
/*
* 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) 2001-2002 by NaN Holding BV.
* All rights reserved.
*/
/** \file
* \ingroup bke
*
* Contains management of #Main database itself.
*/
#include <string.h>
#include "MEM_guardedalloc.h"
#include "BLI_blenlib.h"
#include "BLI_ghash.h"
#include "BLI_mempool.h"
#include "BLI_threads.h"
#include "DNA_ID.h"
#include "BKE_global.h"
#include "BKE_idtype.h"
#include "BKE_lib_id.h"
#include "BKE_lib_query.h"
#include "BKE_main.h"
#include "BKE_main_idmap.h"
#include "IMB_imbuf.h"
#include "IMB_imbuf_types.h"
Main *BKE_main_new(void)
{
Main *bmain = MEM_callocN(sizeof(Main), "new main");
bmain->lock = MEM_mallocN(sizeof(SpinLock), "main lock");
BLI_spin_init((SpinLock *)bmain->lock);
return bmain;
}
void BKE_main_free(Main *mainvar)
{
/* also call when reading a file, erase all, etc */
ListBase *lbarray[INDEX_ID_MAX];
int a;
/* Since we are removing whole main, no need to bother 'properly'
* (and slowly) removing each ID from it. */
const int free_flag = (LIB_ID_FREE_NO_MAIN | LIB_ID_FREE_NO_UI_USER |
LIB_ID_FREE_NO_USER_REFCOUNT | LIB_ID_FREE_NO_DEG_TAG);
MEM_SAFE_FREE(mainvar->blen_thumb);
a = set_listbasepointers(mainvar, lbarray);
while (a--) {
ListBase *lb = lbarray[a];
ID *id, *id_next;
for (id = lb->first; id != NULL; id = id_next) {
id_next = id->next;
#if 1
BKE_id_free_ex(mainvar, id, free_flag, false);
#else
/* errors freeing ID's can be hard to track down,
* enable this so valgrind will give the line number in its error log */
switch (a) {
case 0:
BKE_id_free_ex(mainvar, id, free_flag, false);
break;
case 1:
BKE_id_free_ex(mainvar, id, free_flag, false);
break;
case 2:
BKE_id_free_ex(mainvar, id, free_flag, false);
break;
case 3:
BKE_id_free_ex(mainvar, id, free_flag, false);
break;
case 4:
BKE_id_free_ex(mainvar, id, free_flag, false);
break;
case 5:
BKE_id_free_ex(mainvar, id, free_flag, false);
break;
case 6:
BKE_id_free_ex(mainvar, id, free_flag, false);
break;
case 7:
BKE_id_free_ex(mainvar, id, free_flag, false);
break;
case 8:
BKE_id_free_ex(mainvar, id, free_flag, false);
break;
case 9:
BKE_id_free_ex(mainvar, id, free_flag, false);
break;
case 10:
BKE_id_free_ex(mainvar, id, free_flag, false);
break;
case 11:
BKE_id_free_ex(mainvar, id, free_flag, false);
break;
case 12:
BKE_id_free_ex(mainvar, id, free_flag, false);
break;
case 13:
BKE_id_free_ex(mainvar, id, free_flag, false);
break;
case 14:
BKE_id_free_ex(mainvar, id, free_flag, false);
break;
case 15:
BKE_id_free_ex(mainvar, id, free_flag, false);
break;
case 16:
BKE_id_free_ex(mainvar, id, free_flag, false);
break;
case 17:
BKE_id_free_ex(mainvar, id, free_flag, false);
break;
case 18:
BKE_id_free_ex(mainvar, id, free_flag, false);
break;
case 19:
BKE_id_free_ex(mainvar, id, free_flag, false);
break;
case 20:
BKE_id_free_ex(mainvar, id, free_flag, false);
break;
case 21:
BKE_id_free_ex(mainvar, id, free_flag, false);
break;
case 22:
BKE_id_free_ex(mainvar, id, free_flag, false);
break;
case 23:
BKE_id_free_ex(mainvar, id, free_flag, false);
break;
case 24:
BKE_id_free_ex(mainvar, id, free_flag, false);
break;
case 25:
BKE_id_free_ex(mainvar, id, free_flag, false);
break;
case 26:
BKE_id_free_ex(mainvar, id, free_flag, false);
break;
case 27:
BKE_id_free_ex(mainvar, id, free_flag, false);
break;
case 28:
BKE_id_free_ex(mainvar, id, free_flag, false);
break;
case 29:
BKE_id_free_ex(mainvar, id, free_flag, false);
break;
case 30:
BKE_id_free_ex(mainvar, id, free_flag, false);
break;
case 31:
BKE_id_free_ex(mainvar, id, free_flag, false);
break;
case 32:
BKE_id_free_ex(mainvar, id, free_flag, false);
break;
case 33:
BKE_id_free_ex(mainvar, id, free_flag, false);
break;
case 34:
BKE_id_free_ex(mainvar, id, free_flag, false);
break;
default:
BLI_assert_unreachable();
break;
}
#endif
}
BLI_listbase_clear(lb);
}
if (mainvar->relations) {
BKE_main_relations_free(mainvar);
}
if (mainvar->id_map) {
BKE_main_idmap_destroy(mainvar->id_map);
}
BLI_spin_end((SpinLock *)mainvar->lock);
MEM_freeN(mainvar->lock);
MEM_freeN(mainvar);
}
/* Check whether given `bmain` is empty or contains some IDs. */
bool BKE_main_is_empty(struct Main *bmain)
{
ID *id_iter;
FOREACH_MAIN_ID_BEGIN (bmain, id_iter) {
return false;
}
FOREACH_MAIN_ID_END;
return true;
}
void BKE_main_lock(struct Main *bmain)
{
BLI_spin_lock((SpinLock *)bmain->lock);
}
void BKE_main_unlock(struct Main *bmain)
{
BLI_spin_unlock((SpinLock *)bmain->lock);
}
static int main_relations_create_idlink_cb(LibraryIDLinkCallbackData *cb_data)
{
MainIDRelations *bmain_relations = cb_data->user_data;
ID *id_self = cb_data->id_self;
ID **id_pointer = cb_data->id_pointer;
const int cb_flag = cb_data->cb_flag;
if (*id_pointer) {
MainIDRelationsEntry **entry_p;
/* Add `id_pointer` as child of `id_self`. */
{
if (!BLI_ghash_ensure_p(
bmain_relations->relations_from_pointers, id_self, (void ***)&entry_p)) {
*entry_p = MEM_callocN(sizeof(**entry_p), __func__);
(*entry_p)->session_uuid = id_self->session_uuid;
}
else {
BLI_assert((*entry_p)->session_uuid == id_self->session_uuid);
}
MainIDRelationsEntryItem *to_id_entry = BLI_mempool_alloc(bmain_relations->entry_items_pool);
to_id_entry->next = (*entry_p)->to_ids;
to_id_entry->id_pointer.to = id_pointer;
to_id_entry->session_uuid = (*id_pointer != NULL) ? (*id_pointer)->session_uuid :
MAIN_ID_SESSION_UUID_UNSET;
to_id_entry->usage_flag = cb_flag;
(*entry_p)->to_ids = to_id_entry;
}
/* Add `id_self` as parent of `id_pointer`. */
if (*id_pointer != NULL) {
if (!BLI_ghash_ensure_p(
bmain_relations->relations_from_pointers, *id_pointer, (void ***)&entry_p)) {
*entry_p = MEM_callocN(sizeof(**entry_p), __func__);
(*entry_p)->session_uuid = (*id_pointer)->session_uuid;
}
else {
BLI_assert((*entry_p)->session_uuid == (*id_pointer)->session_uuid);
}
MainIDRelationsEntryItem *from_id_entry = BLI_mempool_alloc(
bmain_relations->entry_items_pool);
from_id_entry->next = (*entry_p)->from_ids;
from_id_entry->id_pointer.from = id_self;
from_id_entry->session_uuid = id_self->session_uuid;
from_id_entry->usage_flag = cb_flag;
(*entry_p)->from_ids = from_id_entry;
}
}
return IDWALK_RET_NOP;
}
/** Generate the mappings between used IDs and their users, and vice-versa. */
void BKE_main_relations_create(Main *bmain, const short flag)
{
if (bmain->relations != NULL) {
BKE_main_relations_free(bmain);
}
bmain->relations = MEM_mallocN(sizeof(*bmain->relations), __func__);
bmain->relations->relations_from_pointers = BLI_ghash_new(
BLI_ghashutil_ptrhash, BLI_ghashutil_ptrcmp, __func__);
bmain->relations->entry_items_pool = BLI_mempool_create(
sizeof(MainIDRelationsEntryItem), 128, 128, BLI_MEMPOOL_NOP);
bmain->relations->flag = flag;
ID *id;
FOREACH_MAIN_ID_BEGIN (bmain, id) {
const int idwalk_flag = IDWALK_READONLY |
((flag & MAINIDRELATIONS_INCLUDE_UI) != 0 ? IDWALK_INCLUDE_UI : 0);
/* Ensure all IDs do have an entry, even if they are not connected to any other. */
MainIDRelationsEntry **entry_p;
if (!BLI_ghash_ensure_p(bmain->relations->relations_from_pointers, id, (void ***)&entry_p)) {
*entry_p = MEM_callocN(sizeof(**entry_p), __func__);
(*entry_p)->session_uuid = id->session_uuid;
}
else {
BLI_assert((*entry_p)->session_uuid == id->session_uuid);
}
BKE_library_foreach_ID_link(
NULL, id, main_relations_create_idlink_cb, bmain->relations, idwalk_flag);
}
FOREACH_MAIN_ID_END;
}
void BKE_main_relations_free(Main *bmain)
{
if (bmain->relations != NULL) {
if (bmain->relations->relations_from_pointers != NULL) {
BLI_ghash_free(bmain->relations->relations_from_pointers, NULL, MEM_freeN);
}
BLI_mempool_destroy(bmain->relations->entry_items_pool);
MEM_freeN(bmain->relations);
bmain->relations = NULL;
}
}
/** Set or clear given `tag` in all relation entries of given `bmain`. */
void BKE_main_relations_tag_set(struct Main *bmain,
const MainIDRelationsEntryTags tag,
const bool value)
{
if (bmain->relations == NULL) {
return;
}
GHashIterator *gh_iter;
for (gh_iter = BLI_ghashIterator_new(bmain->relations->relations_from_pointers);
!BLI_ghashIterator_done(gh_iter);
BLI_ghashIterator_step(gh_iter)) {
MainIDRelationsEntry *entry = BLI_ghashIterator_getValue(gh_iter);
if (value) {
entry->tags |= tag;
}
else {
entry->tags &= ~tag;
}
}
BLI_ghashIterator_free(gh_iter);
}
/**
* Create a GSet storing all IDs present in given \a bmain, by their pointers.
*
* \param gset: If not NULL, given GSet will be extended with IDs from given \a bmain,
* instead of creating a new one.
*/
GSet *BKE_main_gset_create(Main *bmain, GSet *gset)
{
if (gset == NULL) {
gset = BLI_gset_new(BLI_ghashutil_ptrhash, BLI_ghashutil_ptrcmp, __func__);
}
ID *id;
FOREACH_MAIN_ID_BEGIN (bmain, id) {
BLI_gset_add(gset, id);
}
FOREACH_MAIN_ID_END;
return gset;
}
/* Utils for ID's library weak reference API. */
typedef struct LibWeakRefKey {
char filepath[FILE_MAX];
char id_name[MAX_ID_NAME];
} LibWeakRefKey;
static LibWeakRefKey *lib_weak_key_create(LibWeakRefKey *key,
const char *lib_path,
const char *id_name)
{
if (key == NULL) {
key = MEM_mallocN(sizeof(*key), __func__);
}
BLI_strncpy(key->filepath, lib_path, sizeof(key->filepath));
BLI_strncpy(key->id_name, id_name, sizeof(key->id_name));
return key;
}
static uint lib_weak_key_hash(const void *ptr)
{
const LibWeakRefKey *string_pair = ptr;
uint hash = BLI_ghashutil_strhash_p_murmur(string_pair->filepath);
return hash ^ BLI_ghashutil_strhash_p_murmur(string_pair->id_name);
}
static bool lib_weak_key_cmp(const void *a, const void *b)
{
const LibWeakRefKey *string_pair_a = a;
const LibWeakRefKey *string_pair_b = b;
return !(STREQ(string_pair_a->filepath, string_pair_b->filepath) &&
STREQ(string_pair_a->id_name, string_pair_b->id_name));
}
/**
* Generate a mapping between 'library path' of an ID (as a pair (relative blend file path, id
* name)), and a current local ID, if any.
*
* This uses the information stored in `ID.library_weak_reference`.
*/
GHash *BKE_main_library_weak_reference_create(Main *bmain)
{
GHash *library_weak_reference_mapping = BLI_ghash_new(
lib_weak_key_hash, lib_weak_key_cmp, __func__);
ListBase *lb;
FOREACH_MAIN_LISTBASE_BEGIN (bmain, lb) {
ID *id_iter = lb->first;
if (id_iter == NULL) {
continue;
}
if (!BKE_idtype_idcode_append_is_reusable(GS(id_iter->name))) {
continue;
}
BLI_assert(BKE_idtype_idcode_is_linkable(GS(id_iter->name)));
FOREACH_MAIN_LISTBASE_ID_BEGIN (lb, id_iter) {
if (id_iter->library_weak_reference == NULL) {
continue;
}
LibWeakRefKey *key = lib_weak_key_create(NULL,
id_iter->library_weak_reference->library_filepath,
id_iter->library_weak_reference->library_id_name);
BLI_ghash_insert(library_weak_reference_mapping, key, id_iter);
}
FOREACH_MAIN_LISTBASE_ID_END;
}
FOREACH_MAIN_LISTBASE_END;
return library_weak_reference_mapping;
}
/**
* Destroy the data generated by #BKE_main_library_weak_reference_create.
*/
void BKE_main_library_weak_reference_destroy(GHash *library_weak_reference_mapping)
{
BLI_ghash_free(library_weak_reference_mapping, MEM_freeN, NULL);
}
/**
* Search for a local ID matching the given linked ID reference.
*
* \param library_weak_reference_mapping: the mapping data generated by
* #BKE_main_library_weak_reference_create.
* \param library_relative_path: the path of a blend file library (relative to current working
* one).
* \param library_id_name: the full ID name, including the leading two chars encoding the ID
* type.
*/
ID *BKE_main_library_weak_reference_search_item(GHash *library_weak_reference_mapping,
const char *library_filepath,
const char *library_id_name)
{
LibWeakRefKey key;
lib_weak_key_create(&key, library_filepath, library_id_name);
return (ID *)BLI_ghash_lookup(library_weak_reference_mapping, &key);
}
/**
* Add the given ID weak library reference to given local ID and the runtime mapping.
*
* \param library_weak_reference_mapping: the mapping data generated by
* #BKE_main_library_weak_reference_create.
* \param library_relative_path: the path of a blend file library (relative to current working
* one).
* \param library_id_name: the full ID name, including the leading two chars encoding the ID type.
* \param new_id: New local ID matching given weak reference.
*/
void BKE_main_library_weak_reference_add_item(GHash *library_weak_reference_mapping,
const char *library_filepath,
const char *library_id_name,
ID *new_id)
{
BLI_assert(GS(library_id_name) == GS(new_id->name));
BLI_assert(new_id->library_weak_reference == NULL);
BLI_assert(BKE_idtype_idcode_append_is_reusable(GS(new_id->name)));
new_id->library_weak_reference = MEM_mallocN(sizeof(*(new_id->library_weak_reference)),
__func__);
LibWeakRefKey *key = lib_weak_key_create(NULL, library_filepath, library_id_name);
void **id_p;
const bool already_exist_in_mapping = BLI_ghash_ensure_p(
library_weak_reference_mapping, key, &id_p);
BLI_assert(!already_exist_in_mapping);
UNUSED_VARS_NDEBUG(already_exist_in_mapping);
BLI_strncpy(new_id->library_weak_reference->library_filepath,
library_filepath,
sizeof(new_id->library_weak_reference->library_filepath));
BLI_strncpy(new_id->library_weak_reference->library_id_name,
library_id_name,
sizeof(new_id->library_weak_reference->library_id_name));
*id_p = new_id;
}
/**
* Update the status of the given ID weak library reference in current local IDs and the runtime
* mapping.
*
* This effectively transfers the 'ownership' of the given weak reference from `old_id` to
* `new_id`.
*
* \param library_weak_reference_mapping: the mapping data generated by
* #BKE_main_library_weak_reference_create.
* \param library_relative_path: the path of a blend file library (relative to current working
* one).
* \param library_id_name: the full ID name, including the leading two chars encoding the ID type.
* \param old_id: Existing local ID matching given weak reference.
* \param new_id: New local ID matching given weak reference.
*/
void BKE_main_library_weak_reference_update_item(GHash *library_weak_reference_mapping,
const char *library_filepath,
const char *library_id_name,
ID *old_id,
ID *new_id)
{
BLI_assert(GS(library_id_name) == GS(old_id->name));
BLI_assert(GS(library_id_name) == GS(new_id->name));
BLI_assert(old_id->library_weak_reference != NULL);
BLI_assert(new_id->library_weak_reference == NULL);
BLI_assert(STREQ(old_id->library_weak_reference->library_filepath, library_filepath));
BLI_assert(STREQ(old_id->library_weak_reference->library_id_name, library_id_name));
LibWeakRefKey key;
lib_weak_key_create(&key, library_filepath, library_id_name);
void **id_p = BLI_ghash_lookup_p(library_weak_reference_mapping, &key);
BLI_assert(id_p != NULL && *id_p == old_id);
new_id->library_weak_reference = old_id->library_weak_reference;
old_id->library_weak_reference = NULL;
*id_p = new_id;
}
/**
* Remove the given ID weak library reference from the given local ID and the runtime mapping.
*
* \param library_weak_reference_mapping: the mapping data generated by
* #BKE_main_library_weak_reference_create.
* \param library_relative_path: the path of a blend file library (relative to current working
* one).
* \param library_id_name: the full ID name, including the leading two chars encoding the ID type.
* \param old_id: Existing local ID matching given weak reference.
*/
void BKE_main_library_weak_reference_remove_item(GHash *library_weak_reference_mapping,
const char *library_filepath,
const char *library_id_name,
ID *old_id)
{
BLI_assert(GS(library_id_name) == GS(old_id->name));
BLI_assert(old_id->library_weak_reference != NULL);
LibWeakRefKey key;
lib_weak_key_create(&key, library_filepath, library_id_name);
BLI_assert(BLI_ghash_lookup(library_weak_reference_mapping, &key) == old_id);
BLI_ghash_remove(library_weak_reference_mapping, &key, MEM_freeN, NULL);
MEM_SAFE_FREE(old_id->library_weak_reference);
}
/**
* Generates a raw .blend file thumbnail data from given image.
*
* \param bmain: If not NULL, also store generated data in this Main.
* \param img: ImBuf image to generate thumbnail data from.
* \return The generated .blend file raw thumbnail data.
*/
BlendThumbnail *BKE_main_thumbnail_from_imbuf(Main *bmain, ImBuf *img)
{
BlendThumbnail *data = NULL;
if (bmain) {
MEM_SAFE_FREE(bmain->blen_thumb);
}
if (img) {
const size_t sz = BLEN_THUMB_MEMSIZE(img->x, img->y);
data = MEM_mallocN(sz, __func__);
IMB_rect_from_float(img); /* Just in case... */
data->width = img->x;
data->height = img->y;
memcpy(data->rect, img->rect, sz - sizeof(*data));
}
if (bmain) {
bmain->blen_thumb = data;
}
return data;
}
/**
* Generates an image from raw .blend file thumbnail \a data.
*
* \param bmain: Use this bmain->blen_thumb data if given \a data is NULL.
* \param data: Raw .blend file thumbnail data.
* \return An ImBuf from given data, or NULL if invalid.
*/
ImBuf *BKE_main_thumbnail_to_imbuf(Main *bmain, BlendThumbnail *data)
{
ImBuf *img = NULL;
if (!data && bmain) {
data = bmain->blen_thumb;
}
if (data) {
img = IMB_allocFromBuffer(
(const uint *)data->rect, NULL, (uint)data->width, (uint)data->height, 4);
}
return img;
}
/**
* Generates an empty (black) thumbnail for given Main.
*/
void BKE_main_thumbnail_create(struct Main *bmain)
{
MEM_SAFE_FREE(bmain->blen_thumb);
bmain->blen_thumb = MEM_callocN(BLEN_THUMB_MEMSIZE(BLEN_THUMB_SIZE, BLEN_THUMB_SIZE), __func__);
bmain->blen_thumb->width = BLEN_THUMB_SIZE;
bmain->blen_thumb->height = BLEN_THUMB_SIZE;
}
/**
* Return filepath of given \a main.
*/
const char *BKE_main_blendfile_path(const Main *bmain)
{
return bmain->name;
}
/**
* Return filepath of global main #G_MAIN.
*
* \warning Usage is not recommended,
* you should always try to get a valid Main pointer from context...
*/
const char *BKE_main_blendfile_path_from_global(void)
{
return BKE_main_blendfile_path(G_MAIN);
}
/**
* \return A pointer to the \a ListBase of given \a bmain for requested \a type ID type.
*/
ListBase *which_libbase(Main *bmain, short type)
{
switch ((ID_Type)type) {
case ID_SCE:
return &(bmain->scenes);
case ID_LI:
return &(bmain->libraries);
case ID_OB:
return &(bmain->objects);
case ID_ME:
return &(bmain->meshes);
case ID_CU:
return &(bmain->curves);
case ID_MB:
return &(bmain->metaballs);
case ID_MA:
return &(bmain->materials);
case ID_TE:
return &(bmain->textures);
case ID_IM:
return &(bmain->images);
case ID_LT:
return &(bmain->lattices);
case ID_LA:
return &(bmain->lights);
case ID_CA:
return &(bmain->cameras);
case ID_IP:
return &(bmain->ipo);
case ID_KE:
return &(bmain->shapekeys);
case ID_WO:
return &(bmain->worlds);
case ID_SCR:
return &(bmain->screens);
case ID_VF:
return &(bmain->fonts);
case ID_TXT:
return &(bmain->texts);
case ID_SPK:
return &(bmain->speakers);
case ID_LP:
return &(bmain->lightprobes);
case ID_SO:
return &(bmain->sounds);
case ID_GR:
return &(bmain->collections);
case ID_AR:
return &(bmain->armatures);
case ID_AC:
return &(bmain->actions);
case ID_NT:
return &(bmain->nodetrees);
case ID_BR:
return &(bmain->brushes);
case ID_PA:
return &(bmain->particles);
case ID_WM:
return &(bmain->wm);
case ID_GD:
return &(bmain->gpencils);
case ID_MC:
return &(bmain->movieclips);
case ID_MSK:
return &(bmain->masks);
case ID_LS:
return &(bmain->linestyles);
case ID_PAL:
return &(bmain->palettes);
case ID_PC:
return &(bmain->paintcurves);
case ID_CF:
return &(bmain->cachefiles);
case ID_WS:
return &(bmain->workspaces);
case ID_HA:
return &(bmain->hairs);
case ID_PT:
return &(bmain->pointclouds);
case ID_VO:
return &(bmain->volumes);
case ID_SIM:
return &(bmain->simulations);
}
return NULL;
}
/**
* Put the pointers to all the #ListBase structs in given `bmain` into the `*lb[INDEX_ID_MAX]`
* array, and return the number of those for convenience.
*
* This is useful for generic traversal of all the blocks in a #Main (by traversing all the lists
* in turn), without worrying about block types.
*
* \param lb: Array of lists #INDEX_ID_MAX in length.
*
* \note The order of each ID type #ListBase in the array is determined by the `INDEX_ID_<IDTYPE>`
* enum definitions in `DNA_ID.h`. See also the #FOREACH_MAIN_ID_BEGIN macro in `BKE_main.h`
*/
int set_listbasepointers(Main *bmain, ListBase *lb[/*INDEX_ID_MAX*/])
{
/* Libraries may be accessed from pretty much any other ID. */
lb[INDEX_ID_LI] = &(bmain->libraries);
lb[INDEX_ID_IP] = &(bmain->ipo);
/* Moved here to avoid problems when freeing with animato (aligorith). */
lb[INDEX_ID_AC] = &(bmain->actions);
lb[INDEX_ID_KE] = &(bmain->shapekeys);
/* Referenced by gpencil, so needs to be before that to avoid crashes. */
lb[INDEX_ID_PAL] = &(bmain->palettes);
/* Referenced by nodes, objects, view, scene etc, before to free after. */
lb[INDEX_ID_GD] = &(bmain->gpencils);
lb[INDEX_ID_NT] = &(bmain->nodetrees);
lb[INDEX_ID_IM] = &(bmain->images);
lb[INDEX_ID_TE] = &(bmain->textures);
lb[INDEX_ID_MA] = &(bmain->materials);
lb[INDEX_ID_VF] = &(bmain->fonts);
/* Important!: When adding a new object type,
* the specific data should be inserted here. */
lb[INDEX_ID_AR] = &(bmain->armatures);
lb[INDEX_ID_CF] = &(bmain->cachefiles);
lb[INDEX_ID_ME] = &(bmain->meshes);
lb[INDEX_ID_CU] = &(bmain->curves);
lb[INDEX_ID_MB] = &(bmain->metaballs);
lb[INDEX_ID_HA] = &(bmain->hairs);
lb[INDEX_ID_PT] = &(bmain->pointclouds);
lb[INDEX_ID_VO] = &(bmain->volumes);
lb[INDEX_ID_LT] = &(bmain->lattices);
lb[INDEX_ID_LA] = &(bmain->lights);
lb[INDEX_ID_CA] = &(bmain->cameras);
lb[INDEX_ID_TXT] = &(bmain->texts);
lb[INDEX_ID_SO] = &(bmain->sounds);
lb[INDEX_ID_GR] = &(bmain->collections);
lb[INDEX_ID_PAL] = &(bmain->palettes);
lb[INDEX_ID_PC] = &(bmain->paintcurves);
lb[INDEX_ID_BR] = &(bmain->brushes);
lb[INDEX_ID_PA] = &(bmain->particles);
lb[INDEX_ID_SPK] = &(bmain->speakers);
lb[INDEX_ID_LP] = &(bmain->lightprobes);
lb[INDEX_ID_WO] = &(bmain->worlds);
lb[INDEX_ID_MC] = &(bmain->movieclips);
lb[INDEX_ID_SCR] = &(bmain->screens);
lb[INDEX_ID_OB] = &(bmain->objects);
lb[INDEX_ID_LS] = &(bmain->linestyles); /* referenced by scenes */
lb[INDEX_ID_SCE] = &(bmain->scenes);
lb[INDEX_ID_WS] = &(bmain->workspaces); /* before wm, so it's freed after it! */
lb[INDEX_ID_WM] = &(bmain->wm);
lb[INDEX_ID_MSK] = &(bmain->masks);
lb[INDEX_ID_SIM] = &(bmain->simulations);
lb[INDEX_ID_NULL] = NULL;
return (INDEX_ID_MAX - 1);
}
|