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