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