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