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