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