Merge branch 'master' into blender2.8
[blender.git] / source / blender / editors / space_outliner / outliner_tree.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) 2004 Blender Foundation.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): Joshua Leung
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file blender/editors/space_outliner/outliner_tree.c
29  *  \ingroup spoutliner
30  */
31
32 #include <math.h>
33 #include <string.h>
34
35 #include "MEM_guardedalloc.h"
36
37 #include "DNA_anim_types.h"
38 #include "DNA_armature_types.h"
39 #include "DNA_constraint_types.h"
40 #include "DNA_camera_types.h"
41 #include "DNA_cachefile_types.h"
42 #include "DNA_collection_types.h"
43 #include "DNA_gpencil_types.h"
44 #include "DNA_key_types.h"
45 #include "DNA_lamp_types.h"
46 #include "DNA_material_types.h"
47 #include "DNA_mesh_types.h"
48 #include "DNA_meta_types.h"
49 #include "DNA_lightprobe_types.h"
50 #include "DNA_particle_types.h"
51 #include "DNA_scene_types.h"
52 #include "DNA_world_types.h"
53 #include "DNA_sequence_types.h"
54 #include "DNA_speaker_types.h"
55 #include "DNA_object_types.h"
56 #include "DNA_linestyle_types.h"
57
58 #include "BLI_blenlib.h"
59 #include "BLI_utildefines.h"
60 #include "BLI_mempool.h"
61 #include "BLI_fnmatch.h"
62
63 #include "BLT_translation.h"
64
65 #include "BKE_fcurve.h"
66 #include "BKE_idcode.h"
67 #include "BKE_layer.h"
68 #include "BKE_library.h"
69 #include "BKE_main.h"
70 #include "BKE_modifier.h"
71 #include "BKE_outliner_treehash.h"
72 #include "BKE_sequencer.h"
73
74 #include "DEG_depsgraph.h"
75 #include "DEG_depsgraph_build.h"
76
77 #include "ED_armature.h"
78 #include "ED_screen.h"
79
80 #include "WM_api.h"
81 #include "WM_types.h"
82
83 #include "RNA_access.h"
84
85 #include "UI_interface.h"
86
87 #include "outliner_intern.h"
88
89 #ifdef WIN32
90 #  include "BLI_math_base.h" /* M_PI */
91 #endif
92
93 /* prototypes */
94 static TreeElement *outliner_add_collection_recursive(
95         SpaceOops *soops, Collection *collection, TreeElement *ten);
96 static void outliner_make_object_parent_hierarchy(ListBase *lb);
97
98 /* ********************************************************* */
99 /* Persistent Data */
100
101 static void outliner_storage_cleanup(SpaceOops *soops)
102 {
103         BLI_mempool *ts = soops->treestore;
104
105         if (ts) {
106                 TreeStoreElem *tselem;
107                 int unused = 0;
108
109                 /* each element used once, for ID blocks with more users to have each a treestore */
110                 BLI_mempool_iter iter;
111
112                 BLI_mempool_iternew(ts, &iter);
113                 while ((tselem = BLI_mempool_iterstep(&iter))) {
114                         tselem->used = 0;
115                 }
116
117                 /* cleanup only after reading file or undo step, and always for
118                  * RNA datablocks view in order to save memory */
119                 if (soops->storeflag & SO_TREESTORE_CLEANUP) {
120                         soops->storeflag &= ~SO_TREESTORE_CLEANUP;
121
122                         BLI_mempool_iternew(ts, &iter);
123                         while ((tselem = BLI_mempool_iterstep(&iter))) {
124                                 if (tselem->id == NULL) unused++;
125                         }
126
127                         if (unused) {
128                                 if (BLI_mempool_len(ts) == unused) {
129                                         BLI_mempool_destroy(ts);
130                                         soops->treestore = NULL;
131                                         if (soops->treehash) {
132                                                 BKE_outliner_treehash_free(soops->treehash);
133                                                 soops->treehash = NULL;
134                                         }
135                                 }
136                                 else {
137                                         TreeStoreElem *tsenew;
138                                         BLI_mempool *new_ts = BLI_mempool_create(sizeof(TreeStoreElem), BLI_mempool_len(ts) - unused,
139                                                                                  512, BLI_MEMPOOL_ALLOW_ITER);
140                                         BLI_mempool_iternew(ts, &iter);
141                                         while ((tselem = BLI_mempool_iterstep(&iter))) {
142                                                 if (tselem->id) {
143                                                         tsenew = BLI_mempool_alloc(new_ts);
144                                                         *tsenew = *tselem;
145                                                 }
146                                         }
147                                         BLI_mempool_destroy(ts);
148                                         soops->treestore = new_ts;
149                                         if (soops->treehash) {
150                                                 /* update hash table to fix broken pointers */
151                                                 BKE_outliner_treehash_rebuild_from_treestore(soops->treehash, soops->treestore);
152                                         }
153                                 }
154                         }
155                 }
156                 else if (soops->treehash) {
157                         BKE_outliner_treehash_clear_used(soops->treehash);
158                 }
159         }
160 }
161
162 static void check_persistent(SpaceOops *soops, TreeElement *te, ID *id, short type, short nr)
163 {
164         TreeStoreElem *tselem;
165
166         if (soops->treestore == NULL) {
167                 /* if treestore was not created in readfile.c, create it here */
168                 soops->treestore = BLI_mempool_create(sizeof(TreeStoreElem), 1, 512, BLI_MEMPOOL_ALLOW_ITER);
169
170         }
171         if (soops->treehash == NULL) {
172                 soops->treehash = BKE_outliner_treehash_create_from_treestore(soops->treestore);
173         }
174
175         /* find any unused tree element in treestore and mark it as used
176          * (note that there may be multiple unused elements in case of linked objects) */
177         tselem = BKE_outliner_treehash_lookup_unused(soops->treehash, type, nr, id);
178         if (tselem) {
179                 te->store_elem = tselem;
180                 tselem->used = 1;
181                 return;
182         }
183
184         /* add 1 element to treestore */
185         tselem = BLI_mempool_alloc(soops->treestore);
186         tselem->type = type;
187         tselem->nr = type ? nr : 0;
188         tselem->id = id;
189         tselem->used = 0;
190         tselem->flag = TSE_CLOSED;
191         te->store_elem = tselem;
192         BKE_outliner_treehash_add_element(soops->treehash, tselem);
193 }
194
195 /* ********************************************************* */
196 /* Tree Management */
197
198 void outliner_free_tree(ListBase *tree)
199 {
200         for (TreeElement *element = tree->first, *element_next; element; element = element_next) {
201                 element_next = element->next;
202                 outliner_free_tree_element(element, tree);
203         }
204 }
205
206 void outliner_cleanup_tree(SpaceOops *soops)
207 {
208         outliner_free_tree(&soops->tree);
209         outliner_storage_cleanup(soops);
210 }
211
212 /**
213  * Free \a element and its sub-tree and remove its link in \a parent_subtree.
214  *
215  * \note Does not remove the TreeStoreElem of \a element!
216  * \param parent_subtree: Subtree of the parent element, so the list containing \a element.
217  */
218 void outliner_free_tree_element(TreeElement *element, ListBase *parent_subtree)
219 {
220         BLI_assert(BLI_findindex(parent_subtree, element) > -1);
221         BLI_remlink(parent_subtree, element);
222
223         outliner_free_tree(&element->subtree);
224
225         if (element->flag & TE_FREE_NAME) {
226                 MEM_freeN((void *)element->name);
227         }
228         MEM_freeN(element);
229 }
230
231
232 /* ********************************************************* */
233
234 /* Prototype, see functions below */
235 static TreeElement *outliner_add_element(SpaceOops *soops, ListBase *lb, void *idv,
236                                          TreeElement *parent, short type, short index);
237
238 /* -------------------------------------------------------- */
239
240 /* special handling of hierarchical non-lib data */
241 static void outliner_add_bone(SpaceOops *soops, ListBase *lb, ID *id, Bone *curBone,
242                               TreeElement *parent, int *a)
243 {
244         TreeElement *te = outliner_add_element(soops, lb, id, parent, TSE_BONE, *a);
245
246         (*a)++;
247         te->name = curBone->name;
248         te->directdata = curBone;
249
250         for (curBone = curBone->childbase.first; curBone; curBone = curBone->next) {
251                 outliner_add_bone(soops, &te->subtree, id, curBone, te, a);
252         }
253 }
254
255 static bool outliner_animdata_test(AnimData *adt)
256 {
257         if (adt)
258                 return (adt->action || adt->drivers.first || adt->nla_tracks.first);
259         return false;
260 }
261
262 #ifdef WITH_FREESTYLE
263 static void outliner_add_line_styles(SpaceOops *soops, ListBase *lb, Scene *sce, TreeElement *te)
264 {
265         ViewLayer *view_layer;
266         FreestyleLineSet *lineset;
267
268         for (view_layer = sce->view_layers.first; view_layer; view_layer = view_layer->next) {
269                 for (lineset = view_layer->freestyle_config.linesets.first; lineset; lineset = lineset->next) {
270                         FreestyleLineStyle *linestyle = lineset->linestyle;
271                         if (linestyle) {
272                                 linestyle->id.tag |= LIB_TAG_DOIT;
273                         }
274                 }
275         }
276         for (view_layer = sce->view_layers.first; view_layer; view_layer = view_layer->next) {
277                 for (lineset = view_layer->freestyle_config.linesets.first; lineset; lineset = lineset->next) {
278                         FreestyleLineStyle *linestyle = lineset->linestyle;
279                         if (linestyle) {
280                                 if (!(linestyle->id.tag & LIB_TAG_DOIT))
281                                         continue;
282                                 linestyle->id.tag &= ~LIB_TAG_DOIT;
283                                 outliner_add_element(soops, lb, linestyle, te, 0, 0);
284                         }
285                 }
286         }
287 }
288 #endif
289
290 static void outliner_add_scene_contents(SpaceOops *soops, ListBase *lb, Scene *sce, TreeElement *te)
291 {
292         /* View layers */
293         TreeElement *ten = outliner_add_element(soops, lb, sce, te, TSE_R_LAYER_BASE, 0);
294         ten->name = IFACE_("View Layers");
295
296         ViewLayer *view_layer;
297         for (view_layer = sce->view_layers.first; view_layer; view_layer = view_layer->next) {
298                 TreeElement *tenlay = outliner_add_element(soops, &ten->subtree, sce, te, TSE_R_LAYER, 0);
299                 tenlay->name = view_layer->name;
300                 tenlay->directdata = view_layer;
301         }
302
303         /* Collections */
304         ten = outliner_add_element(soops, lb, &sce->id, te, TSE_SCENE_COLLECTION_BASE, 0);
305         ten->name = IFACE_("Scene Collection");
306         outliner_add_collection_recursive(soops, sce->master_collection, ten);
307
308         /* Objects */
309         ten = outliner_add_element(soops, lb, sce, te, TSE_SCENE_OBJECTS_BASE, 0);
310         ten->name = IFACE_("Objects");
311         FOREACH_SCENE_OBJECT_BEGIN(sce, ob)
312         {
313                 outliner_add_element(soops, &ten->subtree, ob, NULL, 0, 0);
314         }
315         FOREACH_SCENE_OBJECT_END;
316         outliner_make_object_parent_hierarchy(&ten->subtree);
317
318         /* Animation Data */
319         if (outliner_animdata_test(sce->adt))
320                 outliner_add_element(soops, lb, sce, te, TSE_ANIM_DATA, 0);
321
322 }
323
324 // can be inlined if necessary
325 static void outliner_add_object_contents(SpaceOops *soops, TreeElement *te, TreeStoreElem *tselem, Object *ob)
326 {
327         if (outliner_animdata_test(ob->adt))
328                 outliner_add_element(soops, &te->subtree, ob, te, TSE_ANIM_DATA, 0);
329
330         outliner_add_element(soops, &te->subtree, ob->poselib, te, 0, 0); // XXX FIXME.. add a special type for this
331
332         if (ob->proxy && !ID_IS_LINKED(ob))
333                 outliner_add_element(soops, &te->subtree, ob->proxy, te, TSE_PROXY, 0);
334
335         outliner_add_element(soops, &te->subtree, ob->gpd, te, 0, 0);
336
337         outliner_add_element(soops, &te->subtree, ob->data, te, 0, 0);
338
339         if (ob->pose) {
340                 bArmature *arm = ob->data;
341                 bPoseChannel *pchan;
342                 TreeElement *tenla = outliner_add_element(soops, &te->subtree, ob, te, TSE_POSE_BASE, 0);
343
344                 tenla->name = IFACE_("Pose");
345
346                 /* channels undefined in editmode, but we want the 'tenla' pose icon itself */
347                 if ((arm->edbo == NULL) && (ob->mode & OB_MODE_POSE)) {
348                         TreeElement *ten;
349                         int a = 0, const_index = 1000;    /* ensure unique id for bone constraints */
350
351                         for (pchan = ob->pose->chanbase.first; pchan; pchan = pchan->next, a++) {
352                                 ten = outliner_add_element(soops, &tenla->subtree, ob, tenla, TSE_POSE_CHANNEL, a);
353                                 ten->name = pchan->name;
354                                 ten->directdata = pchan;
355                                 pchan->temp = (void *)ten;
356
357                                 if (pchan->constraints.first) {
358                                         //Object *target;
359                                         bConstraint *con;
360                                         TreeElement *ten1;
361                                         TreeElement *tenla1 = outliner_add_element(soops, &ten->subtree, ob, ten, TSE_CONSTRAINT_BASE, 0);
362                                         //char *str;
363
364                                         tenla1->name = IFACE_("Constraints");
365                                         for (con = pchan->constraints.first; con; con = con->next, const_index++) {
366                                                 ten1 = outliner_add_element(soops, &tenla1->subtree, ob, tenla1, TSE_CONSTRAINT, const_index);
367 #if 0 /* disabled as it needs to be reworked for recoded constraints system */
368                                                 target = get_constraint_target(con, &str);
369                                                 if (str && str[0]) ten1->name = str;
370                                                 else if (target) ten1->name = target->id.name + 2;
371                                                 else ten1->name = con->name;
372 #endif
373                                                 ten1->name = con->name;
374                                                 ten1->directdata = con;
375                                                 /* possible add all other types links? */
376                                         }
377                                 }
378                         }
379                         /* make hierarchy */
380                         ten = tenla->subtree.first;
381                         while (ten) {
382                                 TreeElement *nten = ten->next, *par;
383                                 tselem = TREESTORE(ten);
384                                 if (tselem->type == TSE_POSE_CHANNEL) {
385                                         pchan = (bPoseChannel *)ten->directdata;
386                                         if (pchan->parent) {
387                                                 BLI_remlink(&tenla->subtree, ten);
388                                                 par = (TreeElement *)pchan->parent->temp;
389                                                 BLI_addtail(&par->subtree, ten);
390                                                 ten->parent = par;
391                                         }
392                                 }
393                                 ten = nten;
394                         }
395                 }
396
397                 /* Pose Groups */
398                 if (ob->pose->agroups.first) {
399                         bActionGroup *agrp;
400                         TreeElement *ten_bonegrp = outliner_add_element(soops, &te->subtree, ob, te, TSE_POSEGRP_BASE, 0);
401                         int a = 0;
402
403                         ten_bonegrp->name = IFACE_("Bone Groups");
404                         for (agrp = ob->pose->agroups.first; agrp; agrp = agrp->next, a++) {
405                                 TreeElement *ten;
406                                 ten = outliner_add_element(soops, &ten_bonegrp->subtree, ob, ten_bonegrp, TSE_POSEGRP, a);
407                                 ten->name = agrp->name;
408                                 ten->directdata = agrp;
409                         }
410                 }
411         }
412
413         for (int a = 0; a < ob->totcol; a++) {
414                 outliner_add_element(soops, &te->subtree, ob->mat[a], te, 0, a);
415         }
416
417         if (ob->constraints.first) {
418                 //Object *target;
419                 bConstraint *con;
420                 TreeElement *ten;
421                 TreeElement *tenla = outliner_add_element(soops, &te->subtree, ob, te, TSE_CONSTRAINT_BASE, 0);
422                 //char *str;
423                 int a;
424
425                 tenla->name = IFACE_("Constraints");
426                 for (con = ob->constraints.first, a = 0; con; con = con->next, a++) {
427                         ten = outliner_add_element(soops, &tenla->subtree, ob, tenla, TSE_CONSTRAINT, a);
428 #if 0 /* disabled due to constraints system targets recode... code here needs review */
429                         target = get_constraint_target(con, &str);
430                         if (str && str[0]) ten->name = str;
431                         else if (target) ten->name = target->id.name + 2;
432                         else ten->name = con->name;
433 #endif
434                         ten->name = con->name;
435                         ten->directdata = con;
436                         /* possible add all other types links? */
437                 }
438         }
439
440         if (ob->modifiers.first) {
441                 ModifierData *md;
442                 TreeElement *ten_mod = outliner_add_element(soops, &te->subtree, ob, te, TSE_MODIFIER_BASE, 0);
443                 int index;
444
445                 ten_mod->name = IFACE_("Modifiers");
446                 for (index = 0, md = ob->modifiers.first; md; index++, md = md->next) {
447                         TreeElement *ten = outliner_add_element(soops, &ten_mod->subtree, ob, ten_mod, TSE_MODIFIER, index);
448                         ten->name = md->name;
449                         ten->directdata = md;
450
451                         if (md->type == eModifierType_Lattice) {
452                                 outliner_add_element(soops, &ten->subtree, ((LatticeModifierData *) md)->object, ten, TSE_LINKED_OB, 0);
453                         }
454                         else if (md->type == eModifierType_Curve) {
455                                 outliner_add_element(soops, &ten->subtree, ((CurveModifierData *) md)->object, ten, TSE_LINKED_OB, 0);
456                         }
457                         else if (md->type == eModifierType_Armature) {
458                                 outliner_add_element(soops, &ten->subtree, ((ArmatureModifierData *) md)->object, ten, TSE_LINKED_OB, 0);
459                         }
460                         else if (md->type == eModifierType_Hook) {
461                                 outliner_add_element(soops, &ten->subtree, ((HookModifierData *) md)->object, ten, TSE_LINKED_OB, 0);
462                         }
463                         else if (md->type == eModifierType_ParticleSystem) {
464                                 ParticleSystem *psys = ((ParticleSystemModifierData *) md)->psys;
465                                 TreeElement *ten_psys;
466
467                                 ten_psys = outliner_add_element(soops, &ten->subtree, ob, te, TSE_LINKED_PSYS, 0);
468                                 ten_psys->directdata = psys;
469                                 ten_psys->name = psys->part->id.name + 2;
470                         }
471                 }
472         }
473
474         /* vertex groups */
475         if (ob->defbase.first) {
476                 bDeformGroup *defgroup;
477                 TreeElement *ten;
478                 TreeElement *tenla = outliner_add_element(soops, &te->subtree, ob, te, TSE_DEFGROUP_BASE, 0);
479                 int a;
480
481                 tenla->name = IFACE_("Vertex Groups");
482                 for (defgroup = ob->defbase.first, a = 0; defgroup; defgroup = defgroup->next, a++) {
483                         ten = outliner_add_element(soops, &tenla->subtree, ob, tenla, TSE_DEFGROUP, a);
484                         ten->name = defgroup->name;
485                         ten->directdata = defgroup;
486                 }
487         }
488
489         /* duplicated group */
490         if (ob->dup_group)
491                 outliner_add_element(soops, &te->subtree, ob->dup_group, te, 0, 0);
492 }
493
494
495 // can be inlined if necessary
496 static void outliner_add_id_contents(SpaceOops *soops, TreeElement *te, TreeStoreElem *tselem, ID *id)
497 {
498         /* tuck pointer back in object, to construct hierarchy */
499         if (GS(id->name) == ID_OB) id->newid = (ID *)te;
500
501         /* expand specific data always */
502         switch (GS(id->name)) {
503                 case ID_LI:
504                 {
505                         te->name = ((Library *)id)->name;
506                         break;
507                 }
508                 case ID_SCE:
509                 {
510                         outliner_add_scene_contents(soops, &te->subtree, (Scene *)id, te);
511                         break;
512                 }
513                 case ID_OB:
514                 {
515                         outliner_add_object_contents(soops, te, tselem, (Object *)id);
516                         break;
517                 }
518                 case ID_ME:
519                 {
520                         Mesh *me = (Mesh *)id;
521                         int a;
522
523                         if (outliner_animdata_test(me->adt))
524                                 outliner_add_element(soops, &te->subtree, me, te, TSE_ANIM_DATA, 0);
525
526                         outliner_add_element(soops, &te->subtree, me->key, te, 0, 0);
527                         for (a = 0; a < me->totcol; a++)
528                                 outliner_add_element(soops, &te->subtree, me->mat[a], te, 0, a);
529                         /* could do tfaces with image links, but the images are not grouped nicely.
530                          * would require going over all tfaces, sort images in use. etc... */
531                         break;
532                 }
533                 case ID_CU:
534                 {
535                         Curve *cu = (Curve *)id;
536                         int a;
537
538                         if (outliner_animdata_test(cu->adt))
539                                 outliner_add_element(soops, &te->subtree, cu, te, TSE_ANIM_DATA, 0);
540
541                         for (a = 0; a < cu->totcol; a++)
542                                 outliner_add_element(soops, &te->subtree, cu->mat[a], te, 0, a);
543                         break;
544                 }
545                 case ID_MB:
546                 {
547                         MetaBall *mb = (MetaBall *)id;
548                         int a;
549
550                         if (outliner_animdata_test(mb->adt))
551                                 outliner_add_element(soops, &te->subtree, mb, te, TSE_ANIM_DATA, 0);
552
553                         for (a = 0; a < mb->totcol; a++)
554                                 outliner_add_element(soops, &te->subtree, mb->mat[a], te, 0, a);
555                         break;
556                 }
557                 case ID_MA:
558                 {
559                         Material *ma = (Material *)id;
560
561                         if (outliner_animdata_test(ma->adt))
562                                 outliner_add_element(soops, &te->subtree, ma, te, TSE_ANIM_DATA, 0);
563                         break;
564                 }
565                 case ID_TE:
566                 {
567                         Tex *tex = (Tex *)id;
568
569                         if (outliner_animdata_test(tex->adt))
570                                 outliner_add_element(soops, &te->subtree, tex, te, TSE_ANIM_DATA, 0);
571
572                         outliner_add_element(soops, &te->subtree, tex->ima, te, 0, 0);
573                         break;
574                 }
575                 case ID_CA:
576                 {
577                         Camera *ca = (Camera *)id;
578
579                         if (outliner_animdata_test(ca->adt))
580                                 outliner_add_element(soops, &te->subtree, ca, te, TSE_ANIM_DATA, 0);
581                         break;
582                 }
583                 case ID_CF:
584                 {
585                         CacheFile *cache_file = (CacheFile *)id;
586
587                         if (outliner_animdata_test(cache_file->adt)) {
588                                 outliner_add_element(soops, &te->subtree, cache_file, te, TSE_ANIM_DATA, 0);
589                         }
590
591                         break;
592                 }
593                 case ID_LA:
594                 {
595                         Lamp *la = (Lamp *)id;
596
597                         if (outliner_animdata_test(la->adt))
598                                 outliner_add_element(soops, &te->subtree, la, te, TSE_ANIM_DATA, 0);
599                         break;
600                 }
601                 case ID_SPK:
602                 {
603                         Speaker *spk = (Speaker *)id;
604
605                         if (outliner_animdata_test(spk->adt))
606                                 outliner_add_element(soops, &te->subtree, spk, te, TSE_ANIM_DATA, 0);
607                         break;
608                 }
609                 case ID_LP:
610                 {
611                         LightProbe *prb = (LightProbe *)id;
612
613                         if (outliner_animdata_test(prb->adt))
614                                 outliner_add_element(soops, &te->subtree, prb, te, TSE_ANIM_DATA, 0);
615                         break;
616                 }
617                 case ID_WO:
618                 {
619                         World *wrld = (World *)id;
620
621                         if (outliner_animdata_test(wrld->adt))
622                                 outliner_add_element(soops, &te->subtree, wrld, te, TSE_ANIM_DATA, 0);
623                         break;
624                 }
625                 case ID_KE:
626                 {
627                         Key *key = (Key *)id;
628
629                         if (outliner_animdata_test(key->adt))
630                                 outliner_add_element(soops, &te->subtree, key, te, TSE_ANIM_DATA, 0);
631                         break;
632                 }
633                 case ID_AC:
634                 {
635                         // XXX do we want to be exposing the F-Curves here?
636                         //bAction *act = (bAction *)id;
637                         break;
638                 }
639                 case ID_AR:
640                 {
641                         bArmature *arm = (bArmature *)id;
642                         int a = 0;
643
644                         if (outliner_animdata_test(arm->adt))
645                                 outliner_add_element(soops, &te->subtree, arm, te, TSE_ANIM_DATA, 0);
646
647                         if (arm->edbo) {
648                                 EditBone *ebone;
649                                 TreeElement *ten;
650
651                                 for (ebone = arm->edbo->first; ebone; ebone = ebone->next, a++) {
652                                         ten = outliner_add_element(soops, &te->subtree, id, te, TSE_EBONE, a);
653                                         ten->directdata = ebone;
654                                         ten->name = ebone->name;
655                                         ebone->temp.p = ten;
656                                 }
657                                 /* make hierarchy */
658                                 ten = arm->edbo->first ? ((EditBone *)arm->edbo->first)->temp.p : NULL;
659                                 while (ten) {
660                                         TreeElement *nten = ten->next, *par;
661                                         ebone = (EditBone *)ten->directdata;
662                                         if (ebone->parent) {
663                                                 BLI_remlink(&te->subtree, ten);
664                                                 par = ebone->parent->temp.p;
665                                                 BLI_addtail(&par->subtree, ten);
666                                                 ten->parent = par;
667                                         }
668                                         ten = nten;
669                                 }
670                         }
671                         else {
672                                 /* do not extend Armature when we have posemode */
673                                 tselem = TREESTORE(te->parent);
674                                 if (GS(tselem->id->name) == ID_OB && ((Object *)tselem->id)->mode & OB_MODE_POSE) {
675                                         /* pass */
676                                 }
677                                 else {
678                                         Bone *curBone;
679                                         for (curBone = arm->bonebase.first; curBone; curBone = curBone->next) {
680                                                 outliner_add_bone(soops, &te->subtree, id, curBone, te, &a);
681                                         }
682                                 }
683                         }
684                         break;
685                 }
686                 case ID_LS:
687                 {
688                         FreestyleLineStyle *linestyle = (FreestyleLineStyle *)id;
689                         int a;
690
691                         if (outliner_animdata_test(linestyle->adt))
692                                 outliner_add_element(soops, &te->subtree, linestyle, te, TSE_ANIM_DATA, 0);
693
694                         for (a = 0; a < MAX_MTEX; a++) {
695                                 if (linestyle->mtex[a])
696                                         outliner_add_element(soops, &te->subtree, linestyle->mtex[a]->tex, te, 0, a);
697                         }
698                         break;
699                 }
700                 case ID_GD:
701                 {
702                         bGPdata *gpd = (bGPdata *)id;
703                         bGPDlayer *gpl;
704                         int a = 0;
705
706                         if (outliner_animdata_test(gpd->adt))
707                                 outliner_add_element(soops, &te->subtree, gpd, te, TSE_ANIM_DATA, 0);
708
709                         // TODO: base element for layers?
710                         for (gpl = gpd->layers.first; gpl; gpl = gpl->next) {
711                                 outliner_add_element(soops, &te->subtree, gpl, te, TSE_GP_LAYER, a);
712                                 a++;
713                         }
714                         break;
715                 }
716                 case ID_GR:
717                 {
718                         /* Don't expand for instances, creates too many elements. */
719                         if (!(te->parent && te->parent->idcode == ID_OB)) {
720                                 Collection *collection = (Collection *)id;
721                                 outliner_add_collection_recursive(soops, collection, te);
722                         }
723                 }
724                 default:
725                         break;
726         }
727 }
728
729 // TODO: this function needs to be split up! It's getting a bit too large...
730 // Note: "ID" is not always a real ID
731 static TreeElement *outliner_add_element(SpaceOops *soops, ListBase *lb, void *idv,
732                                          TreeElement *parent, short type, short index)
733 {
734         TreeElement *te;
735         TreeStoreElem *tselem;
736         ID *id = idv;
737
738         if (ELEM(type, TSE_RNA_STRUCT, TSE_RNA_PROPERTY, TSE_RNA_ARRAY_ELEM)) {
739                 id = ((PointerRNA *)idv)->id.data;
740                 if (!id) id = ((PointerRNA *)idv)->data;
741         }
742         else if (type == TSE_GP_LAYER) {
743                 /* idv is the layer its self */
744                 id = TREESTORE(parent)->id;
745         }
746
747         /* exceptions */
748         if (type == TSE_ID_BASE) {
749                 /* pass */
750         }
751         else if (id == NULL) {
752                 return NULL;
753         }
754
755         if (type == 0) {
756                 /* Zero type means real ID, ensure we do not get non-outliner ID types here... */
757                 BLI_assert(TREESTORE_ID_TYPE(id));
758         }
759
760         te = MEM_callocN(sizeof(TreeElement), "tree elem");
761         /* add to the visual tree */
762         BLI_addtail(lb, te);
763         /* add to the storage */
764         check_persistent(soops, te, id, type, index);
765         tselem = TREESTORE(te);
766
767         /* if we are searching for something expand to see child elements */
768         if (SEARCHING_OUTLINER(soops))
769                 tselem->flag |= TSE_CHILDSEARCH;
770
771         te->parent = parent;
772         te->index = index;   // for data arrays
773         if (ELEM(type, TSE_SEQUENCE, TSE_SEQ_STRIP, TSE_SEQUENCE_DUP)) {
774                 /* pass */
775         }
776         else if (ELEM(type, TSE_RNA_STRUCT, TSE_RNA_PROPERTY, TSE_RNA_ARRAY_ELEM)) {
777                 /* pass */
778         }
779         else if (type == TSE_ANIM_DATA) {
780                 /* pass */
781         }
782         else if (type == TSE_GP_LAYER) {
783                 /* pass */
784         }
785         else if (ELEM(type, TSE_LAYER_COLLECTION, TSE_SCENE_COLLECTION_BASE, TSE_VIEW_COLLECTION_BASE)) {
786                 /* pass */
787         }
788         else if (type == TSE_ID_BASE) {
789                 /* pass */
790         }
791         else {
792                 /* do here too, for blend file viewer, own ID_LI then shows file name */
793                 if (GS(id->name) == ID_LI)
794                         te->name = ((Library *)id)->name;
795                 else
796                         te->name = id->name + 2; // default, can be overridden by Library or non-ID data
797                 te->idcode = GS(id->name);
798         }
799
800         if (type == 0) {
801                 TreeStoreElem *tsepar = parent ? TREESTORE(parent) : NULL;
802
803                 /* ID datablock */
804                 if (tsepar == NULL || tsepar->type != TSE_ID_BASE || soops->filter_id_type) {
805                         outliner_add_id_contents(soops, te, tselem, id);
806                 }
807         }
808         else if (type == TSE_ANIM_DATA) {
809                 IdAdtTemplate *iat = (IdAdtTemplate *)idv;
810                 AnimData *adt = (AnimData *)iat->adt;
811
812                 /* this element's info */
813                 te->name = IFACE_("Animation");
814                 te->directdata = adt;
815
816                 /* Action */
817                 outliner_add_element(soops, &te->subtree, adt->action, te, 0, 0);
818
819                 /* Drivers */
820                 if (adt->drivers.first) {
821                         TreeElement *ted = outliner_add_element(soops, &te->subtree, adt, te, TSE_DRIVER_BASE, 0);
822                         ID *lastadded = NULL;
823                         FCurve *fcu;
824
825                         ted->name = IFACE_("Drivers");
826
827                         for (fcu = adt->drivers.first; fcu; fcu = fcu->next) {
828                                 if (fcu->driver && fcu->driver->variables.first) {
829                                         ChannelDriver *driver = fcu->driver;
830                                         DriverVar *dvar;
831
832                                         for (dvar = driver->variables.first; dvar; dvar = dvar->next) {
833                                                 /* loop over all targets used here */
834                                                 DRIVER_TARGETS_USED_LOOPER_BEGIN(dvar)
835                                                 {
836                                                         if (lastadded != dtar->id) {
837                                                                 // XXX this lastadded check is rather lame, and also fails quite badly...
838                                                                 outliner_add_element(soops, &ted->subtree, dtar->id, ted, TSE_LINKED_OB, 0);
839                                                                 lastadded = dtar->id;
840                                                         }
841                                                 }
842                                                 DRIVER_TARGETS_LOOPER_END;
843                                         }
844                                 }
845                         }
846                 }
847
848                 /* NLA Data */
849                 if (adt->nla_tracks.first) {
850                         TreeElement *tenla = outliner_add_element(soops, &te->subtree, adt, te, TSE_NLA, 0);
851                         NlaTrack *nlt;
852                         int a = 0;
853
854                         tenla->name = IFACE_("NLA Tracks");
855
856                         for (nlt = adt->nla_tracks.first; nlt; nlt = nlt->next) {
857                                 TreeElement *tenlt = outliner_add_element(soops, &tenla->subtree, nlt, tenla, TSE_NLA_TRACK, a);
858                                 NlaStrip *strip;
859                                 TreeElement *ten;
860                                 int b = 0;
861
862                                 tenlt->name = nlt->name;
863
864                                 for (strip = nlt->strips.first; strip; strip = strip->next, b++) {
865                                         ten = outliner_add_element(soops, &tenlt->subtree, strip->act, tenlt, TSE_NLA_ACTION, b);
866                                         if (ten) ten->directdata = strip;
867                                 }
868                         }
869                 }
870         }
871         else if (type == TSE_GP_LAYER) {
872                 bGPDlayer *gpl = (bGPDlayer *)idv;
873
874                 te->name = gpl->info;
875                 te->directdata = gpl;
876         }
877         else if (type == TSE_SEQUENCE) {
878                 Sequence *seq = (Sequence *) idv;
879                 Sequence *p;
880
881                 /*
882                  * The idcode is a little hack, but the outliner
883                  * only check te->idcode if te->type is equal to zero,
884                  * so this is "safe".
885                  */
886                 te->idcode = seq->type;
887                 te->directdata = seq;
888                 te->name = seq->name + 2;
889
890                 if (!(seq->type & SEQ_TYPE_EFFECT)) {
891                         /*
892                          * This work like the sequence.
893                          * If the sequence have a name (not default name)
894                          * show it, in other case put the filename.
895                          */
896
897                         if (seq->type == SEQ_TYPE_META) {
898                                 p = seq->seqbase.first;
899                                 while (p) {
900                                         outliner_add_element(soops, &te->subtree, (void *)p, te, TSE_SEQUENCE, index);
901                                         p = p->next;
902                                 }
903                         }
904                         else
905                                 outliner_add_element(soops, &te->subtree, (void *)seq->strip, te, TSE_SEQ_STRIP, index);
906                 }
907         }
908         else if (type == TSE_SEQ_STRIP) {
909                 Strip *strip = (Strip *)idv;
910
911                 if (strip->dir[0] != '\0')
912                         te->name = strip->dir;
913                 else
914                         te->name = IFACE_("Strip None");
915                 te->directdata = strip;
916         }
917         else if (type == TSE_SEQUENCE_DUP) {
918                 Sequence *seq = (Sequence *)idv;
919
920                 te->idcode = seq->type;
921                 te->directdata = seq;
922                 te->name = seq->strip->stripdata->name;
923         }
924         else if (ELEM(type, TSE_RNA_STRUCT, TSE_RNA_PROPERTY, TSE_RNA_ARRAY_ELEM)) {
925                 PointerRNA pptr, propptr, *ptr = (PointerRNA *)idv;
926                 PropertyRNA *prop, *iterprop;
927                 PropertyType proptype;
928
929                 /* Don't display arrays larger, weak but index is stored as a short,
930                  * also the outliner isn't intended for editing such large data-sets. */
931                 BLI_STATIC_ASSERT(sizeof(te->index) == 2, "Index is no longer short!");
932                 const int tot_limit = SHRT_MAX;
933
934                 int a, tot;
935
936                 /* we do lazy build, for speed and to avoid infinite recursion */
937
938                 if (ptr->data == NULL) {
939                         te->name = IFACE_("(empty)");
940                 }
941                 else if (type == TSE_RNA_STRUCT) {
942                         /* struct */
943                         te->name = RNA_struct_name_get_alloc(ptr, NULL, 0, NULL);
944
945                         if (te->name)
946                                 te->flag |= TE_FREE_NAME;
947                         else
948                                 te->name = RNA_struct_ui_name(ptr->type);
949
950                         /* If searching don't expand RNA entries */
951                         if (SEARCHING_OUTLINER(soops) && BLI_strcasecmp("RNA", te->name) == 0) tselem->flag &= ~TSE_CHILDSEARCH;
952
953                         iterprop = RNA_struct_iterator_property(ptr->type);
954                         tot = RNA_property_collection_length(ptr, iterprop);
955                         CLAMP_MAX(tot, tot_limit);
956
957                         /* auto open these cases */
958                         if (!parent || (RNA_property_type(parent->directdata)) == PROP_POINTER)
959                                 if (!tselem->used)
960                                         tselem->flag &= ~TSE_CLOSED;
961
962                         if (TSELEM_OPEN(tselem, soops)) {
963                                 for (a = 0; a < tot; a++) {
964                                         RNA_property_collection_lookup_int(ptr, iterprop, a, &propptr);
965                                         if (!(RNA_property_flag(propptr.data) & PROP_HIDDEN)) {
966                                                 outliner_add_element(soops, &te->subtree, (void *)ptr, te, TSE_RNA_PROPERTY, a);
967                                         }
968                                 }
969                         }
970                         else if (tot)
971                                 te->flag |= TE_LAZY_CLOSED;
972
973                         te->rnaptr = *ptr;
974                 }
975                 else if (type == TSE_RNA_PROPERTY) {
976                         /* property */
977                         iterprop = RNA_struct_iterator_property(ptr->type);
978                         RNA_property_collection_lookup_int(ptr, iterprop, index, &propptr);
979
980                         prop = propptr.data;
981                         proptype = RNA_property_type(prop);
982
983                         te->name = RNA_property_ui_name(prop);
984                         te->directdata = prop;
985                         te->rnaptr = *ptr;
986
987                         /* If searching don't expand RNA entries */
988                         if (SEARCHING_OUTLINER(soops) && BLI_strcasecmp("RNA", te->name) == 0) tselem->flag &= ~TSE_CHILDSEARCH;
989
990                         if (proptype == PROP_POINTER) {
991                                 pptr = RNA_property_pointer_get(ptr, prop);
992
993                                 if (pptr.data) {
994                                         if (TSELEM_OPEN(tselem, soops))
995                                                 outliner_add_element(soops, &te->subtree, (void *)&pptr, te, TSE_RNA_STRUCT, -1);
996                                         else
997                                                 te->flag |= TE_LAZY_CLOSED;
998                                 }
999                         }
1000                         else if (proptype == PROP_COLLECTION) {
1001                                 tot = RNA_property_collection_length(ptr, prop);
1002                                 CLAMP_MAX(tot, tot_limit);
1003
1004                                 if (TSELEM_OPEN(tselem, soops)) {
1005                                         for (a = 0; a < tot; a++) {
1006                                                 RNA_property_collection_lookup_int(ptr, prop, a, &pptr);
1007                                                 outliner_add_element(soops, &te->subtree, (void *)&pptr, te, TSE_RNA_STRUCT, a);
1008                                         }
1009                                 }
1010                                 else if (tot)
1011                                         te->flag |= TE_LAZY_CLOSED;
1012                         }
1013                         else if (ELEM(proptype, PROP_BOOLEAN, PROP_INT, PROP_FLOAT)) {
1014                                 tot = RNA_property_array_length(ptr, prop);
1015                                 CLAMP_MAX(tot, tot_limit);
1016
1017                                 if (TSELEM_OPEN(tselem, soops)) {
1018                                         for (a = 0; a < tot; a++)
1019                                                 outliner_add_element(soops, &te->subtree, (void *)ptr, te, TSE_RNA_ARRAY_ELEM, a);
1020                                 }
1021                                 else if (tot)
1022                                         te->flag |= TE_LAZY_CLOSED;
1023                         }
1024                 }
1025                 else if (type == TSE_RNA_ARRAY_ELEM) {
1026                         char c;
1027
1028                         prop = parent->directdata;
1029
1030                         te->directdata = prop;
1031                         te->rnaptr = *ptr;
1032                         te->index = index;
1033
1034                         c = RNA_property_array_item_char(prop, index);
1035
1036                         te->name = MEM_callocN(sizeof(char) * 20, "OutlinerRNAArrayName");
1037                         if (c) sprintf((char *)te->name, "  %c", c);
1038                         else sprintf((char *)te->name, "  %d", index + 1);
1039                         te->flag |= TE_FREE_NAME;
1040                 }
1041         }
1042         else if (type == TSE_KEYMAP) {
1043                 wmKeyMap *km = (wmKeyMap *)idv;
1044                 wmKeyMapItem *kmi;
1045                 char opname[OP_MAX_TYPENAME];
1046
1047                 te->directdata = idv;
1048                 te->name = km->idname;
1049
1050                 if (TSELEM_OPEN(tselem, soops)) {
1051                         int a = 0;
1052
1053                         for (kmi = km->items.first; kmi; kmi = kmi->next, a++) {
1054                                 const char *key = WM_key_event_string(kmi->type, false);
1055
1056                                 if (key[0]) {
1057                                         wmOperatorType *ot = NULL;
1058
1059                                         if (kmi->propvalue) {
1060                                                 /* pass */
1061                                         }
1062                                         else {
1063                                                 ot = WM_operatortype_find(kmi->idname, 0);
1064                                         }
1065
1066                                         if (ot || kmi->propvalue) {
1067                                                 TreeElement *ten = outliner_add_element(soops, &te->subtree, kmi, te, TSE_KEYMAP_ITEM, a);
1068
1069                                                 ten->directdata = kmi;
1070
1071                                                 if (kmi->propvalue) {
1072                                                         ten->name = IFACE_("Modal map, not yet");
1073                                                 }
1074                                                 else {
1075                                                         WM_operator_py_idname(opname, ot->idname);
1076                                                         ten->name = BLI_strdup(opname);
1077                                                         ten->flag |= TE_FREE_NAME;
1078                                                 }
1079                                         }
1080                                 }
1081                         }
1082                 }
1083                 else
1084                         te->flag |= TE_LAZY_CLOSED;
1085         }
1086
1087         return te;
1088 }
1089
1090 /* ======================================================= */
1091 /* Sequencer mode tree building */
1092
1093 /* Helped function to put duplicate sequence in the same tree. */
1094 static int need_add_seq_dup(Sequence *seq)
1095 {
1096         Sequence *p;
1097
1098         if ((!seq->strip) || (!seq->strip->stripdata))
1099                 return(1);
1100
1101         /*
1102          * First check backward, if we found a duplicate
1103          * sequence before this, don't need it, just return.
1104          */
1105         p = seq->prev;
1106         while (p) {
1107                 if ((!p->strip) || (!p->strip->stripdata)) {
1108                         p = p->prev;
1109                         continue;
1110                 }
1111
1112                 if (STREQ(p->strip->stripdata->name, seq->strip->stripdata->name))
1113                         return(2);
1114                 p = p->prev;
1115         }
1116
1117         p = seq->next;
1118         while (p) {
1119                 if ((!p->strip) || (!p->strip->stripdata)) {
1120                         p = p->next;
1121                         continue;
1122                 }
1123
1124                 if (STREQ(p->strip->stripdata->name, seq->strip->stripdata->name))
1125                         return(0);
1126                 p = p->next;
1127         }
1128         return(1);
1129 }
1130
1131 static void outliner_add_seq_dup(SpaceOops *soops, Sequence *seq, TreeElement *te, short index)
1132 {
1133         /* TreeElement *ch; */ /* UNUSED */
1134         Sequence *p;
1135
1136         p = seq;
1137         while (p) {
1138                 if ((!p->strip) || (!p->strip->stripdata) || (p->strip->stripdata->name[0] == '\0')) {
1139                         p = p->next;
1140                         continue;
1141                 }
1142
1143                 if (STREQ(p->strip->stripdata->name, seq->strip->stripdata->name))
1144                         /* ch = */ /* UNUSED */ outliner_add_element(soops, &te->subtree, (void *)p, te, TSE_SEQUENCE, index);
1145                 p = p->next;
1146         }
1147 }
1148
1149
1150 /* ----------------------------------------------- */
1151
1152 static const char *outliner_idcode_to_plural(short idcode)
1153 {
1154         const char *propname = BKE_idcode_to_name_plural(idcode);
1155         PropertyRNA *prop = RNA_struct_type_find_property(&RNA_BlendData, propname);
1156         return (prop) ? RNA_property_ui_name(prop) : "UNKNOWN";
1157 }
1158
1159 static bool outliner_library_id_show(Library *lib, ID *id, short filter_id_type)
1160 {
1161         if (id->lib != lib) {
1162                 return false;
1163         }
1164
1165         if (filter_id_type == ID_GR) {
1166                 /* Don't show child collections of non-scene master collection,
1167                  * they are already shown as children. */
1168                 Collection *collection = (Collection *)id;
1169                 bool has_non_scene_parent = false;
1170
1171                 for (CollectionParent *cparent = collection->parents.first; cparent; cparent = cparent->next) {
1172                         if (!(cparent->collection->flag & COLLECTION_IS_MASTER)) {
1173                                 has_non_scene_parent = true;
1174                         }
1175                 }
1176
1177                 if (has_non_scene_parent) {
1178                         return false;
1179                 }
1180         }
1181
1182         return true;
1183 }
1184
1185 static TreeElement *outliner_add_library_contents(Main *mainvar, SpaceOops *soops, ListBase *lb, Library *lib)
1186 {
1187         TreeElement *ten, *tenlib = NULL;
1188         ListBase *lbarray[MAX_LIBARRAY];
1189         int a, tot;
1190         short filter_id_type = (soops->filter & SO_FILTER_ID_TYPE) ? soops->filter_id_type : 0;
1191
1192         if (filter_id_type) {
1193                 lbarray[0] = which_libbase(mainvar, soops->filter_id_type);
1194                 tot = 1;
1195         }
1196         else {
1197                 tot = set_listbasepointers(mainvar, lbarray);
1198         }
1199
1200         for (a = 0; a < tot; a++) {
1201                 if (lbarray[a] && lbarray[a]->first) {
1202                         ID *id = lbarray[a]->first;
1203
1204                         /* check if there's data in current lib */
1205                         for (; id; id = id->next)
1206                                 if (id->lib == lib)
1207                                         break;
1208
1209                         if (id) {
1210                                 if (!tenlib) {
1211                                         /* Create library tree element on demand, depending if there are any datablocks. */
1212                                         if (lib) {
1213                                                 tenlib = outliner_add_element(soops, lb, lib, NULL, 0, 0);
1214                                         }
1215                                         else {
1216                                                 tenlib = outliner_add_element(soops, lb, mainvar, NULL, TSE_ID_BASE, 0);
1217                                                 tenlib->name = IFACE_("Current File");
1218                                         }
1219                                 }
1220
1221                                 /* Create datablock list parent element on demand. */
1222                                 if (filter_id_type) {
1223                                         ten = tenlib;
1224                                 }
1225                                 else {
1226                                         ten = outliner_add_element(soops, &tenlib->subtree, lbarray[a], NULL, TSE_ID_BASE, 0);
1227                                         ten->directdata = lbarray[a];
1228                                         ten->name = outliner_idcode_to_plural(GS(id->name));
1229                                 }
1230
1231                                 for (id = lbarray[a]->first; id; id = id->next) {
1232                                         if (outliner_library_id_show(lib, id, filter_id_type)) {
1233                                                 outliner_add_element(soops, &ten->subtree, id, ten, 0, 0);
1234                                         }
1235                                 }
1236                         }
1237                 }
1238         }
1239
1240         return tenlib;
1241 }
1242
1243 static void outliner_add_orphaned_datablocks(Main *mainvar, SpaceOops *soops)
1244 {
1245         TreeElement *ten;
1246         ListBase *lbarray[MAX_LIBARRAY];
1247         int a, tot;
1248         short filter_id_type = (soops->filter & SO_FILTER_ID_TYPE) ? soops->filter_id_type : 0;
1249
1250         if (filter_id_type) {
1251                 lbarray[0] = which_libbase(mainvar, soops->filter_id_type);
1252                 tot = 1;
1253         }
1254         else {
1255                 tot = set_listbasepointers(mainvar, lbarray);
1256         }
1257
1258         for (a = 0; a < tot; a++) {
1259                 if (lbarray[a] && lbarray[a]->first) {
1260                         ID *id = lbarray[a]->first;
1261
1262                         /* check if there are any datablocks of this type which are orphans */
1263                         for (; id; id = id->next) {
1264                                 if (ID_REAL_USERS(id) <= 0)
1265                                         break;
1266                         }
1267
1268                         if (id) {
1269                                 /* header for this type of datablock */
1270                                 if (filter_id_type) {
1271                                         ten = NULL;
1272                                 }
1273                                 else {
1274                                         ten = outliner_add_element(soops, &soops->tree, lbarray[a], NULL, TSE_ID_BASE, 0);
1275                                         ten->directdata = lbarray[a];
1276                                         ten->name = outliner_idcode_to_plural(GS(id->name));
1277                                 }
1278
1279                                 /* add the orphaned datablocks - these will not be added with any subtrees attached */
1280                                 for (id = lbarray[a]->first; id; id = id->next) {
1281                                         if (ID_REAL_USERS(id) <= 0)
1282                                                 outliner_add_element(soops, (ten) ? &ten->subtree : &soops->tree, id, ten, 0, 0);
1283                                 }
1284                         }
1285                 }
1286         }
1287 }
1288
1289 static void outliner_add_layer_collection_objects(
1290         SpaceOops *soops, ListBase *tree, ViewLayer *layer,
1291         LayerCollection *lc, TreeElement *ten)
1292 {
1293         for (CollectionObject *cob = lc->collection->gobject.first; cob; cob = cob->next) {
1294                 Base *base = BKE_view_layer_base_find(layer, cob->ob);
1295                 TreeElement *te_object = outliner_add_element(soops, tree, base->object, ten, 0, 0);
1296                 te_object->directdata = base;
1297
1298                 if (!(base->flag & BASE_VISIBLE)) {
1299                         te_object->flag |= TE_DISABLED;
1300                 }
1301         }
1302 }
1303
1304 static void outliner_add_layer_collections_recursive(
1305         SpaceOops *soops, ListBase *tree, ViewLayer *layer,
1306         ListBase *layer_collections, TreeElement *parent_ten,
1307         const bool show_objects)
1308 {
1309         for (LayerCollection *lc = layer_collections->first; lc; lc = lc->next) {
1310                 ID *id = &lc->collection->id;
1311                 TreeElement *ten = outliner_add_element(soops, tree, id, parent_ten, TSE_LAYER_COLLECTION, 0);
1312
1313                 ten->name = id->name + 2;
1314                 ten->directdata = lc;
1315
1316                 const bool exclude = (lc->flag & LAYER_COLLECTION_EXCLUDE) != 0;
1317                 if (exclude ||
1318                     ((layer->runtime_flag & VIEW_LAYER_HAS_HIDE) &&
1319                      !(lc->runtime_flag & LAYER_COLLECTION_HAS_VISIBLE_OBJECTS)))
1320                 {
1321                         ten->flag |= TE_DISABLED;
1322                 }
1323
1324                 outliner_add_layer_collections_recursive(soops, &ten->subtree, layer, &lc->layer_collections, ten, show_objects);
1325                 if (!exclude && show_objects) {
1326                         outliner_add_layer_collection_objects(soops, &ten->subtree, layer, lc, ten);
1327                 }
1328         }
1329 }
1330
1331 static void outliner_add_view_layer(SpaceOops *soops, ListBase *tree, TreeElement *parent,
1332                                     ViewLayer *layer, const bool show_objects)
1333 {
1334         /* First layer collection is for master collection, don't show it. */
1335         LayerCollection *lc = layer->layer_collections.first;
1336         if (lc == NULL) {
1337                 return;
1338         }
1339
1340         outliner_add_layer_collections_recursive(soops, tree, layer, &lc->layer_collections, parent, show_objects);
1341         if (show_objects) {
1342                 outliner_add_layer_collection_objects(soops, tree, layer, lc, parent);
1343         }
1344 }
1345
1346 BLI_INLINE void outliner_add_collection_init(TreeElement *te, Collection *collection)
1347 {
1348         te->name = BKE_collection_ui_name_get(collection);
1349         te->directdata = collection;
1350 }
1351
1352 BLI_INLINE void outliner_add_collection_objects(
1353         SpaceOops *soops, ListBase *tree, Collection *collection, TreeElement *parent)
1354 {
1355         for (CollectionObject *cob = collection->gobject.first; cob; cob = cob->next) {
1356                 outliner_add_element(soops, tree, cob->ob, parent, 0, 0);
1357         }
1358 }
1359
1360 static TreeElement *outliner_add_collection_recursive(
1361         SpaceOops *soops, Collection *collection, TreeElement *ten)
1362 {
1363         outliner_add_collection_init(ten, collection);
1364
1365         for (CollectionChild *child = collection->children.first; child; child = child->next) {
1366                 outliner_add_element(soops, &ten->subtree, &child->collection->id, ten, 0, 0);
1367         }
1368
1369         if (soops->outlinevis != SO_SCENES) {
1370                 outliner_add_collection_objects(soops, &ten->subtree, collection, ten);
1371         }
1372
1373         return ten;
1374 }
1375
1376 /* ======================================================= */
1377 /* Generic Tree Building helpers - order these are called is top to bottom */
1378
1379 /* Hierarchy --------------------------------------------- */
1380
1381 /* make sure elements are correctly nested */
1382 static void outliner_make_object_parent_hierarchy(ListBase *lb)
1383 {
1384         TreeElement *te, *ten, *tep;
1385         TreeStoreElem *tselem;
1386
1387         /* build hierarchy */
1388         // XXX also, set extents here...
1389         te = lb->first;
1390         while (te) {
1391                 ten = te->next;
1392                 tselem = TREESTORE(te);
1393
1394                 if (tselem->type == 0 && te->idcode == ID_OB) {
1395                         Object *ob = (Object *)tselem->id;
1396                         if (ob->parent && ob->parent->id.newid) {
1397                                 BLI_remlink(lb, te);
1398                                 tep = (TreeElement *)ob->parent->id.newid;
1399                                 BLI_addtail(&tep->subtree, te);
1400                                 // set correct parent pointers
1401                                 for (te = tep->subtree.first; te; te = te->next) te->parent = tep;
1402                         }
1403                 }
1404                 te = ten;
1405         }
1406 }
1407
1408 /* Sorting ------------------------------------------------------ */
1409
1410 typedef struct tTreeSort {
1411         TreeElement *te;
1412         ID *id;
1413         const char *name;
1414         short idcode;
1415 } tTreeSort;
1416
1417 /* alphabetical comparator, tryping to put objects first */
1418 static int treesort_alpha_ob(const void *v1, const void *v2)
1419 {
1420         const tTreeSort *x1 = v1, *x2 = v2;
1421         int comp;
1422
1423         /* first put objects last (hierarchy) */
1424         comp = (x1->idcode == ID_OB);
1425         if (x2->idcode == ID_OB) comp += 2;
1426
1427         if (comp == 1) return 1;
1428         else if (comp == 2) return -1;
1429         else if (comp == 3) {
1430                 comp = strcmp(x1->name, x2->name);
1431
1432                 if (comp > 0) return 1;
1433                 else if (comp < 0) return -1;
1434                 return 0;
1435         }
1436         return 0;
1437 }
1438
1439 /* alphabetical comparator */
1440 static int treesort_alpha(const void *v1, const void *v2)
1441 {
1442         const tTreeSort *x1 = v1, *x2 = v2;
1443         int comp;
1444
1445         comp = strcmp(x1->name, x2->name);
1446
1447         if (comp > 0) return 1;
1448         else if (comp < 0) return -1;
1449         return 0;
1450 }
1451
1452
1453 /* this is nice option for later? doesn't look too useful... */
1454 #if 0
1455 static int treesort_obtype_alpha(const void *v1, const void *v2)
1456 {
1457         const tTreeSort *x1 = v1, *x2 = v2;
1458
1459         /* first put objects last (hierarchy) */
1460         if (x1->idcode == ID_OB && x2->idcode != ID_OB) {
1461                 return 1;
1462         }
1463         else if (x2->idcode == ID_OB && x1->idcode != ID_OB) {
1464                 return -1;
1465         }
1466         else {
1467                 /* 2nd we check ob type */
1468                 if (x1->idcode == ID_OB && x2->idcode == ID_OB) {
1469                         if      (((Object *)x1->id)->type > ((Object *)x2->id)->type) return  1;
1470                         else if (((Object *)x1->id)->type > ((Object *)x2->id)->type) return -1;
1471                         else return 0;
1472                 }
1473                 else {
1474                         int comp = strcmp(x1->name, x2->name);
1475
1476                         if      (comp > 0) return  1;
1477                         else if (comp < 0) return -1;
1478                         return 0;
1479                 }
1480         }
1481 }
1482 #endif
1483
1484 /* sort happens on each subtree individual */
1485 static void outliner_sort(ListBase *lb)
1486 {
1487         TreeElement *te;
1488         TreeStoreElem *tselem;
1489
1490         te = lb->last;
1491         if (te == NULL) return;
1492         tselem = TREESTORE(te);
1493
1494         /* sorting rules; only object lists, ID lists, or deformgroups */
1495         if (ELEM(tselem->type, TSE_DEFGROUP, TSE_ID_BASE) || (tselem->type == 0 && te->idcode == ID_OB)) {
1496                 int totelem = BLI_listbase_count(lb);
1497
1498                 if (totelem > 1) {
1499                         tTreeSort *tear = MEM_mallocN(totelem * sizeof(tTreeSort), "tree sort array");
1500                         tTreeSort *tp = tear;
1501                         int skip = 0;
1502
1503                         for (te = lb->first; te; te = te->next, tp++) {
1504                                 tselem = TREESTORE(te);
1505                                 tp->te = te;
1506                                 tp->name = te->name;
1507                                 tp->idcode = te->idcode;
1508
1509                                 if (tselem->type && tselem->type != TSE_DEFGROUP)
1510                                         tp->idcode = 0;  // don't sort this
1511                                 if (tselem->type == TSE_ID_BASE)
1512                                         tp->idcode = 1; // do sort this
1513
1514                                 tp->id = tselem->id;
1515                         }
1516
1517                         /* just sort alphabetically */
1518                         if (tear->idcode == 1) {
1519                                 qsort(tear, totelem, sizeof(tTreeSort), treesort_alpha);
1520                         }
1521                         else {
1522                                 /* keep beginning of list */
1523                                 for (tp = tear, skip = 0; skip < totelem; skip++, tp++)
1524                                         if (tp->idcode) break;
1525
1526                                 if (skip < totelem)
1527                                         qsort(tear + skip, totelem - skip, sizeof(tTreeSort), treesort_alpha_ob);
1528                         }
1529
1530                         BLI_listbase_clear(lb);
1531                         tp = tear;
1532                         while (totelem--) {
1533                                 BLI_addtail(lb, tp->te);
1534                                 tp++;
1535                         }
1536                         MEM_freeN(tear);
1537                 }
1538         }
1539
1540         for (te = lb->first; te; te = te->next) {
1541                 outliner_sort(&te->subtree);
1542         }
1543 }
1544
1545 /* Filtering ----------------------------------------------- */
1546
1547 typedef struct OutlinerTreeElementFocus {
1548         TreeStoreElem *tselem;
1549         int ys;
1550 } OutlinerTreeElementFocus;
1551
1552 /**
1553  * Bring the outliner scrolling back to where it was in relation to the original focus element
1554  * Caller is expected to handle redrawing of ARegion.
1555  */
1556 static void outliner_restore_scrolling_position(SpaceOops *soops, ARegion *ar, OutlinerTreeElementFocus *focus)
1557 {
1558         View2D *v2d = &ar->v2d;
1559         int ytop;
1560
1561         if (focus->tselem != NULL) {
1562                 outliner_set_coordinates(ar, soops);
1563
1564                 TreeElement *te_new = outliner_find_tree_element(&soops->tree, focus->tselem);
1565
1566                 if (te_new != NULL) {
1567                         int ys_new, ys_old;
1568
1569                         ys_new = te_new->ys;
1570                         ys_old = focus->ys;
1571
1572                         ytop = v2d->cur.ymax + (ys_new - ys_old) -1;
1573                         if (ytop > 0) ytop = 0;
1574
1575                         v2d->cur.ymax = (float)ytop;
1576                         v2d->cur.ymin = (float)(ytop - BLI_rcti_size_y(&v2d->mask));
1577                 }
1578                 else {
1579                         return;
1580                 }
1581         }
1582 }
1583
1584 static bool test_collection_callback(TreeElement *te)
1585 {
1586         return outliner_is_collection_tree_element(te);
1587 }
1588
1589 static bool test_object_callback(TreeElement *te)
1590 {
1591         TreeStoreElem *tselem = TREESTORE(te);
1592         return ((tselem->type == 0) && (te->idcode == ID_OB));
1593 }
1594
1595 /**
1596  * See if TreeElement or any of its children pass the callback_test.
1597  */
1598 static TreeElement *outliner_find_first_desired_element_at_y_recursive(
1599         const SpaceOops *soops,
1600         TreeElement *te,
1601         const float limit,
1602         bool (*callback_test)(TreeElement *))
1603 {
1604         if (callback_test(te)) {
1605                 return te;
1606         }
1607
1608         if (TSELEM_OPEN(te->store_elem, soops)) {
1609                 TreeElement *te_iter, *te_sub;
1610                 for (te_iter = te->subtree.first; te_iter; te_iter = te_iter->next) {
1611                         te_sub = outliner_find_first_desired_element_at_y_recursive(soops, te_iter, limit, callback_test);
1612                         if (te_sub != NULL) {
1613                                 return te_sub;
1614                         }
1615                 }
1616         }
1617
1618         return NULL;
1619 }
1620
1621 /**
1622  * Find the first element that passes a test starting from a reference vertical coordinate
1623  *
1624  * If the element that is in the position is not what we are looking for, keep looking for its
1625  * children, siblings, and eventually, aunts, cousins, disntant families, ...
1626  *
1627  * Basically we keep going up and down the outliner tree from that point forward, until we find
1628  * what we are looking for. If we are past the visible range and we can't find a valid element
1629  * we return NULL.
1630  */
1631 static TreeElement *outliner_find_first_desired_element_at_y(
1632         const SpaceOops *soops,
1633         const float view_co,
1634         const float view_co_limit)
1635 {
1636         TreeElement *te, *te_sub;
1637         te = outliner_find_item_at_y(soops, &soops->tree, view_co);
1638
1639         bool (*callback_test)(TreeElement *);
1640         if ((soops->outlinevis == SO_VIEW_LAYER) &&
1641              (soops->filter & SO_FILTER_NO_COLLECTION))
1642         {
1643                 callback_test = test_object_callback;
1644         }
1645         else {
1646                 callback_test = test_collection_callback;
1647         }
1648
1649         while (te != NULL) {
1650                 te_sub = outliner_find_first_desired_element_at_y_recursive(soops, te, view_co_limit, callback_test);
1651                 if (te_sub != NULL) {
1652                         /* Skip the element if it was not visible to start with. */
1653                         if (te->ys + UI_UNIT_Y > view_co_limit) {
1654                                 return te_sub;
1655                         }
1656                         else {
1657                                 return NULL;
1658                         }
1659                 }
1660
1661                 if (te->next) {
1662                         te = te->next;
1663                         continue;
1664                 }
1665
1666                 if (te->parent == NULL) {
1667                         break;
1668                 }
1669
1670                 while (te->parent) {
1671                         if (te->parent->next) {
1672                                 te = te->parent->next;
1673                                 break;
1674                         }
1675                         te = te->parent;
1676                 }
1677         }
1678
1679         return NULL;
1680 }
1681
1682 /**
1683  * Store information of current outliner scrolling status to be restored later
1684  *
1685  * Finds the top-most collection visible in the outliner and populates the OutlinerTreeElementFocus
1686  * struct to retrieve this element later to make sure it is in the same original position as before filtering
1687  */
1688 static void outliner_store_scrolling_position(SpaceOops *soops, ARegion *ar, OutlinerTreeElementFocus *focus)
1689 {
1690         TreeElement *te;
1691         float limit = ar->v2d.cur.ymin;
1692
1693         outliner_set_coordinates(ar, soops);
1694
1695         te = outliner_find_first_desired_element_at_y(soops, ar->v2d.cur.ymax, limit);
1696
1697         if (te != NULL) {
1698                 focus->tselem = TREESTORE(te);
1699                 focus->ys = te->ys;
1700         }
1701         else {
1702                 focus->tselem = NULL;
1703         }
1704 }
1705
1706 static int outliner_exclude_filter_get(SpaceOops *soops)
1707 {
1708         int exclude_filter = soops->filter & ~SO_FILTER_OB_STATE;
1709
1710         if (soops->search_string[0] != 0) {
1711                 exclude_filter |= SO_FILTER_SEARCH;
1712         }
1713         else {
1714                 exclude_filter &= ~SO_FILTER_SEARCH;
1715         }
1716
1717         /* Let's have this for the collection options at first. */
1718         if (!SUPPORT_FILTER_OUTLINER(soops)) {
1719                 return (exclude_filter & SO_FILTER_SEARCH);
1720         }
1721
1722         if (soops->filter & SO_FILTER_NO_OBJECT) {
1723                 exclude_filter |= SO_FILTER_OB_TYPE;
1724         }
1725
1726         switch (soops->filter_state) {
1727                 case SO_FILTER_OB_VISIBLE:
1728                         exclude_filter |= SO_FILTER_OB_STATE_VISIBLE;
1729                         break;
1730                 case SO_FILTER_OB_SELECTED:
1731                         exclude_filter |= SO_FILTER_OB_STATE_SELECTED;
1732                         break;
1733                 case SO_FILTER_OB_ACTIVE:
1734                         exclude_filter |= SO_FILTER_OB_STATE_ACTIVE;
1735                         break;
1736         }
1737
1738         return exclude_filter;
1739 }
1740
1741 static bool outliner_element_visible_get(ViewLayer *view_layer, TreeElement *te, const int exclude_filter)
1742 {
1743         if ((exclude_filter & SO_FILTER_ANY) == 0) {
1744                 return true;
1745         }
1746
1747         TreeStoreElem *tselem = TREESTORE(te);
1748         if ((tselem->type == 0) && (te->idcode == ID_OB)) {
1749                 if ((exclude_filter & SO_FILTER_OB_TYPE) == SO_FILTER_OB_TYPE) {
1750                         return false;
1751                 }
1752
1753                 Object *ob = (Object *)tselem->id;
1754                 Base *base = (Base *)te->directdata;
1755                 BLI_assert((base == NULL) || (base->object == ob));
1756
1757                 if (exclude_filter & SO_FILTER_OB_TYPE) {
1758                         switch (ob->type) {
1759                                 case OB_MESH:
1760                                         if (exclude_filter & SO_FILTER_NO_OB_MESH) {
1761                                                 return false;
1762                                         }
1763                                         break;
1764                                 case OB_ARMATURE:
1765                                         if (exclude_filter & SO_FILTER_NO_OB_ARMATURE) {
1766                                                 return false;
1767                                         }
1768                                         break;
1769                                 case OB_EMPTY:
1770                                         if (exclude_filter & SO_FILTER_NO_OB_EMPTY) {
1771                                                 return false;
1772                                         }
1773                                         break;
1774                                 case OB_LAMP:
1775                                         if (exclude_filter & SO_FILTER_NO_OB_LAMP) {
1776                                                 return false;
1777                                         }
1778                                         break;
1779                                 case OB_CAMERA:
1780                                         if (exclude_filter & SO_FILTER_NO_OB_CAMERA) {
1781                                                 return false;
1782                                         }
1783                                         break;
1784                                 default:
1785                                         if (exclude_filter & SO_FILTER_NO_OB_OTHERS) {
1786                                                 return false;
1787                                         }
1788                                         break;
1789                         }
1790                 }
1791
1792                 if (exclude_filter & SO_FILTER_OB_STATE) {
1793                         if (base == NULL) {
1794                                 base = BKE_view_layer_base_find(view_layer, ob);
1795
1796                                 if (base == NULL) {
1797                                         return false;
1798                                 }
1799                         }
1800
1801                         if (exclude_filter & SO_FILTER_OB_STATE_VISIBLE) {
1802                                 if ((base->flag & BASE_VISIBLE) == 0) {
1803                                         return false;
1804                                 }
1805                         }
1806                         else if (exclude_filter & SO_FILTER_OB_STATE_SELECTED) {
1807                                 if ((base->flag & BASE_SELECTED) == 0) {
1808                                         return false;
1809                                 }
1810                         }
1811                         else {
1812                                 BLI_assert(exclude_filter & SO_FILTER_OB_STATE_ACTIVE);
1813                                 if (base != BASACT(view_layer)) {
1814                                         return false;
1815                                 }
1816                         }
1817                 }
1818
1819                 if ((te->parent != NULL) &&
1820                     (TREESTORE(te->parent)->type == 0) && (te->parent->idcode == ID_OB))
1821                 {
1822                         if (exclude_filter & SO_FILTER_NO_CHILDREN) {
1823                                 return false;
1824                         }
1825                 }
1826         }
1827         else if (te->parent != NULL &&
1828                  TREESTORE(te->parent)->type == 0 && te->parent->idcode == ID_OB)
1829         {
1830                 if (exclude_filter & SO_FILTER_NO_OB_CONTENT) {
1831                         return false;
1832                 }
1833         }
1834
1835         return true;
1836 }
1837
1838 static bool outliner_filter_has_name(TreeElement *te, const char *name, int flags)
1839 {
1840         int fn_flag = 0;
1841
1842         if ((flags & SO_FIND_CASE_SENSITIVE) == 0)
1843                 fn_flag |= FNM_CASEFOLD;
1844
1845         return fnmatch(name, te->name, fn_flag) == 0;
1846 }
1847
1848 static int outliner_filter_subtree(
1849         SpaceOops *soops, ViewLayer *view_layer, ListBase *lb, const char *search_string, const int exclude_filter)
1850 {
1851         TreeElement *te, *te_next;
1852         TreeStoreElem *tselem;
1853
1854         for (te = lb->first; te; te = te_next) {
1855                 te_next = te->next;
1856
1857                 if ((outliner_element_visible_get(view_layer, te, exclude_filter) == false)) {
1858                         outliner_free_tree_element(te, lb);
1859                         continue;
1860                 }
1861                 else if ((exclude_filter & SO_FILTER_SEARCH) == 0) {
1862                         /* Filter subtree too. */
1863                         outliner_filter_subtree(soops, view_layer, &te->subtree, search_string, exclude_filter);
1864                         continue;
1865                 }
1866
1867                 if (!outliner_filter_has_name(te, search_string, soops->search_flags)) {
1868                         /* item isn't something we're looking for, but...
1869                          * - if the subtree is expanded, check if there are any matches that can be easily found
1870                          *     so that searching for "cu" in the default scene will still match the Cube
1871                          * - otherwise, we can't see within the subtree and the item doesn't match,
1872                          *     so these can be safely ignored (i.e. the subtree can get freed)
1873                          */
1874                         tselem = TREESTORE(te);
1875
1876                         /* flag as not a found item */
1877                         tselem->flag &= ~TSE_SEARCHMATCH;
1878
1879                         if ((!TSELEM_OPEN(tselem, soops)) ||
1880                             outliner_filter_subtree(soops, view_layer, &te->subtree, search_string, exclude_filter) == 0)
1881                         {
1882                                 outliner_free_tree_element(te, lb);
1883                         }
1884                 }
1885                 else {
1886                         tselem = TREESTORE(te);
1887
1888                         /* flag as a found item - we can then highlight it */
1889                         tselem->flag |= TSE_SEARCHMATCH;
1890
1891                         /* filter subtree too */
1892                         outliner_filter_subtree(soops, view_layer, &te->subtree, search_string, exclude_filter);
1893                 }
1894         }
1895
1896         /* if there are still items in the list, that means that there were still some matches */
1897         return (BLI_listbase_is_empty(lb) == false);
1898 }
1899
1900 static void outliner_filter_tree(SpaceOops *soops, ViewLayer *view_layer)
1901 {
1902         char search_buff[sizeof(((struct SpaceOops *)NULL)->search_string) + 2];
1903         char *search_string;
1904
1905         const int exclude_filter = outliner_exclude_filter_get(soops);
1906
1907         if (exclude_filter == 0) {
1908                 return;
1909         }
1910
1911         if (soops->search_flags & SO_FIND_COMPLETE) {
1912                 search_string = soops->search_string;
1913         }
1914         else {
1915                 /* Implicitly add heading/trailing wildcards if needed. */
1916                 BLI_strncpy_ensure_pad(search_buff, soops->search_string, '*', sizeof(search_buff));
1917                 search_string = search_buff;
1918         }
1919
1920         outliner_filter_subtree(soops, view_layer, &soops->tree, search_string, exclude_filter);
1921 }
1922
1923 /* ======================================================= */
1924 /* Main Tree Building API */
1925
1926 /* Main entry point for building the tree data-structure that the outliner represents */
1927 // TODO: split each mode into its own function?
1928 void outliner_build_tree(Main *mainvar, Scene *scene, ViewLayer *view_layer, SpaceOops *soops, ARegion *ar)
1929 {
1930         TreeElement *te = NULL, *ten;
1931         TreeStoreElem *tselem;
1932         int show_opened = !soops->treestore || !BLI_mempool_len(soops->treestore); /* on first view, we open scenes */
1933
1934         /* Are we looking for something - we want to tag parents to filter child matches
1935          * - NOT in datablocks view - searching all datablocks takes way too long to be useful
1936          * - this variable is only set once per tree build */
1937         if (soops->search_string[0] != 0 && soops->outlinevis != SO_DATA_API)
1938                 soops->search_flags |= SO_SEARCH_RECURSIVE;
1939         else
1940                 soops->search_flags &= ~SO_SEARCH_RECURSIVE;
1941
1942         if (soops->treehash && (soops->storeflag & SO_TREESTORE_REBUILD) && soops->treestore) {
1943                 soops->storeflag &= ~SO_TREESTORE_REBUILD;
1944                 BKE_outliner_treehash_rebuild_from_treestore(soops->treehash, soops->treestore);
1945         }
1946
1947         if (ar->do_draw & RGN_DRAW_NO_REBUILD) {
1948                 return;
1949         }
1950
1951         OutlinerTreeElementFocus focus;
1952         outliner_store_scrolling_position(soops, ar, &focus);
1953
1954         outliner_free_tree(&soops->tree);
1955         outliner_storage_cleanup(soops);
1956
1957         /* options */
1958         if (soops->outlinevis == SO_LIBRARIES) {
1959                 Library *lib;
1960
1961                 /* current file first - mainvar provides tselem with unique pointer - not used */
1962                 ten = outliner_add_library_contents(mainvar, soops, &soops->tree, NULL);
1963                 if (ten) {
1964                         tselem = TREESTORE(ten);
1965                         if (!tselem->used)
1966                                 tselem->flag &= ~TSE_CLOSED;
1967                 }
1968
1969                 for (lib = mainvar->library.first; lib; lib = lib->id.next) {
1970                         ten = outliner_add_library_contents(mainvar, soops, &soops->tree, lib);
1971                         if (ten) {
1972                                 lib->id.newid = (ID *)ten;
1973                         }
1974
1975                 }
1976                 /* make hierarchy */
1977                 ten = soops->tree.first;
1978                 if (ten != NULL) {
1979                         ten = ten->next;  /* first one is main */
1980                         while (ten) {
1981                                 TreeElement *nten = ten->next, *par;
1982                                 tselem = TREESTORE(ten);
1983                                 lib = (Library *)tselem->id;
1984                                 if (lib && lib->parent) {
1985                                         par = (TreeElement *)lib->parent->id.newid;
1986                                         if (tselem->id->tag & LIB_TAG_INDIRECT) {
1987                                                 /* Only remove from 'first level' if lib is not also directly used. */
1988                                                 BLI_remlink(&soops->tree, ten);
1989                                                 BLI_addtail(&par->subtree, ten);
1990                                                 ten->parent = par;
1991                                         }
1992                                         else {
1993                                                 /* Else, make a new copy of the libtree for our parent. */
1994                                                 TreeElement *dupten = outliner_add_library_contents(mainvar, soops, &par->subtree, lib);
1995                                                 if (dupten) {
1996                                                         dupten->parent = par;
1997                                                 }
1998                                         }
1999                                 }
2000                                 ten = nten;
2001                         }
2002                 }
2003                 /* restore newid pointers */
2004                 for (lib = mainvar->library.first; lib; lib = lib->id.next)
2005                         lib->id.newid = NULL;
2006
2007         }
2008         else if (soops->outlinevis == SO_SCENES) {
2009                 Scene *sce;
2010                 for (sce = mainvar->scene.first; sce; sce = sce->id.next) {
2011                         te = outliner_add_element(soops, &soops->tree, sce, NULL, 0, 0);
2012                         tselem = TREESTORE(te);
2013
2014                         if (sce == scene && show_opened) {
2015                                 tselem->flag &= ~TSE_CLOSED;
2016                         }
2017
2018                         outliner_make_object_parent_hierarchy(&te->subtree);
2019                 }
2020         }
2021         else if (soops->outlinevis == SO_SEQUENCE) {
2022                 Sequence *seq;
2023                 Editing *ed = BKE_sequencer_editing_get(scene, false);
2024                 int op;
2025
2026                 if (ed == NULL)
2027                         return;
2028
2029                 seq = ed->seqbasep->first;
2030                 if (!seq)
2031                         return;
2032
2033                 while (seq) {
2034                         op = need_add_seq_dup(seq);
2035                         if (op == 1) {
2036                                 /* ten = */ outliner_add_element(soops, &soops->tree, (void *)seq, NULL, TSE_SEQUENCE, 0);
2037                         }
2038                         else if (op == 0) {
2039                                 ten = outliner_add_element(soops, &soops->tree, (void *)seq, NULL, TSE_SEQUENCE_DUP, 0);
2040                                 outliner_add_seq_dup(soops, seq, ten, 0);
2041                         }
2042                         seq = seq->next;
2043                 }
2044         }
2045         else if (soops->outlinevis == SO_DATA_API) {
2046                 PointerRNA mainptr;
2047
2048                 RNA_main_pointer_create(mainvar, &mainptr);
2049
2050                 ten = outliner_add_element(soops, &soops->tree, (void *)&mainptr, NULL, TSE_RNA_STRUCT, -1);
2051
2052                 if (show_opened) {
2053                         tselem = TREESTORE(ten);
2054                         tselem->flag &= ~TSE_CLOSED;
2055                 }
2056         }
2057         else if (soops->outlinevis == SO_ID_ORPHANS) {
2058                 outliner_add_orphaned_datablocks(mainvar, soops);
2059         }
2060         else if (soops->outlinevis == SO_VIEW_LAYER) {
2061                 if (soops->filter & SO_FILTER_NO_COLLECTION) {
2062                         /* Show objects in the view layer. */
2063                         for (Base *base = view_layer->object_bases.first; base; base = base->next) {
2064                                 TreeElement *te_object = outliner_add_element(soops, &soops->tree, base->object, NULL, 0, 0);
2065                                 te_object->directdata = base;
2066                         }
2067
2068                         outliner_make_object_parent_hierarchy(&soops->tree);
2069                 }
2070                 else {
2071                         /* Show collections in the view layer. */
2072                         ten = outliner_add_element(soops, &soops->tree, scene, NULL, TSE_VIEW_COLLECTION_BASE, 0);
2073                         ten->name = IFACE_("Scene Collection");
2074                         TREESTORE(ten)->flag &= ~TSE_CLOSED;
2075
2076                         bool show_objects = !(soops->filter & SO_FILTER_NO_OBJECT);
2077                         outliner_add_view_layer(soops, &ten->subtree, ten, view_layer, show_objects);
2078                 }
2079         }
2080
2081         if ((soops->flag & SO_SKIP_SORT_ALPHA) == 0) {
2082                 outliner_sort(&soops->tree);
2083         }
2084
2085         outliner_filter_tree(soops, view_layer);
2086         outliner_restore_scrolling_position(soops, ar, &focus);
2087
2088         BKE_main_id_clear_newpoins(mainvar);
2089 }