Fixed various errors with Blender allowing editing Library linked data.
[blender.git] / source / blender / src / editobject.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL/BL DUAL 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. The Blender
10  * Foundation also sells licenses for use in proprietary software under
11  * the Blender License.  See http://www.blender.org/BL/ for information
12  * about this.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22  *
23  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
24  * All rights reserved.
25  *
26  * The Original Code is: all of this file.
27  *
28  * Contributor(s): none yet.
29  *
30  * ***** END GPL/BL DUAL LICENSE BLOCK *****
31  */
32
33 /** 
34  * Theorie: (matrices) A x B x C == A x ( B x C x Binv) x B
35  * ofwel: OB x PAR x EDIT = OB x (PAR x EDIT x PARinv) x PAR
36  */
37
38 #include <stdlib.h>
39 #include <string.h>
40 #include <math.h>
41
42 #ifdef HAVE_CONFIG_H
43 #include <config.h>
44 #endif
45
46 #ifndef WIN32
47 #include <unistd.h>
48 #else
49 #include <io.h>
50 #endif
51 #include "MEM_guardedalloc.h"
52 #include "PIL_time.h"
53
54 #include "BMF_Api.h"
55
56
57 #include "IMB_imbuf_types.h"
58
59 #include "DNA_action_types.h"
60 #include "DNA_armature_types.h"
61 #include "DNA_camera_types.h"
62 #include "DNA_constraint_types.h"
63 #include "DNA_curve_types.h"
64 #include "DNA_effect_types.h"
65 #include "DNA_ika_types.h"
66 #include "DNA_image_types.h"
67 #include "DNA_ipo_types.h"
68 #include "DNA_key_types.h"
69 #include "DNA_lamp_types.h"
70 #include "DNA_lattice_types.h"
71 #include "DNA_material_types.h"
72 #include "DNA_mesh_types.h"
73 #include "DNA_meshdata_types.h"
74 #include "DNA_meta_types.h"
75 #include "DNA_object_types.h"
76 #include "DNA_object_force.h"
77 #include "DNA_scene_types.h"
78 #include "DNA_screen_types.h"
79 #include "DNA_texture_types.h"
80 #include "DNA_property_types.h"
81 #include "DNA_userdef_types.h"
82 #include "DNA_view3d_types.h"
83 #include "DNA_vfont_types.h"
84 #include "DNA_world_types.h"
85
86 #include "BLI_blenlib.h"
87 #include "BLI_arithb.h"
88 #include "BLI_editVert.h"
89 #include "BLI_ghash.h"
90
91 #include "BKE_constraint.h"
92 #include "BKE_action.h"
93 #include "BKE_armature.h"
94 #include "BKE_utildefines.h"
95 #include "BKE_anim.h"
96 #include "BKE_blender.h"
97 #include "BKE_booleanops.h"
98 #include "BKE_curve.h"
99 #include "BKE_displist.h"
100 #include "BKE_DerivedMesh.h"
101 #include "BKE_effect.h"
102 #include "BKE_font.h"
103 #include "BKE_global.h"
104 #include "BKE_ika.h"
105 #include "BKE_ipo.h"
106 #include "BKE_key.h"
107 #include "BKE_lattice.h"
108 #include "BKE_library.h"
109 #include "BKE_main.h"
110 #include "BKE_material.h"
111 #include "BKE_mball.h"
112 #include "BKE_mesh.h"
113 #include "BKE_nla.h"
114 #include "BKE_object.h"
115 #include "BKE_property.h"
116 #include "BKE_sca.h"
117 #include "BKE_scene.h"
118 #include "BKE_softbody.h"
119 #include "BKE_subsurf.h"
120 #include "BKE_texture.h"
121 #include "BKE_utildefines.h"
122
123 #include "BIF_gl.h"
124 #include "BIF_graphics.h"
125 #include "BIF_interface.h"
126 #include "BIF_mywindow.h"
127 #include "BIF_toolbox.h"
128 #include "BIF_screen.h"
129 #include "BIF_space.h"
130 #include "BIF_toets.h"
131 #include "BIF_butspace.h"
132 #include "BIF_editconstraint.h"
133 #include "BIF_editdeform.h"
134 #include "BIF_editfont.h"
135 #include "BIF_editika.h"
136 #include "BIF_editlattice.h"
137 #include "BIF_editmesh.h"
138 #include "BIF_editoops.h"
139 #include "BIF_editview.h"
140 #include "BIF_editarmature.h"
141 #include "BIF_resources.h"
142
143 #include "BSE_edit.h"
144 #include "BSE_editaction.h"
145 #include "BSE_editipo.h"
146 #include "BSE_filesel.h"        /* For activate_databrowse() */
147 #include "BSE_view.h"
148 #include "BSE_drawview.h"
149 #include "BSE_trans_types.h"
150 #include "BSE_editipo_types.h"
151
152 #include "BDR_vpaint.h"
153 #include "BDR_editmball.h"
154 #include "BDR_editobject.h"
155 #include "BDR_drawobject.h"
156 #include "BDR_editcurve.h"
157 #include "BDR_unwrapper.h"
158
159 #include <time.h>
160 #include "mydevice.h"
161 #include "nla.h"
162
163 #include "blendef.h"
164
165 #include "BIF_transform.h"
166
167 #include "BIF_poseobject.h"
168
169 /* used in editipo, editcurve and here */
170 #define BEZSELECTED(bezt)   (((bezt)->f1 & 1) || ((bezt)->f2 & 1) || ((bezt)->f3 & 1))
171
172 /* local prototypes -------------*/
173
174 /* --------------------------------- */
175
176 void add_object_draw(int type)  /* for toolbox or menus, only non-editmode stuff */
177 {
178         Object *ob;
179         
180         G.f &= ~(G_VERTEXPAINT+G_FACESELECT+G_TEXTUREPAINT+G_WEIGHTPAINT);
181         setcursor_space(SPACE_VIEW3D, CURSOR_STD);
182
183         if ELEM3(curarea->spacetype, SPACE_VIEW3D, SPACE_BUTS, SPACE_INFO) {
184                 if (G.obedit) exit_editmode(2); // freedata, and undo
185                 ob= add_object(type);
186                 base_init_from_view3d(BASACT, G.vd);
187                 
188                 /* only undo pushes on objects without editmode... */
189                 if(type==OB_EMPTY) BIF_undo_push("Add Empty");
190                 else if(type==OB_LAMP) {
191                         BIF_undo_push("Add Lamp");
192                         if(G.vd->drawtype == OB_SHADED) reshadeall_displist();
193                 }
194                 else if(type==OB_LATTICE) BIF_undo_push("Add Lattice");
195                 else if(type==OB_CAMERA) BIF_undo_push("Add Camera");
196                 
197                 allqueue(REDRAWVIEW3D, 0);
198         }
199
200         redraw_test_buttons(OBACT);
201
202         allqueue(REDRAWALL, 0);
203
204         deselect_all_area_oops();
205         set_select_flag_oops();
206         allqueue(REDRAWINFO, 1);        /* 1, because header->win==0! */
207 }
208
209 void add_objectLamp(short type)
210 {
211         Lamp *la;
212
213         /* this function also comes from an info window */
214         if ELEM(curarea->spacetype, SPACE_VIEW3D, SPACE_INFO); else return;
215         
216         if(G.obedit==0) {
217                 add_object_draw(OB_LAMP);
218                 base_init_from_view3d(BASACT, G.vd);
219         }
220         
221         la = BASACT->object->data;
222         la->type = type;        
223
224         allqueue(REDRAWALL, 0);
225 }
226
227 // really bad, doesnt do constraints, that has been coded in test_scene_constraints();
228 void free_and_unlink_base(Base *base)
229 {
230         if (base==BASACT)
231                 BASACT= NULL;
232         
233         BLI_remlink(&G.scene->base, base);
234         free_libblock_us(&G.main->object, base->object);
235         MEM_freeN(base);
236 }
237
238 void delete_obj(int ok)
239 {
240         Base *base;
241         int islamp= 0;
242         
243         if(G.obpose) return;
244         if(G.obedit) return;
245         if(G.scene->id.lib) return;
246         
247         base= FIRSTBASE;
248         while(base) {
249                 Base *nbase= base->next;
250
251                 if TESTBASE(base) {
252                         if(ok==0 &&  (ok=okee("Erase selected"))==0) return;
253                         if(base->object->type==OB_LAMP) islamp= 1;
254                         
255                         free_and_unlink_base(base);
256                 }
257                 
258                 base= nbase;
259         }
260         countall();
261
262         G.f &= ~(G_VERTEXPAINT+G_FACESELECT+G_TEXTUREPAINT+G_WEIGHTPAINT);
263         setcursor_space(SPACE_VIEW3D, CURSOR_STD);
264         
265         if(islamp && G.vd->drawtype==OB_SHADED) reshadeall_displist();
266
267         test_scene_constraints(); // do because of delete obj
268         
269         allqueue(REDRAWVIEW3D, 0);
270         redraw_test_buttons(OBACT);
271         allqueue (REDRAWACTION, 0);
272         allqueue(REDRAWIPO, 0);
273         allqueue(REDRAWDATASELECT, 0);
274         allqueue(REDRAWOOPS, 0);
275         allqueue(REDRAWACTION, 0);
276         allqueue(REDRAWNLA, 0);
277         
278         BIF_undo_push("Delete object(s)");
279 }
280
281 static int return_editmesh_indexar(int **indexar, float *cent)
282 {
283         EditMesh *em = G.editMesh;
284         EditVert *eve;
285         int *index, nr, totvert=0;
286         
287         for(eve= em->verts.first; eve; eve= eve->next) {
288                 if(eve->f & SELECT) totvert++;
289         }
290         if(totvert==0) return 0;
291         
292         *indexar= index= MEM_mallocN(4*totvert, "hook indexar");
293         nr= 0;
294         cent[0]= cent[1]= cent[2]= 0.0;
295         
296         for(eve= em->verts.first; eve; eve= eve->next) {
297                 if(eve->f & SELECT) {
298                         *index= nr; index++;
299                         VecAddf(cent, cent, eve->co);
300                 }
301                 nr++;
302         }
303         
304         VecMulf(cent, 1.0/(float)totvert);
305         
306         return totvert;
307 }
308
309 static void select_editmesh_hook(ObHook *hook)
310 {
311         EditMesh *em = G.editMesh;
312         EditVert *eve;
313         int index=0, nr=0;
314         
315         for(eve= em->verts.first; eve; eve= eve->next, nr++) {
316                 if(nr==hook->indexar[index]) {
317                         eve->f |= SELECT;
318                         if(index < hook->totindex-1) index++;
319                 }
320         }
321         EM_select_flush();
322 }
323
324 static int return_editlattice_indexar(int **indexar, float *cent)
325 {
326         BPoint *bp;
327         int *index, nr, totvert=0, a;
328         
329         // count
330         a= editLatt->pntsu*editLatt->pntsv*editLatt->pntsw;
331         bp= editLatt->def;
332         while(a--) {
333                 if(bp->f1 & SELECT) {
334                         if(bp->hide==0) totvert++;
335                 }
336                 bp++;
337         }
338
339         if(totvert==0) return 0;
340         
341         *indexar= index= MEM_mallocN(4*totvert, "hook indexar");
342         nr= 0;
343         cent[0]= cent[1]= cent[2]= 0.0;
344         
345         a= editLatt->pntsu*editLatt->pntsv*editLatt->pntsw;
346         bp= editLatt->def;
347         while(a--) {
348                 if(bp->f1 & SELECT) {
349                         if(bp->hide==0) {
350                                 *index= nr; index++;
351                                 VecAddf(cent, cent, bp->vec);
352                         }
353                 }
354                 bp++;
355                 nr++;
356         }
357         
358         VecMulf(cent, 1.0/(float)totvert);
359         
360         return totvert;
361 }
362
363 static void select_editlattice_hook(ObHook *hook)
364 {
365         BPoint *bp;
366         int index=0, nr=0, a;
367         
368         // count
369         a= editLatt->pntsu*editLatt->pntsv*editLatt->pntsw;
370         bp= editLatt->def;
371         while(a--) {
372                 if(hook->indexar[index]==nr) {
373                         bp->f1 |= SELECT;
374                         if(index < hook->totindex-1) index++;
375                 }
376                 nr++;
377                 bp++;
378         }
379 }
380
381 static int return_editcurve_indexar(int **indexar, float *cent)
382 {
383         extern ListBase editNurb;
384         Nurb *nu;
385         BPoint *bp;
386         BezTriple *bezt;
387         int *index, a, nr, totvert=0;
388         
389         for(nu= editNurb.first; nu; nu= nu->next) {
390                 if((nu->type & 7)==CU_BEZIER) {
391                         bezt= nu->bezt;
392                         a= nu->pntsu;
393                         while(a--) {
394                                 if(bezt->f1 & SELECT) totvert++;
395                                 if(bezt->f2 & SELECT) totvert++;
396                                 if(bezt->f3 & SELECT) totvert++;
397                                 bezt++;
398                         }
399                 }
400                 else {
401                         bp= nu->bp;
402                         a= nu->pntsu*nu->pntsv;
403                         while(a--) {
404                                 if(bp->f1 & SELECT) totvert++;
405                                 bp++;
406                         }
407                 }
408         }
409         if(totvert==0) return 0;
410         
411         *indexar= index= MEM_mallocN(4*totvert, "hook indexar");
412         nr= 0;
413         cent[0]= cent[1]= cent[2]= 0.0;
414         
415         for(nu= editNurb.first; nu; nu= nu->next) {
416                 if((nu->type & 7)==CU_BEZIER) {
417                         bezt= nu->bezt;
418                         a= nu->pntsu;
419                         while(a--) {
420                                 if(bezt->f1 & SELECT) {
421                                         *index= nr; index++;
422                                         VecAddf(cent, cent, bezt->vec[0]);
423                                 }
424                                 nr++;
425                                 if(bezt->f2 & SELECT) {
426                                         *index= nr; index++;
427                                         VecAddf(cent, cent, bezt->vec[1]);
428                                 }
429                                 nr++;
430                                 if(bezt->f3 & SELECT) {
431                                         *index= nr; index++;
432                                         VecAddf(cent, cent, bezt->vec[2]);
433                                 }
434                                 nr++;
435                                 bezt++;
436                         }
437                 }
438                 else {
439                         bp= nu->bp;
440                         a= nu->pntsu*nu->pntsv;
441                         while(a--) {
442                                 if(bp->f1 & SELECT) {
443                                         *index= nr; index++;
444                                         VecAddf(cent, cent, bp->vec);
445                                 }
446                                 nr++;
447                                 bp++;
448                         }
449                 }
450         }
451         
452         VecMulf(cent, 1.0/(float)totvert);
453         
454         return totvert;
455 }
456
457 static void select_editcurve_hook(ObHook *hook)
458 {
459         extern ListBase editNurb;
460         Nurb *nu;
461         BPoint *bp;
462         BezTriple *bezt;
463         int index=0, a, nr=0;
464         
465         for(nu= editNurb.first; nu; nu= nu->next) {
466                 if((nu->type & 7)==CU_BEZIER) {
467                         bezt= nu->bezt;
468                         a= nu->pntsu;
469                         while(a--) {
470                                 if(nr == hook->indexar[index]) {
471                                         bezt->f1 |= SELECT;
472                                         if(index<hook->totindex-1) index++;
473                                 }
474                                 nr++;
475                                 if(nr == hook->indexar[index]) {
476                                         bezt->f2 |= SELECT;
477                                         if(index<hook->totindex-1) index++;
478                                 }
479                                 nr++;
480                                 if(nr == hook->indexar[index]) {
481                                         bezt->f3 |= SELECT;
482                                         if(index<hook->totindex-1) index++;
483                                 }
484                                 nr++;
485
486                                 bezt++;
487                         }
488                 }
489                 else {
490                         bp= nu->bp;
491                         a= nu->pntsu*nu->pntsv;
492                         while(a--) {
493                                 if(nr == hook->indexar[index]) {
494                                         bp->f1 |= SELECT;
495                                         if(index<hook->totindex-1) index++;
496                                 }
497                                 nr++;
498                                 bp++;
499                         }
500                 }
501         }
502 }
503
504 void add_hook(void)
505 {
506         Object *ob=NULL;
507         ObHook *hook=NULL;
508         float cent[3];
509         int tot=0, *indexar, mode;
510
511         if(G.obedit==NULL) return;
512         
513         if(G.obedit->hooks.first)
514                 mode= pupmenu("Hooks %t|Add Hook, To New Empty %x1|Add Hook, To Selected Object %x2|Remove... %x3|Reassign... %x4|Select... %x5|Clear Offset...%x6");
515         else
516                 mode= pupmenu("Hooks %t|Add, New Empty %x1|Add, To Selected Object %x2");
517
518         if(mode<1) return;
519         
520         /* preconditions */
521
522         if(mode==2) { // selected object
523                 Base *base= FIRSTBASE;
524                 while(base) {
525                         if TESTBASELIB(base) {
526                                 if(base!=BASACT) {
527                                         ob= base->object;
528                                         break;
529                                 }
530                         }
531                         base= base->next;
532                 }
533                 if(ob==NULL) {
534                         error("Requires selected Object");
535                         return;
536                 }
537         }
538         else if(mode!=1) {
539                 int maxlen=0, a, nr;
540                 char *cp;
541                 
542                 // make pupmenu with hooks
543                 for(hook= G.obedit->hooks.first; hook; hook= hook->next) maxlen+=32;
544                 
545                 if(maxlen==0) {
546                         error("Object has no hooks yet");
547                         return;
548                 }
549                 
550                 cp= MEM_callocN(maxlen+32, "temp string");
551                 if(mode==3) strcpy(cp, "Remove %t|");
552                 else if(mode==4) strcpy(cp, "Reassign %t|");
553                 else if(mode==5) strcpy(cp, "Select %t|");
554                 else if(mode==6) strcpy(cp, "Clear Offset %t|");
555                 
556                 for(hook= G.obedit->hooks.first; hook; hook= hook->next) {
557                         strcat(cp, hook->name);
558                         strcat(cp, " |");
559                 }
560         
561                 nr= pupmenu(cp);
562                 MEM_freeN(cp);
563                 
564                 if(nr<1) return;
565                 
566                 a= 1;
567                 for(hook= G.obedit->hooks.first; hook; hook= hook->next, a++) {
568                         if(a==nr) break;
569                 }
570                 ob= hook->parent;
571         }
572
573         /* do it, new hooks or reassign */
574         if(mode==1 || mode==2 || mode==4) {
575         
576                 switch(G.obedit->type) {
577                 case OB_MESH:
578                         tot= return_editmesh_indexar(&indexar, cent);
579                         break;
580                 case OB_CURVE:
581                 case OB_SURF:
582                         tot= return_editcurve_indexar(&indexar, cent);
583                         break;
584                 case OB_LATTICE:
585                         tot= return_editlattice_indexar(&indexar, cent);
586                         break;
587                 }
588                 
589                 if(tot==0) {
590                         error("Requires selected vertices");
591                 }
592                 else {
593                         
594                         if(mode==1) {
595                                 Base *base= BASACT;
596
597                                 ob= add_object(OB_EMPTY);
598                                 /* transform cent to global coords for loc */
599                                 VecMat4MulVecfl(ob->loc, G.obedit->obmat, cent);
600                                 
601                                 /* restore, add_object sets active */
602                                 BASACT= base;
603                         }
604                         /* if mode is 2 or 4, ob has been set */
605                                                                         
606                         /* new hook */
607                         if(mode==1 || mode==2) {
608                                 hook= MEM_callocN(sizeof(ObHook), "new hook");
609                                 BLI_addtail(&G.obedit->hooks, hook);
610                                 strncpy(hook->name, ob->id.name+2, 30);
611                                 hook->force= 1.0;
612                         }
613                         else MEM_freeN(hook->indexar); // reassign, hook was set
614
615                         hook->parent= ob;
616                         hook->indexar= indexar;
617                         VECCOPY(hook->cent, cent);
618                         hook->totindex= tot;
619                         
620                         if(mode==1 || mode==2) {
621                                 /* matrix calculus */
622                                 /* vert x (obmat x hook->imat) x hook->obmat x ob->imat */
623                                 /*        (parentinv         )                          */
624                                 
625                                 where_is_object(ob);
626                 
627                                 Mat4Invert(ob->imat, ob->obmat);
628                                 /* apparently this call goes from right to left... */
629                                 Mat4MulSerie(hook->parentinv, ob->imat, G.obedit->obmat, NULL, 
630                                                         NULL, NULL, NULL, NULL, NULL);
631                         }
632                 }
633         }
634         else if(mode==3) { // remove
635                 BLI_remlink(&G.obedit->hooks, hook);
636                 MEM_freeN(hook->indexar);
637                 MEM_freeN(hook);
638         }
639         else if(mode==5) { // select
640                 if(G.obedit->type==OB_MESH) select_editmesh_hook(hook);
641                 else if(G.obedit->type==OB_LATTICE) select_editlattice_hook(hook);
642                 else if(G.obedit->type==OB_CURVE) select_editcurve_hook(hook);
643                 else if(G.obedit->type==OB_SURF) select_editcurve_hook(hook);
644         }
645         else if(mode==6) { // clear offset
646                 where_is_object(ob);    // ob is hook->parent
647
648                 Mat4Invert(ob->imat, ob->obmat);
649                 /* this call goes from right to left... */
650                 Mat4MulSerie(hook->parentinv, ob->imat, G.obedit->obmat, NULL, 
651                                         NULL, NULL, NULL, NULL, NULL);
652         }
653
654         allqueue(REDRAWVIEW3D, 0);
655         allqueue(REDRAWBUTSOBJECT, 0);
656         BIF_undo_push("Add hook");
657 }
658
659 void make_track(void)
660 {
661         Base *base;
662         short mode=0;
663         
664         if(G.scene->id.lib) return;
665         if(G.obedit) {
666                 return;
667         }
668         if(BASACT==0) return;
669
670         mode= pupmenu("Make Track %t|TrackTo Constraint %x1|LockTrack Constraint %x2|Old Track %x3");
671         if (mode == 0){
672                 return;
673         }
674         else if (mode == 1){
675                 bConstraint *con;
676                 bTrackToConstraint *data;
677
678                 base= FIRSTBASE;
679                 while(base) {
680                         if TESTBASELIB(base) {
681                                 if(base!=BASACT) {
682                                         con = add_new_constraint(CONSTRAINT_TYPE_TRACKTO);
683                                         strcpy (con->name, "AutoTrack");
684
685                                         data = con->data;
686                                         data->tar = BASACT->object;
687
688                                         /* Lamp and Camera track differently by default */
689                                         if (base->object->type == OB_LAMP || base->object->type == OB_CAMERA) {
690                                                 data->reserved1 = TRACK_nZ;
691                                                 data->reserved2 = UP_Y;
692                                         }
693
694                                         add_constraint_to_object(con, base->object);
695                                 }
696                         }
697                         base= base->next;
698                 }
699
700                 test_scene_constraints();
701                 allqueue(REDRAWVIEW3D, 0);
702                 sort_baselist(G.scene);
703         }
704         else if (mode == 2){
705                 bConstraint *con;
706                 bLockTrackConstraint *data;
707
708                 base= FIRSTBASE;
709                 while(base) {
710                         if TESTBASELIB(base) {
711                                 if(base!=BASACT) {
712                                         con = add_new_constraint(CONSTRAINT_TYPE_LOCKTRACK);
713                                         strcpy (con->name, "AutoTrack");
714
715                                         data = con->data;
716                                         data->tar = BASACT->object;
717
718                                         /* Lamp and Camera track differently by default */
719                                         if (base->object->type == OB_LAMP || base->object->type == OB_CAMERA) {
720                                                 data->trackflag = TRACK_nZ;
721                                                 data->lockflag = LOCK_Y;
722                                         }
723
724                                         add_constraint_to_object(con, base->object);
725                                 }
726                         }
727                         base= base->next;
728                 }
729
730                 test_scene_constraints();
731                 allqueue(REDRAWVIEW3D, 0);
732                 sort_baselist(G.scene);
733         }
734         else if (mode == 3){
735                 base= FIRSTBASE;
736                 while(base) {
737                         if TESTBASELIB(base) {
738                                 if(base!=BASACT) {
739
740                                         base->object->track= BASACT->object;
741                                 }
742                         }
743                         base= base->next;
744                 }
745
746                 test_scene_constraints();
747                 allqueue(REDRAWVIEW3D, 0);
748                 allqueue(REDRAWOOPS, 0);
749                 sort_baselist(G.scene);
750         }
751         BIF_undo_push("Make Track");
752 }
753
754 void apply_obmat(Object *ob)
755 {
756         float mat[3][3], imat[3][3], tmat[3][3];
757         
758         /* from obmat to loc rot size */
759         
760         if(ob==0) return;
761         Mat3CpyMat4(mat, ob->obmat);
762         
763         VECCOPY(ob->loc, ob->obmat[3]);
764         
765         if(ob->transflag & OB_QUAT) {
766                 Mat3ToQuat(mat, ob->quat);
767                 QuatToMat3(ob->quat, tmat);
768         }
769         else {
770                 Mat3ToEul(mat, ob->rot);
771                 EulToMat3(ob->rot, tmat);
772         }
773         Mat3Inv(imat, tmat);
774         
775         Mat3MulMat3(tmat, imat, mat);
776         
777         ob->size[0]= tmat[0][0];
778         ob->size[1]= tmat[1][1];
779         ob->size[2]= tmat[2][2];
780
781 }
782
783 void clear_parent(void)
784 {
785         Object *par;
786         Base *base;
787         int mode;
788         
789         if(G.obedit) return;
790         if(G.scene->id.lib) return;
791
792         mode= pupmenu("OK? %t|Clear Parent %x1|Clear and Keep Transformation (Clear Track) %x2|Clear Parent Inverse %x3");
793         
794         if(mode<1) return;
795
796         base= FIRSTBASE;
797         while(base) {
798                 if TESTBASELIB(base) {
799                         par= 0;
800                         if(mode==1 || mode==2) {
801                                 if(base->object->type==OB_IKA) {
802                                         Ika *ika= base->object->data;
803                                         ika->parent= 0;
804                                 }
805                                 par= base->object->parent;
806                                 base->object->parent= 0;
807                         
808                                 if(mode==2) {
809                                         base->object->track= 0;
810                                         apply_obmat(base->object);
811                                 }
812                         }
813                         else if(mode==3) {
814                                 Mat4One(base->object->parentinv);
815                         }
816                         
817                         if(par) {
818                                 makeDispList(base->object);     // just always..., checks are not available well (ton)
819                         }
820                 }
821                 base= base->next;
822         }
823
824         test_scene_constraints();
825         allqueue(REDRAWVIEW3D, 0);
826         allqueue(REDRAWOOPS, 0);
827         
828         BIF_undo_push("Clear Parent");  
829 }
830
831 void clear_track(void)
832 {
833         Base *base;
834         int mode;
835         
836         if(G.obedit) return;
837         if(G.scene->id.lib) return;
838
839         mode= pupmenu("OK? %t|Clear Track %x1| Clear Track and Keep Transform %x2");
840
841         if(mode<1) return;
842
843         base= FIRSTBASE;
844         while(base) {
845                 if TESTBASELIB(base) {
846                         base->object->track= 0;
847
848                         if(mode==2) {
849                                 apply_obmat(base->object);
850                         }                       
851                 }
852                 base= base->next;
853         }
854         test_scene_constraints();
855         allqueue(REDRAWVIEW3D, 0);
856         allqueue(REDRAWOOPS, 0);
857         
858         BIF_undo_push("Clear Track");   
859 }
860
861 void clear_object(char mode)
862 {
863         Base *base;
864         Object *ob;
865         float *v1, *v3, mat[3][3];
866         char *str=NULL;
867         
868         if(G.obedit) return;
869         if(G.scene->id.lib) return;
870         
871         if(mode=='r') str= "Clear rotation";
872         else if(mode=='g') str= "Clear location";
873         else if(mode=='s') str= "Clear size";
874         else if(mode=='o') str= "Clear origin";
875         else return;
876         
877         if (G.obpose){
878
879                 switch (G.obpose->type){
880                 case OB_ARMATURE:
881                         clear_armature (G.obpose, mode);
882 #if 1
883                         clear_pose_constraint_status(G.obpose);
884                         make_displists_by_armature (G.obpose);
885 #endif
886                         break;
887                 }
888
889                 allqueue(REDRAWVIEW3D, 0);
890                 BIF_undo_push(str);
891                 return;
892         }
893
894         base= FIRSTBASE;
895         while(base) {
896                 if TESTBASELIB(base) {
897                         ob= base->object;
898                         
899                         if(mode=='r') {
900                                 memset(ob->rot, 0, 3*sizeof(float));
901                                 memset(ob->drot, 0, 3*sizeof(float));
902                                 QuatOne(ob->quat);
903                                 QuatOne(ob->dquat);
904                         }
905                         else if(mode=='g') {
906                                 memset(ob->loc, 0, 3*sizeof(float));
907                                 memset(ob->dloc, 0, 3*sizeof(float));
908                         }
909                         else if(mode=='s') {
910                                 memset(ob->dsize, 0, 3*sizeof(float));
911                                 ob->size[0]= 1.0;
912                                 ob->size[1]= 1.0;
913                                 ob->size[2]= 1.0;
914                         }
915                         else if(mode=='o') {
916                                 if(ob->parent) {
917                                         v1= ob->loc;
918                                         v3= ob->parentinv[3];
919                                         
920                                         Mat3CpyMat4(mat, ob->parentinv);
921                                         VECCOPY(v3, v1);
922                                         v3[0]= -v3[0];
923                                         v3[1]= -v3[1];
924                                         v3[2]= -v3[2];
925                                         Mat3MulVecfl(mat, v3);
926                                 }
927                         }
928                         
929                         if(ob->parent && ob->partype==PARSKEL)
930                                 freedisplist(&ob->disp);
931                         else if(ob->hooks.first)
932                                 freedisplist(&ob->disp);
933                 }
934                 base= base->next;
935         }
936         
937         allqueue(REDRAWVIEW3D, 0);
938         BIF_undo_push(str);
939 }
940
941 void reset_slowparents(void)
942 {
943         /* back to original locations */
944         Base *base;
945         
946         base= FIRSTBASE;
947         while(base) {
948                 if(base->object->parent) {
949                         if(base->object->partype & PARSLOW) {
950                                 base->object->partype -= PARSLOW;
951                                 where_is_object(base->object);
952                                 base->object->partype |= PARSLOW;
953                         }
954                 }
955                 base= base->next;
956         }
957 }
958
959 void set_slowparent(void)
960 {
961         Base *base;
962
963         if( okee("Set slow parent")==0 ) return;
964
965         base= FIRSTBASE;
966         while(base) {
967                 if TESTBASELIB(base) {
968                         if(base->object->parent) base->object->partype |= PARSLOW;
969                 }
970                 base= base->next;
971         }
972         BIF_undo_push("Slow parent");
973 }
974
975 void make_vertex_parent(void)
976 {
977         EditMesh *em = G.editMesh;
978         EditVert *eve;
979         Base *base;
980         Nurb *nu;
981         BezTriple *bezt;
982         BPoint *bp;
983         Object *par, *ob;
984         int a, v1=0, v2=0, v3=0, nr=1;
985         
986         /* we need 1 to 3 selected vertices */
987         
988         if(G.obedit->type==OB_MESH) {
989                 eve= em->verts.first;
990                 while(eve) {
991                         if(eve->f & 1) {
992                                 if(v1==0) v1= nr;
993                                 else if(v2==0) v2= nr;
994                                 else if(v3==0) v3= nr;
995                                 else break;
996                         }
997                         nr++;
998                         eve= eve->next;
999                 }
1000         }
1001         else if ELEM(G.obedit->type, OB_SURF, OB_CURVE) {
1002                 extern ListBase editNurb;
1003                 nu= editNurb.first;
1004                 while(nu) {
1005                         if((nu->type & 7)==CU_BEZIER) {
1006                                 bezt= nu->bezt;
1007                                 a= nu->pntsu;
1008                                 while(a--) {
1009                                         if(BEZSELECTED(bezt)) {
1010                                                 if(v1==0) v1= nr;
1011                                                 else if(v2==0) v2= nr;
1012                                                 else if(v3==0) v3= nr;
1013                                                 else break;
1014                                         }
1015                                         nr++;
1016                                         bezt++;
1017                                 }
1018                         }
1019                         else {
1020                                 bp= nu->bp;
1021                                 a= nu->pntsu*nu->pntsv;
1022                                 while(a--) {
1023                                         if(bp->f1 & SELECT) {
1024                                                 if(v1==0) v1= nr;
1025                                                 else if(v2==0) v2= nr;
1026                                                 else if(v3==0) v3= nr;
1027                                                 else break;
1028                                         }
1029                                         nr++;
1030                                         bp++;
1031                                 }
1032                         }
1033                         nu= nu->next;
1034                 }
1035                 
1036         }
1037         
1038         if( !(v1 && v2==0 && v3==0) && !(v1 && v2 && v3) ) {
1039                 error("Select either 1 or 3 vertices to parent to");
1040                 return;
1041         }
1042         
1043         if(okee("Make vertex parent")==0) return;
1044         
1045         base= FIRSTBASE;
1046         while(base) {
1047                 if TESTBASELIB(base) {
1048                         if(base!=BASACT) {
1049                                 ob= base->object;
1050                                 par= BASACT->object->parent;
1051                                 
1052                                 while(par) {
1053                                         if(par==ob) break;
1054                                         par= par->parent;
1055                                 }
1056                                 if(par) {
1057                                         error("Loop in parents");
1058                                 }
1059                                 else {
1060                                         ob->parent= BASACT->object;
1061                                         if(v3) {
1062                                                 ob->partype= PARVERT3;
1063                                                 ob->par1= v1-1;
1064                                                 ob->par2= v2-1;
1065                                                 ob->par3= v3-1;
1066
1067                                                 /* inverse parent matrix */
1068                                                 what_does_parent(ob);
1069                                                 Mat4Invert(ob->parentinv, workob.obmat);
1070                                                 clear_workob();
1071                                         }
1072                                         else {
1073                                                 ob->partype= PARVERT1;
1074                                                 ob->par1= v1-1;
1075
1076                                                 /* inverse parent matrix */
1077                                                 what_does_parent(ob);
1078                                                 Mat4Invert(ob->parentinv, workob.obmat);
1079                                                 clear_workob();
1080                                         }
1081                                 }
1082                         }
1083                 }
1084                 base= base->next;
1085         }
1086         allqueue(REDRAWVIEW3D, 0);
1087         
1088         // BIF_undo_push(str); not, conflicts with editmode undo...
1089 }
1090
1091 int test_parent_loop(Object *par, Object *ob)
1092 {
1093         /* test if 'ob' is a parent somewhere in par's parents */
1094         
1095         if(par==0) return 0;
1096         if(ob == par) return 1;
1097         
1098         if(par->type==OB_IKA) {
1099                 Ika *ika= par->data;
1100                 
1101                 if( ob == ika->parent ) return 1;
1102                 if( test_parent_loop(ika->parent, ob) ) return 1;
1103         }
1104
1105         return test_parent_loop(par->parent, ob);
1106
1107 }
1108
1109 void make_parent(void)
1110 {
1111         Base *base;
1112         Object *par;
1113         short qual, mode=0, limbnr=0, effchild=0;
1114         char *bonestr=NULL;
1115         Bone    *bone=NULL;
1116         int     bonenr;
1117
1118         if(G.scene->id.lib) return;
1119         if(G.obedit) {
1120                 if ELEM3(G.obedit->type, OB_MESH, OB_CURVE, OB_SURF) make_vertex_parent();
1121                 else if (G.obedit->type==OB_ARMATURE) make_bone_parent();
1122                 return;
1123         }
1124         if(BASACT==0) return;
1125         
1126         qual= G.qual;
1127         par= BASACT->object;
1128
1129         if(par->type == OB_CURVE){
1130                 bConstraint *con;
1131                 bFollowPathConstraint *data;
1132
1133                 mode= pupmenu("Make Parent %t|Normal Parent %x1|Follow Path %x2|Curve Deform %x3");
1134                 if(mode<=0){
1135                         return;
1136                 }
1137                 else if(mode==1) {
1138                         mode= PAROBJECT;
1139                 }
1140                 else if(mode==3) {
1141                         mode= PARSKEL;
1142                 }
1143                 else if(mode==2) {
1144
1145                         base= FIRSTBASE;
1146                         while(base) {
1147                                 if TESTBASELIB(base) {
1148                                         if(base!=BASACT) {
1149                                                 float cmat[4][4], vec[3], size[3];
1150
1151                                                 con = add_new_constraint(CONSTRAINT_TYPE_FOLLOWPATH);
1152                                                 strcpy (con->name, "AutoPath");
1153
1154                                                 data = con->data;
1155                                                 data->tar = BASACT->object;
1156
1157                                                 add_constraint_to_object(con, base->object);
1158
1159                                                 get_constraint_target_matrix(con, TARGET_OBJECT, NULL, cmat, size, G.scene->r.cfra - base->object->sf);
1160                                                 VecSubf(vec, base->object->obmat[3], cmat[3]);
1161
1162                                                 base->object->loc[0] = vec[0];
1163                                                 base->object->loc[1] = vec[1];
1164                                                 base->object->loc[2] = vec[2];
1165                                         }
1166                                 }
1167                                 base= base->next;
1168                         }
1169
1170                         test_scene_constraints();
1171                         allqueue(REDRAWVIEW3D, 0);
1172                         sort_baselist(G.scene);
1173                         BIF_undo_push("make Parent");
1174                         return;
1175                 }
1176         }
1177         else if(par->type == OB_ARMATURE){
1178
1179                         base= FIRSTBASE;
1180                         while(base) {
1181                                 if TESTBASELIB(base) {
1182                                         if(base!=BASACT) {
1183                                                 if(base->object->type==OB_MESH) {
1184                                                         mode= pupmenu("Make Parent To%t|Bone %x1|Armature %x2|Object %x3");
1185                                                         break;
1186                                                 }
1187                                                 else {
1188                                                         mode= pupmenu("Make Parent To %t|Bone %x1|Object %x3");
1189                                                         break;
1190                                                 }
1191                                         }
1192                                 }
1193                                 base= base->next;
1194                         }
1195                 
1196                         switch (mode){
1197                         case 1:
1198                                 mode=PARBONE;
1199                                 /* Make bone popup menu */
1200
1201                                 bonestr = make_bone_menu(get_armature(par));
1202                 //              if(mbutton(&bone, bonestr, 1, 24, "Bone: ")==0) {
1203
1204                                 bonenr= pupmenu_col(bonestr, 20);
1205                                 if (bonestr)
1206                                         MEM_freeN (bonestr);
1207                                 
1208                                 if (bonenr==-1){
1209                                         allqueue(REDRAWVIEW3D, 0);
1210                                         return;
1211                                 }
1212
1213                                 apply_pose_armature(get_armature(par), par->pose, 0);
1214                                 bone=get_indexed_bone(get_armature(par), bonenr); 
1215                                 if (!bone){
1216                 //                      error ("Invalid bone!");
1217                                         allqueue(REDRAWVIEW3D, 0);
1218                                         return;
1219                                 }
1220
1221                                 break;
1222                         case 2:
1223                                 mode=PARSKEL;
1224                                 break;
1225                         case 3:
1226                                 mode=PAROBJECT;
1227                                 break;
1228                         default:
1229                                 return;
1230                         }
1231         }
1232         else {
1233                 if(qual & LR_SHIFTKEY) {
1234                         if(okee("Make parent without inverse")==0) return;
1235                 }
1236                 else {
1237                         if(qual & LR_ALTKEY) {
1238                                 if(okee("Make vertex parent")==0) return;
1239                         }
1240                         else if(okee("Make parent")==0) return;
1241
1242                         /* test effchild */
1243                         base= FIRSTBASE;
1244                         while(base) {
1245                                 if TESTBASELIB(base) {
1246                                         if(base->object->type==OB_IKA && base->object != par) {
1247                                                 if(effchild==0) {
1248                                                         if(okee("Effector as Child")) effchild= 1;
1249                                                         else effchild= 2;
1250                                                 }
1251                                         }
1252                                 }
1253                                 if(effchild) break;
1254                                 base= base->next;
1255                         }
1256                         
1257                         /* now we'll clearparentandkeeptransform all objects */
1258                         base= FIRSTBASE;
1259                         while(base) {
1260                                 if TESTBASELIB(base) {
1261                                         if(base!=BASACT && base->object->parent) {
1262                                                 if(base->object->type==OB_IKA && effchild==1);
1263                                                 else {
1264                                                         base->object->parent= 0;
1265                                                         apply_obmat(base->object);
1266                                                 }
1267                                         }
1268                                 }
1269                                 base= base->next;
1270                         }
1271                 }
1272         }
1273         
1274         
1275         base= FIRSTBASE;
1276         while(base) {
1277                 if TESTBASELIB(base) {
1278                         if(base!=BASACT) {
1279                                 
1280                                 if( test_parent_loop(par, base->object) ) {
1281                                         error("Loop in parents");
1282                                 }
1283                                 else {
1284                                         
1285                                         /* the ifs below are horrible code (ton) */
1286                                         
1287                                         if(par->type==OB_IKA){
1288                                                 base->object->partype= mode;
1289                                                 base->object->par1= limbnr;
1290                                         }
1291                                         else if (par->type==OB_ARMATURE){
1292                                                 base->object->partype= mode;
1293                                                 if (bone)
1294                                                         strcpy (base->object->parsubstr, bone->name);
1295                                                 else
1296                                                         base->object->parsubstr[0]=0;
1297                                         }
1298                                         else {
1299                                                 if(qual & LR_ALTKEY) {
1300                                                         base->object->partype= PARVERT1;
1301                                                 }
1302                                                 else if(par->type==OB_CURVE) {
1303                                                         base->object->partype= mode;
1304                                                 }
1305                                                 else {
1306                                                         base->object->partype= PAROBJECT;
1307                                                 }
1308                                         }
1309                                         base->object->parent= par;
1310                                         
1311                                         /* calculate inverse parent matrix? */
1312                                         if( (qual & LR_SHIFTKEY) ) {
1313                                                 /* not... */
1314                                                 Mat4One(base->object->parentinv);
1315                                                 memset(base->object->loc, 0, 3*sizeof(float));
1316                                         }
1317                                         else {
1318                                                 if(mode==PARSKEL && par->type == OB_ARMATURE) {
1319                                                         /* Prompt the user as to whether he wants to
1320                                                                 * add some vertex groups based on the bones
1321                                                                 * in the parent armature.
1322                                                                 */
1323                                                         create_vgroups_from_armature(base->object, 
1324                                                                                                                         par);
1325
1326                                                         base->object->partype= PAROBJECT;
1327                                                         what_does_parent(base->object);
1328                                                         Mat4One (base->object->parentinv);
1329                                                         base->object->partype= mode;
1330                                                 }
1331                                                 else
1332                                                         what_does_parent(base->object);
1333                                                 Mat4Invert(base->object->parentinv, workob.obmat);
1334                                         }
1335                                         
1336                                         if(par->type==OB_LATTICE) makeDispList(base->object);
1337                                         if(par->type==OB_CURVE && mode==PARSKEL) makeDispList(base->object);
1338                                         if(par->type==OB_ARMATURE && mode == PARSKEL){
1339                                                 verify_defgroups(base->object);
1340                                                 makeDispList(base->object);
1341                                         }
1342                                 }
1343                         }
1344                 }
1345                 base= base->next;
1346         }
1347         allqueue(REDRAWVIEW3D, 0);
1348         allqueue(REDRAWOOPS, 0);
1349         
1350         test_scene_constraints();
1351         sort_baselist(G.scene);
1352
1353         BIF_undo_push("make Parent");
1354 }
1355
1356
1357 void enter_editmode(void)
1358 {
1359         Base *base;
1360         Object *ob;
1361         Ika *ika;
1362         ID *id;
1363         Mesh *me;
1364         int ok= 0;
1365         bArmature *arm;
1366         
1367         if(G.scene->id.lib) return;
1368         base= BASACT;
1369         if(base==0) return;
1370         if((G.vd==NULL || (base->lay & G.vd->lay))==0) return;
1371         
1372         ob= base->object;
1373         if(ob->data==0) return;
1374         
1375         id= ob->data;
1376         if(id->lib) {
1377                 error("Can't edit library data");
1378                 return;
1379         }
1380         
1381         if(ob->type==OB_MESH) {
1382                 me= get_mesh(ob);
1383                 if( me==0 ) return;
1384                 if(me->id.lib) {
1385                         error("Can't edit library data");
1386                         return;
1387                 }
1388                 ok= 1;
1389                 G.obedit= ob;
1390                 make_editMesh();
1391                 allqueue(REDRAWBUTSLOGIC, 0);
1392                 if(G.f & G_FACESELECT) allqueue(REDRAWIMAGE, 0);
1393         }
1394         if (ob->type==OB_ARMATURE){
1395                 arm=base->object->data;
1396                 if (!arm) return;
1397                 if (arm->id.lib){
1398                         error("Can't edit library data");
1399                         return;
1400                 }
1401                 ok=1;
1402                 G.obedit=ob;
1403                 make_editArmature();
1404                 allqueue (REDRAWVIEW3D,0);
1405         }
1406         else if(ob->type==OB_IKA) {     /* grab type */
1407                 base= FIRSTBASE;
1408                 while(base) {
1409                         if TESTBASE(base) {
1410                                 if(base->object->type==OB_IKA) {
1411                                         ika= base->object->data;
1412                                         if(ika->flag & IK_GRABEFF) ika->flag &= ~IK_GRABEFF;
1413                                         else ika->flag |= IK_GRABEFF;
1414                                 }
1415                         }
1416                         base= base->next;
1417                 }
1418                 allqueue(REDRAWVIEW3D, 0);
1419         }
1420         else if(ob->type==OB_FONT) {
1421                 G.obedit= ob;
1422                 ok= 1;
1423                 make_editText();
1424         }
1425         else if(ob->type==OB_MBALL) {
1426                 G.obedit= ob;
1427                 ok= 1;
1428                 make_editMball();
1429         }
1430         else if(ob->type==OB_LATTICE) {
1431                 G.obedit= ob;
1432                 ok= 1;
1433                 make_editLatt();
1434         }
1435         else if(ob->type==OB_SURF || ob->type==OB_CURVE) {
1436                 ok= 1;
1437                 G.obedit= ob;
1438                 make_editNurb();
1439         }
1440         allqueue(REDRAWBUTSEDIT, 0);
1441         allqueue(REDRAWOOPS, 0);
1442         countall();
1443         
1444         if(ok) {
1445                 setcursor_space(SPACE_VIEW3D, CURSOR_EDIT);
1446         
1447                 allqueue(REDRAWVIEW3D, 1);
1448         }
1449         else G.obedit= 0;
1450
1451         if (G.obpose)
1452                 exit_posemode (1);
1453         scrarea_queue_headredraw(curarea);
1454 }
1455
1456 void make_displists_by_parent(Object *ob) {
1457         Base *base;
1458         
1459         for (base= FIRSTBASE; base; base= base->next)
1460                 if (ob==base->object->parent)
1461                         makeDispList(base->object);
1462 }
1463
1464 void exit_editmode(int freedata)        /* freedata==0 at render, 1= freedata, 2= do undo buffer too */
1465 {
1466         Base *base;
1467         Object *ob;
1468         Curve *cu;
1469
1470         if(G.obedit==NULL) return;
1471
1472         if(G.obedit->type==OB_MESH) {
1473
1474                 /* temporal */
1475                 countall();
1476
1477                 if(G.totvert>MESH_MAX_VERTS) {
1478                         error("Too many vertices");
1479                         return;
1480                 }
1481                 load_editMesh();        /* makes new displist */
1482
1483                 if(freedata) free_editMesh(G.editMesh);
1484
1485                 if(G.f & G_FACESELECT) {
1486                         set_seamtface();
1487                         allqueue(REDRAWIMAGE, 0);
1488                 }
1489
1490                 build_particle_system(G.obedit);
1491         }
1492         else if (G.obedit->type==OB_ARMATURE){  
1493                 load_editArmature();
1494                 if (freedata) free_editArmature();
1495         }
1496         else if ELEM(G.obedit->type, OB_CURVE, OB_SURF) {
1497                 extern ListBase editNurb;
1498                 load_editNurb();
1499                 if(freedata) freeNurblist(&editNurb);
1500         }
1501         else if(G.obedit->type==OB_FONT && freedata) {
1502                 load_editText();
1503         }
1504         else if(G.obedit->type==OB_LATTICE) {
1505                 load_editLatt();
1506                 if(freedata) free_editLatt();
1507         }
1508         else if(G.obedit->type==OB_MBALL) {
1509                 extern ListBase editelems;
1510                 load_editMball();
1511                 if(freedata) BLI_freelistN(&editelems);
1512         }
1513
1514         ob= G.obedit;
1515         
1516         /* displist make is different in editmode */
1517         if(freedata) G.obedit= NULL;
1518
1519         /* total remake of softbody data */
1520         if(ob->softflag & OB_SB_ENABLE) {
1521                 SoftBody *sb= ob->soft;
1522                 
1523                 if(sb->keys) {
1524                         if( okee("Erase Baked SoftBody") )
1525                                 sbObjectToSoftbody(ob);
1526                 }
1527                 else sbObjectToSoftbody(ob);
1528         }
1529         makeDispList(ob);
1530
1531         /* has this influence at other objects? */
1532         if(ob->type==OB_CURVE) {
1533
1534                 /* test if ob is use as bevelcurve r textoncurve */
1535                 base= FIRSTBASE;
1536                 while(base) {
1537                         if ELEM(base->object->type, OB_CURVE, OB_FONT) {
1538                                 cu= base->object->data;
1539                                 
1540                                 if(cu->textoncurve==ob) {
1541                                         text_to_curve(base->object, 0);
1542                                         makeDispList(base->object);
1543                                 }
1544                                 if(cu->bevobj==ob || cu->taperobj==ob) {
1545                                         makeDispList(base->object);
1546                                 }
1547                         }
1548                         base= base->next;
1549                 }
1550                 
1551         }
1552         else if(ob->type==OB_LATTICE) {
1553                 make_displists_by_parent(ob);
1554         }
1555
1556         if(freedata) {
1557                 setcursor_space(SPACE_VIEW3D, CURSOR_STD);
1558         
1559                 countall();
1560                 allqueue(REDRAWVIEW3D, 1);
1561                 allqueue(REDRAWBUTSEDIT, 0);
1562                 allqueue(REDRAWBUTSLOGIC, 0);
1563                 allqueue(REDRAWOOPS, 0);
1564         }
1565         scrarea_queue_headredraw(curarea);
1566         
1567         if(G.obedit==NULL && freedata==2) 
1568                 BIF_undo_push("Editmode");
1569 }
1570
1571 void check_editmode(int type)
1572 {
1573         
1574         if (G.obedit==0 || G.obedit->type==type) return;
1575
1576         exit_editmode(2); // freedata, and undo
1577 }
1578
1579 /* 0 == do centre, 1 == centre new, 2 == centre cursor */
1580
1581 void docentre(int centremode)
1582 {
1583         EditMesh *em = G.editMesh;
1584         Base *base;
1585         Object *ob;
1586         Mesh *me, *tme;
1587         Curve *cu;
1588 //      BezTriple *bezt;
1589 //      BPoint *bp;
1590         Nurb *nu, *nu1;
1591         EditVert *eve;
1592         float cent[3], centn[3], min[3], max[3], omat[3][3];
1593         int a, total= 0;
1594         MVert *mvert;
1595
1596         if(G.scene->id.lib) return;
1597         
1598         cent[0]= cent[1]= cent[2]= 0.0;
1599         
1600         if(G.obedit) {
1601
1602                 INIT_MINMAX(min, max);
1603         
1604                 if(G.obedit->type==OB_MESH) {
1605                         for(eve= em->verts.first; eve; eve= eve->next) {
1606                                 if(G.vd->around==V3D_CENTROID) {
1607                                         total++;
1608                                         VECADD(cent, cent, eve->co);
1609                                 }
1610                                 else {
1611                                         DO_MINMAX(eve->co, min, max);
1612                                 }
1613                         }
1614                         
1615                         if(G.vd->around==V3D_CENTROID) {
1616                                 VecMulf(cent, 1.0f/(float)total);
1617                         }
1618                         else {
1619                                 cent[0]= (min[0]+max[0])/2.0f;
1620                                 cent[1]= (min[1]+max[1])/2.0f;
1621                                 cent[2]= (min[2]+max[2])/2.0f;
1622                         }
1623                         
1624                         for(eve= em->verts.first; eve; eve= eve->next) {
1625                                 VecSubf(eve->co, eve->co, cent);                        
1626                         }
1627                 }
1628         }
1629         
1630         /* reset flags */
1631         base= FIRSTBASE;
1632         while(base) {
1633                 if TESTBASELIB(base) {
1634                         base->object->flag &= ~OB_DONE;
1635                 }
1636                 base= base->next;
1637         }
1638         me= G.main->mesh.first;
1639         while(me) {
1640                 me->flag &= ~ME_ISDONE;
1641                 me= me->id.next;
1642         }
1643         
1644         base= FIRSTBASE;
1645         while(base) {
1646                 
1647                 if TESTBASELIB(base) {
1648                         if((base->object->flag & OB_DONE)==0) {
1649                                 
1650                                 base->object->flag |= OB_DONE;
1651                                 
1652                                 if(G.obedit==0 && (me=get_mesh(base->object)) ) {
1653                                         
1654                                         if(me->key) {
1655                                                 error("Can't change the center of a mesh with vertex keys");
1656                                                 return;
1657                                         }
1658                                         
1659                                         if(centremode==2) {
1660                                                 VECCOPY(cent, give_cursor());
1661                                                 Mat4Invert(base->object->imat, base->object->obmat);
1662                                                 Mat4MulVecfl(base->object->imat, cent);
1663                                         } else {
1664                                                 INIT_MINMAX(min, max);
1665                 
1666                                                 mvert= me->mvert;
1667                                                 for(a=0; a<me->totvert; a++, mvert++) {
1668                                                         DO_MINMAX(mvert->co, min, max);
1669                                                 }
1670                                 
1671                                                 cent[0]= (min[0]+max[0])/2.0f;
1672                                                 cent[1]= (min[1]+max[1])/2.0f;
1673                                                 cent[2]= (min[2]+max[2])/2.0f;
1674                                         }
1675                                                 
1676                                         mvert= me->mvert;
1677                                         for(a=0; a<me->totvert; a++, mvert++) {
1678                                                 VecSubf(mvert->co, mvert->co, cent);
1679                                         }
1680                                         me->flag |= ME_ISDONE;
1681                                         
1682                                         if(centremode) {
1683                                                 Mat3CpyMat4(omat, base->object->obmat);
1684                                                 
1685                                                 VECCOPY(centn, cent);
1686                                                 Mat3MulVecfl(omat, centn);
1687                                                 base->object->loc[0]+= centn[0];
1688                                                 base->object->loc[1]+= centn[1];
1689                                                 base->object->loc[2]+= centn[2];
1690                                                 
1691                                                 /* other users? */
1692                                                 ob= G.main->object.first;
1693                                                 while(ob) {
1694                                                         if((ob->flag & OB_DONE)==0) {
1695                                                                 tme= get_mesh(ob);
1696                                                                 
1697                                                                 if(tme==me) {
1698                                                                         
1699                                                                         ob->flag |= OB_DONE;
1700
1701                                                                         Mat3CpyMat4(omat, ob->obmat);
1702                                                                         VECCOPY(centn, cent);
1703                                                                         Mat3MulVecfl(omat, centn);
1704                                                                         ob->loc[0]+= centn[0];
1705                                                                         ob->loc[1]+= centn[1];
1706                                                                         ob->loc[2]+= centn[2];
1707                                                                         
1708                                                                         if(tme && (tme->flag & ME_ISDONE)==0) {
1709                                                                                 mvert= tme->mvert;
1710                                                                                 for(a=0; a<tme->totvert; a++, mvert++) {
1711                                                                                         VecSubf(mvert->co, mvert->co, cent);
1712                                                                                 }
1713                                                                                 tme->flag |= ME_ISDONE;
1714                                                                         }
1715                                                                 }
1716                                                         }
1717                                                         
1718                                                         ob= ob->id.next;
1719                                                 }
1720                                         }
1721                                 
1722                                         /* displist of all users, also this one */
1723                                         makeDispList(base->object);
1724                                         
1725                                         /* DO: check all users... */
1726                                         tex_space_mesh(me);
1727                 
1728                                 }
1729                                 else if ELEM(base->object->type, OB_CURVE, OB_SURF) {
1730                                                                         
1731                                         if(G.obedit) {
1732                                                 extern ListBase editNurb;
1733                                                 nu1= editNurb.first;
1734                                         }
1735                                         else {
1736                                                 cu= base->object->data;
1737                                                 nu1= cu->nurb.first;
1738                                         }
1739                                         
1740                                         if(centremode==2) {
1741                                                 VECCOPY(cent, give_cursor());
1742                                                 Mat4Invert(base->object->imat, base->object->obmat);
1743                                                 Mat4MulVecfl(base->object->imat, cent);
1744                                                         
1745                                                         /* Curves need to be 2d, never offset in
1746                                                          * Z. Is a somewhat arbitrary restriction, 
1747                                                          * would probably be nice to remove.
1748                                                          */
1749                                                 cent[2]= 0.0;
1750                                         } else {
1751                                                 INIT_MINMAX(min, max);
1752         
1753                                                 nu= nu1;
1754                                                 while(nu) {
1755                                                         minmaxNurb(nu, min, max);
1756                                                         nu= nu->next;
1757                                                 }
1758                                                 
1759                                                 cent[0]= (min[0]+max[0])/2.0f;
1760                                                 cent[1]= (min[1]+max[1])/2.0f;
1761                                                 cent[2]= (min[2]+max[2])/2.0f;
1762                                         }
1763                                         
1764                                         nu= nu1;
1765                                         while(nu) {
1766                                                 if( (nu->type & 7)==1) {
1767                                                         a= nu->pntsu;
1768                                                         while (a--) {
1769                                                                 VecSubf(nu->bezt[a].vec[0], nu->bezt[a].vec[0], cent);
1770                                                                 VecSubf(nu->bezt[a].vec[1], nu->bezt[a].vec[1], cent);
1771                                                                 VecSubf(nu->bezt[a].vec[2], nu->bezt[a].vec[2], cent);
1772                                                         }
1773                                                 }
1774                                                 else {
1775                                                         a= nu->pntsu*nu->pntsv;
1776                                                         while (a--)
1777                                                                 VecSubf(nu->bp[a].vec, nu->bp[a].vec, cent);
1778                                                 }
1779                                                 nu= nu->next;
1780                                         }
1781                         
1782                                         if(centremode && G.obedit==0) {
1783                                                 Mat3CpyMat4(omat, base->object->obmat);
1784                                                 
1785                                                 Mat3MulVecfl(omat, cent);
1786                                                 base->object->loc[0]+= cent[0];
1787                                                 base->object->loc[1]+= cent[1];
1788                                                 base->object->loc[2]+= cent[2];
1789                                         }
1790                         
1791                                         if(G.obedit) {
1792                                                 makeDispList(G.obedit);
1793                                                 break;
1794                                         }
1795                                         else makeDispList(base->object);
1796         
1797                                 }
1798                                 else if(base->object->type==OB_FONT) {
1799                                         /* get from bb */
1800                                         
1801                                         cu= base->object->data;
1802                                         if(cu->bb==0) return;
1803                                         
1804                                         cu->xof= -0.5f*( cu->bb->vec[4][0] - cu->bb->vec[0][0]);
1805                                         cu->yof= -0.5f -0.5f*( cu->bb->vec[0][1] - cu->bb->vec[2][1]);  /* extra 0.5 is the height of above line */
1806                                         
1807                                         /* not really ok, do this better once! */
1808                                         cu->xof /= cu->fsize;
1809                                         cu->yof /= cu->fsize;
1810                                         
1811                                         text_to_curve(base->object, 0);
1812                                         makeDispList(base->object);
1813                                         
1814                                         allqueue(REDRAWBUTSEDIT, 0);
1815                                 }
1816                         }
1817                 }
1818                 base= base->next;
1819         }
1820
1821         allqueue(REDRAWVIEW3D, 0);
1822         BIF_undo_push("Do Centre");     
1823 }
1824
1825 void docentre_new(void)
1826 {
1827         if(G.scene->id.lib) return;
1828
1829         if(G.obedit) {
1830                 error("Unable to center new in Edit Mode");
1831         }
1832         else {
1833                 docentre(1);
1834         }
1835 }
1836
1837 void docentre_cursor(void)
1838 {
1839         if(G.scene->id.lib) return;
1840
1841         if(G.obedit) {
1842                 error("Unable to center cursor in Edit Mode");
1843         }
1844         else {
1845                 docentre(2);
1846         }
1847 }
1848
1849 void movetolayer(void)
1850 {
1851         Base *base;
1852         unsigned int lay= 0, local;
1853         int islamp= 0;
1854         
1855         if(G.scene->id.lib) return;
1856
1857         base= FIRSTBASE;
1858         while(base) {
1859                 if TESTBASE(base) lay |= base->lay;
1860                 base= base->next;
1861         }
1862         if(lay==0) return;
1863         lay &= 0xFFFFFF;
1864         
1865         if( movetolayer_buts(&lay)==0 ) return;
1866         if(lay==0) return;
1867
1868         base= FIRSTBASE;
1869         while(base) {
1870                 if TESTBASE(base) {
1871                         /* upper byte is used for local view */
1872                         local= base->lay & 0xFF000000;  
1873                         base->lay= lay + local;
1874                         base->object->lay= lay;
1875                         if(base->object->type==OB_LAMP) islamp= 1;
1876                 }
1877                 base= base->next;
1878         }
1879         
1880         if(islamp && G.vd->drawtype == OB_SHADED) reshadeall_displist();
1881
1882         countall();
1883         allqueue(REDRAWBUTSEDIT, 0);
1884         allqueue(REDRAWVIEW3D, 0);
1885         allqueue(REDRAWOOPS, 0);
1886         allqueue(REDRAWINFO, 0);
1887         
1888         BIF_undo_push("Move to layer");
1889 }
1890
1891
1892 void special_editmenu(void)
1893 {
1894         extern short editbutflag;
1895         extern float doublimit;
1896         float fac;
1897         int nr,ret;
1898         short randfac;
1899         
1900         if(G.obedit==0) {
1901                 if(G.f & G_FACESELECT) {
1902                         Mesh *me= get_mesh(OBACT);
1903                         TFace *tface;
1904                         int a;
1905                         
1906                         if(me==0 || me->tface==0) return;
1907                         
1908                         nr= pupmenu("Specials%t|Set     Tex%x1|         Shared%x2|         Light%x3|         Invisible%x4|         Collision%x5|Clr     Tex%x6|         Shared%x7|         Light%x8|         Invisible%x9|         Collision%x10");
1909         
1910                         for(a=me->totface, tface= me->tface; a>0; a--, tface++) {
1911                                 if(tface->flag & SELECT) {
1912                                         switch(nr) {
1913                                         case 1:
1914                                                 tface->mode |= TF_TEX; break;
1915                                         case 2:
1916                                                 tface->mode |= TF_SHAREDCOL; break;
1917                                         case 3:
1918                                                 tface->mode |= TF_LIGHT; break; 
1919                                         case 4:
1920                                                 tface->mode |= TF_INVISIBLE; break;
1921                                         case 5:
1922                                                 tface->mode |= TF_DYNAMIC; break;
1923                                         case 6:
1924                                                 tface->mode &= ~TF_TEX;
1925                                                 tface->tpage= 0;
1926                                                 break;
1927                                         case 7:
1928                                                 tface->mode &= ~TF_SHAREDCOL; break;
1929                                         case 8:
1930                                                 tface->mode &= ~TF_LIGHT; break;
1931                                         case 9:
1932                                                 tface->mode &= ~TF_INVISIBLE; break;
1933                                         case 10:
1934                                                 tface->mode &= ~TF_DYNAMIC; break;
1935                                         }
1936                                 }
1937                         }
1938                         allqueue(REDRAWVIEW3D, 0);
1939                         allqueue(REDRAWBUTSEDIT, 0);
1940                         BIF_undo_push("Change texture face");
1941                 }
1942                 else if(G.f & G_VERTEXPAINT) {
1943                         Mesh *me= get_mesh(OBACT);
1944                         
1945                         if(me==0 || (me->mcol==NULL && me->tface==NULL) ) return;
1946                         
1947                         nr= pupmenu("Specials%t|Shared VertexCol%x1");
1948                         if(nr==1) {
1949                                 
1950                                 if(me->tface) tface_to_mcol(me);
1951                                 
1952                                 copy_vpaint_undo( (unsigned int *)me->mcol, me->totface);
1953                                 do_shared_vertexcol(me);
1954                                 
1955                                 if(me->tface) mcol_to_tface(me, 1);
1956                                 BIF_undo_push("Shared VertexCol");
1957                         }
1958                 }
1959                 else {
1960                         Base *base, *base_select= NULL;
1961                         Object *ob= OBACT;
1962                         
1963                         // Get the active object mesh.
1964                         Mesh *me= get_mesh(ob);
1965
1966                         // If the active object is a mesh...
1967                         if (me && ob->id.lib==NULL) {
1968                                 // Bring up a little menu with the boolean operation choices on.
1969                                 nr= pupmenu("Boolean %t|Intersect%x1|Union%x2|Difference%x3");
1970
1971                                 if (nr > 0) {
1972                                         // user has made a choice of a menu element.
1973                                         // All of the boolean functions require 2 mesh objects 
1974                                         // we search through the object list to find the other 
1975                                         // selected item and make sure it is distinct and a mesh.
1976
1977                                         base= FIRSTBASE;
1978                                         while(base) {
1979                                                 if TESTBASELIB(base) {
1980                                                         if(base->object != OBACT) base_select= base;
1981                                                 }
1982
1983                                                 base= base->next;
1984                                         }
1985
1986                                         if (base_select) {
1987                                                 if (get_mesh(base_select->object)) {
1988                                                         waitcursor(1);
1989                                                         ret = NewBooleanMesh(BASACT,base_select,nr);
1990                                                         if (ret==0) {
1991                                                                 error("An internal error occurred -- sorry!");
1992                                                         } else if(ret==-1) {
1993                                                                 error("Selected meshes must have faces to perform boolean operations");
1994                                                         }
1995                                                         else BIF_undo_push("Boolean");
1996
1997                                                         waitcursor(0);
1998                                                 } else {
1999                                                         error("Please select 2 meshes");
2000                                                 }
2001                                         } else {
2002                                                 error("Please select 2 meshes");
2003                                         }
2004                                 }
2005
2006                                 allqueue(REDRAWVIEW3D, 0);
2007                         }
2008                 }
2009         }
2010         else if(G.obedit->type==OB_MESH) {
2011
2012                 nr= pupmenu("Specials%t|Subdivide%x1|Subdivide Fractal%x2|Subdivide Smooth%x3|Merge%x4|Remove Doubles%x5|Hide%x6|Reveal%x7|Select Swap%x8|Flip Normals %x9|Smooth %x10|Bevel %x11");
2013                 if(nr>0) waitcursor(1);
2014                 
2015                 switch(nr) {
2016                 case 1:
2017                         subdivideflag(1, 0.0, editbutflag);
2018                         BIF_undo_push("Subdivide");
2019                         break;
2020                 case 2:
2021                         randfac= 10;
2022                         if(button(&randfac, 1, 100, "Rand fac:")==0) return;
2023                         fac= -( (float)randfac )/100;
2024                         subdivideflag(1, fac, editbutflag);
2025                         BIF_undo_push("Subdivide Fractal");
2026                         break;
2027                 case 3:
2028                         subdivideflag(1, 0.0, editbutflag | B_SMOOTH);
2029                         BIF_undo_push("Subdivide Smooth");
2030                         break;
2031                 case 4:
2032                         mergemenu();
2033                         break;
2034                 case 5:
2035                         notice("Removed %d Vertices", removedoublesflag(1, doublimit));
2036                         BIF_undo_push("Remove Doubles");
2037                         break;
2038                 case 6:
2039                         hide_mesh(0);
2040                         break;
2041                 case 7:
2042                         reveal_mesh();
2043                         break;
2044                 case 8:
2045                         selectswap_mesh();
2046                         break;
2047                 case 9:
2048                         flip_editnormals();
2049                         BIF_undo_push("Flip Normals");
2050                         break;
2051                 case 10:
2052                         vertexsmooth();
2053                         break;
2054                 case 11:
2055                         bevel_menu();
2056                         break;
2057                 }               
2058                 
2059                 makeDispList(G.obedit);
2060                 
2061                 if(nr>0) waitcursor(0);
2062                 
2063         }
2064         else if ELEM(G.obedit->type, OB_CURVE, OB_SURF) {
2065
2066                 nr= pupmenu("Specials%t|Subdivide%x1|Switch Direction%x2");
2067                 
2068                 switch(nr) {
2069                 case 1:
2070                         subdivideNurb();
2071                         break;
2072                 case 2:
2073                         switchdirectionNurb2();
2074                         break;
2075                 }
2076         }
2077
2078         countall();
2079         allqueue(REDRAWVIEW3D, 0);
2080         
2081 }
2082
2083 void convertmenu(void)
2084 {
2085         Base *base, *basen, *basact, *basedel=NULL;
2086         Object *ob, *ob1;
2087         Curve *cu;
2088         MetaBall *mb;
2089         Mesh *me;
2090         DispList *dl;
2091         int ok=0, nr = 0, a;
2092         
2093         if(G.scene->id.lib) return;
2094
2095         ob= OBACT;
2096         if(ob==0) return;
2097         if(G.obedit) return;
2098         
2099         basact= BASACT; /* will be restored */
2100                 
2101         if(ob->type==OB_FONT) {
2102                 nr= pupmenu("Convert Font to%t|Curve");
2103                 if(nr>0) ok= 1;
2104         }
2105         else if(ob->type==OB_MBALL) {
2106                 nr= pupmenu("Convert Metaball to%t|Mesh (keep original)%x1|Mesh (Delete Original)%x2");
2107                 if(nr>0) ok= 1;
2108         }
2109         else if(ob->type==OB_CURVE) {
2110                 nr= pupmenu("Convert Curve to%t|Mesh");
2111                 if(nr>0) ok= 1;
2112         }
2113         else if(ob->type==OB_SURF) {
2114                 nr= pupmenu("Convert Nurbs Surface to%t|Mesh");
2115                 if(nr>0) ok= 1;
2116         }
2117         else if(ob->type==OB_MESH && mesh_uses_displist((Mesh*) ob->data)) {
2118                 nr= pupmenu("Convert SubSurf to%t|Mesh (Keep Original)%x1|Mesh (Delete Original)%x2");
2119                 if(nr>0) ok= 1;
2120         }
2121         if(ok==0) return;
2122
2123         /* don't forget multiple users! */
2124
2125         /* reset flags */
2126         base= FIRSTBASE;
2127         while(base) {
2128                 if TESTBASELIB(base) {
2129                         base->object->flag &= ~OB_DONE;
2130                 }
2131                 base= base->next;
2132         }
2133
2134         base= FIRSTBASE;
2135         while(base) {
2136                 if TESTBASELIB(base) {
2137                         
2138                         ob= base->object;
2139                         
2140                         if(ob->flag & OB_DONE);
2141                         else if(ob->type==OB_MESH) {
2142                                 Mesh *oldme= ob->data;
2143                                 
2144                                 if (mesh_uses_displist(oldme)) {
2145                                         DispListMesh *dlm;
2146                                         DerivedMesh *dm;
2147
2148                                         basedel = base;
2149
2150                                         ob->flag |= OB_DONE;
2151
2152                                         ob1= copy_object(ob);
2153
2154                                         basen= MEM_mallocN(sizeof(Base), "duplibase");
2155                                         *basen= *base;
2156                                         BLI_addhead(&G.scene->base, basen);     /* addhead: otherwise eternal loop */
2157                                         basen->object= ob1;
2158                                         basen->flag &= ~SELECT;
2159                                                 
2160                                         me= ob1->data;
2161                                         me->id.us--;
2162                                                 
2163                                         ob1->data= add_mesh();
2164                                         G.totmesh++;
2165                                         ob1->type= OB_MESH;
2166
2167                                         me= ob1->data;
2168                                         me->totcol= oldme->totcol;
2169                                         if(ob1->totcol) {
2170                                                 me->mat= MEM_dupallocN(oldme->mat);
2171                                                 for(a=0; a<ob1->totcol; a++) id_us_plus((ID *)me->mat[a]);
2172                                         }
2173                                         
2174                                         dm= subsurf_make_derived_from_mesh(oldme, oldme->subdiv);
2175                                         dlm= dm->convertToDispListMesh(dm);
2176                                         dm->release(dm);
2177
2178                                         displistmesh_to_mesh(dlm, ob1->data);
2179                                         displistmesh_free(dlm);
2180
2181                                         /* texspace and normals */
2182                                         BASACT= basen;
2183                                         enter_editmode();
2184                                         exit_editmode(1); // freedata, but no undo
2185                                         BASACT= basact;
2186                                         
2187                                 }
2188                         }
2189                         else if(ob->type==OB_FONT) {
2190                                 if(nr==1) {
2191                                 
2192                                         ob->flag |= OB_DONE;
2193                                 
2194                                         ob->type= OB_CURVE;
2195                                         cu= ob->data;
2196                                         
2197                                         if(cu->vfont) {
2198                                                 cu->vfont->id.us--;
2199                                                 cu->vfont= 0;
2200                                         }
2201                                         /* other users */
2202                                         if(cu->id.us>1) {
2203                                                 ob1= G.main->object.first;
2204                                                 while(ob1) {
2205                                                         if(ob1->data==cu) ob1->type= OB_CURVE;
2206                                                         ob1= ob1->id.next;
2207                                                 }
2208                                         }
2209                                 }
2210                         }
2211                         else if ELEM(ob->type, OB_CURVE, OB_SURF) {
2212                                 if(nr==1) {
2213
2214                                         ob->flag |= OB_DONE;
2215                                         cu= ob->data;
2216                                         
2217                                         dl= cu->disp.first;
2218                                         if(dl==0) makeDispList(ob);
2219
2220                                         nurbs_to_mesh(ob); /* also does users */
2221
2222                                         /* texspace and normals */
2223                                         BASACT= base;
2224                                         enter_editmode();
2225                                         exit_editmode(1); // freedata, but no undo
2226                                         BASACT= basact;
2227                                 }
2228                         }
2229                         else if(ob->type==OB_MBALL) {
2230                         
2231                                 if(nr==1 || nr == 2) {
2232                                         ob= find_basis_mball(ob);
2233                                         
2234                                         if(ob->disp.first && !(ob->flag&OB_DONE)) {
2235                                                 basedel = base;
2236                                         
2237                                                 ob->flag |= OB_DONE;
2238
2239                                                 ob1= copy_object(ob);
2240
2241                                                 basen= MEM_mallocN(sizeof(Base), "duplibase");
2242                                                 *basen= *base;
2243                                                 BLI_addhead(&G.scene->base, basen);     /* addhead: othwise eternal loop */
2244                                                 basen->object= ob1;
2245                                                 basen->flag &= ~SELECT;
2246                                                 
2247                                                 mb= ob1->data;
2248                                                 mb->id.us--;
2249                                                 
2250                                                 ob1->data= add_mesh();
2251                                                 G.totmesh++;
2252                                                 ob1->type= OB_MESH;
2253                                                 
2254                                                 me= ob1->data;
2255                                                 me->totcol= mb->totcol;
2256                                                 if(ob1->totcol) {
2257                                                         me->mat= MEM_dupallocN(mb->mat);
2258                                                         for(a=0; a<ob1->totcol; a++) id_us_plus((ID *)me->mat[a]);
2259                                                 }
2260                                                 
2261                                                 mball_to_mesh(&ob->disp, ob1->data);
2262                                                 tex_space_mesh(me);
2263                                         }
2264                                 }
2265                         }
2266                 }
2267                 base= base->next;
2268                 if(basedel != NULL && nr == 2)
2269                         free_and_unlink_base(basedel);  
2270                 basedel = NULL;                         
2271         }
2272         
2273         test_scene_constraints();       // always call after delete object
2274         
2275         countall();
2276         allqueue(REDRAWVIEW3D, 0);
2277         allqueue(REDRAWOOPS, 0);
2278         allqueue(REDRAWBUTSEDIT, 0);
2279         BIF_undo_push("Convert Object");
2280 }
2281
2282         /* Change subdivision properties of mesh object ob, if
2283          * level==-1 then toggle subsurf, else set to level.
2284          */
2285 void flip_subdivison(Object *ob, int level)
2286 {
2287         Mesh *me = ob->data;
2288
2289         if (level == -1) {
2290                 me->flag ^= ME_SUBSURF;
2291         } else {
2292                 me->subdiv = level;
2293         }
2294
2295         allqueue(REDRAWVIEW3D, 0);
2296         allqueue(REDRAWOOPS, 0);
2297         allqueue(REDRAWBUTSEDIT, 0);
2298         makeDispList(ob);
2299         
2300         BIF_undo_push("Switch subsurf on/off");
2301 }
2302  
2303 void copymenu_properties(Object *ob)
2304 {       
2305         bProperty *prop, *propn, *propc;
2306         Base *base;
2307         int nr, tot=0;
2308         char *str;
2309         
2310         prop= ob->prop.first;
2311         while(prop) {
2312                 tot++;
2313                 prop= prop->next;
2314         }
2315         
2316         if(tot==0) {
2317                 error("No properties in the active object to copy");
2318                 return;
2319         }
2320         
2321         str= MEM_callocN(24+32*tot, "copymenu prop");
2322         
2323         strcpy(str, "Copy Property %t");
2324         
2325         tot= 0; 
2326         prop= ob->prop.first;
2327         while(prop) {
2328                 tot++;
2329                 strcat(str, " |");
2330                 strcat(str, prop->name);
2331                 prop= prop->next;
2332         }
2333
2334         nr= pupmenu(str);
2335         if(nr>0) {
2336                 tot= 0;
2337                 prop= ob->prop.first;
2338                 while(prop) {
2339                         tot++;
2340                         if(tot==nr) break;
2341                         prop= prop->next;
2342                 }
2343                 if(prop) {
2344                         propc= prop;
2345                         
2346                         base= FIRSTBASE;
2347                         while(base) {
2348                                 if(base != BASACT) {
2349                                         if(TESTBASELIB(base)) {
2350                                                 prop= get_property(base->object, propc->name);
2351                                                 if(prop) {
2352                                                         free_property(prop);
2353                                                         BLI_remlink(&base->object->prop, prop);
2354                                                 }
2355                                                 propn= copy_property(propc);
2356                                                 BLI_addtail(&base->object->prop, propn);
2357                                         }
2358                                 }
2359                                 base= base->next;
2360                         }
2361                 }
2362         }
2363         MEM_freeN(str);
2364         allqueue(REDRAWVIEW3D, 0);
2365         
2366         BIF_undo_push("Copy properties");
2367 }
2368
2369 void copymenu_logicbricks(Object *ob)
2370 {
2371         Base *base;
2372         
2373         base= FIRSTBASE;
2374         while(base) {
2375                 if(base->object != ob) {
2376                         if(TESTBASELIB(base)) {
2377                                 
2378                                 /* first: free all logic */
2379                                 free_sensors(&base->object->sensors);                           
2380                                 unlink_controllers(&base->object->controllers);
2381                                 free_controllers(&base->object->controllers);
2382                                 unlink_actuators(&base->object->actuators);
2383                                 free_actuators(&base->object->actuators);
2384                                 
2385                                 /* now copy it, this also works without logicbricks! */
2386                                 clear_sca_new_poins_ob(ob);
2387                                 copy_sensors(&base->object->sensors, &ob->sensors);
2388                                 copy_controllers(&base->object->controllers, &ob->controllers);
2389                                 copy_actuators(&base->object->actuators, &ob->actuators);
2390                                 set_sca_new_poins_ob(base->object);
2391                                 
2392                                 /* some menu settings */
2393                                 base->object->scavisflag= ob->scavisflag;
2394                                 base->object->scaflag= ob->scaflag;
2395                                 
2396                         }
2397                 }
2398                 base= base->next;
2399         }
2400         BIF_undo_push("Copy logic");
2401 }
2402
2403 void copy_attr_menu()
2404 {
2405         Object *ob;
2406         short event;
2407         char str[256];
2408
2409         /* If you change this menu, don't forget to update the menu in header_view3d.c
2410          * view3d_edit_object_copyattrmenu() and in toolbox.c
2411          */
2412         strcpy(str, "Copy Attributes %t|Location%x1|Rotation%x2|Size%x3|Drawtype%x4|Time Offset%x5|Dupli%x6|%l|Mass%x7|Damping%x8|Properties%x9|Logic Bricks%x10|%l");
2413
2414         if(!(ob=OBACT)) return;
2415         
2416         strcat (str, "|Object Constraints%x22");
2417         
2418         if ELEM5(ob->type, OB_MESH, OB_CURVE, OB_SURF, OB_FONT, OB_MBALL) {
2419                 strcat(str, "|Texture Space%x17");
2420         }       
2421         
2422         if(ob->type == OB_FONT) strcat(str, "|Font Settings%x18|Bevel Settings%x19");
2423         if(ob->type == OB_CURVE) strcat(str, "|Bevel Settings%x19");
2424
2425         if(ob->type==OB_MESH){
2426                 strcat(str, "|Subdiv%x21");
2427         }
2428
2429         if( give_parteff(ob) ) strcat(str, "|Particle Settings%x20");
2430
2431         if(ob->soft) strcat(str, "|Soft Body Settings%x23");
2432         
2433         event= pupmenu(str);
2434         if(event<= 0) return;
2435         
2436         copy_attr(event);
2437 }
2438
2439 void copy_attr(short event)
2440 {
2441         Object *ob, *obt;
2442         Base *base;
2443         Curve *cu, *cu1;
2444         void *poin1, *poin2=0;
2445         
2446         if(G.scene->id.lib) return;
2447
2448         if(!(ob=OBACT)) return;
2449         
2450         if(G.obedit) {
2451                 /* obedit_copymenu(); */
2452                 return;
2453         }
2454         
2455         if ELEM5(ob->type, OB_MESH, OB_CURVE, OB_SURF, OB_FONT, OB_MBALL) {
2456                 if(ob->type==OB_MESH) poin2= &(((Mesh *)ob->data)->texflag);
2457                 else if ELEM3(ob->type, OB_CURVE, OB_SURF, OB_FONT) poin2= &(((Curve *)ob->data)->texflag);
2458                 else if(ob->type==OB_MBALL) poin2= &(((MetaBall *)ob->data)->texflag);
2459         }       
2460
2461         if(event==9) {
2462                 copymenu_properties(ob);
2463                 return;
2464         }
2465         else if(event==10) {
2466                 copymenu_logicbricks(ob);
2467                 return;
2468         }
2469
2470         base= FIRSTBASE;
2471         while(base) {
2472                 if(base != BASACT) {
2473                         if(TESTBASELIB(base)) {
2474                                 
2475                                 if(event==1) {  /* loc */
2476                                         VECCOPY(base->object->loc, ob->loc);
2477                                         VECCOPY(base->object->dloc, ob->dloc);
2478                                 }
2479                                 else if(event==2) {  /* rot */
2480                                         VECCOPY(base->object->rot, ob->rot);
2481                                         VECCOPY(base->object->drot, ob->drot);
2482                                         VECCOPY(base->object->quat, ob->quat);
2483                                         VECCOPY(base->object->dquat, ob->dquat);
2484                                 }
2485                                 else if(event==3) {  /* size */
2486                                         VECCOPY(base->object->size, ob->size);
2487                                         VECCOPY(base->object->dsize, ob->dsize);
2488                                 }
2489                                 else if(event==4) {  /* drawtype */
2490                                         base->object->dt= ob->dt;
2491                                         base->object->dtx= ob->dtx;
2492                                         }
2493                                 else if(event==5) {  /* time offs */
2494                                         base->object->sf= ob->sf;
2495                                 }
2496                                 else if(event==6) {  /* dupli */
2497                                         base->object->dupon= ob->dupon;
2498                                         base->object->dupoff= ob->dupoff;
2499                                         base->object->dupsta= ob->dupsta;
2500                                         base->object->dupend= ob->dupend;
2501                                         
2502                                         base->object->transflag &= ~OB_DUPLI;
2503                                         base->object->transflag |= (ob->transflag & OB_DUPLI);
2504                                 }
2505                                 else if(event==7) {     /* mass */
2506                                         base->object->mass= ob->mass;
2507                                 }
2508                                 else if(event==8) {     /* damping */
2509                                         base->object->damping= ob->damping;
2510                                         base->object->rdamping= ob->rdamping;
2511                                 }
2512                                 else if(event==17) {    /* tex space */
2513                                         obt= base->object;
2514                                         poin1= 0;
2515                                         if(obt->type==OB_MESH) poin1= &(((Mesh *)obt->data)->texflag);
2516                                         else if ELEM3(obt->type, OB_CURVE, OB_SURF, OB_FONT) poin1= &(((Curve *)obt->data)->texflag);
2517                                         else if(obt->type==OB_MBALL) poin1= &(((MetaBall *)obt->data)->texflag);                                        
2518                                         
2519                                         if(poin1) {
2520                                                 memcpy(poin1, poin2, 4+12+12+12);
2521                                         
2522                                                 if(obt->type==OB_MESH) tex_space_mesh(obt->data);
2523                                                 else if(obt->type==OB_MBALL) tex_space_mball(obt);
2524                                                 else tex_space_curve(obt->data);
2525                                         }
2526                                 }
2527                                 else if(event==18) {    /* font settings */
2528                                         
2529                                         if(base->object->type==ob->type) {
2530                                                 cu= ob->data;
2531                                                 cu1= base->object->data;
2532                                                 
2533                                                 cu1->spacemode= cu->spacemode;
2534                                                 cu1->spacing= cu->spacing;
2535                                                 cu1->linedist= cu->linedist;
2536                                                 cu1->shear= cu->shear;
2537                                                 cu1->fsize= cu->fsize;
2538                                                 cu1->xof= cu->xof;
2539                                                 cu1->yof= cu->yof;
2540                                                 cu1->textoncurve= cu->textoncurve;
2541                                                 if(cu1->vfont) cu1->vfont->id.us--;
2542                                                 cu1->vfont= cu->vfont;
2543                                                 id_us_plus((ID *)cu1->vfont);
2544                                                 text_to_curve(base->object, 0);
2545                                                 
2546                                                 strcpy(cu1->family, cu->family);
2547                                                 
2548                                                 makeDispList(base->object);
2549                                         }
2550                                 }
2551                                 else if(event==19) {    /* bevel settings */
2552                                         
2553                                         if ELEM(base->object->type, OB_CURVE, OB_FONT) {
2554                                                 cu= ob->data;
2555                                                 cu1= base->object->data;
2556                                                 
2557                                                 cu1->bevobj= cu->bevobj;
2558                                                 cu1->taperobj= cu->taperobj;
2559                                                 cu1->width= cu->width;
2560                                                 cu1->bevresol= cu->bevresol;
2561                                                 cu1->ext1= cu->ext1;
2562                                                 cu1->ext2= cu->ext2;
2563                                                 
2564                                                 makeDispList(base->object);
2565                                         }
2566                                 }
2567                                 else if(event==20) {    /* particle settings */
2568                                         PartEff *pa1, *pa2;
2569                                         char *p1, *p2;
2570                                         
2571                                         pa1= give_parteff(ob);
2572                                         pa2= give_parteff(base->object);
2573
2574                                         if(pa1==0 && pa2) {
2575                                                 BLI_remlink( &(base->object->effect), pa2);
2576                                                 free_effect( (Effect *) pa2);
2577                                         }
2578                                         else if(pa1 && pa2==0) {
2579                                                 free_effects(&(base->object->effect));
2580                                                 copy_effects(&(base->object->effect), &ob->effect);
2581                                                 build_particle_system(base->object);
2582                                         }
2583                                         else if(pa1 && pa2) {
2584                                                 if(pa2->keys) MEM_freeN(pa2->keys);
2585                                                 
2586                                                 p1= (char *)pa1; p2= (char *)pa2;
2587                                                 memcpy( p2+8, p1+8, sizeof(PartEff) - 8);
2588                                                 pa2->keys= 0;
2589                                                 
2590                                                 build_particle_system(base->object);
2591                                         }
2592                                 }
2593                                 else if(event==21){
2594                                         if (base->object->type==OB_MESH) {
2595                                                 Mesh *targetme= base->object->data;
2596                                                 Mesh *sourceme= ob->data;
2597
2598                                                 targetme->flag= (targetme->flag&~ME_SUBSURF) | (sourceme->flag&ME_SUBSURF);
2599                                                 targetme->subsurftype = sourceme->subsurftype;
2600                                                 targetme->subdiv= sourceme->subdiv;
2601                                                 targetme->subdivr= sourceme->subdivr;
2602                                                 makeDispList(base->object);
2603                                         }
2604                                 }
2605                                 else if(event==22) {
2606                                         /* Clear the constraints on the target */
2607                                         free_constraints(&base->object->constraints);
2608                                         free_constraint_channels(&base->object->constraintChannels);
2609
2610                                         /* Copy the constraint channels over */
2611                                         copy_constraints(&base->object->constraints, &ob->constraints);
2612                                         if (U.dupflag& USER_DUP_IPO)
2613                                                 copy_constraint_channels(&base->object->constraintChannels, &ob->constraintChannels);
2614                                         else
2615                                                 clone_constraint_channels (&base->object->constraintChannels, &ob->constraintChannels, NULL);
2616
2617                                         base->object->activecon = NULL;
2618                                 }
2619                                 else if(event==23) {
2620                                         base->object->softflag= ob->softflag;
2621                                         if(base->object->soft) sbFree(base->object->soft);
2622                                         
2623                                         base->object->soft= copy_softbody(ob->soft);
2624                                 }
2625                         }
2626                 }
2627                 base= base->next;
2628         }
2629         
2630         allqueue(REDRAWVIEW3D, 0);
2631         if(event==20) {
2632                 allqueue(REDRAWBUTSOBJECT, 0);
2633         }
2634         
2635         BIF_undo_push("Copy attributes");
2636 }
2637
2638 void link_to_scene(unsigned short nr)
2639 {       
2640         Scene *sce= (Scene*) BLI_findlink(&G.main->scene, G.curscreen->scenenr-1);
2641         Base *base, *nbase;
2642         
2643         if(sce==0) return;
2644         if(sce->id.lib) return;
2645         
2646         base= FIRSTBASE;
2647         while(base) {
2648                 if(TESTBASE(base)) {
2649                         
2650                         nbase= MEM_mallocN( sizeof(Base), "newbase");
2651                         *nbase= *base;
2652                         BLI_addhead( &(sce->base), nbase);
2653                         id_us_plus((ID *)base->object);
2654                 }
2655                 base= base->next;
2656         }
2657 }
2658
2659 void make_links_menu()
2660 {
2661         Object *ob;
2662         short event=0;
2663         char str[140];
2664         
2665         if(!(ob=OBACT)) return;
2666         
2667         strcpy(str, "Make Links %t|To Scene...%x1|%l|Object Ipo%x4");
2668         
2669         if(ob->type==OB_MESH)
2670                 strcat(str, "|Mesh Data%x2|Materials%x3");
2671         else if(ob->type==OB_CURVE)
2672                 strcat(str, "|Curve Data%x2|Materials%x3");
2673         else if(ob->type==OB_FONT)
2674                 strcat(str, "|Text Data%x2|Materials%x3");
2675         else if(ob->type==OB_SURF)
2676                 strcat(str, "|Surface Data%x2|Materials%x3");
2677         else if(ob->type==OB_MBALL)
2678                 strcat(str, "|Materials%x3");
2679         else if(ob->type==OB_CAMERA)
2680                 strcat(str, "|Camera Data%x2");
2681         else if(ob->type==OB_LAMP)
2682                 strcat(str, "|Lamp Data%x2");
2683         else if(ob->type==OB_LATTICE)
2684                 strcat(str, "|Lattice Data%x2");
2685         else if(ob->type==OB_ARMATURE)
2686                 strcat(str, "|Armature Data%x2");
2687
2688         event= pupmenu(str);
2689
2690         if(event<= 0) return;
2691         
2692         make_links(event);
2693 }
2694
2695 void make_links(short event)
2696 {
2697         Object *ob, *obt;
2698         Base *base, *nbase, *sbase;
2699         Scene *sce = NULL;
2700         ID *id;
2701         Material ***matarar, ***obmatarar, **matar1, **matar2;
2702         int a;
2703         short *totcolp, nr;
2704         char *strp;
2705
2706         if(!(ob=OBACT)) return;
2707
2708         if(event==1) {
2709                 IDnames_to_pupstring(&strp, NULL, NULL, &(G.main->scene), 0, &nr);
2710                 
2711                 if(strncmp(strp, "DataBrow", 8)==0) {
2712                         MEM_freeN(strp);
2713
2714                         activate_databrowse((ID *)G.scene, ID_SCE, 0, B_INFOSCE, &(G.curscreen->scenenr), link_to_scene );
2715                         
2716                         return;                 
2717                 }
2718                 else {
2719                         event= pupmenu(strp);
2720                         MEM_freeN(strp);
2721                 
2722                         if(event<= 0) return;
2723                 
2724                         nr= 1;
2725                         sce= G.main->scene.first;
2726                         while(sce) {
2727                                 if(nr==event) break;
2728                                 nr++;
2729                                 sce= sce->id.next;
2730                         }
2731                         if(sce==G.scene) {
2732                                 error("This is the current scene");
2733                                 return;
2734                         }
2735                         if(sce==0 || sce->id.lib) return;
2736                         
2737                         /* remember: is needed below */
2738                         event= 1;
2739                 }
2740         }
2741
2742         base= FIRSTBASE;
2743         while(base) {
2744                 if(event==1 || base != BASACT) {
2745                         
2746                         obt= base->object;
2747
2748                         if(TESTBASE(base)) {
2749                                 
2750                                 if(event==1) {          /* to scene */
2751                                         
2752                                         /* test if already linked */
2753                                         sbase= sce->base.first;
2754                                         while(sbase) {
2755                                                 if(sbase->object==base->object) break;
2756                                                 sbase= sbase->next;
2757                                         }
2758                                         if(sbase) {     /* remove */
2759                                                 base= base->next;
2760                                                 continue;
2761                                         }
2762                                         
2763                                         nbase= MEM_mallocN( sizeof(Base), "newbase");
2764                                         *nbase= *base;
2765                                         BLI_addhead( &(sce->base), nbase);
2766                                         id_us_plus((ID *)base->object);
2767                                 }
2768                         }
2769                         if(TESTBASELIB(base)) {
2770                                 if(event==2 || event==5) {  /* obdata */
2771                                         if(ob->type==obt->type) {
2772                                                 
2773                                                         id= obt->data;
2774                                                         id->us--;
2775                                                         
2776                                                         id= ob->data;
2777                                                         id_us_plus(id);
2778                                                         obt->data= id;
2779                                                         
2780                                                         /* if amount of material indices changed: */
2781                                                         test_object_materials(obt->data);
2782                                                 }
2783                                         }
2784                                 else if(event==4) {  /* ob ipo */
2785                                         if(obt->ipo) obt->ipo->id.us--;
2786                                         obt->ipo= ob->ipo;
2787                                         if(obt->ipo) {
2788                                                 id_us_plus((ID *)obt->ipo);
2789                                                 do_ob_ipo(obt);
2790                                         }
2791                                 }
2792                                 else if(event==3) {  /* materials */
2793                                         
2794                                         /* only if obt has no material: make arrays */
2795                                         /* from ob to obt! */
2796                                         
2797                                         obmatarar= give_matarar(ob);
2798                                         matarar= give_matarar(obt);
2799                                         totcolp= give_totcolp(obt);
2800
2801                                         /* if one of the two is zero: no render-able object */                                          
2802                                         if( matarar && obmatarar) {
2803                                                 
2804                                                 /* take care of users! so first a copy of original: */
2805
2806                                                 if(ob->totcol) {
2807                                                         matar1= MEM_dupallocN(ob->mat);
2808                                                         matar2= MEM_dupallocN(*obmatarar);
2809                                                 }
2810                                                 else {
2811                                                         matar1= matar2= 0;
2812                                                 }
2813                                                 
2814                                                 /* remove links from obt */
2815                                                 for(a=0; a<obt->totcol; a++) {
2816                                                         if(obt->mat[a]) obt->mat[a]->id.us--;
2817                                                         if( (*matarar)[a]) (*matarar)[a]->id.us--;
2818                                                 }
2819                                                 
2820                                                 /* free */
2821                                                 if(obt->mat) MEM_freeN(obt->mat);
2822                                                 if(*matarar) MEM_freeN(*matarar);
2823                                                 
2824                                                 /* connect a copy */
2825                                                 obt->mat= matar1;
2826                                                 *matarar= matar2;
2827                                                 obt->totcol= ob->totcol;
2828                                                 *totcolp= ob->totcol;
2829                                         
2830                                                 /* increase users */
2831                                                 for(a=0; a<obt->totcol; a++) {
2832                                                         if(obt->mat[a]) id_us_plus((ID *)obt->mat[a]);
2833                                                         if( (*matarar)[a]) id_us_plus((ID *)(*matarar)[a]);
2834                                                 }
2835
2836                                                 obt->colbits= ob->colbits;
2837                                                 
2838                                                 /* if amount of material indices changed: */
2839                                                 test_object_materials(obt->data);
2840                                         }
2841                                 }
2842                         }
2843                 }
2844                 base= base->next;
2845         }
2846         
2847         allqueue(REDRAWVIEW3D, 0);
2848         allqueue(REDRAWOOPS, 0);
2849         allqueue(REDRAWBUTSHEAD, 0);
2850         
2851         BIF_undo_push("Create links");
2852 }
2853
2854 void make_duplilist_real()
2855 {
2856         Base *base, *basen;
2857         Object *ob;
2858         extern ListBase duplilist;
2859         
2860         if(okee("Make dupli objects real")==0) return;
2861         
2862         base= FIRSTBASE;
2863         while(base) {
2864                 if TESTBASELIB(base) {
2865
2866                         if(base->object->transflag & OB_DUPLI) {
2867                                 
2868                                 make_duplilist(G.scene, base->object);
2869                                 ob= duplilist.first;
2870                                 while(ob) {
2871                                         
2872                                         /* font duplis can have a totcol without material, we get them from parent
2873                                          * should be implemented better...
2874                                          */
2875                                         if(ob->mat==0) ob->totcol= 0;
2876                                         
2877                                         basen= MEM_dupallocN(base);
2878                                         basen->flag &= ~OB_FROMDUPLI;
2879                                         BLI_addhead(&G.scene->base, basen);     /* addhead: othwise eternal loop */
2880                                         ob->ipo= 0;             /* make sure apply works */
2881                                         ob->parent= ob->track= 0;
2882                                         ob-&g