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