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