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