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