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