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