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