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