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