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