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