Some options used for "expert" x264 setup were removed from FFmpeg 0.11
[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         }
364         
365         return 0;
366 }
367
368 int id_unlink(ID *id, int test)
369 {
370         Main *mainlib = G.main;
371         ListBase *lb;
372
373         switch (GS(id->name)) {
374                 case ID_TXT:
375                         if (test) return 1;
376                         BKE_text_unlink(mainlib, (Text *)id);
377                         break;
378                 case ID_GR:
379                         if (test) return 1;
380                         BKE_group_unlink((Group *)id);
381                         break;
382                 case ID_OB:
383                         if (test) return 1;
384                         BKE_object_unlink((Object *)id);
385                         break;
386         }
387
388         if (id->us == 0) {
389                 if (test) return 1;
390
391                 lb = which_libbase(mainlib, GS(id->name));
392                 BKE_libblock_free(lb, id);
393
394                 return 1;
395         }
396
397         return 0;
398 }
399
400 int id_single_user(bContext *C, ID *id, PointerRNA *ptr, PropertyRNA *prop)
401 {
402         ID *newid = NULL;
403         PointerRNA idptr;
404         
405         if (id) {
406                 /* if property isn't editable, we're going to have an extra block hanging around until we save */
407                 if (RNA_property_editable(ptr, prop)) {
408                         if (id_copy(id, &newid, 0) && newid) {
409                                 /* copy animation actions too */
410                                 BKE_copy_animdata_id_action(id);
411                                 /* us is 1 by convention, but RNA_property_pointer_set
412                                  * will also increment it, so set it to zero */
413                                 newid->us = 0;
414                                 
415                                 /* assign copy */
416                                 RNA_id_pointer_create(newid, &idptr);
417                                 RNA_property_pointer_set(ptr, prop, idptr);
418                                 RNA_property_update(C, ptr, prop);
419                                 
420                                 return 1;
421                         }
422                 }
423         }
424         
425         return 0;
426 }
427
428 ListBase *which_libbase(Main *mainlib, short type)
429 {
430         switch (type) {
431                 case ID_SCE:
432                         return &(mainlib->scene);
433                 case ID_LI:
434                         return &(mainlib->library);
435                 case ID_OB:
436                         return &(mainlib->object);
437                 case ID_ME:
438                         return &(mainlib->mesh);
439                 case ID_CU:
440                         return &(mainlib->curve);
441                 case ID_MB:
442                         return &(mainlib->mball);
443                 case ID_MA:
444                         return &(mainlib->mat);
445                 case ID_TE:
446                         return &(mainlib->tex);
447                 case ID_IM:
448                         return &(mainlib->image);
449                 case ID_LT:
450                         return &(mainlib->latt);
451                 case ID_LA:
452                         return &(mainlib->lamp);
453                 case ID_CA:
454                         return &(mainlib->camera);
455                 case ID_IP:
456                         return &(mainlib->ipo);
457                 case ID_KE:
458                         return &(mainlib->key);
459                 case ID_WO:
460                         return &(mainlib->world);
461                 case ID_SCR:
462                         return &(mainlib->screen);
463                 case ID_VF:
464                         return &(mainlib->vfont);
465                 case ID_TXT:
466                         return &(mainlib->text);
467                 case ID_SCRIPT:
468                         return &(mainlib->script);
469                 case ID_SPK:
470                         return &(mainlib->speaker);
471                 case ID_SO:
472                         return &(mainlib->sound);
473                 case ID_GR:
474                         return &(mainlib->group);
475                 case ID_AR:
476                         return &(mainlib->armature);
477                 case ID_AC:
478                         return &(mainlib->action);
479                 case ID_NT:
480                         return &(mainlib->nodetree);
481                 case ID_BR:
482                         return &(mainlib->brush);
483                 case ID_PA:
484                         return &(mainlib->particle);
485                 case ID_WM:
486                         return &(mainlib->wm);
487                 case ID_GD:
488                         return &(mainlib->gpencil);
489                 case ID_MC:
490                         return &(mainlib->movieclip);
491                 case ID_MSK:
492                         return &(mainlib->mask);
493         }
494         return NULL;
495 }
496
497 /* Flag all ids in listbase */
498 void flag_listbase_ids(ListBase *lb, short flag, short value)
499 {
500         ID *id;
501         if (value) {
502                 for (id = lb->first; id; id = id->next) id->flag |= flag;
503         }
504         else {
505                 flag = ~flag;
506                 for (id = lb->first; id; id = id->next) id->flag &= flag;
507         }
508 }
509
510 /* Flag all ids in listbase */
511 void flag_all_listbases_ids(short flag, short value)
512 {
513         ListBase *lbarray[MAX_LIBARRAY];
514         int a;
515         a = set_listbasepointers(G.main, lbarray);
516         while (a--) flag_listbase_ids(lbarray[a], flag, value);
517 }
518
519 void recalc_all_library_objects(Main *main)
520 {
521         Object *ob;
522
523         /* flag for full recalc */
524         for (ob = main->object.first; ob; ob = ob->id.next)
525                 if (ob->id.lib)
526                         ob->recalc |= OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME;
527 }
528
529 /* note: MAX_LIBARRAY define should match this code */
530 int set_listbasepointers(Main *main, ListBase **lb)
531 {
532         int a = 0;
533
534         /* BACKWARDS! also watch order of free-ing! (mesh<->mat), first items freed last.
535          * This is important because freeing data decreases usercounts of other datablocks,
536          * if this data is its self freed it can crash. */
537         lb[a++] = &(main->ipo);
538         lb[a++] = &(main->action); // xxx moved here to avoid problems when freeing with animato (aligorith)
539         lb[a++] = &(main->key);
540         lb[a++] = &(main->gpencil); /* referenced by nodes, objects, view, scene etc, before to free after. */
541         lb[a++] = &(main->nodetree);
542         lb[a++] = &(main->image);
543         lb[a++] = &(main->tex);
544         lb[a++] = &(main->mat);
545         lb[a++] = &(main->vfont);
546         
547         /* Important!: When adding a new object type,
548          * the specific data should be inserted here 
549          */
550
551         lb[a++] = &(main->armature);
552
553         lb[a++] = &(main->mesh);
554         lb[a++] = &(main->curve);
555         lb[a++] = &(main->mball);
556
557         lb[a++] = &(main->latt);
558         lb[a++] = &(main->lamp);
559         lb[a++] = &(main->camera);
560
561         lb[a++] = &(main->text);
562         lb[a++] = &(main->sound);
563         lb[a++] = &(main->group);
564         lb[a++] = &(main->brush);
565         lb[a++] = &(main->script);
566         lb[a++] = &(main->particle);
567         lb[a++] = &(main->speaker);
568
569         lb[a++] = &(main->world);
570         lb[a++] = &(main->screen);
571         lb[a++] = &(main->object);
572         lb[a++] = &(main->scene);
573         lb[a++] = &(main->library);
574         lb[a++] = &(main->wm);
575         lb[a++] = &(main->movieclip);
576         lb[a++] = &(main->mask);
577         
578         lb[a] = NULL;
579
580         return a;
581 }
582
583 /* *********** ALLOC AND FREE *****************
584  *
585  * BKE_libblock_free(ListBase *lb, ID *id )
586  * provide a list-basis and datablock, but only ID is read
587  *
588  * void *BKE_libblock_alloc(ListBase *lb, type, name)
589  * inserts in list and returns a new ID
590  *
591  * **************************** */
592
593 static ID *alloc_libblock_notest(short type)
594 {
595         ID *id = NULL;
596         
597         switch (type) {
598                 case ID_SCE:
599                         id = MEM_callocN(sizeof(Scene), "scene");
600                         break;
601                 case ID_LI:
602                         id = MEM_callocN(sizeof(Library), "library");
603                         break;
604                 case ID_OB:
605                         id = MEM_callocN(sizeof(Object), "object");
606                         break;
607                 case ID_ME:
608                         id = MEM_callocN(sizeof(Mesh), "mesh");
609                         break;
610                 case ID_CU:
611                         id = MEM_callocN(sizeof(Curve), "curve");
612                         break;
613                 case ID_MB:
614                         id = MEM_callocN(sizeof(MetaBall), "mball");
615                         break;
616                 case ID_MA:
617                         id = MEM_callocN(sizeof(Material), "mat");
618                         break;
619                 case ID_TE:
620                         id = MEM_callocN(sizeof(Tex), "tex");
621                         break;
622                 case ID_IM:
623                         id = MEM_callocN(sizeof(Image), "image");
624                         break;
625                 case ID_LT:
626                         id = MEM_callocN(sizeof(Lattice), "latt");
627                         break;
628                 case ID_LA:
629                         id = MEM_callocN(sizeof(Lamp), "lamp");
630                         break;
631                 case ID_CA:
632                         id = MEM_callocN(sizeof(Camera), "camera");
633                         break;
634                 case ID_IP:
635                         id = MEM_callocN(sizeof(Ipo), "ipo");
636                         break;
637                 case ID_KE:
638                         id = MEM_callocN(sizeof(Key), "key");
639                         break;
640                 case ID_WO:
641                         id = MEM_callocN(sizeof(World), "world");
642                         break;
643                 case ID_SCR:
644                         id = MEM_callocN(sizeof(bScreen), "screen");
645                         break;
646                 case ID_VF:
647                         id = MEM_callocN(sizeof(VFont), "vfont");
648                         break;
649                 case ID_TXT:
650                         id = MEM_callocN(sizeof(Text), "text");
651                         break;
652                 case ID_SCRIPT:
653                         //XXX id = MEM_callocN(sizeof(Script), "script");
654                         break;
655                 case ID_SPK:
656                         id = MEM_callocN(sizeof(Speaker), "speaker");
657                         break;
658                 case ID_SO:
659                         id = MEM_callocN(sizeof(bSound), "sound");
660                         break;
661                 case ID_GR:
662                         id = MEM_callocN(sizeof(Group), "group");
663                         break;
664                 case ID_AR:
665                         id = MEM_callocN(sizeof(bArmature), "armature");
666                         break;
667                 case ID_AC:
668                         id = MEM_callocN(sizeof(bAction), "action");
669                         break;
670                 case ID_NT:
671                         id = MEM_callocN(sizeof(bNodeTree), "nodetree");
672                         break;
673                 case ID_BR:
674                         id = MEM_callocN(sizeof(Brush), "brush");
675                         break;
676                 case ID_PA:
677                         id = MEM_callocN(sizeof(ParticleSettings), "ParticleSettings");
678                         break;
679                 case ID_WM:
680                         id = MEM_callocN(sizeof(wmWindowManager), "Window manager");
681                         break;
682                 case ID_GD:
683                         id = MEM_callocN(sizeof(bGPdata), "Grease Pencil");
684                         break;
685                 case ID_MC:
686                         id = MEM_callocN(sizeof(MovieClip), "Movie Clip");
687                         break;
688                 case ID_MSK:
689                         id = MEM_callocN(sizeof(Mask), "Mask");
690                         break;
691         }
692         return id;
693 }
694
695 /* used everywhere in blenkernel and text.c */
696 void *BKE_libblock_alloc(ListBase *lb, short type, const char *name)
697 {
698         ID *id = NULL;
699         
700         id = alloc_libblock_notest(type);
701         if (id) {
702                 BLI_addtail(lb, id);
703                 id->us = 1;
704                 id->icon_id = 0;
705                 *( (short *)id->name) = type;
706                 new_id(lb, id, name);
707                 /* alphabetic insterion: is in new_id */
708         }
709         return id;
710 }
711
712 /* by spec, animdata is first item after ID */
713 /* and, trust that BKE_animdata_from_id() will only find AnimData for valid ID-types */
714 static void id_copy_animdata(ID *id, const short do_action)
715 {
716         AnimData *adt = BKE_animdata_from_id(id);
717         
718         if (adt) {
719                 IdAdtTemplate *iat = (IdAdtTemplate *)id;
720                 iat->adt = BKE_copy_animdata(iat->adt, do_action); /* could be set to FALSE, need to investigate */
721         }
722 }
723
724 /* material nodes use this since they are not treated as libdata */
725 void BKE_libblock_copy_data(ID *id, const ID *id_from, const short do_action)
726 {
727         if (id_from->properties)
728                 id->properties = IDP_CopyProperty(id_from->properties);
729
730         /* the duplicate should get a copy of the animdata */
731         id_copy_animdata(id, do_action);
732 }
733
734 /* used everywhere in blenkernel */
735 void *BKE_libblock_copy(ID *id)
736 {
737         ID *idn;
738         ListBase *lb;
739         size_t idn_len;
740
741         lb = which_libbase(G.main, GS(id->name));
742         idn = BKE_libblock_alloc(lb, GS(id->name), id->name + 2);
743
744         assert(idn != NULL);
745
746         idn_len = MEM_allocN_len(idn);
747         if ((int)idn_len - (int)sizeof(ID) > 0) { /* signed to allow neg result */
748                 const char *cp = (const char *)id;
749                 char *cpn = (char *)idn;
750
751                 memcpy(cpn + sizeof(ID), cp + sizeof(ID), idn_len - sizeof(ID));
752         }
753         
754         id->newid = idn;
755         idn->flag |= LIB_NEW;
756
757         BKE_libblock_copy_data(idn, id, FALSE);
758         
759         return idn;
760 }
761
762 static void BKE_library_free(Library *UNUSED(lib))
763 {
764         /* no freeing needed for libraries yet */
765 }
766
767 static void (*free_windowmanager_cb)(bContext *, wmWindowManager *) = NULL;
768
769 void set_free_windowmanager_cb(void (*func)(bContext *C, wmWindowManager *) )
770 {
771         free_windowmanager_cb = func;
772 }
773
774 static void animdata_dtar_clear_cb(ID *UNUSED(id), AnimData *adt, void *userdata)
775 {
776         ChannelDriver *driver;
777         FCurve *fcu;
778
779         /* find the driver this belongs to and update it */
780         for (fcu = adt->drivers.first; fcu; fcu = fcu->next) {
781                 driver = fcu->driver;
782                 
783                 if (driver) {
784                         DriverVar *dvar;
785                         for (dvar = driver->variables.first; dvar; dvar = dvar->next) {
786                                 DRIVER_TARGETS_USED_LOOPER(dvar) 
787                                 {
788                                         if (dtar->id == userdata)
789                                                 dtar->id = NULL;
790                                 }
791                                 DRIVER_TARGETS_LOOPER_END
792                         }
793                 }
794         }
795 }
796
797
798 /* used in headerbuttons.c image.c mesh.c screen.c sound.c and library.c */
799 void BKE_libblock_free(ListBase *lb, void *idv)
800 {
801         ID *id = idv;
802
803 #ifdef WITH_PYTHON
804         BPY_id_release(id);
805 #endif
806
807         switch (GS(id->name) ) {    /* GetShort from util.h */
808                 case ID_SCE:
809                         BKE_scene_free((Scene *)id);
810                         break;
811                 case ID_LI:
812                         BKE_library_free((Library *)id);
813                         break;
814                 case ID_OB:
815                         BKE_object_free((Object *)id);
816                         break;
817                 case ID_ME:
818                         BKE_mesh_free((Mesh *)id, 1);
819                         break;
820                 case ID_CU:
821                         BKE_curve_free((Curve *)id);
822                         break;
823                 case ID_MB:
824                         BKE_mball_free((MetaBall *)id);
825                         break;
826                 case ID_MA:
827                         BKE_material_free((Material *)id);
828                         break;
829                 case ID_TE:
830                         BKE_texture_free((Tex *)id);
831                         break;
832                 case ID_IM:
833                         BKE_image_free((Image *)id);
834                         break;
835                 case ID_LT:
836                         BKE_lattice_free((Lattice *)id);
837                         break;
838                 case ID_LA:
839                         BKE_lamp_free((Lamp *)id);
840                         break;
841                 case ID_CA:
842                         BKE_camera_free((Camera *) id);
843                         break;
844                 case ID_IP:
845                         BKE_ipo_free((Ipo *)id);
846                         break;
847                 case ID_KE:
848                         BKE_key_free((Key *)id);
849                         break;
850                 case ID_WO:
851                         BKE_world_free((World *)id);
852                         break;
853                 case ID_SCR:
854                         BKE_screen_free((bScreen *)id);
855                         break;
856                 case ID_VF:
857                         BKE_vfont_free((VFont *)id);
858                         break;
859                 case ID_TXT:
860                         BKE_text_free((Text *)id);
861                         break;
862                 case ID_SCRIPT:
863                         //XXX free_script((Script *)id);
864                         break;
865                 case ID_SPK:
866                         BKE_speaker_free((Speaker *)id);
867                         break;
868                 case ID_SO:
869                         BKE_sound_free((bSound *)id);
870                         break;
871                 case ID_GR:
872                         BKE_group_free((Group *)id);
873                         break;
874                 case ID_AR:
875                         BKE_armature_free((bArmature *)id);
876                         break;
877                 case ID_AC:
878                         BKE_action_free((bAction *)id);
879                         break;
880                 case ID_NT:
881                         ntreeFreeTree((bNodeTree *)id);
882                         break;
883                 case ID_BR:
884                         BKE_brush_free((Brush *)id);
885                         break;
886                 case ID_PA:
887                         BKE_particlesettings_free((ParticleSettings *)id);
888                         break;
889                 case ID_WM:
890                         if (free_windowmanager_cb)
891                                 free_windowmanager_cb(NULL, (wmWindowManager *)id);
892                         break;
893                 case ID_GD:
894                         BKE_gpencil_free((bGPdata *)id);
895                         break;
896                 case ID_MC:
897                         BKE_movieclip_free((MovieClip *)id);
898                         break;
899                 case ID_MSK:
900                         BKE_mask_free((Mask *)id);
901                         break;
902         }
903
904         if (id->properties) {
905                 IDP_FreeProperty(id->properties);
906                 MEM_freeN(id->properties);
907         }
908
909         BLI_remlink(lb, id);
910
911         /* this ID may be a driver target! */
912         BKE_animdata_main_cb(G.main, animdata_dtar_clear_cb, (void *)id);
913
914         MEM_freeN(id);
915 }
916
917 void BKE_libblock_free_us(ListBase *lb, void *idv)      /* test users */
918 {
919         ID *id = idv;
920         
921         id->us--;
922
923         if (id->us < 0) {
924                 if (id->lib) printf("ERROR block %s %s users %d\n", id->lib->name, id->name, id->us);
925                 else printf("ERROR block %s users %d\n", id->name, id->us);
926         }
927         if (id->us == 0) {
928                 if (GS(id->name) == ID_OB) BKE_object_unlink((Object *)id);
929                 
930                 BKE_libblock_free(lb, id);
931         }
932 }
933
934
935 void free_main(Main *mainvar)
936 {
937         /* also call when reading a file, erase all, etc */
938         ListBase *lbarray[MAX_LIBARRAY];
939         int a;
940
941         a = set_listbasepointers(mainvar, lbarray);
942         while (a--) {
943                 ListBase *lb = lbarray[a];
944                 ID *id;
945                 
946                 while ( (id = lb->first) ) {
947                         BKE_libblock_free(lb, id);
948                 }
949         }
950
951         MEM_freeN(mainvar);
952 }
953
954 /* ***************** ID ************************ */
955
956
957 ID *BKE_libblock_find_name(const short type, const char *name)      /* type: "OB" or "MA" etc */
958 {
959         ListBase *lb = which_libbase(G.main, type);
960         BLI_assert(lb != NULL);
961         return BLI_findstring(lb, name, offsetof(ID, name) + 2);
962 }
963
964 static void get_flags_for_id(ID *id, char *buf) 
965 {
966         int isfake = id->flag & LIB_FAKEUSER;
967         int isnode = 0;
968         /* Writeout the flags for the entry, note there
969          * is a small hack that writes 5 spaces instead
970          * of 4 if no flags are displayed... this makes
971          * things usually line up ok - better would be
972          * to have that explicit, oh well - zr
973          */
974
975         if (GS(id->name) == ID_MA)
976                 isnode = ((Material *)id)->use_nodes;
977         if (GS(id->name) == ID_TE)
978                 isnode = ((Tex *)id)->use_nodes;
979         
980         if (id->us < 0)
981                 strcpy(buf, "-1W ");
982         else if (!id->lib && !isfake && id->us && !isnode)
983                 strcpy(buf, "     ");
984         else if (isnode)
985                 sprintf(buf, "%c%cN%c ", id->lib ? 'L' : ' ', isfake ? 'F' : ' ', (id->us == 0) ? 'O' : ' ');
986         else
987                 sprintf(buf, "%c%c%c ", id->lib ? 'L' : ' ', isfake ? 'F' : ' ', (id->us == 0) ? 'O' : ' ');
988 }
989
990 #define IDPUP_NO_VIEWER 1
991
992 static void IDnames_to_dyn_pupstring(DynStr *pupds, ListBase *lb, ID *link, short *nr, int hideflag)
993 {
994         int i, nids = BLI_countlist(lb);
995                 
996         if (nr) *nr = -1;
997         
998         if (nr && nids > MAX_IDPUP) {
999                 BLI_dynstr_append(pupds, "DataBrowse %x-2");
1000                 *nr = -2;
1001         }
1002         else {
1003                 ID *id;
1004                 
1005                 for (i = 0, id = lb->first; id; id = id->next, i++) {
1006                         char numstr[32];
1007                         
1008                         if (nr && id == link) *nr = i + 1;
1009
1010                         if (U.uiflag & USER_HIDE_DOT && id->name[2] == '.')
1011                                 continue;
1012                         if (hideflag & IDPUP_NO_VIEWER)
1013                                 if (GS(id->name) == ID_IM)
1014                                         if ( ((Image *)id)->source == IMA_SRC_VIEWER)
1015                                                 continue;
1016                         
1017                         get_flags_for_id(id, numstr);
1018                                 
1019                         BLI_dynstr_append(pupds, numstr);
1020                         BLI_dynstr_append(pupds, id->name + 2);
1021                         BLI_snprintf(numstr, sizeof(numstr), "%%x%d", i + 1);
1022                         BLI_dynstr_append(pupds, numstr);
1023                         
1024                         /* icon */
1025                         switch (GS(id->name)) {
1026                                 case ID_MA: /* fall through */
1027                                 case ID_TE: /* fall through */
1028                                 case ID_IM: /* fall through */
1029                                 case ID_WO: /* fall through */
1030                                 case ID_LA: /* fall through */
1031                                         BLI_snprintf(numstr, sizeof(numstr), "%%i%d", BKE_icon_getid(id));
1032                                         BLI_dynstr_append(pupds, numstr);
1033                                         break;
1034                                 default:
1035                                         break;
1036                         }
1037                         
1038                         if (id->next)
1039                                 BLI_dynstr_append(pupds, "|");
1040                 }
1041         }
1042 }
1043
1044
1045 /* used by headerbuttons.c buttons.c editobject.c editseq.c */
1046 /* if (nr == NULL) no MAX_IDPUP, this for non-header browsing */
1047 void IDnames_to_pupstring(const char **str, const char *title, const char *extraops, ListBase *lb, ID *link, short *nr)
1048 {
1049         DynStr *pupds = BLI_dynstr_new();
1050
1051         if (title) {
1052                 BLI_dynstr_append(pupds, title);
1053                 BLI_dynstr_append(pupds, "%t|");
1054         }
1055         
1056         if (extraops) {
1057                 BLI_dynstr_append(pupds, extraops);
1058                 if (BLI_dynstr_get_len(pupds))
1059                         BLI_dynstr_append(pupds, "|");
1060         }
1061
1062         IDnames_to_dyn_pupstring(pupds, lb, link, nr, 0);
1063         
1064         *str = BLI_dynstr_get_cstring(pupds);
1065         BLI_dynstr_free(pupds);
1066 }
1067
1068 /* skips viewer images */
1069 #if 0 /* unused */
1070 void IMAnames_to_pupstring(const char **str, const char *title, const char *extraops, ListBase *lb, ID *link, short *nr)
1071 {
1072         DynStr *pupds = BLI_dynstr_new();
1073         
1074         if (title) {
1075                 BLI_dynstr_append(pupds, title);
1076                 BLI_dynstr_append(pupds, "%t|");
1077         }
1078         
1079         if (extraops) {
1080                 BLI_dynstr_append(pupds, extraops);
1081                 if (BLI_dynstr_get_len(pupds))
1082                         BLI_dynstr_append(pupds, "|");
1083         }
1084         
1085         IDnames_to_dyn_pupstring(pupds, lb, link, nr, IDPUP_NO_VIEWER);
1086         
1087         *str = BLI_dynstr_get_cstring(pupds);
1088         BLI_dynstr_free(pupds);
1089 }
1090 #endif
1091
1092 void id_sort_by_name(ListBase *lb, ID *id)
1093 {
1094         ID *idtest;
1095         
1096         /* insert alphabetically */
1097         if (lb->first != lb->last) {
1098                 BLI_remlink(lb, id);
1099                 
1100                 idtest = lb->first;
1101                 while (idtest) {
1102                         if (BLI_strcasecmp(idtest->name, id->name) > 0 || (idtest->lib && !id->lib)) {
1103                                 BLI_insertlinkbefore(lb, idtest, id);
1104                                 break;
1105                         }
1106                         idtest = idtest->next;
1107                 }
1108                 /* as last */
1109                 if (idtest == NULL) {
1110                         BLI_addtail(lb, id);
1111                 }
1112         }
1113         
1114 }
1115
1116 /*
1117  * Check to see if there is an ID with the same name as 'name'.
1118  * Returns the ID if so, if not, returns NULL
1119  */
1120 static ID *is_dupid(ListBase *lb, ID *id, const char *name)
1121 {
1122         ID *idtest = NULL;
1123         
1124         for (idtest = lb->first; idtest; idtest = idtest->next) {
1125                 /* if idtest is not a lib */ 
1126                 if (id != idtest && idtest->lib == NULL) {
1127                         /* do not test alphabetic! */
1128                         /* optimized */
1129                         if (idtest->name[2] == name[0]) {
1130                                 if (strcmp(name, idtest->name + 2) == 0) break;
1131                         }
1132                 }
1133         }
1134         
1135         return idtest;
1136 }
1137
1138 /* 
1139  * Check to see if an ID name is already used, and find a new one if so.
1140  * Return 1 if created a new name (returned in name).
1141  *
1142  * Normally the ID that's being check is already in the ListBase, so ID *id
1143  * points at the new entry.  The Python Library module needs to know what
1144  * the name of a datablock will be before it is appended; in this case ID *id
1145  * id is NULL
1146  */
1147
1148 static int check_for_dupid(ListBase *lb, ID *id, char *name)
1149 {
1150         ID *idtest;
1151         int nr = 0, nrtest, a, left_len;
1152         char in_use[64]; /* use as a boolean array, unrelated to name length */
1153
1154         char left[MAX_ID_NAME + 8], leftest[MAX_ID_NAME + 8];
1155
1156         /* make sure input name is terminated properly */
1157         /* if ( strlen(name) > MAX_ID_NAME-3 ) name[MAX_ID_NAME-3]= 0; */
1158         /* removed since this is only ever called from one place - campbell */
1159
1160         while (1) {
1161
1162                 /* phase 1: id already exists? */
1163                 idtest = is_dupid(lb, id, name);
1164
1165                 /* if there is no double, done */
1166                 if (idtest == NULL) return 0;
1167
1168                 /* we have a dup; need to make a new name */
1169                 /* quick check so we can reuse one of first 64 ids if vacant */
1170                 memset(in_use, 0, sizeof(in_use));
1171
1172                 /* get name portion, number portion ("name.number") */
1173                 left_len = BLI_split_name_num(left, &nr, name, '.');
1174
1175                 /* if new name will be too long, truncate it */
1176                 if (nr > 999 && left_len > (MAX_ID_NAME - 8)) {
1177                         left[MAX_ID_NAME - 8] = 0;
1178                         left_len = MAX_ID_NAME - 8;
1179                 }
1180                 else if (left_len > (MAX_ID_NAME - 7)) {
1181                         left[MAX_ID_NAME - 7] = 0;
1182                         left_len = MAX_ID_NAME - 7;
1183                 }
1184
1185                 for (idtest = lb->first; idtest; idtest = idtest->next) {
1186                         if ( (id != idtest) &&
1187                              (idtest->lib == NULL) &&
1188                              (*name == *(idtest->name + 2)) &&
1189                              (strncmp(name, idtest->name + 2, left_len) == 0) &&
1190                              (BLI_split_name_num(leftest, &nrtest, idtest->name + 2, '.') == left_len)
1191                              )
1192                         {
1193                                 if (nrtest < sizeof(in_use))
1194                                         in_use[nrtest] = 1;  /* mark as used */
1195                                 if (nr <= nrtest)
1196                                         nr = nrtest + 1;    /* track largest unused */
1197                         }
1198                 }
1199
1200                 /* decide which value of nr to use */
1201                 for (a = 0; a < sizeof(in_use); a++) {
1202                         if (a >= nr) break;  /* stop when we've check up to biggest */
1203                         if (in_use[a] == 0) { /* found an unused value */
1204                                 nr = a;
1205                                 break;
1206                         }
1207                 }
1208
1209                 /* If the original name has no numeric suffix, 
1210                  * rather than just chopping and adding numbers, 
1211                  * shave off the end chars until we have a unique name.
1212                  * Check the null terminators match as well so we don't get Cube.000 -> Cube.00 */
1213                 if (nr == 0 && name[left_len] == '\0') {
1214                         int len = left_len - 1;
1215                         idtest = is_dupid(lb, id, name);
1216                         
1217                         while (idtest && len > 1) {
1218                                 name[len--] = '\0';
1219                                 idtest = is_dupid(lb, id, name);
1220                         }
1221                         if (idtest == NULL) return 1;
1222                         /* otherwise just continue and use a number suffix */
1223                 }
1224                 
1225                 if (nr > 999 && left_len > (MAX_ID_NAME - 8)) {
1226                         /* this would overflow name buffer */
1227                         left[MAX_ID_NAME - 8] = 0;
1228                         /* left_len = MAX_ID_NAME - 8; */ /* for now this isn't used again */
1229                         memcpy(name, left, sizeof(char) * (MAX_ID_NAME - 7));
1230                         continue;
1231                 }
1232                 /* this format specifier is from hell... */
1233                 BLI_snprintf(name, sizeof(id->name) - 2, "%s.%.3d", left, nr);
1234
1235                 return 1;
1236         }
1237 }
1238
1239 /*
1240  * Only for local blocks: external en indirect blocks already have a
1241  * unique ID.
1242  *
1243  * return 1: created a new name
1244  */
1245
1246 int new_id(ListBase *lb, ID *id, const char *tname)
1247 {
1248         int result;
1249         char name[MAX_ID_NAME - 2];
1250
1251         /* if library, don't rename */
1252         if (id->lib) return 0;
1253
1254         /* if no libdata given, look up based on ID */
1255         if (lb == NULL) lb = which_libbase(G.main, GS(id->name));
1256
1257         /* if no name given, use name of current ID
1258          * else make a copy (tname args can be const) */
1259         if (tname == NULL)
1260                 tname = id->name + 2;
1261
1262         strncpy(name, tname, sizeof(name) - 1);
1263
1264         /* if result > MAX_ID_NAME-3, strncpy don't put the final '\0' to name.
1265          * easier to assign each time then to check if its needed */
1266         name[sizeof(name) - 1] = 0;
1267
1268         if (name[0] == '\0') {
1269                 /* disallow empty names */
1270                 strcpy(name, ID_FALLBACK_NAME);
1271         }
1272         else {
1273                 /* disallow non utf8 chars,
1274                  * the interface checks for this but new ID's based on file names don't */
1275                 BLI_utf8_invalid_strip(name, strlen(name));
1276         }
1277
1278         result = check_for_dupid(lb, id, name);
1279         strcpy(id->name + 2, name);
1280
1281         /* This was in 2.43 and previous releases
1282          * however all data in blender should be sorted, not just duplicate names
1283          * sorting should not hurt, but noting just incause it alters the way other
1284          * functions work, so sort every time */
1285 #if 0
1286         if (result)
1287                 id_sort_by_name(lb, id);
1288 #endif
1289
1290         id_sort_by_name(lb, id);
1291         
1292         return result;
1293 }
1294
1295 /* Pull an ID out of a library (make it local). Only call this for IDs that
1296  * don't have other library users. */
1297 void id_clear_lib_data(Main *bmain, ID *id)
1298 {
1299         BKE_id_lib_local_paths(bmain, id->lib, id);
1300
1301         id->lib = NULL;
1302         id->flag = LIB_LOCAL;
1303         new_id(which_libbase(bmain, GS(id->name)), id, NULL);
1304 }
1305
1306 /* next to indirect usage in read/writefile also in editobject.c scene.c */
1307 void clear_id_newpoins(void)
1308 {
1309         ListBase *lbarray[MAX_LIBARRAY];
1310         ID *id;
1311         int a;
1312
1313         a = set_listbasepointers(G.main, lbarray);
1314         while (a--) {
1315                 id = lbarray[a]->first;
1316                 while (id) {
1317                         id->newid = NULL;
1318                         id->flag &= ~LIB_NEW;
1319                         id = id->next;
1320                 }
1321         }
1322 }
1323
1324 #define LIBTAG(a)   if (a && a->id.lib) {a->id.flag &= ~LIB_INDIRECT; a->id.flag |= LIB_EXTERN; } (void)0
1325
1326 static void lib_indirect_test_id(ID *id, Library *lib)
1327 {
1328         
1329         if (id->lib) {
1330                 /* datablocks that were indirectly related are now direct links
1331                  * without this, appending data that has a link to other data will fail to write */
1332                 if (lib && id->lib->parent == lib) {
1333                         id_lib_extern(id);
1334                 }
1335                 return;
1336         }
1337         
1338         if (GS(id->name) == ID_OB) {
1339                 Object *ob = (Object *)id;
1340                 Mesh *me;
1341
1342                 int a;
1343
1344 #if 0   /* XXX OLD ANIMSYS, NLASTRIPS ARE NO LONGER USED */
1345                 // XXX old animation system! --------------------------------------
1346                 {
1347                         bActionStrip *strip;
1348                         for (strip = ob->nlastrips.first; strip; strip = strip->next) {
1349                                 LIBTAG(strip->object);
1350                                 LIBTAG(strip->act);
1351                                 LIBTAG(strip->ipo);
1352                         }
1353                 }
1354                 // XXX: new animation system needs something like this?
1355 #endif
1356
1357                 for (a = 0; a < ob->totcol; a++) {
1358                         LIBTAG(ob->mat[a]);
1359                 }
1360         
1361                 LIBTAG(ob->dup_group);
1362                 LIBTAG(ob->proxy);
1363                 
1364                 me = ob->data;
1365                 LIBTAG(me);
1366         }
1367 }
1368
1369 void tag_main_lb(ListBase *lb, const short tag)
1370 {
1371         ID *id;
1372         if (tag) {
1373                 for (id = lb->first; id; id = id->next) {
1374                         id->flag |= LIB_DOIT;
1375                 }
1376         }
1377         else {
1378                 for (id = lb->first; id; id = id->next) {
1379                         id->flag &= ~LIB_DOIT;
1380                 }
1381         }
1382 }
1383
1384 void tag_main_idcode(struct Main *mainvar, const short type, const short tag)
1385 {
1386         ListBase *lb = which_libbase(mainvar, type);
1387
1388         tag_main_lb(lb, tag);
1389 }
1390
1391 void tag_main(struct Main *mainvar, const short tag)
1392 {
1393         ListBase *lbarray[MAX_LIBARRAY];
1394         int a;
1395
1396         a = set_listbasepointers(mainvar, lbarray);
1397         while (a--) {
1398                 tag_main_lb(lbarray[a], tag);
1399         }
1400 }
1401
1402 /* if lib!=NULL, only all from lib local
1403  * bmain is almost certainly G.main */
1404 void BKE_library_make_local(Main *bmain, Library *lib, int untagged_only)
1405 {
1406         ListBase *lbarray[MAX_LIBARRAY], tempbase = {NULL, NULL};
1407         ID *id, *idn;
1408         int a;
1409
1410         a = set_listbasepointers(bmain, lbarray);
1411         while (a--) {
1412                 id = lbarray[a]->first;
1413                 
1414                 while (id) {
1415                         id->newid = NULL;
1416                         idn = id->next;      /* id is possibly being inserted again */
1417                         
1418                         /* The check on the second line (LIB_PRE_EXISTING) is done so its
1419                          * possible to tag data you don't want to be made local, used for
1420                          * appending data, so any libdata already linked wont become local
1421                          * (very nasty to discover all your links are lost after appending)  
1422                          * */
1423                         if (id->flag & (LIB_EXTERN | LIB_INDIRECT | LIB_NEW) &&
1424                             (untagged_only == 0 || !(id->flag & LIB_PRE_EXISTING)))
1425                         {
1426                                 if (lib == NULL || id->lib == lib) {
1427                                         if (id->lib) {
1428                                                 id_clear_lib_data(bmain, id); /* sets 'id->flag' */
1429
1430                                                 /* why sort alphabetically here but not in
1431                                                  * id_clear_lib_data() ? - campbell */
1432                                                 id_sort_by_name(lbarray[a], id);
1433                                         }
1434                                         else {
1435                                                 id->flag &= ~(LIB_EXTERN | LIB_INDIRECT | LIB_NEW);
1436                                         }
1437                                 }
1438                         }
1439                         id = idn;
1440                 }
1441                 
1442                 /* patch2: make it aphabetically */
1443                 while ( (id = tempbase.first) ) {
1444                         BLI_remlink(&tempbase, id);
1445                         BLI_addtail(lbarray[a], id);
1446                         new_id(lbarray[a], id, NULL);
1447                 }
1448         }
1449
1450         /* patch 3: make sure library data isn't indirect falsely... */
1451         a = set_listbasepointers(bmain, lbarray);
1452         while (a--) {
1453                 for (id = lbarray[a]->first; id; id = id->next)
1454                         lib_indirect_test_id(id, lib);
1455         }
1456 }
1457
1458
1459 void test_idbutton(char *name)
1460 {
1461         /* called from buttons: when name already exists: call new_id */
1462         ListBase *lb;
1463         ID *idtest;
1464         
1465
1466         lb = which_libbase(G.main, GS(name - 2) );
1467         if (lb == NULL) return;
1468         
1469         /* search for id */
1470         idtest = BLI_findstring(lb, name, offsetof(ID, name) + 2);
1471
1472         if (idtest) if (new_id(lb, idtest, name) == 0) id_sort_by_name(lb, idtest);
1473 }
1474
1475 void text_idbutton(struct ID *id, char *text)
1476 {
1477         if (id) {
1478                 if (GS(id->name) == ID_SCE)
1479                         strcpy(text, "SCE: ");
1480                 else if (GS(id->name) == ID_SCR)
1481                         strcpy(text, "SCR: ");
1482                 else if (GS(id->name) == ID_MA && ((Material *)id)->use_nodes)
1483                         strcpy(text, "NT: ");
1484                 else {
1485                         text[0] = id->name[0];
1486                         text[1] = id->name[1];
1487                         text[2] = ':';
1488                         text[3] = ' ';
1489                         text[4] = 0;
1490                 }
1491         }
1492         else {
1493                 text[0] = '\0';
1494         }
1495 }
1496
1497 void rename_id(ID *id, const char *name)
1498 {
1499         ListBase *lb;
1500
1501         BLI_strncpy(id->name + 2, name, sizeof(id->name) - 2);
1502         lb = which_libbase(G.main, GS(id->name) );
1503         
1504         new_id(lb, id, name);                           
1505 }
1506
1507 void name_uiprefix_id(char *name, ID *id)
1508 {
1509         name[0] = id->lib ? 'L' : ' ';
1510         name[1] = id->flag & LIB_FAKEUSER ? 'F' : (id->us == 0) ? '0' : ' ';
1511         name[2] = ' ';
1512
1513         strcpy(name + 3, id->name + 2);
1514 }
1515
1516 void BKE_library_filepath_set(Library *lib, const char *filepath)
1517 {
1518         /* in some cases this is used to update the absolute path from the
1519          * relative */
1520         if (lib->name != filepath) {
1521                 BLI_strncpy(lib->name, filepath, sizeof(lib->name));
1522         }
1523
1524         BLI_strncpy(lib->filepath, filepath, sizeof(lib->filepath));
1525
1526         /* not essential but set filepath is an absolute copy of value which
1527          * is more useful if its kept in sync */
1528         if (strncmp(lib->filepath, "//", 2) == 0) {
1529                 /* note that the file may be unsaved, in this case, setting the
1530                  * filepath on an indirectly linked path is not allowed from the
1531                  * outliner, and its not really supported but allow from here for now
1532                  * since making local could cause this to be directly linked - campbell
1533                  */
1534                 const char *basepath = lib->parent ? lib->parent->filepath : G.main->name;
1535                 BLI_path_abs(lib->filepath, basepath);
1536         }
1537 }