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