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