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