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