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