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