Internal node links are now cached in a per-node list, instead of being generated...
[blender.git] / source / blender / blenkernel / intern / library.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): none yet.
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file blender/blenkernel/intern/library.c
29  *  \ingroup bke
30  *
31  * Contains management of ID's and libraries
32  * allocate and free of all library data
33  */
34
35 #include <stdio.h>
36 #include <ctype.h>
37 #include <string.h>
38 #include <stdlib.h>
39 #include <stddef.h>
40 #include <assert.h>
41
42 #include "MEM_guardedalloc.h"
43
44 /* all types are needed here, in order to do memory operations */
45 #include "DNA_anim_types.h"
46 #include "DNA_armature_types.h"
47 #include "DNA_brush_types.h"
48 #include "DNA_camera_types.h"
49 #include "DNA_group_types.h"
50 #include "DNA_ipo_types.h"
51 #include "DNA_key_types.h"
52 #include "DNA_lamp_types.h"
53 #include "DNA_lattice_types.h"
54 #include "DNA_material_types.h"
55 #include "DNA_mesh_types.h"
56 #include "DNA_meta_types.h"
57 #include "DNA_nla_types.h"
58 #include "DNA_node_types.h"
59 #include "DNA_scene_types.h"
60 #include "DNA_screen_types.h"
61 #include "DNA_speaker_types.h"
62 #include "DNA_sound_types.h"
63 #include "DNA_text_types.h"
64 #include "DNA_vfont_types.h"
65 #include "DNA_windowmanager_types.h"
66 #include "DNA_world_types.h"
67 #include "DNA_gpencil_types.h"
68 #include "DNA_movieclip_types.h"
69 #include "DNA_mask_types.h"
70
71 #include "BLI_blenlib.h"
72 #include "BLI_dynstr.h"
73 #include "BLI_utildefines.h"
74 #include "BLI_bpath.h"
75
76 #include "BKE_animsys.h"
77 #include "BKE_camera.h"
78 #include "BKE_context.h"
79 #include "BKE_lamp.h"
80 #include "BKE_library.h"
81 #include "BKE_main.h"
82 #include "BKE_global.h"
83 #include "BKE_sound.h"
84 #include "BKE_object.h"
85 #include "BKE_screen.h"
86 #include "BKE_mesh.h"
87 #include "BKE_material.h"
88 #include "BKE_curve.h"
89 #include "BKE_mball.h"
90 #include "BKE_text.h"
91 #include "BKE_texture.h"
92 #include "BKE_scene.h"
93 #include "BKE_icons.h"
94 #include "BKE_image.h"
95 #include "BKE_ipo.h"
96 #include "BKE_key.h"
97 #include "BKE_world.h"
98 #include "BKE_font.h"
99 #include "BKE_group.h"
100 #include "BKE_lattice.h"
101 #include "BKE_armature.h"
102 #include "BKE_action.h"
103 #include "BKE_node.h"
104 #include "BKE_brush.h"
105 #include "BKE_idprop.h"
106 #include "BKE_particle.h"
107 #include "BKE_gpencil.h"
108 #include "BKE_fcurve.h"
109 #include "BKE_speaker.h"
110 #include "BKE_movieclip.h"
111 #include "BKE_mask.h"
112
113 #include "RNA_access.h"
114
115 #ifdef WITH_PYTHON
116 #include "BPY_extern.h"
117 #endif
118
119 #define MAX_IDPUP  60  /* was 24 */
120
121 /* GS reads the memory pointed at in a specific ordering. 
122  * only use this definition, makes little and big endian systems
123  * work fine, in conjunction with MAKE_ID */
124
125 /* from blendef: */
126 #define GS(a)  (*((short *)(a)))
127
128 /* ************* general ************************ */
129
130
131 /* this has to be called from each make_local_* func, we could call
132  * from id_make_local() but then the make local functions would not be self
133  * contained.
134  * also note that the id _must_ have a library - campbell */
135 void BKE_id_lib_local_paths(Main *bmain, Library *lib, ID *id)
136 {
137         char *bpath_user_data[2] = {bmain->name, lib->filepath};
138
139         BLI_bpath_traverse_id(bmain, id,
140                               BLI_bpath_relocate_visitor,
141                               BLI_BPATH_TRAVERSE_SKIP_MULTIFILE,
142                               bpath_user_data);
143 }
144
145 void id_lib_extern(ID *id)
146 {
147         if (id) {
148                 if (id->flag & LIB_INDIRECT) {
149                         id->flag -= LIB_INDIRECT;
150                         id->flag |= LIB_EXTERN;
151                 }
152         }
153 }
154
155 void id_us_plus(ID *id)
156 {
157         if (id) {
158                 id->us++;
159                 if (id->flag & LIB_INDIRECT) {
160                         id->flag -= LIB_INDIRECT;
161                         id->flag |= LIB_EXTERN;
162                 }
163         }
164 }
165
166 void id_us_min(ID *id)
167 {
168         if (id) {
169                 if (id->us < 2 && (id->flag & LIB_FAKEUSER)) {
170                         id->us = 1;
171                 }
172                 else if (id->us <= 0) {
173                         printf("ID user decrement error: %s\n", id->name);
174                 }
175                 else {
176                         id->us--;
177                 }
178         }
179 }
180
181 int id_make_local(ID *id, int test)
182 {
183         if (id->flag & LIB_INDIRECT)
184                 return 0;
185
186         switch (GS(id->name)) {
187                 case ID_SCE:
188                         return 0; /* not implemented */
189                 case ID_LI:
190                         return 0; /* can't be linked */
191                 case ID_OB:
192                         if (!test) BKE_object_make_local((Object *)id);
193                         return 1;
194                 case ID_ME:
195                         if (!test) {
196                                 BKE_mesh_make_local((Mesh *)id);
197                                 BKE_key_make_local(((Mesh *)id)->key);
198                         }
199                         return 1;
200                 case ID_CU:
201                         if (!test) {
202                                 BKE_curve_make_local((Curve *)id);
203                                 BKE_key_make_local(((Curve *)id)->key);
204                         }
205                         return 1;
206                 case ID_MB:
207                         if (!test) BKE_mball_make_local((MetaBall *)id);
208                         return 1;
209                 case ID_MA:
210                         if (!test) BKE_material_make_local((Material *)id);
211                         return 1;
212                 case ID_TE:
213                         if (!test) BKE_texture_make_local((Tex *)id);
214                         return 1;
215                 case ID_IM:
216                         if (!test) BKE_image_make_local((Image *)id);
217                         return 1;
218                 case ID_LT:
219                         if (!test) {
220                                 BKE_lattice_make_local((Lattice *)id);
221                                 BKE_key_make_local(((Lattice *)id)->key);
222                         }
223                         return 1;
224                 case ID_LA:
225                         if (!test) BKE_lamp_make_local((Lamp *)id);
226                         return 1;
227                 case ID_CA:
228                         if (!test) BKE_camera_make_local((Camera *)id);
229                         return 1;
230                 case ID_SPK:
231                         if (!test) BKE_speaker_make_local((Speaker *)id);
232                         return 1;
233                 case ID_IP:
234                         return 0; /* deprecated */
235                 case ID_KE:
236                         if (!test) BKE_key_make_local((Key *)id);
237                         return 1;
238                 case ID_WO:
239                         if (!test) BKE_world_make_local((World *)id);
240                         return 1;
241                 case ID_SCR:
242                         return 0; /* can't be linked */
243                 case ID_VF:
244                         return 0; /* not implemented */
245                 case ID_TXT:
246                         return 0; /* not implemented */
247                 case ID_SCRIPT:
248                         return 0; /* deprecated */
249                 case ID_SO:
250                         return 0; /* not implemented */
251                 case ID_GR:
252                         return 0; /* not implemented */
253                 case ID_AR:
254                         if (!test) BKE_armature_make_local((bArmature *)id);
255                         return 1;
256                 case ID_AC:
257                         if (!test) BKE_action_make_local((bAction *)id);
258                         return 1;
259                 case ID_NT:
260                         return 0; /* not implemented */
261                 case ID_BR:
262                         if (!test) BKE_brush_make_local((Brush *)id);
263                         return 1;
264                 case ID_PA:
265                         if (!test) BKE_particlesettings_make_local((ParticleSettings *)id);
266                         return 1;
267                 case ID_WM:
268                         return 0; /* can't be linked */
269                 case ID_GD:
270                         return 0; /* not implemented */
271         }
272
273         return 0;
274 }
275
276 int id_copy(ID *id, ID **newid, int test)
277 {
278         if (!test) *newid = NULL;
279
280         /* conventions:
281          * - make shallow copy, only this ID block
282          * - id.us of the new ID is set to 1 */
283         switch (GS(id->name)) {
284                 case ID_SCE:
285                         return 0; /* can't be copied from here */
286                 case ID_LI:
287                         return 0; /* can't be copied from here */
288                 case ID_OB:
289                         if (!test) *newid = (ID *)BKE_object_copy((Object *)id);
290                         return 1;
291                 case ID_ME:
292                         if (!test) *newid = (ID *)BKE_mesh_copy((Mesh *)id);
293                         return 1;
294                 case ID_CU:
295                         if (!test) *newid = (ID *)BKE_curve_copy((Curve *)id);
296                         return 1;
297                 case ID_MB:
298                         if (!test) *newid = (ID *)BKE_mball_copy((MetaBall *)id);
299                         return 1;
300                 case ID_MA:
301                         if (!test) *newid = (ID *)BKE_material_copy((Material *)id);
302                         return 1;
303                 case ID_TE:
304                         if (!test) *newid = (ID *)BKE_texture_copy((Tex *)id);
305                         return 1;
306                 case ID_IM:
307                         if (!test) *newid = (ID *)BKE_image_copy((Image *)id);
308                         return 1;
309                 case ID_LT:
310                         if (!test) *newid = (ID *)BKE_lattice_copy((Lattice *)id);
311                         return 1;
312                 case ID_LA:
313                         if (!test) *newid = (ID *)BKE_lamp_copy((Lamp *)id);
314                         return 1;
315                 case ID_SPK:
316                         if (!test) *newid = (ID *)BKE_speaker_copy((Speaker *)id);
317                         return 1;
318                 case ID_CA:
319                         if (!test) *newid = (ID *)BKE_camera_copy((Camera *)id);
320                         return 1;
321                 case ID_IP:
322                         return 0; /* deprecated */
323                 case ID_KE:
324                         if (!test) *newid = (ID *)BKE_key_copy((Key *)id);
325                         return 1;
326                 case ID_WO:
327                         if (!test) *newid = (ID *)BKE_world_copy((World *)id);
328                         return 1;
329                 case ID_SCR:
330                         return 0; /* can't be copied from here */
331                 case ID_VF:
332                         return 0; /* not implemented */
333                 case ID_TXT:
334                         if (!test) *newid = (ID *)BKE_text_copy((Text *)id);
335                         return 1;
336                 case ID_SCRIPT:
337                         return 0; /* deprecated */
338                 case ID_SO:
339                         return 0; /* not implemented */
340                 case ID_GR:
341                         if (!test) *newid = (ID *)BKE_group_copy((Group *)id);
342                         return 1;
343                 case ID_AR:
344                         if (!test) *newid = (ID *)BKE_armature_copy((bArmature *)id);
345                         return 1;
346                 case ID_AC:
347                         if (!test) *newid = (ID *)BKE_action_copy((bAction *)id);
348                         return 1;
349                 case ID_NT:
350                         if (!test) *newid = (ID *)ntreeCopyTree((bNodeTree *)id);
351                         return 1;
352                 case ID_BR:
353                         if (!test) *newid = (ID *)BKE_brush_copy((Brush *)id);
354                         return 1;
355                 case ID_PA:
356                         if (!test) *newid = (ID *)BKE_particlesettings_copy((ParticleSettings *)id);
357                         return 1;
358                 case ID_WM:
359                         return 0; /* can't be copied from here */
360                 case ID_GD:
361                         return 0; /* not implemented */
362                 case ID_MSK:
363                         if (!test) *newid = (ID *)BKE_mask_copy((Mask *)id);
364                         return 1;
365         }
366         
367         return 0;
368 }
369
370 int id_unlink(ID *id, int test)
371 {
372         Main *mainlib = G.main;
373         ListBase *lb;
374
375         switch (GS(id->name)) {
376                 case ID_TXT:
377                         if (test) return 1;
378                         BKE_text_unlink(mainlib, (Text *)id);
379                         break;
380                 case ID_GR:
381                         if (test) return 1;
382                         BKE_group_unlink((Group *)id);
383                         break;
384                 case ID_OB:
385                         if (test) return 1;
386                         BKE_object_unlink((Object *)id);
387                         break;
388         }
389
390         if (id->us == 0) {
391                 if (test) return 1;
392
393                 lb = which_libbase(mainlib, GS(id->name));
394                 BKE_libblock_free(lb, id);
395
396                 return 1;
397         }
398
399         return 0;
400 }
401
402 int id_single_user(bContext *C, ID *id, PointerRNA *ptr, PropertyRNA *prop)
403 {
404         ID *newid = NULL;
405         PointerRNA idptr;
406         
407         if (id) {
408                 /* if property isn't editable, we're going to have an extra block hanging around until we save */
409                 if (RNA_property_editable(ptr, prop)) {
410                         if (id_copy(id, &newid, 0) && newid) {
411                                 /* copy animation actions too */
412                                 BKE_copy_animdata_id_action(id);
413                                 /* us is 1 by convention, but RNA_property_pointer_set
414                                  * will also increment it, so set it to zero */
415                                 newid->us = 0;
416                                 
417                                 /* assign copy */
418                                 RNA_id_pointer_create(newid, &idptr);
419                                 RNA_property_pointer_set(ptr, prop, idptr);
420                                 RNA_property_update(C, ptr, prop);
421                                 
422                                 return 1;
423                         }
424                 }
425         }
426         
427         return 0;
428 }
429
430 ListBase *which_libbase(Main *mainlib, short type)
431 {
432         switch (type) {
433                 case ID_SCE:
434                         return &(mainlib->scene);
435                 case ID_LI:
436                         return &(mainlib->library);
437                 case ID_OB:
438                         return &(mainlib->object);
439                 case ID_ME:
440                         return &(mainlib->mesh);
441                 case ID_CU:
442                         return &(mainlib->curve);
443                 case ID_MB:
444                         return &(mainlib->mball);
445                 case ID_MA:
446                         return &(mainlib->mat);
447                 case ID_TE:
448                         return &(mainlib->tex);
449                 case ID_IM:
450                         return &(mainlib->image);
451                 case ID_LT:
452                         return &(mainlib->latt);
453                 case ID_LA:
454                         return &(mainlib->lamp);
455                 case ID_CA:
456                         return &(mainlib->camera);
457                 case ID_IP:
458                         return &(mainlib->ipo);
459                 case ID_KE:
460                         return &(mainlib->key);
461                 case ID_WO:
462                         return &(mainlib->world);
463                 case ID_SCR:
464                         return &(mainlib->screen);
465                 case ID_VF:
466                         return &(mainlib->vfont);
467                 case ID_TXT:
468                         return &(mainlib->text);
469                 case ID_SCRIPT:
470                         return &(mainlib->script);
471                 case ID_SPK:
472                         return &(mainlib->speaker);
473                 case ID_SO:
474                         return &(mainlib->sound);
475                 case ID_GR:
476                         return &(mainlib->group);
477                 case ID_AR:
478                         return &(mainlib->armature);
479                 case ID_AC:
480                         return &(mainlib->action);
481                 case ID_NT:
482                         return &(mainlib->nodetree);
483                 case ID_BR:
484                         return &(mainlib->brush);
485                 case ID_PA:
486                         return &(mainlib->particle);
487                 case ID_WM:
488                         return &(mainlib->wm);
489                 case ID_GD:
490                         return &(mainlib->gpencil);
491                 case ID_MC:
492                         return &(mainlib->movieclip);
493                 case ID_MSK:
494                         return &(mainlib->mask);
495         }
496         return NULL;
497 }
498
499 /* Flag all ids in listbase */
500 void flag_listbase_ids(ListBase *lb, short flag, short value)
501 {
502         ID *id;
503         if (value) {
504                 for (id = lb->first; id; id = id->next) id->flag |= flag;
505         }
506         else {
507                 flag = ~flag;
508                 for (id = lb->first; id; id = id->next) id->flag &= flag;
509         }
510 }
511
512 /* Flag all ids in listbase */
513 void flag_all_listbases_ids(short flag, short value)
514 {
515         ListBase *lbarray[MAX_LIBARRAY];
516         int a;
517         a = set_listbasepointers(G.main, lbarray);
518         while (a--) flag_listbase_ids(lbarray[a], flag, value);
519 }
520
521 void recalc_all_library_objects(Main *main)
522 {
523         Object *ob;
524
525         /* flag for full recalc */
526         for (ob = main->object.first; ob; ob = ob->id.next)
527                 if (ob->id.lib)
528                         ob->recalc |= OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME;
529 }
530
531 /* note: MAX_LIBARRAY define should match this code */
532 int set_listbasepointers(Main *main, ListBase **lb)
533 {
534         int a = 0;
535
536         /* BACKWARDS! also watch order of free-ing! (mesh<->mat), first items freed last.
537          * This is important because freeing data decreases usercounts of other datablocks,
538          * if this data is its self freed it can crash. */
539         lb[a++] = &(main->ipo);
540         lb[a++] = &(main->action); // xxx moved here to avoid problems when freeing with animato (aligorith)
541         lb[a++] = &(main->key);
542         lb[a++] = &(main->gpencil); /* referenced by nodes, objects, view, scene etc, before to free after. */
543         lb[a++] = &(main->nodetree);
544         lb[a++] = &(main->image);
545         lb[a++] = &(main->tex);
546         lb[a++] = &(main->mat);
547         lb[a++] = &(main->vfont);
548         
549         /* Important!: When adding a new object type,
550          * the specific data should be inserted here 
551          */
552
553         lb[a++] = &(main->armature);
554
555         lb[a++] = &(main->mesh);
556         lb[a++] = &(main->curve);
557         lb[a++] = &(main->mball);
558
559         lb[a++] = &(main->latt);
560         lb[a++] = &(main->lamp);
561         lb[a++] = &(main->camera);
562
563         lb[a++] = &(main->text);
564         lb[a++] = &(main->sound);
565         lb[a++] = &(main->group);
566         lb[a++] = &(main->brush);
567         lb[a++] = &(main->script);
568         lb[a++] = &(main->particle);
569         lb[a++] = &(main->speaker);
570
571         lb[a++] = &(main->world);
572         lb[a++] = &(main->screen);
573         lb[a++] = &(main->object);
574         lb[a++] = &(main->scene);
575         lb[a++] = &(main->library);
576         lb[a++] = &(main->wm);
577         lb[a++] = &(main->movieclip);
578         lb[a++] = &(main->mask);
579         
580         lb[a] = NULL;
581
582         return a;
583 }
584
585 /* *********** ALLOC AND FREE *****************
586  *
587  * BKE_libblock_free(ListBase *lb, ID *id )
588  * provide a list-basis and datablock, but only ID is read
589  *
590  * void *BKE_libblock_alloc(ListBase *lb, type, name)
591  * inserts in list and returns a new ID
592  *
593  * **************************** */
594
595 static ID *alloc_libblock_notest(short type)
596 {
597         ID *id = NULL;
598         
599         switch (type) {
600                 case ID_SCE:
601                         id = MEM_callocN(sizeof(Scene), "scene");
602                         break;
603                 case ID_LI:
604                         id = MEM_callocN(sizeof(Library), "library");
605                         break;
606                 case ID_OB:
607                         id = MEM_callocN(sizeof(Object), "object");
608                         break;
609                 case ID_ME:
610                         id = MEM_callocN(sizeof(Mesh), "mesh");
611                         break;
612                 case ID_CU:
613                         id = MEM_callocN(sizeof(Curve), "curve");
614                         break;
615                 case ID_MB:
616                         id = MEM_callocN(sizeof(MetaBall), "mball");
617                         break;
618                 case ID_MA:
619                         id = MEM_callocN(sizeof(Material), "mat");
620                         break;
621                 case ID_TE:
622                         id = MEM_callocN(sizeof(Tex), "tex");
623                         break;
624                 case ID_IM:
625                         id = MEM_callocN(sizeof(Image), "image");
626                         break;
627                 case ID_LT:
628                         id = MEM_callocN(sizeof(Lattice), "latt");
629                         break;
630                 case ID_LA:
631                         id = MEM_callocN(sizeof(Lamp), "lamp");
632                         break;
633                 case ID_CA:
634                         id = MEM_callocN(sizeof(Camera), "camera");
635                         break;
636                 case ID_IP:
637                         id = MEM_callocN(sizeof(Ipo), "ipo");
638                         break;
639                 case ID_KE:
640                         id = MEM_callocN(sizeof(Key), "key");
641                         break;
642                 case ID_WO:
643                         id = MEM_callocN(sizeof(World), "world");
644                         break;
645                 case ID_SCR:
646                         id = MEM_callocN(sizeof(bScreen), "screen");
647                         break;
648                 case ID_VF:
649                         id = MEM_callocN(sizeof(VFont), "vfont");
650                         break;
651                 case ID_TXT:
652                         id = MEM_callocN(sizeof(Text), "text");
653                         break;
654                 case ID_SCRIPT:
655                         //XXX id = MEM_callocN(sizeof(Script), "script");
656                         break;
657                 case ID_SPK:
658                         id = MEM_callocN(sizeof(Speaker), "speaker");
659                         break;
660                 case ID_SO:
661                         id = MEM_callocN(sizeof(bSound), "sound");
662                         break;
663                 case ID_GR:
664                         id = MEM_callocN(sizeof(Group), "group");
665                         break;
666                 case ID_AR:
667                         id = MEM_callocN(sizeof(bArmature), "armature");
668                         break;
669                 case ID_AC:
670                         id = MEM_callocN(sizeof(bAction), "action");
671                         break;
672                 case ID_NT:
673                         id = MEM_callocN(sizeof(bNodeTree), "nodetree");
674                         break;
675                 case ID_BR:
676                         id = MEM_callocN(sizeof(Brush), "brush");
677                         break;
678                 case ID_PA:
679                         id = MEM_callocN(sizeof(ParticleSettings), "ParticleSettings");
680                         break;
681                 case ID_WM:
682                         id = MEM_callocN(sizeof(wmWindowManager), "Window manager");
683                         break;
684                 case ID_GD:
685                         id = MEM_callocN(sizeof(bGPdata), "Grease Pencil");
686                         break;
687                 case ID_MC:
688                         id = MEM_callocN(sizeof(MovieClip), "Movie Clip");
689                         break;
690                 case ID_MSK:
691                         id = MEM_callocN(sizeof(Mask), "Mask");
692                         break;
693         }
694         return id;
695 }
696
697 /* used everywhere in blenkernel and text.c */
698 void *BKE_libblock_alloc(ListBase *lb, short type, const char *name)
699 {
700         ID *id = NULL;
701         
702         id = alloc_libblock_notest(type);
703         if (id) {
704                 BLI_addtail(lb, id);
705                 id->us = 1;
706                 id->icon_id = 0;
707                 *( (short *)id->name) = type;
708                 new_id(lb, id, name);
709                 /* alphabetic insterion: is in new_id */
710         }
711         return id;
712 }
713
714 /* by spec, animdata is first item after ID */
715 /* and, trust that BKE_animdata_from_id() will only find AnimData for valid ID-types */
716 static void id_copy_animdata(ID *id, const short do_action)
717 {
718         AnimData *adt = BKE_animdata_from_id(id);
719         
720         if (adt) {
721                 IdAdtTemplate *iat = (IdAdtTemplate *)id;
722                 iat->adt = BKE_copy_animdata(iat->adt, do_action); /* could be set to FALSE, need to investigate */
723         }
724 }
725
726 /* material nodes use this since they are not treated as libdata */
727 void BKE_libblock_copy_data(ID *id, const ID *id_from, const short do_action)
728 {
729         if (id_from->properties)
730                 id->properties = IDP_CopyProperty(id_from->properties);
731
732         /* the duplicate should get a copy of the animdata */
733         id_copy_animdata(id, do_action);
734 }
735
736 /* used everywhere in blenkernel */
737 void *BKE_libblock_copy(ID *id)
738 {
739         ID *idn;
740         ListBase *lb;
741         size_t idn_len;
742
743         lb = which_libbase(G.main, GS(id->name));
744         idn = BKE_libblock_alloc(lb, GS(id->name), id->name + 2);
745
746         assert(idn != NULL);
747
748         idn_len = MEM_allocN_len(idn);
749         if ((int)idn_len - (int)sizeof(ID) > 0) { /* signed to allow neg result */
750                 const char *cp = (const char *)id;
751                 char *cpn = (char *)idn;
752
753                 memcpy(cpn + sizeof(ID), cp + sizeof(ID), idn_len - sizeof(ID));
754         }
755         
756         id->newid = idn;
757         idn->flag |= LIB_NEW;
758
759         BKE_libblock_copy_data(idn, id, FALSE);
760         
761         return idn;
762 }
763
764 static void BKE_library_free(Library *UNUSED(lib))
765 {
766         /* no freeing needed for libraries yet */
767 }
768
769 static void (*free_windowmanager_cb)(bContext *, wmWindowManager *) = NULL;
770
771 void set_free_windowmanager_cb(void (*func)(bContext *C, wmWindowManager *) )
772 {
773         free_windowmanager_cb = func;
774 }
775
776 static void animdata_dtar_clear_cb(ID *UNUSED(id), AnimData *adt, void *userdata)
777 {
778         ChannelDriver *driver;
779         FCurve *fcu;
780
781         /* find the driver this belongs to and update it */
782         for (fcu = adt->drivers.first; fcu; fcu = fcu->next) {
783                 driver = fcu->driver;
784                 
785                 if (driver) {
786                         DriverVar *dvar;
787                         for (dvar = driver->variables.first; dvar; dvar = dvar->next) {
788                                 DRIVER_TARGETS_USED_LOOPER(dvar) 
789                                 {
790                                         if (dtar->id == userdata)
791                                                 dtar->id = NULL;
792                                 }
793                                 DRIVER_TARGETS_LOOPER_END
794                         }
795                 }
796         }
797 }
798
799
800 /* used in headerbuttons.c image.c mesh.c screen.c sound.c and library.c */
801 void BKE_libblock_free(ListBase *lb, void *idv)
802 {
803         Main *bmain = G.main;  /* should eventually be an arg */
804         ID *id = idv;
805
806 #ifdef WITH_PYTHON
807         BPY_id_release(id);
808 #endif
809
810         switch (GS(id->name) ) {    /* GetShort from util.h */
811                 case ID_SCE:
812                         BKE_scene_free((Scene *)id);
813                         break;
814                 case ID_LI:
815                         BKE_library_free((Library *)id);
816                         break;
817                 case ID_OB:
818                         BKE_object_free((Object *)id);
819                         break;
820                 case ID_ME:
821                         BKE_mesh_free((Mesh *)id, 1);
822                         break;
823                 case ID_CU:
824                         BKE_curve_free((Curve *)id);
825                         break;
826                 case ID_MB:
827                         BKE_mball_free((MetaBall *)id);
828                         break;
829                 case ID_MA:
830                         BKE_material_free((Material *)id);
831                         break;
832                 case ID_TE:
833                         BKE_texture_free((Tex *)id);
834                         break;
835                 case ID_IM:
836                         BKE_image_free((Image *)id);
837                         break;
838                 case ID_LT:
839                         BKE_lattice_free((Lattice *)id);
840                         break;
841                 case ID_LA:
842                         BKE_lamp_free((Lamp *)id);
843                         break;
844                 case ID_CA:
845                         BKE_camera_free((Camera *) id);
846                         break;
847                 case ID_IP:
848                         BKE_ipo_free((Ipo *)id);
849                         break;
850                 case ID_KE:
851                         BKE_key_free((Key *)id);
852                         break;
853                 case ID_WO:
854                         BKE_world_free((World *)id);
855                         break;
856                 case ID_SCR:
857                         BKE_screen_free((bScreen *)id);
858                         break;
859                 case ID_VF:
860                         BKE_vfont_free((VFont *)id);
861                         break;
862                 case ID_TXT:
863                         BKE_text_free((Text *)id);
864                         break;
865                 case ID_SCRIPT:
866                         /* deprecated */
867                         break;
868                 case ID_SPK:
869                         BKE_speaker_free((Speaker *)id);
870                         break;
871                 case ID_SO:
872                         BKE_sound_free((bSound *)id);
873                         break;
874                 case ID_GR:
875                         BKE_group_free((Group *)id);
876                         break;
877                 case ID_AR:
878                         BKE_armature_free((bArmature *)id);
879                         break;
880                 case ID_AC:
881                         BKE_action_free((bAction *)id);
882                         break;
883                 case ID_NT:
884                         ntreeFreeTree((bNodeTree *)id);
885                         break;
886                 case ID_BR:
887                         BKE_brush_free((Brush *)id);
888                         break;
889                 case ID_PA:
890                         BKE_particlesettings_free((ParticleSettings *)id);
891                         break;
892                 case ID_WM:
893                         if (free_windowmanager_cb)
894                                 free_windowmanager_cb(NULL, (wmWindowManager *)id);
895                         break;
896                 case ID_GD:
897                         BKE_gpencil_free((bGPdata *)id);
898                         break;
899                 case ID_MC:
900                         BKE_movieclip_free((MovieClip *)id);
901                         break;
902                 case ID_MSK:
903                         BKE_mask_free(bmain, (Mask *)id);
904                         break;
905         }
906
907         if (id->properties) {
908                 IDP_FreeProperty(id->properties);
909                 MEM_freeN(id->properties);
910         }
911
912         BLI_remlink(lb, id);
913
914         /* this ID may be a driver target! */
915         BKE_animdata_main_cb(bmain, animdata_dtar_clear_cb, (void *)id);
916
917         MEM_freeN(id);
918 }
919
920 void BKE_libblock_free_us(ListBase *lb, void *idv)      /* test users */
921 {
922         ID *id = idv;
923         
924         id->us--;
925
926         if (id->us < 0) {
927                 if (id->lib) printf("ERROR block %s %s users %d\n", id->lib->name, id->name, id->us);
928                 else printf("ERROR block %s users %d\n", id->name, id->us);
929         }
930         if (id->us == 0) {
931                 if (GS(id->name) == ID_OB) BKE_object_unlink((Object *)id);
932                 
933                 BKE_libblock_free(lb, id);
934         }
935 }
936
937
938 void free_main(Main *mainvar)
939 {
940         /* also call when reading a file, erase all, etc */
941         ListBase *lbarray[MAX_LIBARRAY];
942         int a;
943
944         a = set_listbasepointers(mainvar, lbarray);
945         while (a--) {
946                 ListBase *lb = lbarray[a];
947                 ID *id;
948                 
949                 while ( (id = lb->first) ) {
950 #if 1
951                         BKE_libblock_free(lb, id);
952 #else
953                         /* errors freeing ID's can be hard to track down,
954                          * enable this so valgrind will give the line number in its error log */
955                         switch (a) {
956                                 case   0: BKE_libblock_free(lb, id); break;
957                                 case   1: BKE_libblock_free(lb, id); break;
958                                 case   2: BKE_libblock_free(lb, id); break;
959                                 case   3: BKE_libblock_free(lb, id); break;
960                                 case   4: BKE_libblock_free(lb, id); break;
961                                 case   5: BKE_libblock_free(lb, id); break;
962                                 case   6: BKE_libblock_free(lb, id); break;
963                                 case   7: BKE_libblock_free(lb, id); break;
964                                 case   8: BKE_libblock_free(lb, id); break;
965                                 case   9: BKE_libblock_free(lb, id); break;
966                                 case  10: BKE_libblock_free(lb, id); break;
967                                 case  11: BKE_libblock_free(lb, id); break;
968                                 case  12: BKE_libblock_free(lb, id); break;
969                                 case  13: BKE_libblock_free(lb, id); break;
970                                 case  14: BKE_libblock_free(lb, id); break;
971                                 case  15: BKE_libblock_free(lb, id); break;
972                                 case  16: BKE_libblock_free(lb, id); break;
973                                 case  17: BKE_libblock_free(lb, id); break;
974                                 case  18: BKE_libblock_free(lb, id); break;
975                                 case  19: BKE_libblock_free(lb, id); break;
976                                 case  20: BKE_libblock_free(lb, id); break;
977                                 case  21: BKE_libblock_free(lb, id); break;
978                                 case  22: BKE_libblock_free(lb, id); break;
979                                 case  23: BKE_libblock_free(lb, id); break;
980                                 case  24: BKE_libblock_free(lb, id); break;
981                                 case  25: BKE_libblock_free(lb, id); break;
982                                 case  26: BKE_libblock_free(lb, id); break;
983                                 case  27: BKE_libblock_free(lb, id); break;
984                                 case  28: BKE_libblock_free(lb, id); break;
985                                 case  29: BKE_libblock_free(lb, id); break;
986                                 case  30: BKE_libblock_free(lb, id); break;
987                                 case  31: BKE_libblock_free(lb, id); break;
988                                 case  32: BKE_libblock_free(lb, id); break;
989                                 default:
990                                         BLI_assert(0);
991                         }
992 #endif
993                 }
994         }
995         a = set_listbasepointers(mainvar, lbarray);
996
997         MEM_freeN(mainvar);
998 }
999
1000 /* ***************** ID ************************ */
1001
1002
1003 ID *BKE_libblock_find_name(const short type, const char *name)      /* type: "OB" or "MA" etc */
1004 {
1005         ListBase *lb = which_libbase(G.main, type);
1006         BLI_assert(lb != NULL);
1007         return BLI_findstring(lb, name, offsetof(ID, name) + 2);
1008 }
1009
1010 static void get_flags_for_id(ID *id, char *buf) 
1011 {
1012         int isfake = id->flag & LIB_FAKEUSER;
1013         int isnode = 0;
1014         /* Writeout the flags for the entry, note there
1015          * is a small hack that writes 5 spaces instead
1016          * of 4 if no flags are displayed... this makes
1017          * things usually line up ok - better would be
1018          * to have that explicit, oh well - zr
1019          */
1020
1021         if (GS(id->name) == ID_MA)
1022                 isnode = ((Material *)id)->use_nodes;
1023         if (GS(id->name) == ID_TE)
1024                 isnode = ((Tex *)id)->use_nodes;
1025         
1026         if (id->us < 0)
1027                 strcpy(buf, "-1W ");
1028         else if (!id->lib && !isfake && id->us && !isnode)
1029                 strcpy(buf, "     ");
1030         else if (isnode)
1031                 sprintf(buf, "%c%cN%c ", id->lib ? 'L' : ' ', isfake ? 'F' : ' ', (id->us == 0) ? 'O' : ' ');
1032         else
1033                 sprintf(buf, "%c%c%c ", id->lib ? 'L' : ' ', isfake ? 'F' : ' ', (id->us == 0) ? 'O' : ' ');
1034 }
1035
1036 #define IDPUP_NO_VIEWER 1
1037
1038 static void IDnames_to_dyn_pupstring(DynStr *pupds, ListBase *lb, ID *link, short *nr, int hideflag)
1039 {
1040         int i, nids = BLI_countlist(lb);
1041                 
1042         if (nr) *nr = -1;
1043         
1044         if (nr && nids > MAX_IDPUP) {
1045                 BLI_dynstr_append(pupds, "DataBrowse %x-2");
1046                 *nr = -2;
1047         }
1048         else {
1049                 ID *id;
1050                 
1051                 for (i = 0, id = lb->first; id; id = id->next, i++) {
1052                         char numstr[32];
1053                         
1054                         if (nr && id == link) *nr = i + 1;
1055
1056                         if (U.uiflag & USER_HIDE_DOT && id->name[2] == '.')
1057                                 continue;
1058                         if (hideflag & IDPUP_NO_VIEWER)
1059                                 if (GS(id->name) == ID_IM)
1060                                         if ( ((Image *)id)->source == IMA_SRC_VIEWER)
1061                                                 continue;
1062                         
1063                         get_flags_for_id(id, numstr);
1064                                 
1065                         BLI_dynstr_append(pupds, numstr);
1066                         BLI_dynstr_append(pupds, id->name + 2);
1067                         BLI_snprintf(numstr, sizeof(numstr), "%%x%d", i + 1);
1068                         BLI_dynstr_append(pupds, numstr);
1069                         
1070                         /* icon */
1071                         switch (GS(id->name)) {
1072                                 case ID_MA: /* fall through */
1073                                 case ID_TE: /* fall through */
1074                                 case ID_IM: /* fall through */
1075                                 case ID_WO: /* fall through */
1076                                 case ID_LA: /* fall through */
1077                                         BLI_snprintf(numstr, sizeof(numstr), "%%i%d", BKE_icon_getid(id));
1078                                         BLI_dynstr_append(pupds, numstr);
1079                                         break;
1080                                 default:
1081                                         break;
1082                         }
1083                         
1084                         if (id->next)
1085                                 BLI_dynstr_append(pupds, "|");
1086                 }
1087         }
1088 }
1089
1090
1091 /* used by headerbuttons.c buttons.c editobject.c editseq.c */
1092 /* if (nr == NULL) no MAX_IDPUP, this for non-header browsing */
1093 void IDnames_to_pupstring(const char **str, const char *title, const char *extraops, ListBase *lb, ID *link, short *nr)
1094 {
1095         DynStr *pupds = BLI_dynstr_new();
1096
1097         if (title) {
1098                 BLI_dynstr_append(pupds, title);
1099                 BLI_dynstr_append(pupds, "%t|");
1100         }
1101         
1102         if (extraops) {
1103                 BLI_dynstr_append(pupds, extraops);
1104                 if (BLI_dynstr_get_len(pupds))
1105                         BLI_dynstr_append(pupds, "|");
1106         }
1107
1108         IDnames_to_dyn_pupstring(pupds, lb, link, nr, 0);
1109         
1110         *str = BLI_dynstr_get_cstring(pupds);
1111         BLI_dynstr_free(pupds);
1112 }
1113
1114 /* skips viewer images */
1115 #if 0 /* unused */
1116 void IMAnames_to_pupstring(const char **str, const char *title, const char *extraops, ListBase *lb, ID *link, short *nr)
1117 {
1118         DynStr *pupds = BLI_dynstr_new();
1119         
1120         if (title) {
1121                 BLI_dynstr_append(pupds, title);
1122                 BLI_dynstr_append(pupds, "%t|");
1123         }
1124         
1125         if (extraops) {
1126                 BLI_dynstr_append(pupds, extraops);
1127                 if (BLI_dynstr_get_len(pupds))
1128                         BLI_dynstr_append(pupds, "|");
1129         }
1130         
1131         IDnames_to_dyn_pupstring(pupds, lb, link, nr, IDPUP_NO_VIEWER);
1132         
1133         *str = BLI_dynstr_get_cstring(pupds);
1134         BLI_dynstr_free(pupds);
1135 }
1136 #endif
1137
1138 void id_sort_by_name(ListBase *lb, ID *id)
1139 {
1140         ID *idtest;
1141         
1142         /* insert alphabetically */
1143         if (lb->first != lb->last) {
1144                 BLI_remlink(lb, id);
1145                 
1146                 idtest = lb->first;
1147                 while (idtest) {
1148                         if (BLI_strcasecmp(idtest->name, id->name) > 0 || (idtest->lib && !id->lib)) {
1149                                 BLI_insertlinkbefore(lb, idtest, id);
1150                                 break;
1151                         }
1152                         idtest = idtest->next;
1153                 }
1154                 /* as last */
1155                 if (idtest == NULL) {
1156                         BLI_addtail(lb, id);
1157                 }
1158         }
1159         
1160 }
1161
1162 /*
1163  * Check to see if there is an ID with the same name as 'name'.
1164  * Returns the ID if so, if not, returns NULL
1165  */
1166 static ID *is_dupid(ListBase *lb, ID *id, const char *name)
1167 {
1168         ID *idtest = NULL;
1169         
1170         for (idtest = lb->first; idtest; idtest = idtest->next) {
1171                 /* if idtest is not a lib */ 
1172                 if (id != idtest && idtest->lib == NULL) {
1173                         /* do not test alphabetic! */
1174                         /* optimized */
1175                         if (idtest->name[2] == name[0]) {
1176                                 if (strcmp(name, idtest->name + 2) == 0) break;
1177                         }
1178                 }
1179         }
1180         
1181         return idtest;
1182 }
1183
1184 /* 
1185  * Check to see if an ID name is already used, and find a new one if so.
1186  * Return 1 if created a new name (returned in name).
1187  *
1188  * Normally the ID that's being check is already in the ListBase, so ID *id
1189  * points at the new entry.  The Python Library module needs to know what
1190  * the name of a datablock will be before it is appended; in this case ID *id
1191  * id is NULL
1192  */
1193
1194 static int check_for_dupid(ListBase *lb, ID *id, char *name)
1195 {
1196         ID *idtest;
1197         int nr = 0, nrtest, a, left_len;
1198         char in_use[64]; /* use as a boolean array, unrelated to name length */
1199
1200         char left[MAX_ID_NAME + 8], leftest[MAX_ID_NAME + 8];
1201
1202         /* make sure input name is terminated properly */
1203         /* if ( strlen(name) > MAX_ID_NAME-3 ) name[MAX_ID_NAME-3] = 0; */
1204         /* removed since this is only ever called from one place - campbell */
1205
1206         while (1) {
1207
1208                 /* phase 1: id already exists? */
1209                 idtest = is_dupid(lb, id, name);
1210
1211                 /* if there is no double, done */
1212                 if (idtest == NULL) return 0;
1213
1214                 /* we have a dup; need to make a new name */
1215                 /* quick check so we can reuse one of first 64 ids if vacant */
1216                 memset(in_use, 0, sizeof(in_use));
1217
1218                 /* get name portion, number portion ("name.number") */
1219                 left_len = BLI_split_name_num(left, &nr, name, '.');
1220
1221                 /* if new name will be too long, truncate it */
1222                 if (nr > 999 && left_len > (MAX_ID_NAME - 8)) {
1223                         left[MAX_ID_NAME - 8] = 0;
1224                         left_len = MAX_ID_NAME - 8;
1225                 }
1226                 else if (left_len > (MAX_ID_NAME - 7)) {
1227                         left[MAX_ID_NAME - 7] = 0;
1228                         left_len = MAX_ID_NAME - 7;
1229                 }
1230
1231                 for (idtest = lb->first; idtest; idtest = idtest->next) {
1232                         if ( (id != idtest) &&
1233                              (idtest->lib == NULL) &&
1234                              (*name == *(idtest->name + 2)) &&
1235                              (strncmp(name, idtest->name + 2, left_len) == 0) &&
1236                              (BLI_split_name_num(leftest, &nrtest, idtest->name + 2, '.') == left_len)
1237                              )
1238                         {
1239                                 if (nrtest < sizeof(in_use))
1240                                         in_use[nrtest] = 1;  /* mark as used */
1241                                 if (nr <= nrtest)
1242                                         nr = nrtest + 1;    /* track largest unused */
1243                         }
1244                 }
1245
1246                 /* decide which value of nr to use */
1247                 for (a = 0; a < sizeof(in_use); a++) {
1248                         if (a >= nr) break;  /* stop when we've check up to biggest */
1249                         if (in_use[a] == 0) { /* found an unused value */
1250                                 nr = a;
1251                                 break;
1252                         }
1253                 }
1254
1255                 /* If the original name has no numeric suffix, 
1256                  * rather than just chopping and adding numbers, 
1257                  * shave off the end chars until we have a unique name.
1258                  * Check the null terminators match as well so we don't get Cube.000 -> Cube.00 */
1259                 if (nr == 0 && name[left_len] == '\0') {
1260                         int len = left_len - 1;
1261                         idtest = is_dupid(lb, id, name);
1262                         
1263                         while (idtest && len > 1) {
1264                                 name[len--] = '\0';
1265                                 idtest = is_dupid(lb, id, name);
1266                         }
1267                         if (idtest == NULL) return 1;
1268                         /* otherwise just continue and use a number suffix */
1269                 }
1270                 
1271                 if (nr > 999 && left_len > (MAX_ID_NAME - 8)) {
1272                         /* this would overflow name buffer */
1273                         left[MAX_ID_NAME - 8] = 0;
1274                         /* left_len = MAX_ID_NAME - 8; */ /* for now this isn't used again */
1275                         memcpy(name, left, sizeof(char) * (MAX_ID_NAME - 7));
1276                         continue;
1277                 }
1278                 /* this format specifier is from hell... */
1279                 BLI_snprintf(name, sizeof(id->name) - 2, "%s.%.3d", left, nr);
1280
1281                 return 1;
1282         }
1283 }
1284
1285 /*
1286  * Only for local blocks: external en indirect blocks already have a
1287  * unique ID.
1288  *
1289  * return 1: created a new name
1290  */
1291
1292 int new_id(ListBase *lb, ID *id, const char *tname)
1293 {
1294         int result;
1295         char name[MAX_ID_NAME - 2];
1296
1297         /* if library, don't rename */
1298         if (id->lib) return 0;
1299
1300         /* if no libdata given, look up based on ID */
1301         if (lb == NULL) lb = which_libbase(G.main, GS(id->name));
1302
1303         /* if no name given, use name of current ID
1304          * else make a copy (tname args can be const) */
1305         if (tname == NULL)
1306                 tname = id->name + 2;
1307
1308         strncpy(name, tname, sizeof(name) - 1);
1309
1310         /* if result > MAX_ID_NAME-3, strncpy don't put the final '\0' to name.
1311          * easier to assign each time then to check if its needed */
1312         name[sizeof(name) - 1] = 0;
1313
1314         if (name[0] == '\0') {
1315                 /* disallow empty names */
1316                 strcpy(name, ID_FALLBACK_NAME);
1317         }
1318         else {
1319                 /* disallow non utf8 chars,
1320                  * the interface checks for this but new ID's based on file names don't */
1321                 BLI_utf8_invalid_strip(name, strlen(name));
1322         }
1323
1324         result = check_for_dupid(lb, id, name);
1325         strcpy(id->name + 2, name);
1326
1327         /* This was in 2.43 and previous releases
1328          * however all data in blender should be sorted, not just duplicate names
1329          * sorting should not hurt, but noting just incause it alters the way other
1330          * functions work, so sort every time */
1331 #if 0
1332         if (result)
1333                 id_sort_by_name(lb, id);
1334 #endif
1335
1336         id_sort_by_name(lb, id);
1337         
1338         return result;
1339 }
1340
1341 /* Pull an ID out of a library (make it local). Only call this for IDs that
1342  * don't have other library users. */
1343 void id_clear_lib_data(Main *bmain, ID *id)
1344 {
1345         BKE_id_lib_local_paths(bmain, id->lib, id);
1346
1347         id->lib = NULL;
1348         id->flag = LIB_LOCAL;
1349         new_id(which_libbase(bmain, GS(id->name)), id, NULL);
1350 }
1351
1352 /* next to indirect usage in read/writefile also in editobject.c scene.c */
1353 void clear_id_newpoins(void)
1354 {
1355         ListBase *lbarray[MAX_LIBARRAY];
1356         ID *id;
1357         int a;
1358
1359         a = set_listbasepointers(G.main, lbarray);
1360         while (a--) {
1361                 id = lbarray[a]->first;
1362                 while (id) {
1363                         id->newid = NULL;
1364                         id->flag &= ~LIB_NEW;
1365                         id = id->next;
1366                 }
1367         }
1368 }
1369
1370 #define LIBTAG(a)   if (a && a->id.lib) { a->id.flag &= ~LIB_INDIRECT; a->id.flag |= LIB_EXTERN; } (void)0
1371
1372 static void lib_indirect_test_id(ID *id, Library *lib)
1373 {
1374         
1375         if (id->lib) {
1376                 /* datablocks that were indirectly related are now direct links
1377                  * without this, appending data that has a link to other data will fail to write */
1378                 if (lib && id->lib->parent == lib) {
1379                         id_lib_extern(id);
1380                 }
1381                 return;
1382         }
1383         
1384         if (GS(id->name) == ID_OB) {
1385                 Object *ob = (Object *)id;
1386                 Mesh *me;
1387
1388                 int a;
1389
1390 #if 0   /* XXX OLD ANIMSYS, NLASTRIPS ARE NO LONGER USED */
1391                 /* XXX old animation system! -------------------------------------- */
1392                 {
1393                         bActionStrip *strip;
1394                         for (strip = ob->nlastrips.first; strip; strip = strip->next) {
1395                                 LIBTAG(strip->object);
1396                                 LIBTAG(strip->act);
1397                                 LIBTAG(strip->ipo);
1398                         }
1399                 }
1400                 /* XXX: new animation system needs something like this? */
1401 #endif
1402
1403                 for (a = 0; a < ob->totcol; a++) {
1404                         LIBTAG(ob->mat[a]);
1405                 }
1406         
1407                 LIBTAG(ob->dup_group);
1408                 LIBTAG(ob->proxy);
1409                 
1410                 me = ob->data;
1411                 LIBTAG(me);
1412         }
1413 }
1414
1415 void tag_main_lb(ListBase *lb, const short tag)
1416 {
1417         ID *id;
1418         if (tag) {
1419                 for (id = lb->first; id; id = id->next) {
1420                         id->flag |= LIB_DOIT;
1421                 }
1422         }
1423         else {
1424                 for (id = lb->first; id; id = id->next) {
1425                         id->flag &= ~LIB_DOIT;
1426                 }
1427         }
1428 }
1429
1430 void tag_main_idcode(struct Main *mainvar, const short type, const short tag)
1431 {
1432         ListBase *lb = which_libbase(mainvar, type);
1433
1434         tag_main_lb(lb, tag);
1435 }
1436
1437 void tag_main(struct Main *mainvar, const short tag)
1438 {
1439         ListBase *lbarray[MAX_LIBARRAY];
1440         int a;
1441
1442         a = set_listbasepointers(mainvar, lbarray);
1443         while (a--) {
1444                 tag_main_lb(lbarray[a], tag);
1445         }
1446 }
1447
1448 /* if lib!=NULL, only all from lib local
1449  * bmain is almost certainly G.main */
1450 void BKE_library_make_local(Main *bmain, Library *lib, int untagged_only)
1451 {
1452         ListBase *lbarray[MAX_LIBARRAY], tempbase = {NULL, NULL};
1453         ID *id, *idn;
1454         int a;
1455
1456         a = set_listbasepointers(bmain, lbarray);
1457         while (a--) {
1458                 id = lbarray[a]->first;
1459                 
1460                 while (id) {
1461                         id->newid = NULL;
1462                         idn = id->next;      /* id is possibly being inserted again */
1463                         
1464                         /* The check on the second line (LIB_PRE_EXISTING) is done so its
1465                          * possible to tag data you don't want to be made local, used for
1466                          * appending data, so any libdata already linked wont become local
1467                          * (very nasty to discover all your links are lost after appending)  
1468                          * */
1469                         if (id->flag & (LIB_EXTERN | LIB_INDIRECT | LIB_NEW) &&
1470                             (untagged_only == 0 || !(id->flag & LIB_PRE_EXISTING)))
1471                         {
1472                                 if (lib == NULL || id->lib == lib) {
1473                                         if (id->lib) {
1474                                                 id_clear_lib_data(bmain, id); /* sets 'id->flag' */
1475
1476                                                 /* why sort alphabetically here but not in
1477                                                  * id_clear_lib_data() ? - campbell */
1478                                                 id_sort_by_name(lbarray[a], id);
1479                                         }
1480                                         else {
1481                                                 id->flag &= ~(LIB_EXTERN | LIB_INDIRECT | LIB_NEW);
1482                                         }
1483                                 }
1484                         }
1485                         id = idn;
1486                 }
1487                 
1488                 /* patch2: make it aphabetically */
1489                 while ( (id = tempbase.first) ) {
1490                         BLI_remlink(&tempbase, id);
1491                         BLI_addtail(lbarray[a], id);
1492                         new_id(lbarray[a], id, NULL);
1493                 }
1494         }
1495
1496         /* patch 3: make sure library data isn't indirect falsely... */
1497         a = set_listbasepointers(bmain, lbarray);
1498         while (a--) {
1499                 for (id = lbarray[a]->first; id; id = id->next)
1500                         lib_indirect_test_id(id, lib);
1501         }
1502 }
1503
1504
1505 void test_idbutton(char *name)
1506 {
1507         /* called from buttons: when name already exists: call new_id */
1508         ListBase *lb;
1509         ID *idtest;
1510         
1511
1512         lb = which_libbase(G.main, GS(name - 2) );
1513         if (lb == NULL) return;
1514         
1515         /* search for id */
1516         idtest = BLI_findstring(lb, name, offsetof(ID, name) + 2);
1517
1518         if (idtest) if (new_id(lb, idtest, name) == 0) id_sort_by_name(lb, idtest);
1519 }
1520
1521 void text_idbutton(struct ID *id, char *text)
1522 {
1523         if (id) {
1524                 if (GS(id->name) == ID_SCE)
1525                         strcpy(text, "SCE: ");
1526                 else if (GS(id->name) == ID_SCR)
1527                         strcpy(text, "SCR: ");
1528                 else if (GS(id->name) == ID_MA && ((Material *)id)->use_nodes)
1529                         strcpy(text, "NT: ");
1530                 else {
1531                         text[0] = id->name[0];
1532                         text[1] = id->name[1];
1533                         text[2] = ':';
1534                         text[3] = ' ';
1535                         text[4] = 0;
1536                 }
1537         }
1538         else {
1539                 text[0] = '\0';
1540         }
1541 }
1542
1543 void rename_id(ID *id, const char *name)
1544 {
1545         ListBase *lb;
1546
1547         BLI_strncpy(id->name + 2, name, sizeof(id->name) - 2);
1548         lb = which_libbase(G.main, GS(id->name) );
1549         
1550         new_id(lb, id, name);
1551 }
1552
1553 void name_uiprefix_id(char *name, ID *id)
1554 {
1555         name[0] = id->lib ? 'L' : ' ';
1556         name[1] = id->flag & LIB_FAKEUSER ? 'F' : (id->us == 0) ? '0' : ' ';
1557         name[2] = ' ';
1558
1559         strcpy(name + 3, id->name + 2);
1560 }
1561
1562 void BKE_library_filepath_set(Library *lib, const char *filepath)
1563 {
1564         /* in some cases this is used to update the absolute path from the
1565          * relative */
1566         if (lib->name != filepath) {
1567                 BLI_strncpy(lib->name, filepath, sizeof(lib->name));
1568         }
1569
1570         BLI_strncpy(lib->filepath, filepath, sizeof(lib->filepath));
1571
1572         /* not essential but set filepath is an absolute copy of value which
1573          * is more useful if its kept in sync */
1574         if (BLI_path_is_rel(lib->filepath)) {
1575                 /* note that the file may be unsaved, in this case, setting the
1576                  * filepath on an indirectly linked path is not allowed from the
1577                  * outliner, and its not really supported but allow from here for now
1578                  * since making local could cause this to be directly linked - campbell
1579                  */
1580                 const char *basepath = lib->parent ? lib->parent->filepath : G.main->name;
1581                 BLI_path_abs(lib->filepath, basepath);
1582         }
1583 }