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