Code refactoring: split lamp functions from object.c into new lamp.c.
[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
32
33 /*
34  *  Contains management of ID's and libraries
35  *  allocate and free of all library data
36  * 
37  */
38
39
40 #include <stdio.h>
41 #include <ctype.h>
42 #include <string.h>
43 #include <stdlib.h>
44 #include <stddef.h>
45 #include <assert.h>
46
47 #include "MEM_guardedalloc.h"
48
49 /* all types are needed here, in order to do memory operations */
50 #include "DNA_anim_types.h"
51 #include "DNA_armature_types.h"
52 #include "DNA_brush_types.h"
53 #include "DNA_camera_types.h"
54 #include "DNA_group_types.h"
55 #include "DNA_ipo_types.h"
56 #include "DNA_key_types.h"
57 #include "DNA_lamp_types.h"
58 #include "DNA_lattice_types.h"
59 #include "DNA_material_types.h"
60 #include "DNA_mesh_types.h"
61 #include "DNA_meta_types.h"
62 #include "DNA_nla_types.h"
63 #include "DNA_node_types.h"
64 #include "DNA_scene_types.h"
65 #include "DNA_screen_types.h"
66 #include "DNA_speaker_types.h"
67 #include "DNA_sound_types.h"
68 #include "DNA_text_types.h"
69 #include "DNA_vfont_types.h"
70 #include "DNA_windowmanager_types.h"
71 #include "DNA_world_types.h"
72 #include "DNA_gpencil_types.h"
73
74 #include "BLI_blenlib.h"
75 #include "BLI_dynstr.h"
76 #include "BLI_utildefines.h"
77 #include "BLI_bpath.h"
78
79 #include "BKE_animsys.h"
80 #include "BKE_camera.h"
81 #include "BKE_context.h"
82 #include "BKE_lamp.h"
83 #include "BKE_library.h"
84 #include "BKE_main.h"
85 #include "BKE_global.h"
86 #include "BKE_sound.h"
87 #include "BKE_object.h"
88 #include "BKE_screen.h"
89 #include "BKE_mesh.h"
90 #include "BKE_material.h"
91 #include "BKE_curve.h"
92 #include "BKE_mball.h"
93 #include "BKE_text.h"
94 #include "BKE_texture.h"
95 #include "BKE_scene.h"
96 #include "BKE_icons.h"
97 #include "BKE_image.h"
98 #include "BKE_ipo.h"
99 #include "BKE_key.h"
100 #include "BKE_world.h"
101 #include "BKE_font.h"
102 #include "BKE_group.h"
103 #include "BKE_lattice.h"
104 #include "BKE_armature.h"
105 #include "BKE_action.h"
106 #include "BKE_node.h"
107 #include "BKE_brush.h"
108 #include "BKE_idprop.h"
109 #include "BKE_particle.h"
110 #include "BKE_gpencil.h"
111 #include "BKE_fcurve.h"
112 #include "BKE_speaker.h"
113 #include "BKE_utildefines.h"
114
115 #include "RNA_access.h"
116
117 #ifdef WITH_PYTHON
118 #include "BPY_extern.h"
119 #endif
120
121 #define MAX_IDPUP               60      /* was 24 */
122
123 /* GS reads the memory pointed at in a specific ordering. 
124    only use this definition, makes little and big endian systems
125    work fine, in conjunction with MAKE_ID */
126
127 /* from blendef: */
128 #define GS(a)   (*((short *)(a)))
129
130 /* ************* general ************************ */
131
132
133 /* this has to be called from each make_local_* func, we could call
134  * from id_make_local() but then the make local functions would not be self
135  * contained.
136  * also note that the id _must_ have a library - campbell */
137 void BKE_id_lib_local_paths(Main *bmain, ID *id)
138 {
139         char *bpath_user_data[2]= {bmain->name, (id)->lib->filepath};
140
141         bpath_traverse_id(bmain, id,
142                                           bpath_relocate_visitor,
143                                           BPATH_TRAVERSE_SKIP_MULTIFILE,
144                                           bpath_user_data);
145 }
146
147 void id_lib_extern(ID *id)
148 {
149         if(id) {
150                 if(id->flag & LIB_INDIRECT) {
151                         id->flag -= LIB_INDIRECT;
152                         id->flag |= LIB_EXTERN;
153                 }
154         }
155 }
156
157 void id_us_plus(ID *id)
158 {
159         if(id) {
160                 id->us++;
161                 if(id->flag & LIB_INDIRECT) {
162                         id->flag -= LIB_INDIRECT;
163                         id->flag |= LIB_EXTERN;
164                 }
165         }
166 }
167
168 void id_us_min(ID *id)
169 {
170         if(id) {
171                 if(id->us<2 && (id->flag & LIB_FAKEUSER))
172                    id->us= 1;
173                 else if(id->us<=0)
174                         printf("ID user decrement error: %s \n", id->name);
175                 else
176                         id->us--;
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) make_local_object((Object*)id);
192                         return 1;
193                 case ID_ME:
194                         if(!test) {
195                                 make_local_mesh((Mesh*)id);
196                                 make_local_key(((Mesh*)id)->key);
197                         }
198                         return 1;
199                 case ID_CU:
200                         if(!test) {
201                                 make_local_curve((Curve*)id);
202                                 make_local_key(((Curve*)id)->key);
203                         }
204                         return 1;
205                 case ID_MB:
206                         if(!test) make_local_mball((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) make_local_image((Image*)id);
216                         return 1;
217                 case ID_LT:
218                         if(!test) {
219                                 make_local_lattice((Lattice*)id);
220                                 make_local_key(((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) make_local_camera((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) make_local_key((Key*)id);
236                         return 1;
237                 case ID_WO:
238                         if(!test) make_local_world((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) make_local_armature((bArmature*)id);
254                         return 1;
255                 case ID_AC:
256                         if(!test) make_local_action((bAction*)id);
257                         return 1;
258                 case ID_NT:
259                         return 0; /* not implemented */
260                 case ID_BR:
261                         if(!test) make_local_brush((Brush*)id);
262                         return 1;
263                 case ID_PA:
264                         if(!test) make_local_particlesettings((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*)copy_object((Object*)id);
289                         return 1;
290                 case ID_ME:
291                         if(!test) *newid= (ID*)copy_mesh((Mesh*)id);
292                         return 1;
293                 case ID_CU:
294                         if(!test) *newid= (ID*)copy_curve((Curve*)id);
295                         return 1;
296                 case ID_MB:
297                         if(!test) *newid= (ID*)copy_mball((MetaBall*)id);
298                         return 1;
299                 case ID_MA:
300                         if(!test) *newid= (ID*)copy_material((Material*)id);
301                         return 1;
302                 case ID_TE:
303                         if(!test) *newid= (ID*)copy_texture((Tex*)id);
304                         return 1;
305                 case ID_IM:
306                         if(!test) *newid= (ID*)copy_image((Image*)id);
307                         return 1;
308                 case ID_LT:
309                         if(!test) *newid= (ID*)copy_lattice((Lattice*)id);
310                         return 1;
311                 case ID_LA:
312                         if(!test) *newid= (ID*)copy_lamp((Lamp*)id);
313                         return 1;
314                 case ID_SPK:
315                         if(!test) *newid= (ID*)copy_speaker((Speaker*)id);
316                         return 1;
317                 case ID_CA:
318                         if(!test) *newid= (ID*)copy_camera((Camera*)id);
319                         return 1;
320                 case ID_IP:
321                         return 0; /* deprecated */
322                 case ID_KE:
323                         if(!test) *newid= (ID*)copy_key((Key*)id);
324                         return 1;
325                 case ID_WO:
326                         if(!test) *newid= (ID*)copy_world((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*)copy_text((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*)copy_group((Group*)id);
341                         return 1;
342                 case ID_AR:
343                         if(!test) *newid= (ID*)copy_armature((bArmature*)id);
344                         return 1;
345                 case ID_AC:
346                         if(!test) *newid= (ID*)copy_action((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*)copy_brush((Brush*)id);
353                         return 1;
354                 case ID_PA:
355                         if(!test) *newid= (ID*)psys_copy_settings((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                         unlink_text(mainlib, (Text*)id);
375                         break;
376                 case ID_GR:
377                         if(test) return 1;
378                         unlink_group((Group*)id);
379                         break;
380                 case ID_OB:
381                         if(test) return 1;
382                         unlink_object((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                 free_libblock(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 incremement 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         }
488         return NULL;
489 }
490
491 /* Flag all ids in listbase */
492 void flag_listbase_ids(ListBase *lb, short flag, short value)
493 {
494         ID *id;
495         if (value) {
496                 for(id= lb->first; id; id= id->next) id->flag |= flag;
497         } else {
498                 flag = ~flag;
499                 for(id= lb->first; id; id= id->next) id->flag &= flag;
500         }
501 }
502
503 /* Flag all ids in listbase */
504 void flag_all_listbases_ids(short flag, short value)
505 {
506         ListBase *lbarray[MAX_LIBARRAY];
507         int a;
508         a= set_listbasepointers(G.main, lbarray);
509         while(a--)      flag_listbase_ids(lbarray[a], flag, value);
510 }
511
512 void recalc_all_library_objects(Main *main)
513 {
514         Object *ob;
515
516         /* flag for full recalc */
517         for(ob=main->object.first; ob; ob=ob->id.next)
518                 if(ob->id.lib)
519                         ob->recalc |= OB_RECALC_OB|OB_RECALC_DATA|OB_RECALC_TIME;
520 }
521
522 /* note: MAX_LIBARRAY define should match this code */
523 int set_listbasepointers(Main *main, ListBase **lb)
524 {
525         int a = 0;
526
527         /* BACKWARDS! also watch order of free-ing! (mesh<->mat), first items freed last.
528          * This is important because freeing data decreases usercounts of other datablocks,
529          * if this data is its self freed it can crash. */
530         lb[a++]= &(main->ipo);
531         lb[a++]= &(main->action); // xxx moved here to avoid problems when freeing with animato (aligorith)
532         lb[a++]= &(main->key);
533         lb[a++]= &(main->gpencil); /* referenced by nodes, objects, view, scene etc, before to free after. */
534         lb[a++]= &(main->nodetree);
535         lb[a++]= &(main->image);
536         lb[a++]= &(main->tex);
537         lb[a++]= &(main->mat);
538         lb[a++]= &(main->vfont);
539         
540         /* Important!: When adding a new object type,
541          * the specific data should be inserted here 
542          */
543
544         lb[a++]= &(main->armature);
545
546         lb[a++]= &(main->mesh);
547         lb[a++]= &(main->curve);
548         lb[a++]= &(main->mball);
549
550         lb[a++]= &(main->latt);
551         lb[a++]= &(main->lamp);
552         lb[a++]= &(main->camera);
553
554         lb[a++]= &(main->text);
555         lb[a++]= &(main->sound);
556         lb[a++]= &(main->group);
557         lb[a++]= &(main->brush);
558         lb[a++]= &(main->script);
559         lb[a++]= &(main->particle);
560         lb[a++]= &(main->speaker);
561
562         lb[a++]= &(main->world);
563         lb[a++]= &(main->screen);
564         lb[a++]= &(main->object);
565         lb[a++]= &(main->scene);
566         lb[a++]= &(main->library);
567         lb[a++]= &(main->wm);
568         
569         lb[a]= NULL;
570
571         return a;
572 }
573
574 /* *********** ALLOC AND FREE *****************
575   
576 free_libblock(ListBase *lb, ID *id )
577         provide a list-basis and datablock, but only ID is read
578
579 void *alloc_libblock(ListBase *lb, type, name)
580         inserts in list and returns a new ID
581
582  ***************************** */
583
584 static ID *alloc_libblock_notest(short type)
585 {
586         ID *id= NULL;
587         
588         switch( type ) {
589                 case ID_SCE:
590                         id= MEM_callocN(sizeof(Scene), "scene");
591                         break;
592                 case ID_LI:
593                         id= MEM_callocN(sizeof(Library), "library");
594                         break;
595                 case ID_OB:
596                         id= MEM_callocN(sizeof(Object), "object");
597                         break;
598                 case ID_ME:
599                         id= MEM_callocN(sizeof(Mesh), "mesh");
600                         break;
601                 case ID_CU:
602                         id= MEM_callocN(sizeof(Curve), "curve");
603                         break;
604                 case ID_MB:
605                         id= MEM_callocN(sizeof(MetaBall), "mball");
606                         break;
607                 case ID_MA:
608                         id= MEM_callocN(sizeof(Material), "mat");
609                         break;
610                 case ID_TE:
611                         id= MEM_callocN(sizeof(Tex), "tex");
612                         break;
613                 case ID_IM:
614                         id= MEM_callocN(sizeof(Image), "image");
615                         break;
616                 case ID_LT:
617                         id= MEM_callocN(sizeof(Lattice), "latt");
618                         break;
619                 case ID_LA:
620                         id= MEM_callocN(sizeof(Lamp), "lamp");
621                         break;
622                 case ID_CA:
623                         id= MEM_callocN(sizeof(Camera), "camera");
624                         break;
625                 case ID_IP:
626                         id= MEM_callocN(sizeof(Ipo), "ipo");
627                         break;
628                 case ID_KE:
629                         id= MEM_callocN(sizeof(Key), "key");
630                         break;
631                 case ID_WO:
632                         id= MEM_callocN(sizeof(World), "world");
633                         break;
634                 case ID_SCR:
635                         id= MEM_callocN(sizeof(bScreen), "screen");
636                         break;
637                 case ID_VF:
638                         id= MEM_callocN(sizeof(VFont), "vfont");
639                         break;
640                 case ID_TXT:
641                         id= MEM_callocN(sizeof(Text), "text");
642                         break;
643                 case ID_SCRIPT:
644                         //XXX id= MEM_callocN(sizeof(Script), "script");
645                         break;
646                 case ID_SPK:
647                         id= MEM_callocN(sizeof(Speaker), "speaker");
648                         break;
649                 case ID_SO:
650                         id= MEM_callocN(sizeof(bSound), "sound");
651                         break;
652                 case ID_GR:
653                         id= MEM_callocN(sizeof(Group), "group");
654                         break;
655                 case ID_AR:
656                         id = MEM_callocN(sizeof(bArmature), "armature");
657                         break;
658                 case ID_AC:
659                         id = MEM_callocN(sizeof(bAction), "action");
660                         break;
661                 case ID_NT:
662                         id = MEM_callocN(sizeof(bNodeTree), "nodetree");
663                         break;
664                 case ID_BR:
665                         id = MEM_callocN(sizeof(Brush), "brush");
666                         break;
667                 case ID_PA:
668                         id = MEM_callocN(sizeof(ParticleSettings), "ParticleSettings");
669                           break;
670                 case ID_WM:
671                         id = MEM_callocN(sizeof(wmWindowManager), "Window manager");
672                           break;
673                 case ID_GD:
674                         id = MEM_callocN(sizeof(bGPdata), "Grease Pencil");
675                         break;
676         }
677         return id;
678 }
679
680 /* used everywhere in blenkernel and text.c */
681 void *alloc_libblock(ListBase *lb, short type, const char *name)
682 {
683         ID *id= NULL;
684         
685         id= alloc_libblock_notest(type);
686         if(id) {
687                 BLI_addtail(lb, id);
688                 id->us= 1;
689                 id->icon_id = 0;
690                 *( (short *)id->name )= type;
691                 new_id(lb, id, name);
692                 /* alphabetic insterion: is in new_id */
693         }
694         return id;
695 }
696
697 /* by spec, animdata is first item after ID */
698 /* and, trust that BKE_animdata_from_id() will only find AnimData for valid ID-types */
699 static void id_copy_animdata(ID *id, const short do_action)
700 {
701         AnimData *adt= BKE_animdata_from_id(id);
702         
703         if (adt) {
704                 IdAdtTemplate *iat = (IdAdtTemplate *)id;
705                 iat->adt= BKE_copy_animdata(iat->adt, do_action); /* could be set to FALSE, need to investigate */
706         }
707 }
708
709 /* material nodes use this since they are not treated as libdata */
710 void copy_libblock_data(ID *id, const ID *id_from, const short do_action)
711 {
712         if (id_from->properties)
713                 id->properties = IDP_CopyProperty(id_from->properties);
714
715         /* the duplicate should get a copy of the animdata */
716         id_copy_animdata(id, do_action);
717 }
718
719 /* used everywhere in blenkernel */
720 void *copy_libblock(void *rt)
721 {
722         ID *idn, *id;
723         ListBase *lb;
724         char *cp, *cpn;
725         size_t idn_len;
726         
727         id= rt;
728
729         lb= which_libbase(G.main, GS(id->name));
730         idn= alloc_libblock(lb, GS(id->name), id->name+2);
731
732         assert(idn != NULL);
733
734         idn_len= MEM_allocN_len(idn);
735         if((int)idn_len - (int)sizeof(ID) > 0) { /* signed to allow neg result */
736                 cp= (char *)id;
737                 cpn= (char *)idn;
738                 memcpy(cpn+sizeof(ID), cp+sizeof(ID), idn_len - sizeof(ID));
739         }
740         
741         id->newid= idn;
742         idn->flag |= LIB_NEW;
743
744         copy_libblock_data(idn, id, FALSE);
745         
746         return idn;
747 }
748
749 static void free_library(Library *UNUSED(lib))
750 {
751         /* no freeing needed for libraries yet */
752 }
753
754 static void (*free_windowmanager_cb)(bContext *, wmWindowManager *)= NULL;
755
756 void set_free_windowmanager_cb(void (*func)(bContext *C, wmWindowManager *) )
757 {
758         free_windowmanager_cb= func;
759 }
760
761 static void animdata_dtar_clear_cb(ID *UNUSED(id), AnimData *adt, void *userdata)
762 {
763         ChannelDriver *driver;
764         FCurve *fcu;
765
766         /* find the driver this belongs to and update it */
767         for (fcu=adt->drivers.first; fcu; fcu=fcu->next) {
768                 driver= fcu->driver;
769                 
770                 if (driver) {
771                         DriverVar *dvar;
772                         for (dvar= driver->variables.first; dvar; dvar= dvar->next) {
773                                 DRIVER_TARGETS_USED_LOOPER(dvar) 
774                                 {
775                                         if (dtar->id == userdata)
776                                                 dtar->id= NULL;
777                                 }
778                                 DRIVER_TARGETS_LOOPER_END
779                         }
780                 }
781         }
782 }
783
784
785 /* used in headerbuttons.c image.c mesh.c screen.c sound.c and library.c */
786 void free_libblock(ListBase *lb, void *idv)
787 {
788         ID *id= idv;
789
790 #ifdef WITH_PYTHON
791         BPY_id_release(id);
792 #endif
793
794         switch( GS(id->name) ) {        /* GetShort from util.h */
795                 case ID_SCE:
796                         free_scene((Scene *)id);
797                         break;
798                 case ID_LI:
799                         free_library((Library *)id);
800                         break;
801                 case ID_OB:
802                         free_object((Object *)id);
803                         break;
804                 case ID_ME:
805                         free_mesh((Mesh *)id);
806                         break;
807                 case ID_CU:
808                         free_curve((Curve *)id);
809                         break;
810                 case ID_MB:
811                         free_mball((MetaBall *)id);
812                         break;
813                 case ID_MA:
814                         free_material((Material *)id);
815                         break;
816                 case ID_TE:
817                         free_texture((Tex *)id);
818                         break;
819                 case ID_IM:
820                         free_image((Image *)id);
821                         break;
822                 case ID_LT:
823                         free_lattice((Lattice *)id);
824                         break;
825                 case ID_LA:
826                         free_lamp((Lamp *)id);
827                         break;
828                 case ID_CA:
829                         free_camera((Camera*) id);
830                         break;
831                 case ID_IP:
832                         free_ipo((Ipo *)id);
833                         break;
834                 case ID_KE:
835                         free_key((Key *)id);
836                         break;
837                 case ID_WO:
838                         free_world((World *)id);
839                         break;
840                 case ID_SCR:
841                         free_screen((bScreen *)id);
842                         break;
843                 case ID_VF:
844                         free_vfont((VFont *)id);
845                         break;
846                 case ID_TXT:
847                         free_text((Text *)id);
848                         break;
849                 case ID_SCRIPT:
850                         //XXX free_script((Script *)id);
851                         break;
852                 case ID_SPK:
853                         free_speaker((Speaker *)id);
854                         break;
855                 case ID_SO:
856                         sound_free((bSound*)id);
857                         break;
858                 case ID_GR:
859                         free_group_objects((Group *)id);
860                         break;
861                 case ID_AR:
862                         free_armature((bArmature *)id);
863                         break;
864                 case ID_AC:
865                         free_action((bAction *)id);
866                         break;
867                 case ID_NT:
868                         ntreeFreeTree((bNodeTree *)id);
869                         break;
870                 case ID_BR:
871                         free_brush((Brush *)id);
872                         break;
873                 case ID_PA:
874                         psys_free_settings((ParticleSettings *)id);
875                         break;
876                 case ID_WM:
877                         if(free_windowmanager_cb)
878                                 free_windowmanager_cb(NULL, (wmWindowManager *)id);
879                         break;
880                 case ID_GD:
881                         free_gpencil_data((bGPdata *)id);
882                         break;
883         }
884
885         if (id->properties) {
886                 IDP_FreeProperty(id->properties);
887                 MEM_freeN(id->properties);
888         }
889
890         BLI_remlink(lb, id);
891
892         /* this ID may be a driver target! */
893         BKE_animdata_main_cb(G.main, animdata_dtar_clear_cb, (void *)id);
894
895         MEM_freeN(id);
896 }
897
898 void free_libblock_us(ListBase *lb, void *idv)          /* test users */
899 {
900         ID *id= idv;
901         
902         id->us--;
903
904         if(id->us<0) {
905                 if(id->lib) printf("ERROR block %s %s users %d\n", id->lib->name, id->name, id->us);
906                 else printf("ERROR block %s users %d\n", id->name, id->us);
907         }
908         if(id->us==0) {
909                 if( GS(id->name)==ID_OB ) unlink_object((Object *)id);
910                 
911                 free_libblock(lb, id);
912         }
913 }
914
915
916 void free_main(Main *mainvar)
917 {
918         /* also call when reading a file, erase all, etc */
919         ListBase *lbarray[MAX_LIBARRAY];
920         int a;
921
922         a= set_listbasepointers(mainvar, lbarray);
923         while(a--) {
924                 ListBase *lb= lbarray[a];
925                 ID *id;
926                 
927                 while ( (id= lb->first) ) {
928                         free_libblock(lb, id);
929                 }
930         }
931
932         MEM_freeN(mainvar);
933 }
934
935 /* ***************** ID ************************ */
936
937
938 ID *find_id(const char *type, const char *name)         /* type: "OB" or "MA" etc */
939 {
940         ListBase *lb= which_libbase(G.main, GS(type));
941         return BLI_findstring(lb, name, offsetof(ID, name) + 2);
942 }
943
944 static void get_flags_for_id(ID *id, char *buf) 
945 {
946         int isfake= id->flag & LIB_FAKEUSER;
947         int isnode=0;
948                 /* Writeout the flags for the entry, note there
949                  * is a small hack that writes 5 spaces instead
950                  * of 4 if no flags are displayed... this makes
951                  * things usually line up ok - better would be
952                  * to have that explicit, oh well - zr
953                  */
954
955         if(GS(id->name)==ID_MA)
956                 isnode= ((Material *)id)->use_nodes;
957         if(GS(id->name)==ID_TE)
958                 isnode= ((Tex *)id)->use_nodes;
959         
960         if (id->us<0)
961                 strcpy(buf, "-1W ");
962         else if (!id->lib && !isfake && id->us && !isnode)
963                 strcpy(buf, "     ");
964         else if(isnode)
965                 sprintf(buf, "%c%cN%c ", id->lib?'L':' ', isfake?'F':' ', (id->us==0)?'O':' ');
966         else
967                 sprintf(buf, "%c%c%c ", id->lib?'L':' ', isfake?'F':' ', (id->us==0)?'O':' ');
968 }
969
970 #define IDPUP_NO_VIEWER 1
971
972 static void IDnames_to_dyn_pupstring(DynStr *pupds, ListBase *lb, ID *link, short *nr, int hideflag)
973 {
974         int i, nids= BLI_countlist(lb);
975                 
976         if (nr) *nr= -1;
977         
978         if (nr && nids>MAX_IDPUP) {
979                 BLI_dynstr_append(pupds, "DataBrowse %x-2");
980                 *nr= -2;
981         } else {
982                 ID *id;
983                 
984                 for (i=0, id= lb->first; id; id= id->next, i++) {
985                         char buf[32];
986                         
987                         if (nr && id==link) *nr= i+1;
988
989                         if (U.uiflag & USER_HIDE_DOT && id->name[2]=='.')
990                                 continue;
991                         if (hideflag & IDPUP_NO_VIEWER)
992                                 if (GS(id->name)==ID_IM)
993                                         if ( ((Image *)id)->source==IMA_SRC_VIEWER )
994                                                 continue;
995                         
996                         get_flags_for_id(id, buf);
997                                 
998                         BLI_dynstr_append(pupds, buf);
999                         BLI_dynstr_append(pupds, id->name+2);
1000                         BLI_snprintf(buf, sizeof(buf), "%%x%d", i+1);
1001                         BLI_dynstr_append(pupds, buf);
1002                         
1003                         /* icon */
1004                         switch(GS(id->name))
1005                         {
1006                         case ID_MA: /* fall through */
1007                         case ID_TE: /* fall through */
1008                         case ID_IM: /* fall through */
1009                         case ID_WO: /* fall through */
1010                         case ID_LA: /* fall through */
1011                                 BLI_snprintf(buf, sizeof(buf), "%%i%d", BKE_icon_getid(id) );
1012                                 BLI_dynstr_append(pupds, buf);
1013                                 break;
1014                         default:
1015                                 break;
1016                         }
1017                         
1018                         if(id->next)
1019                                 BLI_dynstr_append(pupds, "|");
1020                 }
1021         }
1022 }
1023
1024
1025 /* used by headerbuttons.c buttons.c editobject.c editseq.c */
1026 /* if nr==NULL no MAX_IDPUP, this for non-header browsing */
1027 void IDnames_to_pupstring(const char **str, const char *title, const char *extraops, ListBase *lb, ID *link, short *nr)
1028 {
1029         DynStr *pupds= BLI_dynstr_new();
1030
1031         if (title) {
1032                 BLI_dynstr_append(pupds, title);
1033                 BLI_dynstr_append(pupds, "%t|");
1034         }
1035         
1036         if (extraops) {
1037                 BLI_dynstr_append(pupds, extraops);
1038                 if (BLI_dynstr_get_len(pupds))
1039                         BLI_dynstr_append(pupds, "|");
1040         }
1041
1042         IDnames_to_dyn_pupstring(pupds, lb, link, nr, 0);
1043         
1044         *str= BLI_dynstr_get_cstring(pupds);
1045         BLI_dynstr_free(pupds);
1046 }
1047
1048 /* skips viewer images */
1049 void IMAnames_to_pupstring(const char **str, const char *title, const char *extraops, ListBase *lb, ID *link, short *nr)
1050 {
1051         DynStr *pupds= BLI_dynstr_new();
1052         
1053         if (title) {
1054                 BLI_dynstr_append(pupds, title);
1055                 BLI_dynstr_append(pupds, "%t|");
1056         }
1057         
1058         if (extraops) {
1059                 BLI_dynstr_append(pupds, extraops);
1060                 if (BLI_dynstr_get_len(pupds))
1061                         BLI_dynstr_append(pupds, "|");
1062         }
1063         
1064         IDnames_to_dyn_pupstring(pupds, lb, link, nr, IDPUP_NO_VIEWER);
1065         
1066         *str= BLI_dynstr_get_cstring(pupds);
1067         BLI_dynstr_free(pupds);
1068 }
1069
1070 static void sort_alpha_id(ListBase *lb, ID *id)
1071 {
1072         ID *idtest;
1073         
1074         /* insert alphabetically */
1075         if(lb->first!=lb->last) {
1076                 BLI_remlink(lb, id);
1077                 
1078                 idtest= lb->first;
1079                 while(idtest) {
1080                         if(BLI_strcasecmp(idtest->name, id->name)>0 || idtest->lib) {
1081                                 BLI_insertlinkbefore(lb, idtest, id);
1082                                 break;
1083                         }
1084                         idtest= idtest->next;
1085                 }
1086                 /* as last */
1087                 if(idtest==NULL) {
1088                         BLI_addtail(lb, id);
1089                 }
1090         }
1091         
1092 }
1093
1094 /*
1095  * Check to see if there is an ID with the same name as 'name'.
1096  * Returns the ID if so, if not, returns NULL
1097  */
1098 static ID *is_dupid(ListBase *lb, ID *id, const char *name)
1099 {
1100         ID *idtest=NULL;
1101         
1102         for( idtest = lb->first; idtest; idtest = idtest->next ) {
1103                 /* if idtest is not a lib */ 
1104                 if( id != idtest && idtest->lib == NULL ) {
1105                         /* do not test alphabetic! */
1106                         /* optimized */
1107                         if( idtest->name[2] == name[0] ) {
1108                                 if(strcmp(name, idtest->name+2)==0) break;
1109                         }
1110                 }
1111         }
1112         
1113         return idtest;
1114 }
1115
1116 /* 
1117  * Check to see if an ID name is already used, and find a new one if so.
1118  * Return 1 if created a new name (returned in name).
1119  *
1120  * Normally the ID that's being check is already in the ListBase, so ID *id
1121  * points at the new entry.  The Python Library module needs to know what
1122  * the name of a datablock will be before it is appended; in this case ID *id
1123  * id is NULL;
1124  */
1125
1126 static int check_for_dupid(ListBase *lb, ID *id, char *name)
1127 {
1128         ID *idtest;
1129         int nr= 0, nrtest, a, left_len;
1130         char left[32], leftest[32], in_use[32];
1131
1132         /* make sure input name is terminated properly */
1133         /* if( strlen(name) > 21 ) name[21]= 0; */
1134         /* removed since this is only ever called from one place - campbell */
1135
1136         while (1) {
1137
1138                 /* phase 1: id already exists? */
1139                 idtest = is_dupid(lb, id, name);
1140
1141                 /* if there is no double, done */
1142                 if( idtest == NULL ) return 0;
1143
1144                 /* we have a dup; need to make a new name */
1145                 /* quick check so we can reuse one of first 32 ids if vacant */
1146                 memset(in_use, 0, sizeof(in_use));
1147
1148                 /* get name portion, number portion ("name.number") */
1149                 left_len= BLI_split_name_num(left, &nr, name, '.');
1150
1151                 /* if new name will be too long, truncate it */
1152                 if(nr > 999 && left_len > 16) {
1153                         left[16]= 0;
1154                         left_len= 16;
1155                 }
1156                 else if(left_len > 17) {
1157                         left[17]= 0;
1158                         left_len= 17;
1159                 }
1160
1161                 for(idtest= lb->first; idtest; idtest= idtest->next) {
1162                         if(             (id != idtest) &&
1163                                         (idtest->lib == NULL) &&
1164                                         (*name == *(idtest->name+2)) &&
1165                                         (strncmp(name, idtest->name+2, left_len)==0) &&
1166                                         (BLI_split_name_num(leftest, &nrtest, idtest->name+2, '.') == left_len)
1167                         ) {
1168                                 if(nrtest < sizeof(in_use))
1169                                         in_use[nrtest]= 1;      /* mark as used */
1170                                 if(nr <= nrtest)
1171                                         nr= nrtest+1;           /* track largest unused */
1172                         }
1173                 }
1174
1175                 /* decide which value of nr to use */
1176                 for(a=0; a < sizeof(in_use); a++) {
1177                         if(a>=nr) break;        /* stop when we've check up to biggest */
1178                         if( in_use[a]==0 ) { /* found an unused value */
1179                                 nr = a;
1180                                 break;
1181                         }
1182                 }
1183
1184                 /* If the original name has no numeric suffix, 
1185                  * rather than just chopping and adding numbers, 
1186                  * shave off the end chars until we have a unique name.
1187                  * Check the null terminators match as well so we dont get Cube.000 -> Cube.00 */
1188                 if (nr==0 && name[left_len]== '\0') {
1189                         int len = left_len-1;
1190                         idtest= is_dupid(lb, id, name);
1191                         
1192                         while (idtest && len> 1) {
1193                                 name[len--] = '\0';
1194                                 idtest= is_dupid(lb, id, name);
1195                         }
1196                         if (idtest == NULL) return 1;
1197                         /* otherwise just continue and use a number suffix */
1198                 }
1199                 
1200                 if(nr > 999 && left_len > 16) {
1201                         /* this would overflow name buffer */
1202                         left[16] = 0;
1203                         /* left_len = 16; */ /* for now this isnt used again */
1204                         memcpy(name, left, sizeof(char) * 17);
1205                         continue;
1206                 }
1207                 /* this format specifier is from hell... */
1208                 BLI_snprintf(name, sizeof(id->name) - 2,"%s.%.3d", left, nr);
1209
1210                 return 1;
1211         }
1212 }
1213
1214 /*
1215  * Only for local blocks: external en indirect blocks already have a
1216  * unique ID.
1217  *
1218  * return 1: created a new name
1219  */
1220
1221 int new_id(ListBase *lb, ID *id, const char *tname)
1222 {
1223         int result;
1224         char name[MAX_ID_NAME-2];
1225
1226         /* if library, don't rename */
1227         if(id->lib) return 0;
1228
1229         /* if no libdata given, look up based on ID */
1230         if(lb==NULL) lb= which_libbase(G.main, GS(id->name));
1231
1232         /* if no name given, use name of current ID
1233          * else make a copy (tname args can be const) */
1234         if(tname==NULL)
1235                 tname= id->name+2;
1236
1237         strncpy(name, tname, sizeof(name)-1);
1238
1239         /* if result > 21, strncpy don't put the final '\0' to name.
1240          * easier to assign each time then to check if its needed */
1241         name[sizeof(name)-1]= 0;
1242
1243         if(name[0] == '\0') {
1244                 /* disallow empty names */
1245                 strcpy(name, ID_FALLBACK_NAME);
1246         }
1247         else {
1248                 /* disallow non utf8 chars,
1249                  * the interface checks for this but new ID's based on file names dont */
1250                 BLI_utf8_invalid_strip(name, strlen(name));
1251         }
1252
1253         result = check_for_dupid(lb, id, name);
1254         strcpy(id->name+2, name);
1255
1256         /* This was in 2.43 and previous releases
1257          * however all data in blender should be sorted, not just duplicate names
1258          * sorting should not hurt, but noting just incause it alters the way other
1259          * functions work, so sort every time */
1260         /* if( result )
1261                 sort_alpha_id(lb, id);*/
1262         
1263         sort_alpha_id(lb, id);
1264         
1265         return result;
1266 }
1267
1268 /* Pull an ID out of a library (make it local). Only call this for IDs that
1269    don't have other library users. */
1270 void id_clear_lib_data(Main *bmain, ID *id)
1271 {
1272         BKE_id_lib_local_paths(bmain, id);
1273
1274         id->lib= NULL;
1275         id->flag= LIB_LOCAL;
1276         new_id(which_libbase(bmain, GS(id->name)), id, NULL);
1277 }
1278
1279 /* next to indirect usage in read/writefile also in editobject.c scene.c */
1280 void clear_id_newpoins(void)
1281 {
1282         ListBase *lbarray[MAX_LIBARRAY];
1283         ID *id;
1284         int a;
1285
1286         a= set_listbasepointers(G.main, lbarray);
1287         while(a--) {
1288                 id= lbarray[a]->first;
1289                 while(id) {
1290                         id->newid= NULL;
1291                         id->flag &= ~LIB_NEW;
1292                         id= id->next;
1293                 }
1294         }
1295 }
1296
1297 #define LIBTAG(a)       if(a && a->id.lib) {a->id.flag &=~LIB_INDIRECT; a->id.flag |= LIB_EXTERN;}
1298
1299 static void lib_indirect_test_id(ID *id, Library *lib)
1300 {
1301         
1302         if(id->lib) {
1303                 /* datablocks that were indirectly related are now direct links
1304                  * without this, appending data that has a link to other data will fail to write */
1305                 if(lib && id->lib->parent == lib) {
1306                         id_lib_extern(id);
1307                 }
1308                 return;
1309         }
1310         
1311         if(GS(id->name)==ID_OB) {               
1312                 Object *ob= (Object *)id;
1313                 bActionStrip *strip;
1314                 Mesh *me;
1315
1316                 int a;
1317         
1318                 // XXX old animation system! --------------------------------------
1319                 for (strip=ob->nlastrips.first; strip; strip=strip->next){
1320                         LIBTAG(strip->object); 
1321                         LIBTAG(strip->act);
1322                         LIBTAG(strip->ipo);
1323                 }
1324                 // XXX: new animation system needs something like this?
1325         
1326                 for(a=0; a<ob->totcol; a++) {
1327                         LIBTAG(ob->mat[a]);
1328                 }
1329         
1330                 LIBTAG(ob->dup_group);
1331                 LIBTAG(ob->proxy);
1332                 
1333                 me= ob->data;
1334                 LIBTAG(me);
1335         }
1336 }
1337
1338 void tag_main_lb(ListBase *lb, const short tag)
1339 {
1340         ID *id;
1341         if(tag) {
1342                 for(id= lb->first; id; id= id->next) {
1343                         id->flag |= LIB_DOIT;
1344                 }
1345         }
1346         else {
1347                 for(id= lb->first; id; id= id->next) {
1348                         id->flag &= ~LIB_DOIT;
1349                 }
1350         }
1351 }
1352
1353 void tag_main_idcode(struct Main *mainvar, const short type, const short tag)
1354 {
1355         ListBase *lb= which_libbase(mainvar, type);
1356
1357         tag_main_lb(lb, tag);
1358 }
1359
1360 void tag_main(struct Main *mainvar, const short tag)
1361 {
1362         ListBase *lbarray[MAX_LIBARRAY];
1363         int a;
1364
1365         a= set_listbasepointers(mainvar, lbarray);
1366         while(a--) {
1367                 tag_main_lb(lbarray[a], tag);
1368         }
1369 }
1370
1371 /* if lib!=NULL, only all from lib local
1372  * bmain is almost certainly G.main */
1373 void BKE_library_make_local(Main *bmain, Library *lib, int untagged_only)
1374 {
1375         ListBase *lbarray[MAX_LIBARRAY], tempbase={NULL, NULL};
1376         ID *id, *idn;
1377         int a;
1378
1379         a= set_listbasepointers(bmain, lbarray);
1380         while(a--) {
1381                 id= lbarray[a]->first;
1382                 
1383                 while(id) {
1384                         id->newid= NULL;
1385                         idn= id->next;          /* id is possibly being inserted again */
1386                         
1387                         /* The check on the second line (LIB_PRE_EXISTING) is done so its
1388                          * possible to tag data you dont want to be made local, used for
1389                          * appending data, so any libdata already linked wont become local
1390                          * (very nasty to discover all your links are lost after appending)  
1391                          * */
1392                         if(id->flag & (LIB_EXTERN|LIB_INDIRECT|LIB_NEW) &&
1393                           (untagged_only==0 || !(id->flag & LIB_PRE_EXISTING)))
1394                         {
1395                                 if(lib==NULL || id->lib==lib) {
1396                                         if(id->lib) {
1397                                                 id_clear_lib_data(bmain, id); /* sets 'id->flag' */
1398
1399                                                 /* why sort alphabetically here but not in
1400                                                  * id_clear_lib_data() ? - campbell */
1401                                                 sort_alpha_id(lbarray[a], id);
1402                                         }
1403                                         else {
1404                                                 id->flag &= ~(LIB_EXTERN|LIB_INDIRECT|LIB_NEW);
1405                                         }
1406                                 }
1407                         }
1408                         id= idn;
1409                 }
1410                 
1411                 /* patch2: make it aphabetically */
1412                 while( (id=tempbase.first) ) {
1413                         BLI_remlink(&tempbase, id);
1414                         BLI_addtail(lbarray[a], id);
1415                         new_id(lbarray[a], id, NULL);
1416                 }
1417         }
1418
1419         /* patch 3: make sure library data isn't indirect falsely... */
1420         a= set_listbasepointers(bmain, lbarray);
1421         while(a--) {
1422                 for(id= lbarray[a]->first; id; id=id->next)
1423                         lib_indirect_test_id(id, lib);
1424         }
1425 }
1426
1427
1428 void test_idbutton(char *name)
1429 {
1430         /* called from buttons: when name already exists: call new_id */
1431         ListBase *lb;
1432         ID *idtest;
1433         
1434
1435         lb= which_libbase(G.main, GS(name-2) );
1436         if(lb==NULL) return;
1437         
1438         /* search for id */
1439         idtest= BLI_findstring(lb, name, offsetof(ID, name) + 2);
1440
1441         if(idtest) if( new_id(lb, idtest, name)==0 ) sort_alpha_id(lb, idtest);
1442 }
1443
1444 void text_idbutton(struct ID *id, char *text)
1445 {
1446         if(id) {
1447                 if(GS(id->name)==ID_SCE)
1448                         strcpy(text, "SCE: ");
1449                 else if(GS(id->name)==ID_SCR)
1450                         strcpy(text, "SCR: ");
1451                 else if(GS(id->name)==ID_MA && ((Material*)id)->use_nodes)
1452                         strcpy(text, "NT: ");
1453                 else {
1454                         text[0]= id->name[0];
1455                         text[1]= id->name[1];
1456                         text[2]= ':';
1457                         text[3]= ' ';
1458                         text[4]= 0;
1459                 }
1460         }
1461         else {
1462                 text[0]= '\0';
1463         }
1464 }
1465
1466 void rename_id(ID *id, const char *name)
1467 {
1468         ListBase *lb;
1469
1470         BLI_strncpy(id->name+2, name, sizeof(id->name)-2);
1471         lb= which_libbase(G.main, GS(id->name) );
1472         
1473         new_id(lb, id, name);                           
1474 }
1475
1476 void name_uiprefix_id(char *name, ID *id)
1477 {
1478         name[0] = id->lib ? 'L':' ';
1479         name[1] = id->flag & LIB_FAKEUSER ? 'F': (id->us==0)?'0':' ';
1480         name[2] = ' ';
1481
1482         strcpy(name+3, id->name+2);
1483 }
1484
1485 void BKE_library_filepath_set(Library *lib, const char *filepath)
1486 {
1487         /* in some cases this is used to update the absolute path from the
1488          * relative */
1489         if (lib->name != filepath) {
1490                 BLI_strncpy(lib->name, filepath, sizeof(lib->name));
1491         }
1492
1493         BLI_strncpy(lib->filepath, filepath, sizeof(lib->filepath));
1494
1495         /* not essential but set filepath is an absolute copy of value which
1496          * is more useful if its kept in sync */
1497         if (strncmp(lib->filepath, "//", 2) == 0) {
1498                 /* note that the file may be unsaved, in this case, setting the
1499                  * filepath on an indirectly linked path is not allowed from the
1500                  * outliner, and its not really supported but allow from here for now
1501                  * since making local could cause this to be directly linked - campbell
1502                  */
1503                 const char *basepath= lib->parent ? lib->parent->filepath : G.main->name;
1504                 BLI_path_abs(lib->filepath, basepath);
1505         }
1506 }