Merge branch 'blender2.7'
[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                 /* Open by default. */
1317                 TreeStoreElem *tselem = TREESTORE(ten);
1318                 if (!tselem->used) {
1319                         tselem->flag &= ~TSE_CLOSED;
1320                 }
1321
1322                 const bool exclude = (lc->flag & LAYER_COLLECTION_EXCLUDE) != 0;
1323                 if (exclude ||
1324                     ((layer->runtime_flag & VIEW_LAYER_HAS_HIDE) &&
1325                      !(lc->runtime_flag & LAYER_COLLECTION_HAS_VISIBLE_OBJECTS)))
1326                 {
1327                         ten->flag |= TE_DISABLED;
1328                 }
1329
1330                 outliner_add_layer_collections_recursive(soops, &ten->subtree, layer, &lc->layer_collections, ten, show_objects);
1331                 if (!exclude && show_objects) {
1332                         outliner_add_layer_collection_objects(soops, &ten->subtree, layer, lc, ten);
1333                 }
1334         }
1335 }
1336
1337 static void outliner_add_view_layer(SpaceOops *soops, ListBase *tree, TreeElement *parent,
1338                                     ViewLayer *layer, const bool show_objects)
1339 {
1340         /* First layer collection is for master collection, don't show it. */
1341         LayerCollection *lc = layer->layer_collections.first;
1342         if (lc == NULL) {
1343                 return;
1344         }
1345
1346         outliner_add_layer_collections_recursive(soops, tree, layer, &lc->layer_collections, parent, show_objects);
1347         if (show_objects) {
1348                 outliner_add_layer_collection_objects(soops, tree, layer, lc, parent);
1349         }
1350 }
1351
1352 BLI_INLINE void outliner_add_collection_init(TreeElement *te, Collection *collection)
1353 {
1354         te->name = BKE_collection_ui_name_get(collection);
1355         te->directdata = collection;
1356 }
1357
1358 BLI_INLINE void outliner_add_collection_objects(
1359         SpaceOops *soops, ListBase *tree, Collection *collection, TreeElement *parent)
1360 {
1361         for (CollectionObject *cob = collection->gobject.first; cob; cob = cob->next) {
1362                 outliner_add_element(soops, tree, cob->ob, parent, 0, 0);
1363         }
1364 }
1365
1366 static TreeElement *outliner_add_collection_recursive(
1367         SpaceOops *soops, Collection *collection, TreeElement *ten)
1368 {
1369         outliner_add_collection_init(ten, collection);
1370
1371         for (CollectionChild *child = collection->children.first; child; child = child->next) {
1372                 outliner_add_element(soops, &ten->subtree, &child->collection->id, ten, 0, 0);
1373         }
1374
1375         if (soops->outlinevis != SO_SCENES) {
1376                 outliner_add_collection_objects(soops, &ten->subtree, collection, ten);
1377         }
1378
1379         return ten;
1380 }
1381
1382 /* ======================================================= */
1383 /* Generic Tree Building helpers - order these are called is top to bottom */
1384
1385 /* Hierarchy --------------------------------------------- */
1386
1387 /* make sure elements are correctly nested */
1388 static void outliner_make_object_parent_hierarchy(ListBase *lb)
1389 {
1390         TreeElement *te, *ten, *tep;
1391         TreeStoreElem *tselem;
1392
1393         /* build hierarchy */
1394         // XXX also, set extents here...
1395         te = lb->first;
1396         while (te) {
1397                 ten = te->next;
1398                 tselem = TREESTORE(te);
1399
1400                 if (tselem->type == 0 && te->idcode == ID_OB) {
1401                         Object *ob = (Object *)tselem->id;
1402                         if (ob->parent && ob->parent->id.newid) {
1403                                 BLI_remlink(lb, te);
1404                                 tep = (TreeElement *)ob->parent->id.newid;
1405                                 BLI_addtail(&tep->subtree, te);
1406                                 // set correct parent pointers
1407                                 for (te = tep->subtree.first; te; te = te->next) te->parent = tep;
1408                         }
1409                 }
1410                 te = ten;
1411         }
1412 }
1413
1414 /* Sorting ------------------------------------------------------ */
1415
1416 typedef struct tTreeSort {
1417         TreeElement *te;
1418         ID *id;
1419         const char *name;
1420         short idcode;
1421 } tTreeSort;
1422
1423 /* alphabetical comparator, tryping to put objects first */
1424 static int treesort_alpha_ob(const void *v1, const void *v2)
1425 {
1426         const tTreeSort *x1 = v1, *x2 = v2;
1427         int comp;
1428
1429         /* first put objects last (hierarchy) */
1430         comp = (x1->idcode == ID_OB);
1431         if (x2->idcode == ID_OB) comp += 2;
1432
1433         if (comp == 1) return 1;
1434         else if (comp == 2) return -1;
1435         else if (comp == 3) {
1436                 comp = strcmp(x1->name, x2->name);
1437
1438                 if (comp > 0) return 1;
1439                 else if (comp < 0) return -1;
1440                 return 0;
1441         }
1442         return 0;
1443 }
1444
1445 /* alphabetical comparator */
1446 static int treesort_alpha(const void *v1, const void *v2)
1447 {
1448         const tTreeSort *x1 = v1, *x2 = v2;
1449         int comp;
1450
1451         comp = strcmp(x1->name, x2->name);
1452
1453         if (comp > 0) return 1;
1454         else if (comp < 0) return -1;
1455         return 0;
1456 }
1457
1458
1459 /* this is nice option for later? doesn't look too useful... */
1460 #if 0
1461 static int treesort_obtype_alpha(const void *v1, const void *v2)
1462 {
1463         const tTreeSort *x1 = v1, *x2 = v2;
1464
1465         /* first put objects last (hierarchy) */
1466         if (x1->idcode == ID_OB && x2->idcode != ID_OB) {
1467                 return 1;
1468         }
1469         else if (x2->idcode == ID_OB && x1->idcode != ID_OB) {
1470                 return -1;
1471         }
1472         else {
1473                 /* 2nd we check ob type */
1474                 if (x1->idcode == ID_OB && x2->idcode == ID_OB) {
1475                         if      (((Object *)x1->id)->type > ((Object *)x2->id)->type) return  1;
1476                         else if (((Object *)x1->id)->type > ((Object *)x2->id)->type) return -1;
1477                         else return 0;
1478                 }
1479                 else {
1480                         int comp = strcmp(x1->name, x2->name);
1481
1482                         if      (comp > 0) return  1;
1483                         else if (comp < 0) return -1;
1484                         return 0;
1485                 }
1486         }
1487 }
1488 #endif
1489
1490 /* sort happens on each subtree individual */
1491 static void outliner_sort(ListBase *lb)
1492 {
1493         TreeElement *te;
1494         TreeStoreElem *tselem;
1495
1496         te = lb->last;
1497         if (te == NULL) return;
1498         tselem = TREESTORE(te);
1499
1500         /* sorting rules; only object lists, ID lists, or deformgroups */
1501         if (ELEM(tselem->type, TSE_DEFGROUP, TSE_ID_BASE) || (tselem->type == 0 && te->idcode == ID_OB)) {
1502                 int totelem = BLI_listbase_count(lb);
1503
1504                 if (totelem > 1) {
1505                         tTreeSort *tear = MEM_mallocN(totelem * sizeof(tTreeSort), "tree sort array");
1506                         tTreeSort *tp = tear;
1507                         int skip = 0;
1508
1509                         for (te = lb->first; te; te = te->next, tp++) {
1510                                 tselem = TREESTORE(te);
1511                                 tp->te = te;
1512                                 tp->name = te->name;
1513                                 tp->idcode = te->idcode;
1514
1515                                 if (tselem->type && tselem->type != TSE_DEFGROUP)
1516                                         tp->idcode = 0;  // don't sort this
1517                                 if (tselem->type == TSE_ID_BASE)
1518                                         tp->idcode = 1; // do sort this
1519
1520                                 tp->id = tselem->id;
1521                         }
1522
1523                         /* just sort alphabetically */
1524                         if (tear->idcode == 1) {
1525                                 qsort(tear, totelem, sizeof(tTreeSort), treesort_alpha);
1526                         }
1527                         else {
1528                                 /* keep beginning of list */
1529                                 for (tp = tear, skip = 0; skip < totelem; skip++, tp++)
1530                                         if (tp->idcode) break;
1531
1532                                 if (skip < totelem)
1533                                         qsort(tear + skip, totelem - skip, sizeof(tTreeSort), treesort_alpha_ob);
1534                         }
1535
1536                         BLI_listbase_clear(lb);
1537                         tp = tear;
1538                         while (totelem--) {
1539                                 BLI_addtail(lb, tp->te);
1540                                 tp++;
1541                         }
1542                         MEM_freeN(tear);
1543                 }
1544         }
1545
1546         for (te = lb->first; te; te = te->next) {
1547                 outliner_sort(&te->subtree);
1548         }
1549 }
1550
1551 /* Filtering ----------------------------------------------- */
1552
1553 typedef struct OutlinerTreeElementFocus {
1554         TreeStoreElem *tselem;
1555         int ys;
1556 } OutlinerTreeElementFocus;
1557
1558 /**
1559  * Bring the outliner scrolling back to where it was in relation to the original focus element
1560  * Caller is expected to handle redrawing of ARegion.
1561  */
1562 static void outliner_restore_scrolling_position(SpaceOops *soops, ARegion *ar, OutlinerTreeElementFocus *focus)
1563 {
1564         View2D *v2d = &ar->v2d;
1565         int ytop;
1566
1567         if (focus->tselem != NULL) {
1568                 outliner_set_coordinates(ar, soops);
1569
1570                 TreeElement *te_new = outliner_find_tree_element(&soops->tree, focus->tselem);
1571
1572                 if (te_new != NULL) {
1573                         int ys_new, ys_old;
1574
1575                         ys_new = te_new->ys;
1576                         ys_old = focus->ys;
1577
1578                         ytop = v2d->cur.ymax + (ys_new - ys_old) -1;
1579                         if (ytop > 0) ytop = 0;
1580
1581                         v2d->cur.ymax = (float)ytop;
1582                         v2d->cur.ymin = (float)(ytop - BLI_rcti_size_y(&v2d->mask));
1583                 }
1584                 else {
1585                         return;
1586                 }
1587         }
1588 }
1589
1590 static bool test_collection_callback(TreeElement *te)
1591 {
1592         return outliner_is_collection_tree_element(te);
1593 }
1594
1595 static bool test_object_callback(TreeElement *te)
1596 {
1597         TreeStoreElem *tselem = TREESTORE(te);
1598         return ((tselem->type == 0) && (te->idcode == ID_OB));
1599 }
1600
1601 /**
1602  * See if TreeElement or any of its children pass the callback_test.
1603  */
1604 static TreeElement *outliner_find_first_desired_element_at_y_recursive(
1605         const SpaceOops *soops,
1606         TreeElement *te,
1607         const float limit,
1608         bool (*callback_test)(TreeElement *))
1609 {
1610         if (callback_test(te)) {
1611                 return te;
1612         }
1613
1614         if (TSELEM_OPEN(te->store_elem, soops)) {
1615                 TreeElement *te_iter, *te_sub;
1616                 for (te_iter = te->subtree.first; te_iter; te_iter = te_iter->next) {
1617                         te_sub = outliner_find_first_desired_element_at_y_recursive(soops, te_iter, limit, callback_test);
1618                         if (te_sub != NULL) {
1619                                 return te_sub;
1620                         }
1621                 }
1622         }
1623
1624         return NULL;
1625 }
1626
1627 /**
1628  * Find the first element that passes a test starting from a reference vertical coordinate
1629  *
1630  * If the element that is in the position is not what we are looking for, keep looking for its
1631  * children, siblings, and eventually, aunts, cousins, disntant families, ...
1632  *
1633  * Basically we keep going up and down the outliner tree from that point forward, until we find
1634  * what we are looking for. If we are past the visible range and we can't find a valid element
1635  * we return NULL.
1636  */
1637 static TreeElement *outliner_find_first_desired_element_at_y(
1638         const SpaceOops *soops,
1639         const float view_co,
1640         const float view_co_limit)
1641 {
1642         TreeElement *te, *te_sub;
1643         te = outliner_find_item_at_y(soops, &soops->tree, view_co);
1644
1645         bool (*callback_test)(TreeElement *);
1646         if ((soops->outlinevis == SO_VIEW_LAYER) &&
1647              (soops->filter & SO_FILTER_NO_COLLECTION))
1648         {
1649                 callback_test = test_object_callback;
1650         }
1651         else {
1652                 callback_test = test_collection_callback;
1653         }
1654
1655         while (te != NULL) {
1656                 te_sub = outliner_find_first_desired_element_at_y_recursive(soops, te, view_co_limit, callback_test);
1657                 if (te_sub != NULL) {
1658                         /* Skip the element if it was not visible to start with. */
1659                         if (te->ys + UI_UNIT_Y > view_co_limit) {
1660                                 return te_sub;
1661                         }
1662                         else {
1663                                 return NULL;
1664                         }
1665                 }
1666
1667                 if (te->next) {
1668                         te = te->next;
1669                         continue;
1670                 }
1671
1672                 if (te->parent == NULL) {
1673                         break;
1674                 }
1675
1676                 while (te->parent) {
1677                         if (te->parent->next) {
1678                                 te = te->parent->next;
1679                                 break;
1680                         }
1681                         te = te->parent;
1682                 }
1683         }
1684
1685         return NULL;
1686 }
1687
1688 /**
1689  * Store information of current outliner scrolling status to be restored later
1690  *
1691  * Finds the top-most collection visible in the outliner and populates the OutlinerTreeElementFocus
1692  * struct to retrieve this element later to make sure it is in the same original position as before filtering
1693  */
1694 static void outliner_store_scrolling_position(SpaceOops *soops, ARegion *ar, OutlinerTreeElementFocus *focus)
1695 {
1696         TreeElement *te;
1697         float limit = ar->v2d.cur.ymin;
1698
1699         outliner_set_coordinates(ar, soops);
1700
1701         te = outliner_find_first_desired_element_at_y(soops, ar->v2d.cur.ymax, limit);
1702
1703         if (te != NULL) {
1704                 focus->tselem = TREESTORE(te);
1705                 focus->ys = te->ys;
1706         }
1707         else {
1708                 focus->tselem = NULL;
1709         }
1710 }
1711
1712 static int outliner_exclude_filter_get(SpaceOops *soops)
1713 {
1714         int exclude_filter = soops->filter & ~SO_FILTER_OB_STATE;
1715
1716         if (soops->search_string[0] != 0) {
1717                 exclude_filter |= SO_FILTER_SEARCH;
1718         }
1719         else {
1720                 exclude_filter &= ~SO_FILTER_SEARCH;
1721         }
1722
1723         /* Let's have this for the collection options at first. */
1724         if (!SUPPORT_FILTER_OUTLINER(soops)) {
1725                 return (exclude_filter & SO_FILTER_SEARCH);
1726         }
1727
1728         if (soops->filter & SO_FILTER_NO_OBJECT) {
1729                 exclude_filter |= SO_FILTER_OB_TYPE;
1730         }
1731
1732         switch (soops->filter_state) {
1733                 case SO_FILTER_OB_VISIBLE:
1734                         exclude_filter |= SO_FILTER_OB_STATE_VISIBLE;
1735                         break;
1736                 case SO_FILTER_OB_SELECTED:
1737                         exclude_filter |= SO_FILTER_OB_STATE_SELECTED;
1738                         break;
1739                 case SO_FILTER_OB_ACTIVE:
1740                         exclude_filter |= SO_FILTER_OB_STATE_ACTIVE;
1741                         break;
1742         }
1743
1744         return exclude_filter;
1745 }
1746
1747 static bool outliner_element_visible_get(ViewLayer *view_layer, TreeElement *te, const int exclude_filter)
1748 {
1749         if ((exclude_filter & SO_FILTER_ANY) == 0) {
1750                 return true;
1751         }
1752
1753         TreeStoreElem *tselem = TREESTORE(te);
1754         if ((tselem->type == 0) && (te->idcode == ID_OB)) {
1755                 if ((exclude_filter & SO_FILTER_OB_TYPE) == SO_FILTER_OB_TYPE) {
1756                         return false;
1757                 }
1758
1759                 Object *ob = (Object *)tselem->id;
1760                 Base *base = (Base *)te->directdata;
1761                 BLI_assert((base == NULL) || (base->object == ob));
1762
1763                 if (exclude_filter & SO_FILTER_OB_TYPE) {
1764                         switch (ob->type) {
1765                                 case OB_MESH:
1766                                         if (exclude_filter & SO_FILTER_NO_OB_MESH) {
1767                                                 return false;
1768                                         }
1769                                         break;
1770                                 case OB_ARMATURE:
1771                                         if (exclude_filter & SO_FILTER_NO_OB_ARMATURE) {
1772                                                 return false;
1773                                         }
1774                                         break;
1775                                 case OB_EMPTY:
1776                                         if (exclude_filter & SO_FILTER_NO_OB_EMPTY) {
1777                                                 return false;
1778                                         }
1779                                         break;
1780                                 case OB_LAMP:
1781                                         if (exclude_filter & SO_FILTER_NO_OB_LAMP) {
1782                                                 return false;
1783                                         }
1784                                         break;
1785                                 case OB_CAMERA:
1786                                         if (exclude_filter & SO_FILTER_NO_OB_CAMERA) {
1787                                                 return false;
1788                                         }
1789                                         break;
1790                                 default:
1791                                         if (exclude_filter & SO_FILTER_NO_OB_OTHERS) {
1792                                                 return false;
1793                                         }
1794                                         break;
1795                         }
1796                 }
1797
1798                 if (exclude_filter & SO_FILTER_OB_STATE) {
1799                         if (base == NULL) {
1800                                 base = BKE_view_layer_base_find(view_layer, ob);
1801
1802                                 if (base == NULL) {
1803                                         return false;
1804                                 }
1805                         }
1806
1807                         if (exclude_filter & SO_FILTER_OB_STATE_VISIBLE) {
1808                                 if ((base->flag & BASE_VISIBLE) == 0) {
1809                                         return false;
1810                                 }
1811                         }
1812                         else if (exclude_filter & SO_FILTER_OB_STATE_SELECTED) {
1813                                 if ((base->flag & BASE_SELECTED) == 0) {
1814                                         return false;
1815                                 }
1816                         }
1817                         else {
1818                                 BLI_assert(exclude_filter & SO_FILTER_OB_STATE_ACTIVE);
1819                                 if (base != BASACT(view_layer)) {
1820                                         return false;
1821                                 }
1822                         }
1823                 }
1824
1825                 if ((te->parent != NULL) &&
1826                     (TREESTORE(te->parent)->type == 0) && (te->parent->idcode == ID_OB))
1827                 {
1828                         if (exclude_filter & SO_FILTER_NO_CHILDREN) {
1829                                 return false;
1830                         }
1831                 }
1832         }
1833         else if (te->parent != NULL &&
1834                  TREESTORE(te->parent)->type == 0 && te->parent->idcode == ID_OB)
1835         {
1836                 if (exclude_filter & SO_FILTER_NO_OB_CONTENT) {
1837                         return false;
1838                 }
1839         }
1840
1841         return true;
1842 }
1843
1844 static bool outliner_filter_has_name(TreeElement *te, const char *name, int flags)
1845 {
1846         int fn_flag = 0;
1847
1848         if ((flags & SO_FIND_CASE_SENSITIVE) == 0)
1849                 fn_flag |= FNM_CASEFOLD;
1850
1851         return fnmatch(name, te->name, fn_flag) == 0;
1852 }
1853
1854 static int outliner_filter_subtree(
1855         SpaceOops *soops, ViewLayer *view_layer, ListBase *lb, const char *search_string, const int exclude_filter)
1856 {
1857         TreeElement *te, *te_next;
1858         TreeStoreElem *tselem;
1859
1860         for (te = lb->first; te; te = te_next) {
1861                 te_next = te->next;
1862
1863                 if ((outliner_element_visible_get(view_layer, te, exclude_filter) == false)) {
1864                         outliner_free_tree_element(te, lb);
1865                         continue;
1866                 }
1867                 else if ((exclude_filter & SO_FILTER_SEARCH) == 0) {
1868                         /* Filter subtree too. */
1869                         outliner_filter_subtree(soops, view_layer, &te->subtree, search_string, exclude_filter);
1870                         continue;
1871                 }
1872
1873                 if (!outliner_filter_has_name(te, search_string, soops->search_flags)) {
1874                         /* item isn't something we're looking for, but...
1875                          * - if the subtree is expanded, check if there are any matches that can be easily found
1876                          *     so that searching for "cu" in the default scene will still match the Cube
1877                          * - otherwise, we can't see within the subtree and the item doesn't match,
1878                          *     so these can be safely ignored (i.e. the subtree can get freed)
1879                          */
1880                         tselem = TREESTORE(te);
1881
1882                         /* flag as not a found item */
1883                         tselem->flag &= ~TSE_SEARCHMATCH;
1884
1885                         if ((!TSELEM_OPEN(tselem, soops)) ||
1886                             outliner_filter_subtree(soops, view_layer, &te->subtree, search_string, exclude_filter) == 0)
1887                         {
1888                                 outliner_free_tree_element(te, lb);
1889                         }
1890                 }
1891                 else {
1892                         tselem = TREESTORE(te);
1893
1894                         /* flag as a found item - we can then highlight it */
1895                         tselem->flag |= TSE_SEARCHMATCH;
1896
1897                         /* filter subtree too */
1898                         outliner_filter_subtree(soops, view_layer, &te->subtree, search_string, exclude_filter);
1899                 }
1900         }
1901
1902         /* if there are still items in the list, that means that there were still some matches */
1903         return (BLI_listbase_is_empty(lb) == false);
1904 }
1905
1906 static void outliner_filter_tree(SpaceOops *soops, ViewLayer *view_layer)
1907 {
1908         char search_buff[sizeof(((struct SpaceOops *)NULL)->search_string) + 2];
1909         char *search_string;
1910
1911         const int exclude_filter = outliner_exclude_filter_get(soops);
1912
1913         if (exclude_filter == 0) {
1914                 return;
1915         }
1916
1917         if (soops->search_flags & SO_FIND_COMPLETE) {
1918                 search_string = soops->search_string;
1919         }
1920         else {
1921                 /* Implicitly add heading/trailing wildcards if needed. */
1922                 BLI_strncpy_ensure_pad(search_buff, soops->search_string, '*', sizeof(search_buff));
1923                 search_string = search_buff;
1924         }
1925
1926         outliner_filter_subtree(soops, view_layer, &soops->tree, search_string, exclude_filter);
1927 }
1928
1929 /* ======================================================= */
1930 /* Main Tree Building API */
1931
1932 /* Main entry point for building the tree data-structure that the outliner represents */
1933 // TODO: split each mode into its own function?
1934 void outliner_build_tree(Main *mainvar, Scene *scene, ViewLayer *view_layer, SpaceOops *soops, ARegion *ar)
1935 {
1936         TreeElement *te = NULL, *ten;
1937         TreeStoreElem *tselem;
1938         /* on first view, we open scenes */
1939         int show_opened = !soops->treestore || !BLI_mempool_len(soops->treestore);
1940
1941         /* Are we looking for something - we want to tag parents to filter child matches
1942          * - NOT in datablocks view - searching all datablocks takes way too long to be useful
1943          * - this variable is only set once per tree build */
1944         if (soops->search_string[0] != 0 && soops->outlinevis != SO_DATA_API)
1945                 soops->search_flags |= SO_SEARCH_RECURSIVE;
1946         else
1947                 soops->search_flags &= ~SO_SEARCH_RECURSIVE;
1948
1949         if (soops->treehash && (soops->storeflag & SO_TREESTORE_REBUILD) && soops->treestore) {
1950                 soops->storeflag &= ~SO_TREESTORE_REBUILD;
1951                 BKE_outliner_treehash_rebuild_from_treestore(soops->treehash, soops->treestore);
1952         }
1953
1954         if (ar->do_draw & RGN_DRAW_NO_REBUILD) {
1955                 return;
1956         }
1957
1958         OutlinerTreeElementFocus focus;
1959         outliner_store_scrolling_position(soops, ar, &focus);
1960
1961         outliner_free_tree(&soops->tree);
1962         outliner_storage_cleanup(soops);
1963
1964         /* options */
1965         if (soops->outlinevis == SO_LIBRARIES) {
1966                 Library *lib;
1967
1968                 /* current file first - mainvar provides tselem with unique pointer - not used */
1969                 ten = outliner_add_library_contents(mainvar, soops, &soops->tree, NULL);
1970                 if (ten) {
1971                         tselem = TREESTORE(ten);
1972                         if (!tselem->used)
1973                                 tselem->flag &= ~TSE_CLOSED;
1974                 }
1975
1976                 for (lib = mainvar->library.first; lib; lib = lib->id.next) {
1977                         ten = outliner_add_library_contents(mainvar, soops, &soops->tree, lib);
1978                         if (ten) {
1979                                 lib->id.newid = (ID *)ten;
1980                         }
1981
1982                 }
1983                 /* make hierarchy */
1984                 ten = soops->tree.first;
1985                 if (ten != NULL) {
1986                         ten = ten->next;  /* first one is main */
1987                         while (ten) {
1988                                 TreeElement *nten = ten->next, *par;
1989                                 tselem = TREESTORE(ten);
1990                                 lib = (Library *)tselem->id;
1991                                 if (lib && lib->parent) {
1992                                         par = (TreeElement *)lib->parent->id.newid;
1993                                         if (tselem->id->tag & LIB_TAG_INDIRECT) {
1994                                                 /* Only remove from 'first level' if lib is not also directly used. */
1995                                                 BLI_remlink(&soops->tree, ten);
1996                                                 BLI_addtail(&par->subtree, ten);
1997                                                 ten->parent = par;
1998                                         }
1999                                         else {
2000                                                 /* Else, make a new copy of the libtree for our parent. */
2001                                                 TreeElement *dupten = outliner_add_library_contents(mainvar, soops, &par->subtree, lib);
2002                                                 if (dupten) {
2003                                                         dupten->parent = par;
2004                                                 }
2005                                         }
2006                                 }
2007                                 ten = nten;
2008                         }
2009                 }
2010                 /* restore newid pointers */
2011                 for (lib = mainvar->library.first; lib; lib = lib->id.next)
2012                         lib->id.newid = NULL;
2013
2014         }
2015         else if (soops->outlinevis == SO_SCENES) {
2016                 Scene *sce;
2017                 for (sce = mainvar->scene.first; sce; sce = sce->id.next) {
2018                         te = outliner_add_element(soops, &soops->tree, sce, NULL, 0, 0);
2019                         tselem = TREESTORE(te);
2020
2021                         if (sce == scene && show_opened) {
2022                                 tselem->flag &= ~TSE_CLOSED;
2023                         }
2024
2025                         outliner_make_object_parent_hierarchy(&te->subtree);
2026                 }
2027         }
2028         else if (soops->outlinevis == SO_SEQUENCE) {
2029                 Sequence *seq;
2030                 Editing *ed = BKE_sequencer_editing_get(scene, false);
2031                 int op;
2032
2033                 if (ed == NULL)
2034                         return;
2035
2036                 seq = ed->seqbasep->first;
2037                 if (!seq)
2038                         return;
2039
2040                 while (seq) {
2041                         op = need_add_seq_dup(seq);
2042                         if (op == 1) {
2043                                 /* ten = */ outliner_add_element(soops, &soops->tree, (void *)seq, NULL, TSE_SEQUENCE, 0);
2044                         }
2045                         else if (op == 0) {
2046                                 ten = outliner_add_element(soops, &soops->tree, (void *)seq, NULL, TSE_SEQUENCE_DUP, 0);
2047                                 outliner_add_seq_dup(soops, seq, ten, 0);
2048                         }
2049                         seq = seq->next;
2050                 }
2051         }
2052         else if (soops->outlinevis == SO_DATA_API) {
2053                 PointerRNA mainptr;
2054
2055                 RNA_main_pointer_create(mainvar, &mainptr);
2056
2057                 ten = outliner_add_element(soops, &soops->tree, (void *)&mainptr, NULL, TSE_RNA_STRUCT, -1);
2058
2059                 if (show_opened) {
2060                         tselem = TREESTORE(ten);
2061                         tselem->flag &= ~TSE_CLOSED;
2062                 }
2063         }
2064         else if (soops->outlinevis == SO_ID_ORPHANS) {
2065                 outliner_add_orphaned_datablocks(mainvar, soops);
2066         }
2067         else if (soops->outlinevis == SO_VIEW_LAYER) {
2068                 if (soops->filter & SO_FILTER_NO_COLLECTION) {
2069                         /* Show objects in the view layer. */
2070                         for (Base *base = view_layer->object_bases.first; base; base = base->next) {
2071                                 TreeElement *te_object = outliner_add_element(soops, &soops->tree, base->object, NULL, 0, 0);
2072                                 te_object->directdata = base;
2073                         }
2074
2075                         outliner_make_object_parent_hierarchy(&soops->tree);
2076                 }
2077                 else {
2078                         /* Show collections in the view layer. */
2079                         ten = outliner_add_element(soops, &soops->tree, scene, NULL, TSE_VIEW_COLLECTION_BASE, 0);
2080                         ten->name = IFACE_("Scene Collection");
2081                         TREESTORE(ten)->flag &= ~TSE_CLOSED;
2082
2083                         bool show_objects = !(soops->filter & SO_FILTER_NO_OBJECT);
2084                         outliner_add_view_layer(soops, &ten->subtree, ten, view_layer, show_objects);
2085                 }
2086         }
2087
2088         if ((soops->flag & SO_SKIP_SORT_ALPHA) == 0) {
2089                 outliner_sort(&soops->tree);
2090         }
2091
2092         outliner_filter_tree(soops, view_layer);
2093         outliner_restore_scrolling_position(soops, ar, &focus);
2094
2095         BKE_main_id_clear_newpoins(mainvar);
2096 }