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