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