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