397dc352a94ff49838fb6785569181ec147e7764
[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         CTX_DATA_BEGIN(C, Base*, base, visible_bases) {
1124                 if (base->flag & SELECT)
1125                         ED_base_object_select(base, BA_DESELECT);
1126                 else
1127                         ED_base_object_select(base, BA_SELECT);
1128         }
1129         CTX_DATA_END;
1130         
1131         /* undo? */
1132         WM_event_add_notifier(C, NC_SCENE|ND_OB_SELECT, CTX_data_scene(C));
1133         
1134         return OPERATOR_FINISHED;
1135 }
1136
1137 void OBJECT_OT_select_invert(wmOperatorType *ot)
1138 {
1139         
1140         /* identifiers */
1141         ot->name= "Invert selection";
1142         ot->idname= "OBJECT_OT_select_invert";
1143         
1144         /* api callbacks */
1145         ot->exec= object_select_invert_exec;
1146         ot->poll= ED_operator_view3d_active;
1147
1148 }
1149 /* ****** (de)select All *******/
1150
1151 static int object_de_select_all_exec(bContext *C, wmOperator *op)
1152 {
1153         int a=0, ok=0; 
1154         
1155         CTX_DATA_BEGIN(C, Base*, base, visible_bases) {
1156                 if (base->flag & SELECT) {
1157                         ok= a= 1;
1158                         break;
1159                 }
1160                 else ok=1;
1161         }
1162         CTX_DATA_END;
1163         
1164         if (!ok) return OPERATOR_PASS_THROUGH;
1165         
1166         CTX_DATA_BEGIN(C, Base*, base, visible_bases) {
1167                 if (a) ED_base_object_select(base, BA_DESELECT);
1168                 else ED_base_object_select(base, BA_SELECT);
1169         }
1170         CTX_DATA_END;
1171         
1172         /* undo? */
1173         WM_event_add_notifier(C, NC_SCENE|ND_OB_SELECT, CTX_data_scene(C));
1174         
1175         return OPERATOR_FINISHED;
1176 }
1177
1178 void OBJECT_OT_de_select_all(wmOperatorType *ot)
1179 {
1180         
1181         /* identifiers */
1182         ot->name= "deselect all";
1183         ot->idname= "OBJECT_OT_de_select_all";
1184         
1185         /* api callbacks */
1186         ot->exec= object_de_select_all_exec;
1187         ot->poll= ED_operator_view3d_active;
1188
1189 }
1190 /* ****** random selection *******/
1191
1192 static int object_select_random_exec(bContext *C, wmOperator *op)
1193 {       
1194         int percent;
1195         
1196         percent = RNA_int_get(op->ptr, "percent");
1197                 
1198         CTX_DATA_BEGIN(C, Base*, base, visible_bases) {
1199                 if ((!base->flag & SELECT && (BLI_frand() * 100) < percent)) {
1200                                 ED_base_object_select(base, BA_SELECT);
1201                 }
1202         }
1203         CTX_DATA_END;
1204         
1205         /* undo? */
1206         WM_event_add_notifier(C, NC_SCENE|ND_OB_SELECT, CTX_data_scene(C));
1207         
1208         return OPERATOR_FINISHED;
1209 }
1210
1211 void OBJECT_OT_select_random(wmOperatorType *ot)
1212 {
1213         PropertyRNA *prop;
1214         
1215         /* identifiers */
1216         ot->name= "Random selection";
1217         ot->idname= "OBJECT_OT_select_random";
1218         
1219         /* api callbacks */
1220         /*ot->invoke= object_select_random_invoke XXX - need a number popup ;*/
1221         ot->exec = object_select_random_exec;
1222         ot->poll= ED_operator_view3d_active;
1223         
1224         prop = RNA_def_property(ot->srna, "percent", PROP_INT, PROP_NONE);
1225         RNA_def_property_ui_range(prop, 1, 100,1, 1);
1226         RNA_def_property_ui_text(prop, "Percent", "Max persentage that will be selected");
1227         RNA_def_property_int_default(prop, 50);
1228 }
1229 void clear_track(Scene *scene, View3D *v3d)
1230 {
1231         Base *base;
1232         int mode;
1233         
1234         if(G.obedit) return;
1235         if(scene->id.lib) return;
1236
1237         mode= pupmenu("OK? %t|Clear Track %x1| Clear Track and Keep Transform %x2");
1238
1239         if(mode<1) return;
1240
1241         for(base= FIRSTBASE; base; base= base->next) {
1242                 if(TESTBASELIB(v3d, base)) {
1243                         base->object->track= NULL;
1244                         base->object->recalc |= OB_RECALC;
1245                         
1246                         if(mode==2) {
1247                                 apply_obmat(base->object);
1248                         }                       
1249                 }
1250         }
1251
1252         DAG_scene_sort(scene);
1253         allqueue(REDRAWVIEW3D, 0);
1254         allqueue(REDRAWOOPS, 0);
1255         
1256         BIF_undo_push("Clear Track");   
1257 }
1258
1259 void clear_object(Scene *scene, View3D *v3d, char mode)
1260 {
1261         Base *base;
1262         Object *ob;
1263         float *v1, *v3, mat[3][3];
1264         int armature_clear= 0;
1265         char *str=NULL;
1266         
1267         if(G.obedit) return;
1268         if(scene->id.lib) return;
1269         
1270         if(mode=='r') str= "Clear rotation";
1271         else if(mode=='g') str= "Clear location";
1272         else if(mode=='s') str= "Clear scale";
1273         else if(mode=='o') str= "Clear origin";
1274         else return;
1275         
1276         for(base= FIRSTBASE; base; base= base->next) {
1277                 if(TESTBASELIB(v3d, base)) {
1278                         ob= base->object;
1279                         
1280                         if ((ob->flag & OB_POSEMODE)) {
1281                                 /* only clear pose transforms if:
1282                                  *      - with a mesh in weightpaint mode, it's related armature needs to be cleared
1283                                  *      - with clearing transform of object being edited at the time
1284                                  */
1285                                 if ((G.f & G_WEIGHTPAINT) || (ob==OBACT)) {
1286 // XXX                                  clear_armature(ob, mode);
1287                                         armature_clear= 1;      /* silly system to prevent another dag update, so no action applied */
1288                                 }
1289                         }
1290                         else if((G.f & G_WEIGHTPAINT)==0) {
1291                                 /* only clear transforms of 'normal' (not armature) object if:
1292                                  *      - not in weightpaint mode or editmode
1293                                  *      - if that object's transform locks are not enabled (this is done on a per-channel basis)
1294                                  */
1295                                 if (mode=='r') {
1296                                         /* eulers can only get cleared if they are not protected */
1297                                         if ((ob->protectflag & OB_LOCK_ROTX)==0)
1298                                                 ob->rot[0]= ob->drot[0]= 0.0f;
1299                                         if ((ob->protectflag & OB_LOCK_ROTY)==0)
1300                                                 ob->rot[1]= ob->drot[1]= 0.0f;
1301                                         if ((ob->protectflag & OB_LOCK_ROTZ)==0)
1302                                                 ob->rot[2]= ob->drot[2]= 0.0f;
1303                                         
1304                                         /* quats here are not really used anymore anywhere, so it probably doesn't 
1305                                          * matter to not clear them whether the euler-based rotation is used
1306                                          */
1307                                         /*QuatOne(ob->quat);
1308                                         QuatOne(ob->dquat);*/
1309                                         
1310
1311                                 }
1312                                 else if (mode=='g') {
1313                                         if ((ob->protectflag & OB_LOCK_LOCX)==0)
1314                                                 ob->loc[0]= ob->dloc[0]= 0.0f;
1315                                         if ((ob->protectflag & OB_LOCK_LOCY)==0)
1316                                                 ob->loc[1]= ob->dloc[1]= 0.0f;
1317                                         if ((ob->protectflag & OB_LOCK_LOCZ)==0)
1318                                                 ob->loc[2]= ob->dloc[2]= 0.0f;
1319                                         
1320                                 }
1321                                 else if (mode=='s') {
1322                                         if ((ob->protectflag & OB_LOCK_SCALEX)==0) {
1323                                                 ob->dsize[0]= 0.0f;
1324                                                 ob->size[0]= 1.0f;
1325                                         }
1326                                         if ((ob->protectflag & OB_LOCK_SCALEY)==0) {
1327                                                 ob->dsize[1]= 0.0f;
1328                                                 ob->size[1]= 1.0f;
1329                                         }
1330                                         if ((ob->protectflag & OB_LOCK_SCALEZ)==0) {
1331                                                 ob->dsize[2]= 0.0f;
1332                                                 ob->size[2]= 1.0f;
1333                                         }
1334                                 }
1335                                 else if(mode=='o') {
1336                                         if(ob->parent) {
1337                                                 v1= ob->loc;
1338                                                 v3= ob->parentinv[3];
1339                                                 
1340                                                 Mat3CpyMat4(mat, ob->parentinv);
1341                                                 VECCOPY(v3, v1);
1342                                                 v3[0]= -v3[0];
1343                                                 v3[1]= -v3[1];
1344                                                 v3[2]= -v3[2];
1345                                                 Mat3MulVecfl(mat, v3);
1346                                         }
1347                                 }
1348                                 
1349                                 ob->recalc |= OB_RECALC_OB;
1350                         }                       
1351                 }
1352         }
1353         
1354         allqueue(REDRAWVIEW3D, 0);
1355         if(armature_clear==0) /* in this case flush was done */
1356                 ED_anim_dag_flush_update(C);    
1357         BIF_undo_push(str);
1358 }
1359
1360 void reset_slowparents(Scene *scene, View3D *v3d)
1361 {
1362         /* back to original locations */
1363         Base *base;
1364
1365         for(base= FIRSTBASE; base; base= base->next) {
1366                 if(base->object->parent) {
1367                         if(base->object->partype & PARSLOW) {
1368                                 base->object->partype -= PARSLOW;
1369                                 where_is_object(base->object);
1370                                 base->object->partype |= PARSLOW;
1371                         }
1372                 }
1373         }
1374 }
1375
1376 void set_slowparent(Scene *scene, View3D *v3d)
1377 {
1378         Base *base;
1379
1380         if( okee("Set slow parent")==0 ) return;
1381
1382         for(base= FIRSTBASE; base; base= base->next) {
1383                 if(TESTBASELIB(v3d, base)) {
1384                         if(base->object->parent) base->object->partype |= PARSLOW;
1385                 }
1386         }
1387         BIF_undo_push("Slow parent");
1388 }
1389
1390 // XXX
1391 #define BEZSELECTED_HIDDENHANDLES(bezt)   ((G.f & G_HIDDENHANDLES) ? (bezt)->f2 & SELECT : BEZSELECTED(bezt))
1392 void make_vertex_parent(Scene *scene, View3D *v3d)
1393 {
1394         EditMesh *em = G.editMesh;
1395         EditVert *eve;
1396         Base *base;
1397         Nurb *nu;
1398         BezTriple *bezt;
1399         BPoint *bp;
1400         Object *par, *ob;
1401         int a, v1=0, v2=0, v3=0, v4=0, nr=1;
1402         
1403         /* we need 1 to 3 selected vertices */
1404         
1405         if(G.obedit->type==OB_MESH) {
1406                 eve= em->verts.first;
1407                 while(eve) {
1408                         if(eve->f & 1) {
1409                                 if(v1==0) v1= nr;
1410                                 else if(v2==0) v2= nr;
1411                                 else if(v3==0) v3= nr;
1412                                 else if(v4==0) v4= nr;
1413                                 else break;
1414                         }
1415                         nr++;
1416                         eve= eve->next;
1417                 }
1418         }
1419         else if(ELEM(G.obedit->type, OB_SURF, OB_CURVE)) {
1420                 extern ListBase editNurb;
1421                 nu= editNurb.first;
1422                 while(nu) {
1423                         if((nu->type & 7)==CU_BEZIER) {
1424                                 bezt= nu->bezt;
1425                                 a= nu->pntsu;
1426                                 while(a--) {
1427                                         if(BEZSELECTED_HIDDENHANDLES(bezt)) {
1428                                                 if(v1==0) v1= nr;
1429                                                 else if(v2==0) v2= nr;
1430                                                 else if(v3==0) v3= nr;
1431                                                 else if(v4==0) v4= nr;
1432                                                 else break;
1433                                         }
1434                                         nr++;
1435                                         bezt++;
1436                                 }
1437                         }
1438                         else {
1439                                 bp= nu->bp;
1440                                 a= nu->pntsu*nu->pntsv;
1441                                 while(a--) {
1442                                         if(bp->f1 & SELECT) {
1443                                                 if(v1==0) v1= nr;
1444                                                 else if(v2==0) v2= nr;
1445                                                 else if(v3==0) v3= nr;
1446                                                 else if(v4==0) v4= nr;
1447                                                 else break;
1448                                         }
1449                                         nr++;
1450                                         bp++;
1451                                 }
1452                         }
1453                         nu= nu->next;
1454                 }
1455         }
1456         else if(G.obedit->type==OB_LATTICE) {
1457                 
1458                 a= editLatt->pntsu*editLatt->pntsv*editLatt->pntsw;
1459                 bp= editLatt->def;
1460                 while(a--) {
1461                         if(bp->f1 & SELECT) {
1462                                 if(v1==0) v1= nr;
1463                                 else if(v2==0) v2= nr;
1464                                 else if(v3==0) v3= nr;
1465                                 else if(v4==0) v4= nr;
1466                                 else break;
1467                         }
1468                         nr++;
1469                         bp++;
1470                 }
1471         }
1472         
1473         if(v4 || !((v1 && v2==0 && v3==0) || (v1 && v2 && v3)) ) {
1474                 error("Select either 1 or 3 vertices to parent to");
1475                 return;
1476         }
1477         
1478         if(okee("Make vertex parent")==0) return;
1479         
1480         for(base= FIRSTBASE; base; base= base->next) {
1481                 if(TESTBASELIB(v3d, base)) {
1482                         if(base!=BASACT) {
1483                                 
1484                                 ob= base->object;
1485                                 ob->recalc |= OB_RECALC;
1486                                 par= BASACT->object->parent;
1487                                 
1488                                 while(par) {
1489                                         if(par==ob) break;
1490                                         par= par->parent;
1491                                 }
1492                                 if(par) {
1493                                         error("Loop in parents");
1494                                 }
1495                                 else {
1496                                         Object workob;
1497                                         
1498                                         ob->parent= BASACT->object;
1499                                         if(v3) {
1500                                                 ob->partype= PARVERT3;
1501                                                 ob->par1= v1-1;
1502                                                 ob->par2= v2-1;
1503                                                 ob->par3= v3-1;
1504
1505                                                 /* inverse parent matrix */
1506                                                 what_does_parent(ob, &workob);
1507                                                 Mat4Invert(ob->parentinv, workob.obmat);
1508                                         }
1509                                         else {
1510                                                 ob->partype= PARVERT1;
1511                                                 ob->par1= v1-1;
1512
1513                                                 /* inverse parent matrix */
1514                                                 what_does_parent(ob, &workob);
1515                                                 Mat4Invert(ob->parentinv, workob.obmat);
1516                                         }
1517                                 }
1518                         }
1519                 }
1520         }
1521         allqueue(REDRAWVIEW3D, 0);
1522         
1523         DAG_scene_sort(scene);
1524         /* BIF_undo_push(str); not, conflicts with editmode undo... */
1525 }
1526
1527 static Object *group_objects_menu(Group *group)
1528 {
1529         GroupObject *go;
1530         int len= 0;
1531         short a, nr;
1532         char *str;
1533                 
1534         for(go= group->gobject.first; go; go= go->next) {
1535                 if(go->ob)
1536                         len++;
1537         }
1538         if(len==0) return NULL;
1539         
1540         str= MEM_callocN(40+32*len, "menu");
1541         
1542         strcpy(str, "Make Proxy for: %t");
1543         a= strlen(str);
1544         for(nr=1, go= group->gobject.first; go; go= go->next, nr++) {
1545                 a+= sprintf(str+a, "|%s %%x%d", go->ob->id.name+2, nr);
1546         }
1547         
1548         a= pupmenu_col(str, 20);
1549         MEM_freeN(str);
1550         if(a>0) {
1551                 go= BLI_findlink(&group->gobject, a-1);
1552                 return go->ob;
1553         }
1554         return NULL;
1555 }
1556
1557
1558 /* adds empty object to become local replacement data of a library-linked object */
1559 void make_proxy(Scene *scene)
1560 {
1561         Object *ob= OBACT;
1562         Object *gob= NULL;
1563         
1564         if(scene->id.lib) return;
1565         if(ob==NULL) return;
1566         
1567         
1568         if(ob->dup_group && ob->dup_group->id.lib) {
1569                 gob= ob;
1570                 /* gives menu with list of objects in group */
1571                 ob= group_objects_menu(ob->dup_group);
1572         }
1573         else if(ob->id.lib) {
1574                 if(okee("Make Proxy Object")==0)
1575                 return;
1576         }
1577         else {
1578                 error("Can only make proxy for a referenced object or group");
1579                 return;
1580         }
1581         
1582         if(ob) {
1583                 Object *newob;
1584                 Base *newbase, *oldbase= BASACT;
1585                 char name[32];
1586                 
1587                 newob= add_object(OB_EMPTY);
1588                 if(gob)
1589                         strcpy(name, gob->id.name+2);
1590                 else
1591                         strcpy(name, ob->id.name+2);
1592                 strcat(name, "_proxy");
1593                 rename_id(&newob->id, name);
1594                 
1595                 /* set layers OK */
1596                 newbase= BASACT;        /* add_object sets active... */
1597                 newbase->lay= oldbase->lay;
1598                 newob->lay= newbase->lay;
1599                 
1600                 /* remove base, leave user count of object, it gets linked in object_make_proxy */
1601                 if(gob==NULL) {
1602                         BLI_remlink(&scene->base, oldbase);
1603                         MEM_freeN(oldbase);
1604                 }               
1605                 object_make_proxy(newob, ob, gob);
1606                 
1607                 DAG_scene_sort(scene);
1608                 DAG_object_flush_update(scene, newob, OB_RECALC);
1609                 allqueue(REDRAWALL, 0);
1610                 BIF_undo_push("Make Proxy Object");
1611         }
1612 }
1613
1614 /* ******************** make parent operator *********************** */
1615
1616 #if 0
1617 oldcode()
1618 {
1619         else if(mode==4) {
1620                 bConstraint *con;
1621                 bFollowPathConstraint *data;
1622                         
1623                 for(base= FIRSTBASE; base; base= base->next) {
1624                         if(TESTBASELIB(v3d, base)) {
1625                                 if(base!=BASACT) {
1626                                         float cmat[4][4], vec[3];
1627                                         
1628 // XXX                                          con = add_new_constraint(CONSTRAINT_TYPE_FOLLOWPATH);
1629                                         strcpy (con->name, "AutoPath");
1630                                         
1631                                         data = con->data;
1632                                         data->tar = BASACT->object;
1633                                         
1634 // XXX                                          add_constraint_to_object(con, base->object);
1635                                         
1636                                         get_constraint_target_matrix(con, 0, CONSTRAINT_OBTYPE_OBJECT, NULL, cmat, scene->r.cfra - give_timeoffset(base->object));
1637                                         VecSubf(vec, base->object->obmat[3], cmat[3]);
1638                                         
1639                                         base->object->loc[0] = vec[0];
1640                                         base->object->loc[1] = vec[1];
1641                                         base->object->loc[2] = vec[2];
1642                                 }
1643                         }
1644                 }
1645
1646                 if(mode==PARSKEL && base->object->type==OB_MESH && par->type == OB_ARMATURE) {
1647                         /* Prompt the user as to whether he wants to
1648                                 * add some vertex groups based on the bones
1649                                 * in the parent armature.
1650                                 */
1651 // XXX                                                  create_vgroups_from_armature(base->object, par);
1652
1653                         base->object->partype= PAROBJECT;
1654                         what_does_parent(base->object);
1655                         Mat4One (base->object->parentinv);
1656                         base->object->partype= mode;
1657                 }
1658                 else
1659                         what_does_parent(base->object, &workob);
1660                 Mat4Invert(base->object->parentinv, workob.obmat);
1661         }
1662 }
1663 #endif
1664
1665 #define PAR_OBJECT              0
1666 #define PAR_ARMATURE    1
1667 #define PAR_BONE                2
1668 #define PAR_CURVE               3
1669 #define PAR_FOLLOW              4
1670 #define PAR_PATH_CONST  5
1671 #define PAR_LATTICE             6
1672 #define PAR_VERTEX              7
1673 #define PAR_TRIA                8
1674
1675 static EnumPropertyItem prop_make_parent_types[] = {
1676         {PAR_OBJECT, "OBJECT", "Object", ""},
1677         {PAR_ARMATURE, "ARMATURE", "Armature Deform", ""},
1678         {PAR_BONE, "BONE", "Bone", ""},
1679         {PAR_CURVE, "CURVE", "Curve Deform", ""},
1680         {PAR_FOLLOW, "FOLLOW", "Follow Path", ""},
1681         {PAR_PATH_CONST, "PATH_CONST", "Path Constraint", ""},
1682         {PAR_LATTICE, "LATTICE", "Lattice Deform", ""},
1683         {PAR_VERTEX, "VERTEX", "Vertex", ""},
1684         {PAR_TRIA, "TRIA", "Triangle", ""},
1685         {0, NULL, NULL, NULL}
1686 };
1687
1688 static int test_parent_loop(Object *par, Object *ob)
1689 {
1690         /* test if 'ob' is a parent somewhere in par's parents */
1691         
1692         if(par == NULL) return 0;
1693         if(ob == par) return 1;
1694         
1695         return test_parent_loop(par->parent, ob);
1696 }
1697
1698
1699 static int make_parent_exec(bContext *C, wmOperator *op)
1700 {
1701         Object *par= CTX_data_active_object(C);
1702         bPoseChannel *pchan= NULL;
1703         int partype= RNA_enum_get(op->ptr, "type");
1704         
1705         par->recalc |= OB_RECALC_OB;
1706         
1707         /* preconditions */
1708         if(partype==PAR_FOLLOW || partype==PAR_PATH_CONST) {
1709                 if(par->type!=OB_CURVE)
1710                         return OPERATOR_CANCELLED;
1711                 else {
1712                         Curve *cu= par->data;
1713                         
1714                         if((cu->flag & CU_PATH)==0) {
1715                                 cu->flag |= CU_PATH|CU_FOLLOW;
1716                                 makeDispListCurveTypes(par, 0);  /* force creation of path data */
1717                         }
1718                         else cu->flag |= CU_FOLLOW;
1719                         
1720                         /* fall back on regular parenting now */
1721                         partype= PAR_OBJECT;
1722                 }               
1723         }
1724         else if(partype==PAR_BONE) {
1725                 pchan= get_active_posechannel(par);
1726                 
1727                 if(pchan==NULL) {
1728                         error("No active Bone");
1729                         return OPERATOR_CANCELLED;
1730                 }
1731         }
1732         
1733         /* context itterator */
1734         CTX_DATA_BEGIN(C, Object*, ob, selected_objects) {
1735                 
1736                 if(ob!=par) {
1737                         
1738                         if( test_parent_loop(par, ob) ) {
1739                                 error("Loop in parents");
1740                         }
1741                         else {
1742                                 Object workob;
1743                                 
1744                                 /* apply transformation of previous parenting */
1745                                 apply_obmat(ob);
1746                                 
1747                                 ob->parent= par;
1748                                 
1749                                 /* handle types */
1750                                 if (pchan)
1751                                         strcpy (ob->parsubstr, pchan->name);
1752                                 else
1753                                         ob->parsubstr[0]= 0;
1754                                 
1755                                 /* constraint XXX */
1756                                 if(partype==PAR_PATH_CONST) {
1757                                 }
1758                                 
1759                                 /* calculate inverse parent matrix */
1760                                 what_does_parent(ob, &workob);
1761                                 Mat4Invert(ob->parentinv, workob.obmat);
1762                                 
1763                                 ob->recalc |= OB_RECALC_OB|OB_RECALC_DATA;
1764                                 
1765                                 if( ELEM3(partype, PAR_CURVE, PAR_ARMATURE, PAR_LATTICE) )
1766                                         ob->partype= PARSKEL; /* note, dna define, not operator property */
1767                                 else
1768                                         ob->partype= PAROBJECT; /* note, dna define, not operator property */
1769                         }
1770                 }
1771         }
1772         CTX_DATA_END;
1773         
1774         DAG_scene_sort(CTX_data_scene(C));
1775         ED_anim_dag_flush_update(C);    
1776         
1777         BIF_undo_push("make Parent");
1778         
1779         return OPERATOR_FINISHED;
1780 }
1781
1782 static int make_parent_invoke(bContext *C, wmOperator *op, wmEvent *event)
1783 {
1784         Object *ob= CTX_data_active_object(C);
1785         char *str, string[256];
1786         char formatstr[] = "|%s %%x%d";
1787         
1788         str= string + sprintf(string, "Make Parent To %%t");
1789         
1790         /* ob becomes parent, make the associated menus */
1791         if(ob->type==OB_ARMATURE) {
1792                 str += sprintf(str, formatstr, "Object", PAR_OBJECT);
1793                 str += sprintf(str, formatstr, "Armature Deform", PAR_ARMATURE);
1794                 str += sprintf(str, formatstr, "Bone", PAR_BONE);
1795         }
1796         else if(ob->type==OB_CURVE) {
1797                 str += sprintf(str, formatstr, "Object", PAR_OBJECT);
1798                 str += sprintf(str, formatstr, "Curve Deform", PAR_CURVE);
1799                 str += sprintf(str, formatstr, "Follow Path", PAR_FOLLOW);
1800                 str += sprintf(str, formatstr, "Path Constraint", PAR_PATH_CONST);
1801         }
1802         else if(ob->type == OB_LATTICE) {
1803                 str += sprintf(str, formatstr, "Object", PAR_OBJECT);
1804                 str += sprintf(str, formatstr, "Lattice Deform", PAR_LATTICE);
1805         }
1806         else
1807                 str += sprintf(str, formatstr, "Object", PAR_OBJECT);
1808         
1809         uiPupmenuOperator(C, 0, op, "type", string);
1810         
1811         return OPERATOR_RUNNING_MODAL;
1812 }
1813
1814
1815 void OBJECT_OT_make_parent(wmOperatorType *ot)
1816 {
1817         PropertyRNA *prop;
1818         
1819         /* identifiers */
1820         ot->name= "Make parent";
1821         ot->idname= "OBJECT_OT_make_parent";
1822         
1823         /* api callbacks */
1824         ot->invoke= make_parent_invoke;
1825         ot->exec= make_parent_exec;
1826         
1827         ot->poll= ED_operator_areaactive;       // XXX solve
1828         ot->flag= OPTYPE_REGISTER;
1829         
1830         prop = RNA_def_property(ot->srna, "type", PROP_ENUM, PROP_NONE);
1831         RNA_def_property_enum_items(prop, prop_make_parent_types);
1832 }
1833
1834 /* *******************  ***************** */
1835
1836 void enter_editmode(Scene *scene, View3D *v3d, int wc)
1837 {
1838         Base *base;
1839         Object *ob;
1840         Mesh *me;
1841         bArmature *arm;
1842         int ok= 0;
1843         
1844         if(scene->id.lib) return;
1845         base= BASACT;
1846         if(base==0) return;
1847         if((v3d==NULL || (base->lay & v3d->lay))==0) return;
1848         
1849         strcpy(G.editModeTitleExtra, "");
1850
1851         ob= base->object;
1852         if(ob->data==0) return;
1853         
1854         if (object_data_is_libdata(ob)) {
1855                 error_libdata();
1856                 return;
1857         }
1858         
1859         if(wc) waitcursor(1);
1860         
1861         if(ob->type==OB_MESH) {
1862                 me= get_mesh(ob);
1863                 if( me==0 ) return;
1864                 if(me->pv) mesh_pmv_off(ob, me);
1865                 ok= 1;
1866                 G.obedit= ob;
1867 // XXX          make_editMesh();
1868                 allqueue(REDRAWBUTSLOGIC, 0);
1869                 /*if(G.f & G_FACESELECT) allqueue(REDRAWIMAGE, 0);*/
1870 // XXX          if (EM_texFaceCheck())
1871 //                      allqueue(REDRAWIMAGE, 0);
1872                 
1873         }
1874         if (ob->type==OB_ARMATURE){
1875                 arm= base->object->data;
1876                 if (!arm) return;
1877                 /*
1878                  * The function object_data_is_libdata make a problem here, the
1879                  * check for ob->proxy return 0 and let blender enter to edit mode
1880                  * this causa a crash when you try leave the edit mode.
1881                  * The problem is that i can't remove the ob->proxy check from
1882                  * object_data_is_libdata that prevent the bugfix #6614, so
1883                  * i add this little hack here.
1884                  */
1885                 if(arm->id.lib) {
1886                         error_libdata();
1887                         return;
1888                 }
1889                 ok=1;
1890                 G.obedit=ob;
1891 // XXX          make_editArmature();
1892                 /* to ensure all goes in restposition and without striding */
1893                 DAG_object_flush_update(scene, G.obedit, OB_RECALC);
1894
1895                 allqueue (REDRAWVIEW3D,0);
1896         }
1897         else if(ob->type==OB_FONT) {
1898                 G.obedit= ob;
1899                 ok= 1;
1900 // XXX          make_editText();
1901         }
1902         else if(ob->type==OB_MBALL) {
1903                 G.obedit= ob;
1904                 ok= 1;
1905 // XXX          make_editMball();
1906         }
1907         else if(ob->type==OB_LATTICE) {
1908                 G.obedit= ob;
1909                 ok= 1;
1910 // XXX          make_editLatt();
1911         }
1912         else if(ob->type==OB_SURF || ob->type==OB_CURVE) {
1913                 ok= 1;
1914                 G.obedit= ob;
1915 // XXX          make_editNurb();
1916         }
1917         allqueue(REDRAWBUTSEDIT, 0);
1918         allqueue(REDRAWOOPS, 0);
1919         
1920         if(ok) {
1921         
1922                 allqueue(REDRAWVIEW3D, 1);
1923                 DAG_object_flush_update(scene, G.obedit, OB_RECALC_DATA);
1924                 
1925         }
1926         else G.obedit= NULL;
1927         
1928         if(wc) waitcursor(0);
1929         
1930 }
1931
1932 void exit_editmode(Scene *scene, int flag)      /* freedata==0 at render, 1= freedata, 2= do undo buffer too */
1933 {
1934 #if 0
1935         Object *ob;
1936         int freedata = flag; // XXX & EM_FREEDATA;
1937         
1938         if(G.obedit==NULL) return;
1939
1940         if(flag & EM_WAITCURSOR) waitcursor(1);
1941         if(G.obedit->type==OB_MESH) {
1942
1943                 
1944                 if(EM_texFaceCheck())
1945                         allqueue(REDRAWIMAGE, 0);
1946                 
1947                 if(retopo_mesh_paint_check())
1948                         retopo_end_okee();
1949
1950                 if(G.totvert>MESH_MAX_VERTS) {
1951                         error("Too many vertices");
1952                         return;
1953                 }
1954                 load_editMesh();
1955
1956                 if(freedata) free_editMesh(G.editMesh);
1957                 
1958                 if(G.f & G_WEIGHTPAINT)
1959                         mesh_octree_table(G.obedit, NULL, 'e');
1960         }
1961         else if (G.obedit->type==OB_ARMATURE){  
1962                 load_editArmature();
1963                 if (freedata) free_editArmature();
1964         }
1965         else if(ELEM(G.obedit->type, OB_CURVE, OB_SURF)) {
1966                 extern ListBase editNurb;
1967                 load_editNurb();
1968                 if(freedata) freeNurblist(&editNurb);
1969         }
1970         else if(G.obedit->type==OB_FONT && freedata) {
1971                 load_editText();
1972         }
1973         else if(G.obedit->type==OB_LATTICE) {
1974                 load_editLatt();
1975                 if(freedata) free_editLatt();
1976         }
1977         else if(G.obedit->type==OB_MBALL) {
1978                 extern ListBase editelems;
1979                 load_editMball();
1980                 if(freedata) BLI_freelistN(&editelems);
1981         }
1982
1983         ob= G.obedit;
1984         
1985         /* for example; displist make is different in editmode */
1986         if(freedata) G.obedit= NULL;
1987
1988         if(ob->type==OB_MESH && get_mesh(ob)->mr)
1989                 multires_edge_level_update(ob, get_mesh(ob));
1990         
1991         /* also flush ob recalc, doesn't take much overhead, but used for particles */
1992         DAG_object_flush_update(scene, ob, OB_RECALC_OB|OB_RECALC_DATA);
1993
1994         allqueue(REDRAWVIEW3D, 1);
1995         allqueue(REDRAWBUTSALL, 0);
1996         allqueue(REDRAWACTION, 0);
1997         allqueue(REDRAWNLA, 0);
1998         allqueue(REDRAWIPO, 0);
1999         allqueue(REDRAWOOPS, 0);
2000
2001         if(G.obedit==NULL && (flag & EM_FREEUNDO)) 
2002                 BIF_undo_push("Editmode");
2003         
2004         if(flag & EM_WAITCURSOR) waitcursor(0);
2005 #endif
2006 }
2007
2008 void check_editmode(int type)
2009 {
2010         
2011         if (G.obedit==0 || G.obedit->type==type) return;
2012
2013 // XXX  exit_editmode(EM_FREEDATA|EM_FREEUNDO|EM_WAITCURSOR); /* freedata, and undo */
2014 }
2015
2016 /* 0 == do center, 1 == center new, 2 == center cursor */
2017
2018 void docenter(Scene *scene, View3D *v3d, int centermode)
2019 {
2020         EditMesh *em = G.editMesh;
2021         Base *base;
2022         Object *ob;
2023         Mesh *me, *tme;
2024         Curve *cu;
2025 /*      BezTriple *bezt;
2026         BPoint *bp; */
2027         Nurb *nu, *nu1;
2028         EditVert *eve;
2029         float cent[3], centn[3], min[3], max[3], omat[3][3];
2030         int a, total= 0;
2031         
2032         /* keep track of what is changed */
2033         int tot_change=0, tot_lib_error=0, tot_multiuser_arm_error=0;
2034         MVert *mvert;
2035
2036         if(scene->id.lib || v3d==NULL) return;
2037         
2038         cent[0]= cent[1]= cent[2]= 0.0;
2039         
2040         if(G.obedit) {
2041
2042                 INIT_MINMAX(min, max);
2043         
2044                 if(G.obedit->type==OB_MESH) {
2045                         for(eve= em->verts.first; eve; eve= eve->next) {
2046                                 if(v3d->around==V3D_CENTROID) {
2047                                         total++;
2048                                         VECADD(cent, cent, eve->co);
2049                                 }
2050                                 else {
2051                                         DO_MINMAX(eve->co, min, max);
2052                                 }
2053                         }
2054                         
2055                         if(v3d->around==V3D_CENTROID) {
2056                                 VecMulf(cent, 1.0f/(float)total);
2057                         }
2058                         else {
2059                                 cent[0]= (min[0]+max[0])/2.0f;
2060                                 cent[1]= (min[1]+max[1])/2.0f;
2061                                 cent[2]= (min[2]+max[2])/2.0f;
2062                         }
2063                         
2064                         for(eve= em->verts.first; eve; eve= eve->next) {
2065                                 VecSubf(eve->co, eve->co, cent);                        
2066                         }
2067                         
2068 // XXX                  recalc_editnormals();
2069                         tot_change++;
2070                         DAG_object_flush_update(scene, G.obedit, OB_RECALC_DATA);
2071                 }
2072         }
2073         
2074         /* reset flags */
2075         for (base=FIRSTBASE; base; base= base->next) {
2076                 if(TESTBASELIB(v3d, base))
2077                         base->object->flag &= ~OB_DONE;
2078         }
2079         
2080         for (me= G.main->mesh.first; me; me= me->id.next) {
2081                 me->flag &= ~ME_ISDONE;
2082         }
2083         
2084         for(base= FIRSTBASE; base; base= base->next) {
2085                 if(TESTBASELIB(v3d, base)) {
2086                         if((base->object->flag & OB_DONE)==0) {
2087                                 base->object->flag |= OB_DONE;
2088                                 
2089                                 if(base->object->id.lib) {
2090                                         tot_lib_error++;
2091                                 }
2092                                 else if(G.obedit==0 && (me=get_mesh(base->object)) ) {
2093                                         if (me->id.lib) {
2094                                                 tot_lib_error++;
2095                                         } else {
2096                                                 if(centermode==2) {
2097                                                         VECCOPY(cent, give_cursor(scene, v3d));
2098                                                         Mat4Invert(base->object->imat, base->object->obmat);
2099                                                         Mat4MulVecfl(base->object->imat, cent);
2100                                                 } else {
2101                                                         INIT_MINMAX(min, max);
2102                                                         mvert= me->mvert;
2103                                                         for(a=0; a<me->totvert; a++, mvert++) {
2104                                                                 DO_MINMAX(mvert->co, min, max);
2105                                                         }
2106                                         
2107                                                         cent[0]= (min[0]+max[0])/2.0f;
2108                                                         cent[1]= (min[1]+max[1])/2.0f;
2109                                                         cent[2]= (min[2]+max[2])/2.0f;
2110                                                 }
2111
2112                                                 mvert= me->mvert;
2113                                                 for(a=0; a<me->totvert; a++, mvert++) {
2114                                                         VecSubf(mvert->co, mvert->co, cent);
2115                                                 }
2116                                                 
2117                                                 if (me->key) {
2118                                                         KeyBlock *kb;
2119                                                         for (kb=me->key->block.first; kb; kb=kb->next) {
2120                                                                 float *fp= kb->data;
2121                                                                 
2122                                                                 for (a=0; a<kb->totelem; a++, fp+=3) {
2123                                                                         VecSubf(fp, fp, cent);
2124                                                                 }
2125                                                         }
2126                                                 }
2127                                                 
2128                                                 me->flag |= ME_ISDONE;
2129                                                 
2130                                                 if(centermode) {
2131                                                         Mat3CpyMat4(omat, base->object->obmat);
2132                                                         
2133                                                         VECCOPY(centn, cent);
2134                                                         Mat3MulVecfl(omat, centn);
2135                                                         base->object->loc[0]+= centn[0];
2136                                                         base->object->loc[1]+= centn[1];
2137                                                         base->object->loc[2]+= centn[2];
2138                                                         
2139                                                         where_is_object(base->object);
2140                                                         ignore_parent_tx(base->object);
2141                                                         
2142                                                         /* other users? */
2143                                                         ob= G.main->object.first;
2144                                                         while(ob) {
2145                                                                 if((ob->flag & OB_DONE)==0) {
2146                                                                         tme= get_mesh(ob);
2147                                                                         
2148                                                                         if(tme==me) {
2149                                                                                 
2150                                                                                 ob->flag |= OB_DONE;
2151                                                                                 ob->recalc= OB_RECALC_OB|OB_RECALC_DATA;
2152
2153                                                                                 Mat3CpyMat4(omat, ob->obmat);
2154                                                                                 VECCOPY(centn, cent);
2155                                                                                 Mat3MulVecfl(omat, centn);
2156                                                                                 ob->loc[0]+= centn[0];
2157                                                                                 ob->loc[1]+= centn[1];
2158                                                                                 ob->loc[2]+= centn[2];
2159                                                                                 
2160                                                                                 where_is_object(ob);
2161                                                                                 ignore_parent_tx(ob);
2162                                                                                 
2163                                                                                 if(tme && (tme->flag & ME_ISDONE)==0) {
2164                                                                                         mvert= tme->mvert;
2165                                                                                         for(a=0; a<tme->totvert; a++, mvert++) {
2166                                                                                                 VecSubf(mvert->co, mvert->co, cent);
2167                                                                                         }
2168                                                                                         
2169                                                                                         if (tme->key) {
2170                                                                                                 KeyBlock *kb;
2171                                                                                                 for (kb=tme->key->block.first; kb; kb=kb->next) {
2172                                                                                                         float *fp= kb->data;
2173                                                                                                         
2174                                                                                                         for (a=0; a<kb->totelem; a++, fp+=3) {
2175                                                                                                                 VecSubf(fp, fp, cent);
2176                                                                                                         }
2177                                                                                                 }
2178                                                                                         }
2179                                                                                         
2180                                                                                         tme->flag |= ME_ISDONE;
2181                                                                                 }
2182                                                                         }
2183                                                                 }
2184                                                                 
2185                                                                 ob= ob->id.next;
2186                                                         }
2187                                                 }
2188                                                 tot_change++;
2189                                         }
2190                                 }
2191                                 else if (ELEM(base->object->type, OB_CURVE, OB_SURF)) {
2192                                         
2193                                         /* totally weak code here... (ton) */
2194                                         if(G.obedit==base->object) {
2195                                                 extern ListBase editNurb;
2196                                                 nu1= editNurb.first;
2197                                                 cu= G.obedit->data;
2198                                         }
2199                                         else {
2200                                                 cu= base->object->data;
2201                                                 nu1= cu->nurb.first;
2202                                         }
2203                                         
2204                                         if (cu->id.lib) {
2205                                                 tot_lib_error++;
2206                                         } else {
2207                                                 if(centermode==2) {
2208                                                         VECCOPY(cent, give_cursor(scene, v3d));
2209                                                         Mat4Invert(base->object->imat, base->object->obmat);
2210                                                         Mat4MulVecfl(base->object->imat, cent);
2211
2212                                                         /* don't allow Z change if curve is 2D */
2213                                                         if( !( cu->flag & CU_3D ) )
2214                                                                 cent[2] = 0.0;
2215                                                 } 
2216                                                 else {
2217                                                         INIT_MINMAX(min, max);
2218                                                         
2219                                                         nu= nu1;
2220                                                         while(nu) {
2221                                                                 minmaxNurb(nu, min, max);
2222                                                                 nu= nu->next;
2223                                                         }
2224                                                         
2225                                                         cent[0]= (min[0]+max[0])/2.0f;
2226                                                         cent[1]= (min[1]+max[1])/2.0f;
2227                                                         cent[2]= (min[2]+max[2])/2.0f;
2228                                                 }
2229                                                 
2230                                                 nu= nu1;
2231                                                 while(nu) {
2232                                                         if( (nu->type & 7)==1) {
2233                                                                 a= nu->pntsu;
2234                                                                 while (a--) {
2235                                                                         VecSubf(nu->bezt[a].vec[0], nu->bezt[a].vec[0], cent);
2236                                                                         VecSubf(nu->bezt[a].vec[1], nu->bezt[a].vec[1], cent);
2237                                                                         VecSubf(nu->bezt[a].vec[2], nu->bezt[a].vec[2], cent);
2238                                                                 }
2239                                                         }
2240                                                         else {
2241                                                                 a= nu->pntsu*nu->pntsv;
2242                                                                 while (a--)
2243                                                                         VecSubf(nu->bp[a].vec, nu->bp[a].vec, cent);
2244                                                         }
2245                                                         nu= nu->next;
2246                                                 }
2247                                 
2248                                                 if(centermode && G.obedit==0) {
2249                                                         Mat3CpyMat4(omat, base->object->obmat);
2250                                                         
2251                                                         Mat3MulVecfl(omat, cent);
2252                                                         base->object->loc[0]+= cent[0];
2253                                                         base->object->loc[1]+= cent[1];
2254                                                         base->object->loc[2]+= cent[2];
2255                                                         
2256                                                         where_is_object(base->object);
2257                                                         ignore_parent_tx(base->object);
2258                                                 }
2259                                                 
2260                                                 tot_change++;
2261                                                 if(G.obedit) {
2262                                                         if (centermode==0) {
2263                                                                 DAG_object_flush_update(scene, G.obedit, OB_RECALC_DATA);
2264                                                         }
2265                                                         break;
2266                                                 }
2267                                         }
2268                                 }
2269                                 else if(base->object->type==OB_FONT) {
2270                                         /* get from bb */
2271                                         
2272                                         cu= base->object->data;
2273                                         
2274                                         if(cu->bb==0) {
2275                                                 /* do nothing*/
2276                                         } else if (cu->id.lib) {
2277                                                 tot_lib_error++;
2278                                         } else {
2279                                                 cu->xof= -0.5f*( cu->bb->vec[4][0] - cu->bb->vec[0][0]);
2280                                                 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 */
2281                                                 
2282                                                 /* not really ok, do this better once! */
2283                                                 cu->xof /= cu->fsize;
2284                                                 cu->yof /= cu->fsize;
2285
2286                                                 allqueue(REDRAWBUTSEDIT, 0);
2287                                                 tot_change++;
2288                                         }
2289                                 }
2290                                 else if(base->object->type==OB_ARMATURE) {
2291                                         bArmature *arm = base->object->data;
2292                                         
2293                                         if (arm->id.lib) {
2294                                                 tot_lib_error++;
2295                                         } else if(arm->id.us>1) {
2296                                                 /*error("Can't apply to a multi user armature");
2297                                                 return;*/
2298                                                 tot_multiuser_arm_error++;
2299                                         } else {
2300                                                 /* Function to recenter armatures in editarmature.c 
2301                                                  * Bone + object locations are handled there.
2302                                                  */
2303 // XXX                                          docenter_armature(base->object, centermode);
2304                                                 tot_change++;
2305                                                 
2306                                                 where_is_object(base->object);
2307                                                 ignore_parent_tx(base->object);
2308                                                 
2309                                                 if(G.obedit) 
2310                                                         break;
2311                                         }
2312                                 }
2313                                 base->object->recalc= OB_RECALC_OB|OB_RECALC_DATA;
2314                         }
2315                 }
2316         }
2317         if (tot_change) {
2318                 ED_anim_dag_flush_update(C);
2319                 allqueue(REDRAWVIEW3D, 0);
2320                 BIF_undo_push("Do Center");     
2321         }
2322         
2323         /* Warn if any errors occured */
2324         if (tot_lib_error+tot_multiuser_arm_error) {
2325                 char err[512];
2326                 sprintf(err, "Warning %i Object(s) Not Centered, %i Changed:", tot_lib_error+tot_multiuser_arm_error, tot_change);
2327                 
2328                 if (tot_lib_error)
2329                         sprintf(err+strlen(err), "|%i linked library objects", tot_lib_error);
2330                 if (tot_multiuser_arm_error)
2331                         sprintf(err+strlen(err), "|%i multiuser armature object(s)", tot_multiuser_arm_error);
2332                 
2333                 error(err);
2334         }
2335 }
2336
2337 void docenter_new(Scene *scene, View3D *v3d)
2338 {
2339         if(scene->id.lib) return;
2340
2341         if(G.obedit) {
2342                 error("Unable to center new in Edit Mode");
2343         }
2344         else {
2345                 docenter(scene, v3d, 1);
2346         }
2347 }
2348
2349 void docenter_cursor(Scene *scene, View3D *v3d)
2350 {
2351         if(scene->id.lib) return;
2352
2353         if(G.obedit) {
2354                 error("Unable to center cursor in Edit Mode");
2355         }
2356         else {
2357                 docenter(scene, v3d, 2);
2358         }
2359 }
2360
2361 void movetolayer(Scene *scene, View3D *v3d)
2362 {
2363         Base *base;
2364         unsigned int lay= 0, local;
2365         int islamp= 0;
2366         
2367         if(scene->id.lib) return;
2368
2369         for(base= FIRSTBASE; base; base= base->next) {
2370                 if (TESTBASE(v3d, base)) lay |= base->lay;
2371         }
2372         if(lay==0) return;
2373         lay &= 0xFFFFFF;
2374         
2375         if(lay==0) return;
2376         
2377         if(v3d->localview) {
2378                 /* now we can move out of localview. */
2379                 if (!okee("Move from localview")) return;
2380                 for(base= FIRSTBASE; base; base= base->next) {
2381                         if (TESTBASE(v3d, base)) {
2382                                 lay= base->lay & ~v3d->lay;
2383                                 base->lay= lay;
2384                                 base->object->lay= lay;
2385                                 base->object->flag &= ~SELECT;
2386                                 base->flag &= ~SELECT;
2387                                 if(base->object->type==OB_LAMP) islamp= 1;
2388                         }
2389                 }
2390         } else {
2391 // XXX          if( movetolayer_buts(&lay, NULL)==0 ) return;
2392                 
2393                 /* normal non localview operation */
2394                 for(base= FIRSTBASE; base; base= base->next) {
2395                         if (TESTBASE(v3d, base)) {
2396                                 /* upper byte is used for local view */
2397                                 local= base->lay & 0xFF000000;  
2398                                 base->lay= lay + local;
2399                                 base->object->lay= lay;
2400                                 if(base->object->type==OB_LAMP) islamp= 1;
2401                         }
2402                 }
2403         }
2404         if(islamp) reshadeall_displist();       /* only frees */
2405         
2406         /* warning, active object may be hidden now */
2407         
2408         DAG_scene_sort(scene);
2409         
2410         allqueue(REDRAWBUTSEDIT, 0);
2411         allqueue(REDRAWVIEW3D, 0);
2412         allqueue(REDRAWOOPS, 0);
2413         allqueue(REDRAWINFO, 0);
2414         
2415         BIF_undo_push("Move to layer");
2416 }
2417
2418
2419 #if 0
2420 // XXX should be in view3d?
2421
2422 /* context: ob = lamp */
2423 /* code should be replaced with proper (custom) transform handles for lamp properties */
2424 static void spot_interactive(Object *ob, int mode)
2425 {
2426         Lamp *la= ob->data;
2427         float transfac, dx, dy, ratio, origval;
2428         int keep_running= 1, center2d[2];
2429         short mval[2], mvalo[2];
2430         
2431 //      getmouseco_areawin(mval);
2432 //      getmouseco_areawin(mvalo);
2433         
2434         project_int(ob->obmat[3], center2d);
2435         if( center2d[0] > 100000 ) {            /* behind camera */
2436 //              center2d[0]= curarea->winx/2;
2437 //              center2d[1]= curarea->winy/2;
2438         }
2439
2440 //      helpline(mval, center2d);
2441         
2442         /* ratio is like scaling */
2443         dx = (float)(center2d[0] - mval[0]);
2444         dy = (float)(center2d[1] - mval[1]);
2445         transfac = (float)sqrt( dx*dx + dy*dy);
2446         if(transfac==0.0f) transfac= 1.0f;
2447         
2448         if(mode==1)     
2449                 origval= la->spotsize;
2450         else if(mode==2)        
2451                 origval= la->dist;
2452         else if(mode==3)        
2453                 origval= la->clipsta;
2454         else    
2455                 origval= la->clipend;
2456         
2457         while (keep_running>0) {
2458                 
2459 //              getmouseco_areawin(mval);
2460                 
2461                 /* essential for idling subloop */
2462                 if(mval[0]==mvalo[0] && mval[1]==mvalo[1]) {
2463                         PIL_sleep_ms(2);
2464                 }
2465                 else {
2466                         char str[32];
2467                         
2468                         dx = (float)(center2d[0] - mval[0]);
2469                         dy = (float)(center2d[1] - mval[1]);
2470                         ratio = (float)(sqrt( dx*dx + dy*dy))/transfac;
2471                         
2472                         /* do the trick */
2473                         
2474                         if(mode==1) {   /* spot */
2475                                 la->spotsize = ratio*origval;
2476                                 CLAMP(la->spotsize, 1.0f, 180.0f);
2477                                 sprintf(str, "Spot size %.2f\n", la->spotsize);
2478                         }
2479                         else if(mode==2) {      /* dist */
2480                                 la->dist = ratio*origval;
2481                                 CLAMP(la->dist, 0.01f, 5000.0f);
2482                                 sprintf(str, "Distance %.2f\n", la->dist);
2483                         }
2484                         else if(mode==3) {      /* sta */
2485                                 la->clipsta = ratio*origval;
2486                                 CLAMP(la->clipsta, 0.001f, 5000.0f);
2487                                 sprintf(str, "Distance %.2f\n", la->clipsta);
2488                         }
2489                         else if(mode==4) {      /* end */
2490                                 la->clipend = ratio*origval;
2491                                 CLAMP(la->clipend, 0.1f, 5000.0f);
2492                                 sprintf(str, "Clip End %.2f\n", la->clipend);
2493                         }
2494
2495                         /* cleanup */
2496                         mvalo[0]= mval[0];
2497                         mvalo[1]= mval[1];
2498                         
2499                         /* handle shaded mode */
2500 // XXX                  shade_buttons_change_3d();
2501
2502                         /* DRAW */      
2503                         headerprint(str);
2504                         force_draw_plus(SPACE_BUTS, 0);
2505
2506 //                      helpline(mval, center2d);
2507                 }
2508                 
2509                 while( qtest() ) {
2510                         short val;
2511                         unsigned short event= extern_qread(&val);
2512                         
2513                         switch (event){
2514                                 case ESCKEY:
2515                                 case RIGHTMOUSE:
2516                                         keep_running= 0;
2517                                         break;
2518                                 case LEFTMOUSE:
2519                                 case SPACEKEY:
2520                                 case PADENTER:
2521                                 case RETKEY:
2522                                         if(val)
2523                                                 keep_running= -1;
2524                                         break;
2525                         }
2526                 }
2527         }
2528
2529         if(keep_running==0) {
2530                 if(mode==1)     
2531                         la->spotsize= origval;
2532                 else if(mode==2)        
2533                         la->dist= origval;
2534                 else if(mode==3)        
2535                         la->clipsta= origval;
2536                 else    
2537                         la->clipend= origval;
2538         }
2539
2540         allqueue(REDRAWVIEW3D, 0);
2541         allqueue(REDRAWBUTSSHADING, 0);
2542         BIF_preview_changed(ID_LA);
2543 }
2544 #endif
2545
2546 void special_editmenu(Scene *scene, View3D *v3d)
2547 {
2548 // XXX  static short numcuts= 2;
2549         Object *ob= OBACT;
2550         float fac;
2551         int nr,ret;
2552         short randfac;
2553         
2554         if(ob==NULL) return;
2555         
2556         if(G.obedit==NULL) {
2557                 
2558                 if(ob->flag & OB_POSEMODE) {
2559 // XXX                  pose_special_editmenu();
2560                 }
2561                 else if(FACESEL_PAINT_TEST) {
2562                         Mesh *me= get_mesh(ob);
2563                         MTFace *tface;
2564                         MFace *mface;
2565                         int a;
2566                         
2567                         if(me==0 || me->mtface==0) return;
2568                         
2569                         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");
2570                         
2571                         tface= me->mtface;
2572                         mface= me->mface;
2573                         for(a=me->totface; a>0; a--, tface++, mface++) {
2574                                 if(mface->flag & ME_FACE_SEL) {
2575                                         switch(nr) {
2576                                         case 1:
2577                                                 tface->mode |= TF_TEX; break;
2578                                         case 2:
2579                                                 tface->mode |= TF_SHAREDCOL; break;
2580                                         case 3:
2581                                                 tface->mode |= TF_LIGHT; break; 
2582                                         case 4:
2583                                                 tface->mode |= TF_INVISIBLE; break;
2584                                         case 5:
2585                                                 tface->mode |= TF_DYNAMIC; break;
2586                                         case 6:
2587                                                 tface->mode |= TF_TWOSIDE; break;
2588                                         case 7:
2589                                                 tface->mode &= ~TF_TEX;
2590                                                 tface->tpage= 0;
2591                                                 break;
2592                                         case 8:
2593                                                 tface->mode &= ~TF_SHAREDCOL; break;
2594                                         case 9:
2595                                                 tface->mode &= ~TF_LIGHT; break;
2596                                         case 10:
2597                                                 tface->mode &= ~TF_INVISIBLE; break;
2598                                         case 11:
2599                                                 tface->mode &= ~TF_DYNAMIC; break;
2600                                         case 12:
2601                                                 tface->mode &= ~TF_TWOSIDE; break;
2602                                         }
2603                                 }
2604                         }
2605                         DAG_object_flush_update(scene, ob, OB_RECALC_DATA);
2606                         allqueue(REDRAWVIEW3D, 0);
2607                         allqueue(REDRAWBUTSEDIT, 0);
2608                         BIF_undo_push("Change texture face");
2609                 }
2610                 else if(G.f & G_VERTEXPAINT) {
2611                         Mesh *me= get_mesh(ob);
2612                         
2613                         if(me==0 || (me->mcol==NULL && me->mtface==NULL) ) return;
2614                         
2615                         nr= pupmenu("Specials%t|Shared VertexCol%x1");
2616                         if(nr==1) {
2617                                 
2618 // XXX                          do_shared_vertexcol(me);
2619                                 
2620                                 BIF_undo_push("Shared VertexCol");
2621
2622                                 DAG_object_flush_update(scene, ob, OB_RECALC_DATA);
2623                         }
2624                 }
2625                 else if(G.f & G_WEIGHTPAINT) {
2626                         Object *par= modifiers_isDeformedByArmature(ob);
2627
2628                         if(par && (par->flag & OB_POSEMODE)) {
2629                                 nr= pupmenu("Specials%t|Apply Bone Envelopes to Vertex Groups %x1|Apply Bone Heat Weights to Vertex Groups %x2");
2630
2631 // XXX                          if(nr==1 || nr==2)
2632 // XXX                                  pose_adds_vgroups(ob, (nr == 2));
2633                         }
2634                 }
2635                 else if(G.f & G_PARTICLEEDIT) {
2636 #if 0
2637                         // XXX
2638                         ParticleSystem *psys = PE_get_current(ob);
2639                         ParticleEditSettings *pset = PE_settings();
2640
2641                         if(!psys)
2642                                 return;
2643
2644                         if(scene->selectmode & SCE_SELECT_POINT)
2645                                 nr= pupmenu("Specials%t|Rekey%x1|Subdivide%x2|Select First%x3|Select Last%x4|Remove Doubles%x5");
2646                         else
2647                                 nr= pupmenu("Specials%t|Rekey%x1|Remove Doubles%x5");
2648                         
2649                         switch(nr) {
2650                         case 1:
2651 // XXX                          if(button(&pset->totrekey, 2, 100, "Number of Keys:")==0) return;
2652                                 waitcursor(1);
2653                                 PE_rekey();
2654                                 break;
2655                         case 2:
2656                                 PE_subdivide();
2657                                 break;
2658                         case 3:
2659                                 PE_select_root();
2660                                 break;
2661                         case 4:
2662                                 PE_select_tip();
2663                                 break;
2664                         case 5:
2665                                 PE_remove_doubles();
2666                                 break;
2667                         }
2668                         
2669                         DAG_object_flush_update(scene, G.obedit, OB_RECALC_DATA);
2670                         
2671                         if(nr>0) waitcursor(0);
2672 #endif
2673                 }
2674                 else {
2675                         Base *base, *base_select= NULL;
2676                         
2677                         /* Get the active object mesh. */
2678                         Mesh *me= get_mesh(ob);
2679
2680                         /* Booleans, if the active object is a mesh... */
2681                         if (me && ob->id.lib==NULL) {
2682                                 
2683                                 /* Bring up a little menu with the boolean operation choices on. */
2684                                 nr= pupmenu("Boolean Tools%t|Intersect%x1|Union%x2|Difference%x3|Add Intersect Modifier%x4|Add Union Modifier%x5|Add Difference Modifier%x6");
2685
2686                                 if (nr > 0) {
2687                                         /* user has made a choice of a menu element.
2688                                            All of the boolean functions require 2 mesh objects 
2689                                            we search through the object list to find the other 
2690                                            selected item and make sure it is distinct and a mesh. */
2691
2692                                         for(base= FIRSTBASE; base; base= base->next) {
2693                                                 if(TESTBASELIB(v3d, base)) {
2694                                                         if(base->object != ob) base_select= base;
2695                                                 }
2696                                         }
2697
2698                                         if (base_select) {
2699                                                 if (get_mesh(base_select->object)) {
2700                                                         if(nr <= 3){
2701                                                                 waitcursor(1);
2702 // XXX                                                          ret = NewBooleanMesh(BASACT,base_select,nr);
2703                                                                 if (ret==0) {
2704                                                                         error("An internal error occurred");
2705                                                                 } else if(ret==-1) {
2706                                                                         error("Selected meshes must have faces to perform boolean operations");
2707                                                                 } else if (ret==-2) {
2708                                                                         error("Both meshes must be a closed mesh");
2709                                                                 }
2710                                                                 else BIF_undo_push("Boolean");
2711                                                                 waitcursor(0);
2712                                                         } else {
2713                                                                 BooleanModifierData *bmd = NULL;
2714                                                                 bmd = (BooleanModifierData *)modifier_new(eModifierType_Boolean);
2715                                                                 BLI_addtail(&ob->modifiers, bmd);
2716                                                                 bmd->object = base_select->object;
2717                                                                 bmd->modifier.mode |= eModifierMode_Realtime;
2718                                                                 switch(nr){
2719                                                                         case 4: bmd->operation = eBooleanModifierOp_Intersect; break;
2720                                                                         case 5: bmd->operation = eBooleanModifierOp_Union; break;
2721                                                                         case 6: bmd->operation = eBooleanModifierOp_Difference; break;
2722                                                                 }
2723 // XXX                                                          do_common_editbuts(B_CHANGEDEP);
2724                                                                 BIF_undo_push("Add Boolean modifier");                                                          
2725                                                         }                                                               
2726                                                 } else {
2727                                                         error("Please select 2 meshes");
2728                                                 }
2729                                         } else {
2730                                                 error("Please select 2 meshes");
2731                                         }
2732                                 }
2733
2734                                 allqueue(REDRAWVIEW3D, 0);
2735                         }
2736                         else if (ob->type == OB_FONT) {
2737                                 /* removed until this gets a decent implementation (ton) */
2738 /*                              nr= pupmenu("Split %t|Characters%x1");
2739                                 if (nr > 0) {
2740                                         switch(nr) {
2741                                                 case 1: split_font();
2742                                         }
2743                                 }
2744 */
2745                         }                       
2746                 }
2747         }
2748         else if(G.obedit->type==OB_MESH) {
2749                 /* 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 */
2750                 /*
2751                 nr= pupmenu("Subdivide Mesh%t|Subdivide%x1|Subdivide Multi%x2|Subdivide Multi Fractal%x3|Subdivide Smooth%x4");
2752                 switch(nr) {
2753                 case 1:
2754                         waitcursor(1);
2755                         esubdivideflag(1, 0.0, scene->toolsettings->editbutflag, 1, 0);
2756                         
2757                         BIF_undo_push("ESubdivide Single");            
2758                         break;
2759                 case 2:
2760                         if(button(&numcuts, 1, 128, "Number of Cuts:")==0) return;
2761                         waitcursor(1);
2762                         esubdivideflag(1, 0.0, scene->toolsettings->editbutflag, numcuts, 0);
2763                         BIF_undo_push("ESubdivide");
2764                         break;
2765                 case 3:
2766                         if(button(&numcuts, 1, 128, "Number of Cuts:")==0) return;
2767                         randfac= 10;
2768                         if(button(&randfac, 1, 100, "Rand fac:")==0) return;
2769                         waitcursor(1);                  
2770                         fac= -( (float)randfac )/100;
2771                         esubdivideflag(1, fac, scene->toolsettings->editbutflag, numcuts, 0);
2772                         BIF_undo_push("Subdivide Fractal");
2773                         break;
2774                         
2775                 case 4:
2776                         fac= 1.0f;
2777                         if(fbutton(&fac, 0.0f, 5.0f, 10, 10, "Smooth:")==0) return;
2778                                 fac= 0.292f*fac;
2779                         
2780                         waitcursor(1);
2781                         esubdivideflag(1, fac, scene->toolsettings->editbutflag | B_SMOOTH, 1, 0);
2782                         BIF_undo_push("Subdivide Smooth");
2783                         break;          
2784                 }
2785                 */
2786                 
2787                 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");
2788                 
2789                 switch(nr) {
2790                 case 1:
2791                         waitcursor(1);
2792 // XXX                  esubdivideflag(1, 0.0, scene->toolsettings->editbutflag, 1, 0);
2793                         
2794                         BIF_undo_push("ESubdivide Single");            
2795                         break;
2796                 case 2:
2797 // XXX                  if(button(&numcuts, 1, 128, "Number of Cuts:")==0) return;
2798                         waitcursor(1);
2799 // XXX                  esubdivideflag(1, 0.0, scene->toolsettings->editbutflag, numcuts, 0);
2800                         BIF_undo_push("ESubdivide");
2801                         break;
2802                 case 3:
2803 // XXX                  if(button(&numcuts, 1, 128, "Number of Cuts:")==0) return;
2804                         randfac= 10;
2805 // XXX                  if(button(&randfac, 1, 100, "Rand fac:")==0) return;
2806                         waitcursor(1);                  
2807                         fac= -( (float)randfac )/100;
2808 // XXX                  esubdivideflag(1, fac, scene->toolsettings->editbutflag, numcuts, 0);
2809                         BIF_undo_push("Subdivide Fractal");
2810                         break;
2811                         
2812                 case 12:        /* smooth */
2813                         /* if(button(&numcuts, 1, 128, "Number of Cuts:")==0) return; */
2814                         fac= 1.0f;
2815 // XXX                  if(fbutton(&fac, 0.0f, 5.0f, 10, 10, "Smooth:")==0) return;
2816 // XXX                          fac= 0.292f*fac;
2817                         
2818                         waitcursor(1);
2819 // XXX                  esubdivideflag(1, fac, scene->toolsettings->editbutflag | B_SMOOTH, 1, 0);
2820                         BIF_undo_push("Subdivide Smooth");
2821                         break;          
2822
2823                 case 4:
2824 // XXX                  mergemenu();
2825                         break;
2826                 case 5:
2827 // XXX                  notice("Removed %d Vertices", removedoublesflag(1, 0, scene->toolsettings->doublimit));
2828                         BIF_undo_push("Remove Doubles");
2829                         break;
2830                 case 6:
2831 // XXX                  hide_mesh(0);
2832                         break;
2833                 case 7:
2834 // XXX                  reveal_mesh();
2835                         break;
2836                 case 8:
2837 // XXX                  selectswap_mesh();
2838                         break;
2839                 case 9:
2840 // XXX                  flip_editnormals();
2841                         BIF_undo_push("Flip Normals");
2842                         break;
2843                 case 10:
2844 // XXX                  vertexsmooth();
2845                         break;
2846                 case 11:
2847 // XXX                  bevel_menu();
2848                         break;
2849                 case 14:
2850 // XXX                  mesh_set_smooth_faces(1);
2851                         break;
2852                 case 15: 
2853 // XXX                  mesh_set_smooth_faces(0);
2854                         break;
2855                 case 16: 
2856 // XXX                  shape_copy_select_from();
2857                         break;
2858                 case 17: 
2859 // XXX                  shape_propagate();
2860                         break;
2861                 case 18:
2862 //