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