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