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