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