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