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