Remove unneeded notifier data added in revision 26219.
[blender-staging.git] / source / blender / editors / object / object_edit.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21  * All rights reserved.
22  *
23  * Contributor(s): Blender Foundation, 2002-2008 full recode
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27 #include <stdlib.h>
28 #include <string.h>
29 #include <math.h>
30 #include <time.h>
31 #include <float.h>
32 #include <ctype.h>
33
34 #include "MEM_guardedalloc.h"
35
36 #include "IMB_imbuf_types.h"
37
38 #include "DNA_action_types.h"
39 #include "DNA_armature_types.h"
40 #include "DNA_camera_types.h"
41 #include "DNA_constraint_types.h"
42 #include "DNA_curve_types.h"
43 #include "DNA_effect_types.h"
44 #include "DNA_group_types.h"
45 #include "DNA_image_types.h"
46 #include "DNA_key_types.h"
47 #include "DNA_lamp_types.h"
48 #include "DNA_lattice_types.h"
49 #include "DNA_material_types.h"
50 #include "DNA_mesh_types.h"
51 #include "DNA_meshdata_types.h"
52 #include "DNA_meta_types.h"
53 #include "DNA_nla_types.h"
54 #include "DNA_object_types.h"
55 #include "DNA_object_fluidsim.h"
56 #include "DNA_object_force.h"
57 #include "DNA_scene_types.h"
58 #include "DNA_space_types.h"
59 #include "DNA_screen_types.h"
60 #include "DNA_texture_types.h"
61 #include "DNA_particle_types.h"
62 #include "DNA_property_types.h"
63 #include "DNA_userdef_types.h"
64 #include "DNA_view3d_types.h"
65 #include "DNA_vfont_types.h"
66 #include "DNA_world_types.h"
67 #include "DNA_modifier_types.h"
68
69 #include "BLI_blenlib.h"
70 #include "BLI_math.h"
71 #include "BLI_editVert.h"
72 #include "BLI_ghash.h"
73 #include "BLI_rand.h"
74
75 #include "BKE_action.h"
76 #include "BKE_anim.h"
77 #include "BKE_armature.h"
78 #include "BKE_booleanops.h"
79 #include "BKE_constraint.h"
80 #include "BKE_context.h"
81 #include "BKE_customdata.h"
82 #include "BKE_blender.h"
83 #include "BKE_booleanops.h"
84 #include "BKE_cloth.h"
85 #include "BKE_curve.h"
86 #include "BKE_displist.h"
87 #include "BKE_depsgraph.h"
88 #include "BKE_DerivedMesh.h"
89 #include "BKE_effect.h"
90 #include "BKE_font.h"
91 #include "BKE_global.h"
92 #include "BKE_group.h"
93 #include "BKE_image.h"
94 #include "BKE_key.h"
95 #include "BKE_lattice.h"
96 #include "BKE_library.h"
97 #include "BKE_main.h"
98 #include "BKE_material.h"
99 #include "BKE_mball.h"
100 #include "BKE_mesh.h"
101 #include "BKE_nla.h"
102 #include "BKE_object.h"
103 #include "BKE_paint.h"
104 #include "BKE_particle.h"
105 #include "BKE_pointcache.h"
106 #include "BKE_property.h"
107 #include "BKE_report.h"
108 #include "BKE_sca.h"
109 #include "BKE_scene.h"
110 #include "BKE_softbody.h"
111 #include "BKE_subsurf.h"
112 #include "BKE_texture.h"
113 #include "BKE_utildefines.h"
114 #include "BKE_modifier.h"
115
116 #include "ED_anim_api.h"
117 #include "ED_armature.h"
118 #include "ED_curve.h"
119 #include "ED_particle.h"
120 #include "ED_mesh.h"
121 #include "ED_mball.h"
122 #include "ED_object.h"
123 #include "ED_screen.h"
124 #include "ED_transform.h"
125 #include "ED_types.h"
126 #include "ED_util.h"
127 #include "ED_view3d.h"
128
129 #include "UI_interface.h"
130
131 #include "RNA_access.h"
132 #include "RNA_define.h"
133 #include "RNA_enum_types.h"
134
135 /* for menu/popup icons etc etc*/
136 #include "UI_interface.h"
137 #include "UI_resources.h"
138
139 #include "WM_api.h"
140 #include "WM_types.h"
141
142 #include "object_intern.h"      // own include
143
144 /* ************* XXX **************** */
145 static void error() {}
146 static void waitcursor(int val) {}
147 static int pupmenu(const char *msg) {return 0;}
148
149 /* port over here */
150 static bContext *C;
151 static void error_libdata() {}
152
153 /* ********************************** */
154
155 /* --------------------------------- */
156
157 void ED_object_apply_obmat(Object *ob)
158 {
159         float mat[3][3], imat[3][3], tmat[3][3];
160         
161         /* from obmat to loc rot size */
162         
163         if(ob==NULL) return;
164         copy_m3_m4(mat, ob->obmat);
165         
166         VECCOPY(ob->loc, ob->obmat[3]);
167
168         mat3_to_eul( ob->rot,mat);
169         eul_to_mat3( tmat,ob->rot);
170
171         invert_m3_m3(imat, tmat);
172         
173         mul_m3_m3m3(tmat, imat, mat);
174         
175         ob->size[0]= tmat[0][0];
176         ob->size[1]= tmat[1][1];
177         ob->size[2]= tmat[2][2];
178         
179 }
180
181 /* ********* clear/set restrict view *********/
182 static int object_restrictview_clear_exec(bContext *C, wmOperator *op)
183 {
184         ScrArea *sa= CTX_wm_area(C);
185         View3D *v3d= sa->spacedata.first;
186         Scene *scene= CTX_data_scene(C);
187         Base *base;
188         int changed = 0;
189         
190         /* XXX need a context loop to handle such cases */
191         for(base = FIRSTBASE; base; base=base->next){
192                 if((base->lay & v3d->lay) && base->object->restrictflag & OB_RESTRICT_VIEW) {
193                         base->flag |= SELECT;
194                         base->object->flag = base->flag;
195                         base->object->restrictflag &= ~OB_RESTRICT_VIEW; 
196                         changed = 1;
197                 }
198         }
199         if (changed) {
200                 DAG_scene_sort(scene);
201                 WM_event_add_notifier(C, NC_SCENE|ND_OB_SELECT, scene);
202         }
203
204         return OPERATOR_FINISHED;
205 }
206
207 void OBJECT_OT_restrictview_clear(wmOperatorType *ot)
208 {
209         
210         /* identifiers */
211         ot->name= "Clear Restrict View";
212         ot->description = "Reveal the object by setting the restrictview flag.";
213         ot->idname= "OBJECT_OT_restrictview_clear";
214         
215         /* api callbacks */
216         ot->exec= object_restrictview_clear_exec;
217         ot->poll= ED_operator_view3d_active;
218         
219         /* flags */
220         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
221 }
222
223 static int object_restrictview_set_exec(bContext *C, wmOperator *op)
224 {
225         Scene *scene= CTX_data_scene(C);
226         short changed = 0;
227         int unselected= RNA_boolean_get(op->ptr, "unselected");
228         
229         CTX_DATA_BEGIN(C, Base*, base, visible_bases) {
230                 if(!unselected) {
231                         if (base->flag & SELECT){
232                                 base->flag &= ~SELECT;
233                                 base->object->flag = base->flag;
234                                 base->object->restrictflag |= OB_RESTRICT_VIEW;
235                                 changed = 1;
236                                 if (base==BASACT) {
237                                         ED_base_object_activate(C, NULL);
238                                 }
239                         }
240                 }
241                 else {
242                         if (!(base->flag & SELECT)){
243                                 base->object->restrictflag |= OB_RESTRICT_VIEW;
244                                 changed = 1;
245                         }
246                 }       
247         }
248         CTX_DATA_END;
249
250         if (changed) {
251                 DAG_scene_sort(scene);
252                 
253                 WM_event_add_notifier(C, NC_SCENE|ND_OB_SELECT, CTX_data_scene(C));
254                 
255         }
256
257         return OPERATOR_FINISHED;
258 }
259
260 void OBJECT_OT_restrictview_set(wmOperatorType *ot)
261 {
262         /* identifiers */
263         ot->name= "Set Restrict View";
264         ot->description = "Hide the object by setting the restrictview flag.";
265         ot->idname= "OBJECT_OT_restrictview_set";
266         
267         /* api callbacks */
268         ot->exec= object_restrictview_set_exec;
269         ot->poll= ED_operator_view3d_active;
270         
271         /* flags */
272         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
273         
274         RNA_def_boolean(ot->srna, "unselected", 0, "Unselected", "Hide unselected rather than selected objects.");
275         
276 }
277
278
279 /* ******************* toggle editmode operator  ***************** */
280
281 void ED_object_exit_editmode(bContext *C, int flag)
282 {
283         /* Note! only in exceptional cases should 'EM_DO_UNDO' NOT be in the flag */
284
285         Scene *scene= CTX_data_scene(C);
286         Object *obedit= CTX_data_edit_object(C);
287         int freedata = flag & EM_FREEDATA;
288         
289         if(obedit==NULL) return;
290         
291         if(flag & EM_WAITCURSOR) waitcursor(1);
292         if(obedit->type==OB_MESH) {
293                 Mesh *me= obedit->data;
294                 
295 //              if(EM_texFaceCheck())
296                 
297 //              if(retopo_mesh_paint_check())
298 //                      retopo_end_okee();
299                 
300                 if(me->edit_mesh->totvert>MESH_MAX_VERTS) {
301                         error("Too many vertices");
302                         return;
303                 }
304                 load_editMesh(scene, obedit);
305                 
306                 if(freedata) {
307                         free_editMesh(me->edit_mesh);
308                         MEM_freeN(me->edit_mesh);
309                         me->edit_mesh= NULL;
310                 }
311                 
312                 if(obedit->restore_mode & OB_MODE_WEIGHT_PAINT)
313                         mesh_octree_table(obedit, NULL, NULL, 'e');
314         }
315         else if (obedit->type==OB_ARMATURE) {   
316                 ED_armature_from_edit(obedit);
317                 if(freedata)
318                         ED_armature_edit_free(obedit);
319         }
320         else if(ELEM(obedit->type, OB_CURVE, OB_SURF)) {
321                 load_editNurb(obedit);
322                 if(freedata) free_editNurb(obedit);
323         }
324         else if(obedit->type==OB_FONT && freedata) {
325                 load_editText(obedit);
326                 if(freedata) free_editText(obedit);
327         }
328         else if(obedit->type==OB_LATTICE) {
329                 load_editLatt(obedit);
330                 if(freedata) free_editLatt(obedit);
331         }
332         else if(obedit->type==OB_MBALL) {
333                 load_editMball(obedit);
334                 if(freedata) free_editMball(obedit);
335         }
336
337         /* freedata only 0 now on file saves and render */
338         if(freedata) {
339                 ListBase pidlist;
340                 PTCacheID *pid;
341
342                 /* for example; displist make is different in editmode */
343                 scene->obedit= NULL; // XXX for context
344
345                 /* flag object caches as outdated */
346                 BKE_ptcache_ids_from_object(&pidlist, obedit);
347                 for(pid=pidlist.first; pid; pid=pid->next) {
348                         if(pid->type != PTCACHE_TYPE_PARTICLES) /* particles don't need reset on geometry change */
349                                 pid->cache->flag |= PTCACHE_OUTDATED;
350                 }
351                 BLI_freelistN(&pidlist);
352                 
353                 BKE_ptcache_object_reset(scene, obedit, PTCACHE_RESET_OUTDATED);
354
355                 /* also flush ob recalc, doesn't take much overhead, but used for particles */
356                 DAG_id_flush_update(&obedit->id, OB_RECALC_OB|OB_RECALC_DATA);
357         
358                 if(flag & EM_DO_UNDO)
359                         ED_undo_push(C, "Editmode");
360         
361                 if(flag & EM_WAITCURSOR) waitcursor(0);
362         
363                 WM_event_add_notifier(C, NC_SCENE|ND_MODE|NS_MODE_OBJECT, scene);
364
365                 obedit->mode &= ~OB_MODE_EDIT;
366                 ED_object_toggle_modes(C, obedit->restore_mode);
367         }
368 }
369
370
371 void ED_object_enter_editmode(bContext *C, int flag)
372 {
373         Scene *scene= CTX_data_scene(C);
374         Base *base= NULL;
375         Object *ob;
376         ScrArea *sa= CTX_wm_area(C);
377         View3D *v3d= NULL;
378         int ok= 0;
379         
380         if(scene->id.lib) return;
381         
382         if(sa && sa->spacetype==SPACE_VIEW3D)
383                 v3d= sa->spacedata.first;
384         
385         if((flag & EM_IGNORE_LAYER)==0) {
386                 base= CTX_data_active_base(C); /* active layer checked here for view3d */
387
388                 if(base==NULL) return;
389                 else if(v3d && (base->lay & v3d->lay)==0) return;
390                 else if(!v3d && (base->lay & scene->lay)==0) return;
391         }
392         else {
393                 base= scene->basact;
394         }
395
396         if (ELEM3(NULL, base, base->object, base->object->data)) return;
397
398         ob = base->object;
399         
400         if (object_data_is_libdata(ob)) {
401                 error_libdata();
402                 return;
403         }
404         
405         if(flag & EM_WAITCURSOR) waitcursor(1);
406
407         ob->restore_mode = ob->mode;
408         ED_object_toggle_modes(C, ob->mode);
409
410         ob->mode |= OB_MODE_EDIT;
411         
412         if(ob->type==OB_MESH) {
413                 Mesh *me= ob->data;
414                 
415                 if(me->pv) mesh_pmv_off(ob, me);
416                 ok= 1;
417                 scene->obedit= ob;      // context sees this
418                 
419                 make_editMesh(scene, ob);
420
421                 WM_event_add_notifier(C, NC_SCENE|ND_MODE|NS_EDITMODE_MESH, scene);
422         }
423         else if (ob->type==OB_ARMATURE){
424                 bArmature *arm= base->object->data;
425                 if (!arm) return;
426                 /*
427                  * The function object_data_is_libdata make a problem here, the
428                  * check for ob->proxy return 0 and let blender enter to edit mode
429                  * this causa a crash when you try leave the edit mode.
430                  * The problem is that i can't remove the ob->proxy check from
431                  * object_data_is_libdata that prevent the bugfix #6614, so
432                  * i add this little hack here.
433                  */
434                 if(arm->id.lib) {
435                         error_libdata();
436                         return;
437                 }
438                 ok=1;
439                 scene->obedit= ob;
440                 ED_armature_to_edit(ob);
441                 /* to ensure all goes in restposition and without striding */
442                 DAG_id_flush_update(&ob->id, OB_RECALC);
443
444                 WM_event_add_notifier(C, NC_SCENE|ND_MODE|NS_EDITMODE_ARMATURE, scene);
445         }
446         else if(ob->type==OB_FONT) {
447                 scene->obedit= ob; // XXX for context
448                 ok= 1;
449                 make_editText(ob);
450
451                 WM_event_add_notifier(C, NC_SCENE|ND_MODE|NS_EDITMODE_TEXT, scene);
452         }
453         else if(ob->type==OB_MBALL) {
454                 scene->obedit= ob; // XXX for context
455                 ok= 1;
456                 make_editMball(ob);
457
458                 WM_event_add_notifier(C, NC_SCENE|ND_MODE|NS_EDITMODE_MBALL, scene);
459         }
460         else if(ob->type==OB_LATTICE) {
461                 scene->obedit= ob; // XXX for context
462                 ok= 1;
463                 make_editLatt(ob);
464                 
465                 WM_event_add_notifier(C, NC_SCENE|ND_MODE|NS_EDITMODE_LATTICE, scene);
466         }
467         else if(ob->type==OB_SURF || ob->type==OB_CURVE) {
468                 ok= 1;
469                 scene->obedit= ob; // XXX for context
470                 make_editNurb(ob);
471                 
472                 WM_event_add_notifier(C, NC_SCENE|ND_MODE|NS_EDITMODE_CURVE, scene);
473         }
474         
475         if(ok) {
476                 DAG_id_flush_update(&ob->id, OB_RECALC_DATA);
477         }
478         else {
479                 scene->obedit= NULL; // XXX for context
480                 ob->mode &= ~OB_MODE_EDIT;
481                 WM_event_add_notifier(C, NC_SCENE|ND_MODE|NS_MODE_OBJECT, scene);
482         }
483         
484         if(flag & EM_DO_UNDO) ED_undo_push(C, "Enter Editmode");
485         if(flag & EM_WAITCURSOR) waitcursor(0);
486 }
487
488 static int editmode_toggle_exec(bContext *C, wmOperator *op)
489 {
490         if(!CTX_data_edit_object(C))
491                 ED_object_enter_editmode(C, EM_WAITCURSOR);
492         else
493                 ED_object_exit_editmode(C, EM_FREEDATA|EM_FREEUNDO|EM_WAITCURSOR|EM_DO_UNDO);
494         
495         return OPERATOR_FINISHED;
496 }
497
498 static int editmode_toggle_poll(bContext *C)
499 {
500         Object *ob = CTX_data_active_object(C);
501
502         /* covers proxies too */
503         if(ob->data==NULL || ((ID *)ob->data)->lib)
504                 return 0;
505
506         return ob && (ob->type == OB_MESH || ob->type == OB_ARMATURE ||
507                       ob->type == OB_FONT || ob->type == OB_MBALL ||
508                       ob->type == OB_LATTICE || ob->type == OB_SURF ||
509                       ob->type == OB_CURVE);
510 }
511
512 void OBJECT_OT_editmode_toggle(wmOperatorType *ot)
513 {
514         
515         /* identifiers */
516         ot->name= "Toggle Editmode";
517         ot->description = "Toggle object's editmode.";
518         ot->idname= "OBJECT_OT_editmode_toggle";
519         
520         /* api callbacks */
521         ot->exec= editmode_toggle_exec;
522         
523         ot->poll= editmode_toggle_poll;
524         
525         /* flags */
526         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
527 }
528
529 /* *************************** */
530
531 static int posemode_exec(bContext *C, wmOperator *op)
532 {
533         Base *base= CTX_data_active_base(C);
534         
535         if(base->object->type==OB_ARMATURE) {
536                 if(base->object==CTX_data_edit_object(C)) {
537                         ED_object_exit_editmode(C, EM_FREEDATA|EM_DO_UNDO);
538                         ED_armature_enter_posemode(C, base);
539                 }
540                 else if(base->object->mode & OB_MODE_POSE)
541                         ED_armature_exit_posemode(C, base);
542                 else
543                         ED_armature_enter_posemode(C, base);
544                 
545                 return OPERATOR_FINISHED;
546         }
547         
548         return OPERATOR_PASS_THROUGH;
549 }
550
551 void OBJECT_OT_posemode_toggle(wmOperatorType *ot) 
552 {
553         /* identifiers */
554         ot->name= "Toggle Pose Mode";
555         ot->idname= "OBJECT_OT_posemode_toggle";
556         ot->description= "Enables or disables posing/selecting bones";
557         
558         /* api callbacks */
559         ot->exec= posemode_exec;
560         ot->poll= ED_operator_object_active_editable;
561         
562         /* flag */
563         ot->flag= OPTYPE_REGISTER;
564 }
565
566 /* *********************** */
567
568 void check_editmode(int type)
569 {
570         Object *obedit= NULL; // XXX
571         
572         if (obedit==NULL || obedit->type==type) return;
573
574 // XXX  ED_object_exit_editmode(C, EM_FREEDATA|EM_FREEUNDO|EM_WAITCURSOR|EM_DO_UNDO); /* freedata, and undo */
575 }
576
577 #if 0
578 // XXX should be in view3d?
579
580 /* context: ob = lamp */
581 /* code should be replaced with proper (custom) transform handles for lamp properties */
582 static void spot_interactive(Object *ob, int mode)
583 {
584         Lamp *la= ob->data;
585         float transfac, dx, dy, ratio, origval;
586         int keep_running= 1, center2d[2];
587         short mval[2], mvalo[2];
588         
589 //      getmouseco_areawin(mval);
590 //      getmouseco_areawin(mvalo);
591         
592         project_int(ob->obmat[3], center2d);
593         if( center2d[0] > 100000 ) {            /* behind camera */
594 //              center2d[0]= curarea->winx/2;
595 //              center2d[1]= curarea->winy/2;
596         }
597
598 //      helpline(mval, center2d);
599         
600         /* ratio is like scaling */
601         dx = (float)(center2d[0] - mval[0]);
602         dy = (float)(center2d[1] - mval[1]);
603         transfac = (float)sqrt( dx*dx + dy*dy);
604         if(transfac==0.0f) transfac= 1.0f;
605         
606         if(mode==1)     
607                 origval= la->spotsize;
608         else if(mode==2)        
609                 origval= la->dist;
610         else if(mode==3)        
611                 origval= la->clipsta;
612         else    
613                 origval= la->clipend;
614         
615         while (keep_running>0) {
616                 
617 //              getmouseco_areawin(mval);
618                 
619                 /* essential for idling subloop */
620                 if(mval[0]==mvalo[0] && mval[1]==mvalo[1]) {
621                         PIL_sleep_ms(2);
622                 }
623                 else {
624                         char str[32];
625                         
626                         dx = (float)(center2d[0] - mval[0]);
627                         dy = (float)(center2d[1] - mval[1]);
628                         ratio = (float)(sqrt( dx*dx + dy*dy))/transfac;
629                         
630                         /* do the trick */
631                         
632                         if(mode==1) {   /* spot */
633                                 la->spotsize = ratio*origval;
634                                 CLAMP(la->spotsize, 1.0f, 180.0f);
635                                 sprintf(str, "Spot size %.2f\n", la->spotsize);
636                         }
637                         else if(mode==2) {      /* dist */
638                                 la->dist = ratio*origval;
639                                 CLAMP(la->dist, 0.01f, 5000.0f);
640                                 sprintf(str, "Distance %.2f\n", la->dist);
641                         }
642                         else if(mode==3) {      /* sta */
643                                 la->clipsta = ratio*origval;
644                                 CLAMP(la->clipsta, 0.001f, 5000.0f);
645                                 sprintf(str, "Distance %.2f\n", la->clipsta);
646                         }
647                         else if(mode==4) {      /* end */
648                                 la->clipend = ratio*origval;
649                                 CLAMP(la->clipend, 0.1f, 5000.0f);
650                                 sprintf(str, "Clip End %.2f\n", la->clipend);
651                         }
652
653                         /* cleanup */
654                         mvalo[0]= mval[0];
655                         mvalo[1]= mval[1];
656                         
657                         /* handle shaded mode */
658 // XXX                  shade_buttons_change_3d();
659
660                         /* DRAW */      
661                         headerprint(str);
662                         force_draw_plus(SPACE_BUTS, 0);
663
664 //                      helpline(mval, center2d);
665                 }
666                 
667                 while( qtest() ) {
668                         short val;
669                         unsigned short event= extern_qread(&val);
670                         
671                         switch (event){
672                                 case ESCKEY:
673                                 case RIGHTMOUSE:
674                                         keep_running= 0;
675                                         break;
676                                 case LEFTMOUSE:
677                                 case SPACEKEY:
678                                 case PADENTER:
679                                 case RETKEY:
680                                         if(val)
681                                                 keep_running= -1;
682                                         break;
683                         }
684                 }
685         }
686
687         if(keep_running==0) {
688                 if(mode==1)     
689                         la->spotsize= origval;
690                 else if(mode==2)        
691                         la->dist= origval;
692                 else if(mode==3)        
693                         la->clipsta= origval;
694                 else    
695                         la->clipend= origval;
696         }
697
698 }
699 #endif
700
701 void special_editmenu(Scene *scene, View3D *v3d)
702 {
703 // XXX  static short numcuts= 2;
704         Object *ob= OBACT;
705         Object *obedit= NULL; // XXX
706         int nr,ret=0;
707         
708         if(ob==NULL) return;
709         
710         if(obedit==NULL) {
711                 
712                 if(ob->mode & OB_MODE_POSE) {
713 // XXX                  pose_special_editmenu();
714                 }
715                 else if(paint_facesel_test(ob)) {
716                         Mesh *me= get_mesh(ob);
717                         MTFace *tface;
718                         MFace *mface;
719                         int a;
720                         
721                         if(me==0 || me->mtface==0) return;
722                         
723                         nr= pupmenu("Specials%t|Set     Tex%x1|         Shared%x2|         Light%x3|         Invisible%x4|         Collision%x5|         TwoSide%x6|Clr     Tex%x7|         Shared%x8|         Light%x9|         Invisible%x10|         Collision%x11|         TwoSide%x12");
724                         
725                         tface= me->mtface;
726                         mface= me->mface;
727                         for(a=me->totface; a>0; a--, tface++, mface++) {
728                                 if(mface->flag & ME_FACE_SEL) {
729                                         switch(nr) {
730                                         case 1:
731                                                 tface->mode |= TF_TEX; break;
732                                         case 2:
733                                                 tface->mode |= TF_SHAREDCOL; break;
734                                         case 3:
735                                                 tface->mode |= TF_LIGHT; break; 
736                                         case 4:
737                                                 tface->mode |= TF_INVISIBLE; break;
738                                         case 5:
739                                                 tface->mode |= TF_DYNAMIC; break;
740                                         case 6:
741                                                 tface->mode |= TF_TWOSIDE; break;
742                                         case 7:
743                                                 tface->mode &= ~TF_TEX;
744                                                 tface->tpage= 0;
745                                                 break;
746                                         case 8:
747                                                 tface->mode &= ~TF_SHAREDCOL; break;
748                                         case 9:
749                                                 tface->mode &= ~TF_LIGHT; break;
750                                         case 10:
751                                                 tface->mode &= ~TF_INVISIBLE; break;
752                                         case 11:
753                                                 tface->mode &= ~TF_DYNAMIC; break;
754                                         case 12:
755                                                 tface->mode &= ~TF_TWOSIDE; break;
756                                         }
757                                 }
758                         }
759                         DAG_id_flush_update(&ob->id, OB_RECALC_DATA);
760                 }
761                 else if(ob->mode & OB_MODE_VERTEX_PAINT) {
762                         Mesh *me= get_mesh(ob);
763                         
764                         if(me==0 || (me->mcol==NULL && me->mtface==NULL) ) return;
765                         
766                         nr= pupmenu("Specials%t|Shared VertexCol%x1");
767                         if(nr==1) {
768                                 
769 // XXX                          do_shared_vertexcol(me);
770                                 
771                                 DAG_id_flush_update(&ob->id, OB_RECALC_DATA);
772                         }
773                 }
774                 else if(ob->mode & OB_MODE_WEIGHT_PAINT) {
775                         Object *par= modifiers_isDeformedByArmature(ob);
776
777                         if(par && (par->mode & OB_MODE_POSE)) {
778                                 nr= pupmenu("Specials%t|Apply Bone Envelopes to Vertex Groups %x1|Apply Bone Heat Weights to Vertex Groups %x2");
779
780 // XXX                          if(nr==1 || nr==2)
781 // XXX                                  pose_adds_vgroups(ob, (nr == 2));
782                         }
783                 }
784                 else if(ob->mode & OB_MODE_PARTICLE_EDIT) {
785 #if 0
786                         // XXX
787                         ParticleSystem *psys = PE_get_current(ob);
788                         ParticleEditSettings *pset = PE_settings();
789
790                         if(!psys)
791                                 return;
792
793                         if(pset->selectmode & SCE_SELECT_POINT)
794                                 nr= pupmenu("Specials%t|Rekey%x1|Subdivide%x2|Select First%x3|Select Last%x4|Remove Doubles%x5");
795                         else
796                                 nr= pupmenu("Specials%t|Rekey%x1|Remove Doubles%x5");
797                         
798                         switch(nr) {
799                         case 1:
800 // XXX                          if(button(&pset->totrekey, 2, 100, "Number of Keys:")==0) return;
801                                 waitcursor(1);
802                                 PE_rekey();
803                                 break;
804                         case 2:
805                                 PE_subdivide();
806                                 break;
807                         case 3:
808                                 PE_select_root();
809                                 break;
810                         case 4:
811                                 PE_select_tip();
812                                 break;
813                         case 5:
814                                 PE_remove_doubles();
815                                 break;
816                         }
817                         
818                         DAG_id_flush_update(&obedit->id, OB_RECALC_DATA);
819                         
820                         if(nr>0) waitcursor(0);
821 #endif
822                 }
823                 else {
824                         Base *base, *base_select= NULL;
825                         
826                         /* Get the active object mesh. */
827                         Mesh *me= get_mesh(ob);
828
829                         /* Booleans, if the active object is a mesh... */
830                         if (me && ob->id.lib==NULL) {
831                                 
832                                 /* Bring up a little menu with the boolean operation choices on. */
833                                 nr= pupmenu("Boolean Tools%t|Intersect%x1|Union%x2|Difference%x3|Add Intersect Modifier%x4|Add Union Modifier%x5|Add Difference Modifier%x6");
834
835                                 if (nr > 0) {
836                                         /* user has made a choice of a menu element.
837                                            All of the boolean functions require 2 mesh objects 
838                                            we search through the object list to find the other 
839                                            selected item and make sure it is distinct and a mesh. */
840
841                                         for(base= FIRSTBASE; base; base= base->next) {
842                                                 if(TESTBASELIB(v3d, base)) {
843                                                         if(base->object != ob) base_select= base;
844                                                 }
845                                         }
846
847                                         if (base_select) {
848                                                 if (get_mesh(base_select->object)) {
849                                                         if(nr <= 3){
850                                                                 waitcursor(1);
851 // XXX                                                          ret = NewBooleanMesh(BASACT,base_select,nr);
852                                                                 if (ret==0) {
853                                                                         error("An internal error occurred");
854                                                                 } else if(ret==-1) {
855                                                                         error("Selected meshes must have faces to perform boolean operations");
856                                                                 } else if (ret==-2) {
857                                                                         error("Both meshes must be a closed mesh");
858                                                                 }
859                                                                 waitcursor(0);
860                                                         } else {
861                                                                 BooleanModifierData *bmd = NULL;
862                                                                 bmd = (BooleanModifierData *)modifier_new(eModifierType_Boolean);
863                                                                 BLI_addtail(&ob->modifiers, bmd);
864                                                                 modifier_unique_name(&ob->modifiers, (ModifierData*)bmd);
865                                                                 bmd->object = base_select->object;
866                                                                 bmd->modifier.mode |= eModifierMode_Realtime;
867                                                                 switch(nr){
868                                                                         case 4: bmd->operation = eBooleanModifierOp_Intersect; break;
869                                                                         case 5: bmd->operation = eBooleanModifierOp_Union; break;
870                                                                         case 6: bmd->operation = eBooleanModifierOp_Difference; break;
871                                                                 }
872 // XXX                                                          do_common_editbuts(B_CHANGEDEP);
873                                                         }                                                               
874                                                 } else {
875                                                         error("Please select 2 meshes");
876                                                 }
877                                         } else {
878                                                 error("Please select 2 meshes");
879                                         }
880                                 }
881
882                         }
883                         else if (ob->type == OB_FONT) {
884                                 /* removed until this gets a decent implementation (ton) */
885 /*                              nr= pupmenu("Split %t|Characters%x1");
886                                 if (nr > 0) {
887                                         switch(nr) {
888                                                 case 1: split_font();
889                                         }
890                                 }
891 */
892                         }                       
893                 }
894         }
895         else if(obedit->type==OB_MESH) {
896         }
897         else if(ELEM(obedit->type, OB_CURVE, OB_SURF)) {
898         }
899         else if(obedit->type==OB_ARMATURE) {
900                 nr= pupmenu("Specials%t|Subdivide %x1|Subdivide Multi%x2|Switch Direction%x7|Flip Left-Right Names%x3|%l|AutoName Left-Right%x4|AutoName Front-Back%x5|AutoName Top-Bottom%x6");
901 //              if(nr==1)
902 // XXX                  subdivide_armature(1);
903                 if(nr==2) {
904 // XXX                  if(button(&numcuts, 1, 128, "Number of Cuts:")==0) return;
905                         waitcursor(1);
906 // XXX                  subdivide_armature(numcuts);
907                 }
908 //              else if(nr==3)
909 // XXX                  armature_flip_names();
910                 else if(ELEM3(nr, 4, 5, 6)) {
911 // XXX                  armature_autoside_names(nr-4);
912                 }
913 //              else if(nr == 7)
914 // XXX                  switch_direction_armature();
915         }
916         else if(obedit->type==OB_LATTICE) {
917                 Lattice *lt= obedit->data;
918                 static float weight= 1.0f;
919                 { // XXX
920 // XXX          if(fbutton(&weight, 0.0f, 1.0f, 10, 10, "Set Weight")) {
921                         int a= lt->editlatt->pntsu*lt->editlatt->pntsv*lt->editlatt->pntsw;
922                         BPoint *bp= lt->editlatt->def;
923                         
924                         while(a--) {
925                                 if(bp->f1 & SELECT)
926                                         bp->weight= weight;
927                                 bp++;
928                         }       
929                 }
930         }
931
932 }
933
934 /* Change subdivision or particle properties of mesh object ob, if level==-1
935  * then toggle subsurf, else set to level set allows to toggle multiple
936  * selections */
937
938 static void object_has_subdivision_particles(Object *ob, int *havesubdiv, int *havepart, int depth)
939 {
940         if(ob->type==OB_MESH) {
941                 if(modifiers_findByType(ob, eModifierType_Subsurf))
942                         *havesubdiv= 1;
943                 if(modifiers_findByType(ob, eModifierType_ParticleSystem))
944                         *havepart= 1;
945         }
946
947         if(ob->dup_group && depth <= 4) {
948                 GroupObject *go;
949
950                 for(go= ob->dup_group->gobject.first; go; go= go->next)
951                         object_has_subdivision_particles(go->ob, havesubdiv, havepart, depth+1);
952         }
953 }
954
955 static void object_flip_subdivison_particles(Scene *scene, Object *ob, int *set, int level, int mode, int particles, int depth)
956 {
957         ModifierData *md;
958
959         if(ob->type==OB_MESH) {
960                 if(particles) {
961                         for(md=ob->modifiers.first; md; md=md->next) {
962                                 if(md->type == eModifierType_ParticleSystem) {
963                                         ParticleSystemModifierData *psmd = (ParticleSystemModifierData*)md;
964
965                                         if(*set == -1)
966                                                 *set= psmd->modifier.mode&(mode);
967
968                                         if (*set)
969                                                 psmd->modifier.mode &= ~(mode);
970                                         else
971                                                 psmd->modifier.mode |= (mode);
972                                 }
973                         }
974                 }
975                 else {
976                         md = modifiers_findByType(ob, eModifierType_Subsurf);
977
978                         if (md) {
979                                 SubsurfModifierData *smd = (SubsurfModifierData*) md;
980
981                                 if (level == -1) {
982                                         if(*set == -1) 
983                                                 *set= smd->modifier.mode&(mode);
984
985                                         if (*set)
986                                                 smd->modifier.mode &= ~(mode);
987                                         else
988                                                 smd->modifier.mode |= (mode);
989                                 } else {
990                                         smd->levels = level;
991                                 }
992                         } 
993                         else if(depth == 0 && *set != 0) {
994                                 SubsurfModifierData *smd = (SubsurfModifierData*) modifier_new(eModifierType_Subsurf);
995                                 
996                                 BLI_addtail(&ob->modifiers, smd);
997                                 modifier_unique_name(&ob->modifiers, (ModifierData*)smd);
998                                 
999                                 if (level!=-1) {
1000                                         smd->levels = level;
1001                                 }
1002                                 
1003                                 if(*set == -1)
1004                                         *set= 1;
1005                         }
1006                 }
1007
1008                 DAG_id_flush_update(&ob->id, OB_RECALC_DATA);
1009         }
1010
1011         if(ob->dup_group && depth<=4) {
1012                 GroupObject *go;
1013
1014                 for(go= ob->dup_group->gobject.first; go; go= go->next)
1015                         object_flip_subdivison_particles(scene, go->ob, set, level, mode, particles, depth+1);
1016         }
1017 }
1018
1019 /* Change subdivision properties of mesh object ob, if
1020 * level==-1 then toggle subsurf, else set to level.
1021 */
1022
1023 void flip_subdivison(Scene *scene, View3D *v3d, int level)
1024 {
1025         Base *base;
1026         int set= -1;
1027         int mode, pupmode, particles= 0, havesubdiv= 0, havepart= 0;
1028         int alt= 0; // XXX
1029         
1030         if(alt)
1031                 mode= eModifierMode_Realtime;
1032         else
1033                 mode= eModifierMode_Render|eModifierMode_Realtime;
1034         
1035         if(level == -1) {
1036                 if (scene->obedit) { // XXX get from context
1037                         object_has_subdivision_particles(scene->obedit, &havesubdiv, &havepart, 0);                     
1038                 } else {
1039                         for(base= scene->base.first; base; base= base->next) {
1040                                 if(((level==-1) && (TESTBASE(v3d, base))) || (TESTBASELIB(v3d, base))) {
1041                                         object_has_subdivision_particles(base->object, &havesubdiv, &havepart, 0);
1042                                 }
1043                         }
1044                 }
1045         }
1046         else
1047                 havesubdiv= 1;
1048         
1049         if(havesubdiv && havepart) {
1050                 pupmode= pupmenu("Switch%t|Subsurf %x1|Particle Systems %x2");
1051                 if(pupmode <= 0)
1052                         return;
1053                 else if(pupmode == 2)
1054                         particles= 1;
1055         }
1056         else if(havepart)
1057                 particles= 1;
1058
1059         if (scene->obedit) {     // XXX get from context
1060                 object_flip_subdivison_particles(scene, scene->obedit, &set, level, mode, particles, 0);
1061         } else {
1062                 for(base= scene->base.first; base; base= base->next) {
1063                         if(((level==-1) && (TESTBASE(v3d, base))) || (TESTBASELIB(v3d, base))) {
1064                                 object_flip_subdivison_particles(scene, base->object, &set, level, mode, particles, 0);
1065                         }
1066                 }
1067         }
1068         
1069         DAG_ids_flush_update(0);
1070 }
1071  
1072 static void copymenu_properties(Scene *scene, View3D *v3d, Object *ob)
1073 {       
1074         bProperty *prop;
1075         Base *base;
1076         int nr, tot=0;
1077         char *str;
1078         
1079         prop= ob->prop.first;
1080         while(prop) {
1081                 tot++;
1082                 prop= prop->next;
1083         }
1084         
1085         str= MEM_callocN(50 + 33*tot, "copymenu prop");
1086         
1087         if (tot)
1088                 strcpy(str, "Copy Property %t|Replace All|Merge All|%l");
1089         else
1090                 strcpy(str, "Copy Property %t|Clear All (no properties on active)");
1091         
1092         tot= 0; 
1093         prop= ob->prop.first;
1094         while(prop) {
1095                 tot++;
1096                 strcat(str, "|");
1097                 strcat(str, prop->name);
1098                 prop= prop->next;
1099         }
1100
1101         nr= pupmenu(str);
1102         
1103         if ( nr==1 || nr==2 ) {
1104                 for(base= FIRSTBASE; base; base= base->next) {
1105                         if((base != BASACT) &&(TESTBASELIB(v3d, base))) {
1106                                 if (nr==1) { /* replace */
1107                                         copy_properties( &base->object->prop, &ob->prop );
1108                                 } else {
1109                                         for(prop = ob->prop.first; prop; prop= prop->next ) {
1110                                                 set_ob_property(base->object, prop);
1111                                         }
1112                                 }
1113                         }
1114                 }
1115         } else if(nr>0) {
1116                 prop = BLI_findlink(&ob->prop, nr-4); /* account for first 3 menu items & menu index starting at 1*/
1117                 
1118                 if(prop) {
1119                         for(base= FIRSTBASE; base; base= base->next) {
1120                                 if((base != BASACT) &&(TESTBASELIB(v3d, base))) {
1121                                         set_ob_property(base->object, prop);
1122                                 }
1123                         }
1124                 }
1125         }
1126         MEM_freeN(str);
1127         
1128 }
1129
1130 static void copymenu_logicbricks(Scene *scene, View3D *v3d, Object *ob)
1131 {
1132         Base *base;
1133         
1134         for(base= FIRSTBASE; base; base= base->next) {
1135                 if(base->object != ob) {
1136                         if(TESTBASELIB(v3d, base)) {
1137                                 
1138                                 /* first: free all logic */
1139                                 free_sensors(&base->object->sensors);                           
1140                                 unlink_controllers(&base->object->controllers);
1141                                 free_controllers(&base->object->controllers);
1142                                 unlink_actuators(&base->object->actuators);
1143                                 free_actuators(&base->object->actuators);
1144                                 
1145                                 /* now copy it, this also works without logicbricks! */
1146                                 clear_sca_new_poins_ob(ob);
1147                                 copy_sensors(&base->object->sensors, &ob->sensors);
1148                                 copy_controllers(&base->object->controllers, &ob->controllers);
1149                                 copy_actuators(&base->object->actuators, &ob->actuators);
1150                                 set_sca_new_poins_ob(base->object);
1151                                 
1152                                 /* some menu settings */
1153                                 base->object->scavisflag= ob->scavisflag;
1154                                 base->object->scaflag= ob->scaflag;
1155                                 
1156                                 /* set the initial state */
1157                                 base->object->state= ob->state;
1158                                 base->object->init_state= ob->init_state;
1159                         }
1160                 }
1161         }
1162 }
1163
1164 static void copymenu_modifiers(Scene *scene, View3D *v3d, Object *ob)
1165 {
1166         Base *base;
1167         int i, event;
1168         char str[512];
1169         char *errorstr= NULL;
1170
1171         strcpy(str, "Copy Modifiers %t");
1172
1173         sprintf(str+strlen(str), "|All%%x%d|%%l", NUM_MODIFIER_TYPES);
1174
1175         for (i=eModifierType_None+1; i<NUM_MODIFIER_TYPES; i++) {
1176                 ModifierTypeInfo *mti = modifierType_getInfo(i);
1177
1178                 if(ELEM3(i, eModifierType_Hook, eModifierType_Softbody, eModifierType_ParticleInstance)) continue;
1179                 
1180                 if(i == eModifierType_Collision)
1181                         continue;
1182
1183                 if (    (mti->flags&eModifierTypeFlag_AcceptsCVs) || 
1184                                 (ob->type==OB_MESH && (mti->flags&eModifierTypeFlag_AcceptsMesh))) {
1185                         sprintf(str+strlen(str), "|%s%%x%d", mti->name, i);
1186                 }
1187         }
1188
1189         event = pupmenu(str);
1190         if(event<=0) return;
1191
1192         for (base= FIRSTBASE; base; base= base->next) {
1193                 if(base->object != ob) {
1194                         if(TESTBASELIB(v3d, base)) {
1195                                 ModifierData *md;
1196
1197                                 base->object->recalc |= OB_RECALC_OB|OB_RECALC_DATA;
1198
1199                                 if (base->object->type==ob->type) {
1200                                         /* copy all */
1201                                         if (event==NUM_MODIFIER_TYPES) {
1202                                                 object_free_modifiers(base->object);
1203
1204                                                 for (md=ob->modifiers.first; md; md=md->next) {
1205                                                         ModifierData *nmd = NULL;
1206                                                         
1207                                                         if(ELEM3(md->type, eModifierType_Hook, eModifierType_Softbody, eModifierType_ParticleInstance)) continue;
1208                 
1209                                                         if(md->type == eModifierType_Collision)
1210                                                                 continue;
1211                                                         
1212                                                         nmd = modifier_new(md->type);
1213                                                         modifier_copyData(md, nmd);
1214                                                         BLI_addtail(&base->object->modifiers, nmd);
1215                                                         modifier_unique_name(&base->object->modifiers, nmd);
1216                                                 }
1217
1218                                                 copy_object_particlesystems(base->object, ob);
1219                                                 copy_object_softbody(base->object, ob);
1220                                         } else {
1221                                                 /* copy specific types */
1222                                                 ModifierData *md, *mdn;
1223                                                 
1224                                                 /* remove all with type 'event' */
1225                                                 for (md=base->object->modifiers.first; md; md=mdn) {
1226                                                         mdn= md->next;
1227                                                         if(md->type==event) {
1228                                                                 BLI_remlink(&base->object->modifiers, md);
1229                                                                 modifier_free(md);
1230                                                         }
1231                                                 }
1232                                                 
1233                                                 /* copy all with type 'event' */
1234                                                 for (md=ob->modifiers.first; md; md=md->next) {
1235                                                         if (md->type==event) {
1236                                                                 
1237                                                                 mdn = modifier_new(event);
1238                                                                 BLI_addtail(&base->object->modifiers, mdn);
1239                                                                 modifier_unique_name(&base->object->modifiers, mdn);
1240
1241                                                                 modifier_copyData(md, mdn);
1242                                                         }
1243                                                 }
1244
1245                                                 if(event == eModifierType_ParticleSystem) {
1246                                                         object_free_particlesystems(base->object);
1247                                                         copy_object_particlesystems(base->object, ob);
1248                                                 }
1249                                                 else if(event == eModifierType_Softbody) {
1250                                                         object_free_softbody(base->object);
1251                                                         copy_object_softbody(base->object, ob);
1252                                                 }
1253                                         }
1254                                 }
1255                                 else
1256                                         errorstr= "Did not copy modifiers to other Object types";
1257                         }
1258                 }
1259         }
1260         
1261 //      if(errorstr) notice(errorstr);
1262         
1263         DAG_scene_sort(scene);
1264         
1265 }
1266
1267 /* both pointers should exist */
1268 static void copy_texture_space(Object *to, Object *ob)
1269 {
1270         float *poin1= NULL, *poin2= NULL;
1271         short texflag= 0;
1272         
1273         if(ob->type==OB_MESH) {
1274                 texflag= ((Mesh *)ob->data)->texflag;
1275                 poin2= ((Mesh *)ob->data)->loc;
1276         }
1277         else if (ELEM3(ob->type, OB_CURVE, OB_SURF, OB_FONT)) {
1278                 texflag= ((Curve *)ob->data)->texflag;
1279                 poin2= ((Curve *)ob->data)->loc;
1280         }
1281         else if(ob->type==OB_MBALL) {
1282                 texflag= ((MetaBall *)ob->data)->texflag;
1283                 poin2= ((MetaBall *)ob->data)->loc;
1284         }
1285         else
1286                 return;
1287                 
1288         if(to->type==OB_MESH) {
1289                 ((Mesh *)to->data)->texflag= texflag;
1290                 poin1= ((Mesh *)to->data)->loc;
1291         }
1292         else if (ELEM3(to->type, OB_CURVE, OB_SURF, OB_FONT)) {
1293                 ((Curve *)to->data)->texflag= texflag;
1294                 poin1= ((Curve *)to->data)->loc;
1295         }
1296         else if(to->type==OB_MBALL) {
1297                 ((MetaBall *)to->data)->texflag= texflag;
1298                 poin1= ((MetaBall *)to->data)->loc;
1299         }
1300         else
1301                 return;
1302         
1303         memcpy(poin1, poin2, 9*sizeof(float));  /* this was noted in DNA_mesh, curve, mball */
1304         
1305         if(to->type==OB_MESH) ;
1306         else if(to->type==OB_MBALL) tex_space_mball(to);
1307         else tex_space_curve(to->data);
1308         
1309 }
1310
1311 void copy_attr(Scene *scene, View3D *v3d, short event)
1312 {
1313         Object *ob;
1314         Base *base;
1315         Curve *cu, *cu1;
1316         Nurb *nu;
1317         int do_scene_sort= 0;
1318         
1319         if(scene->id.lib) return;
1320
1321         if(!(ob=OBACT)) return;
1322         
1323         if(scene->obedit) { // XXX get from context
1324                 /* obedit_copymenu(); */
1325                 return;
1326         }
1327         if(event==9) {
1328                 copymenu_properties(scene, v3d, ob);
1329                 return;
1330         }
1331         else if(event==10) {
1332                 copymenu_logicbricks(scene, v3d, ob);
1333                 return;
1334         }
1335         else if(event==24) {
1336                 copymenu_modifiers(scene, v3d, ob);
1337                 return;
1338         }
1339
1340         for(base= FIRSTBASE; base; base= base->next) {
1341                 if(base != BASACT) {
1342                         if(TESTBASELIB(v3d, base)) {
1343                                 base->object->recalc |= OB_RECALC_OB;
1344                                 
1345                                 if(event==1) {  /* loc */
1346                                         VECCOPY(base->object->loc, ob->loc);
1347                                         VECCOPY(base->object->dloc, ob->dloc);
1348                                 }
1349                                 else if(event==2) {  /* rot */
1350                                         VECCOPY(base->object->rot, ob->rot);
1351                                         VECCOPY(base->object->drot, ob->drot);
1352                                         /* Quats arnt used yet */
1353                                         /*VECCOPY(base->object->quat, ob->quat);
1354                                         VECCOPY(base->object->dquat, ob->dquat);*/
1355                                 }
1356                                 else if(event==3) {  /* size */
1357                                         VECCOPY(base->object->size, ob->size);
1358                                         VECCOPY(base->object->dsize, ob->dsize);
1359                                 }
1360                                 else if(event==4) {  /* drawtype */
1361                                         base->object->dt= ob->dt;
1362                                         base->object->dtx= ob->dtx;
1363                                         base->object->empty_drawtype= ob->empty_drawtype;
1364                                         base->object->empty_drawsize= ob->empty_drawsize;
1365                                 }
1366                                 else if(event==5) {  /* time offs */
1367                                         base->object->sf= ob->sf;
1368                                 }
1369                                 else if(event==6) {  /* dupli */
1370                                         base->object->dupon= ob->dupon;
1371                                         base->object->dupoff= ob->dupoff;
1372                                         base->object->dupsta= ob->dupsta;
1373                                         base->object->dupend= ob->dupend;
1374                                         
1375                                         base->object->transflag &= ~OB_DUPLI;
1376                                         base->object->transflag |= (ob->transflag & OB_DUPLI);
1377
1378                                         base->object->dup_group= ob->dup_group;
1379                                         if(ob->dup_group)
1380                                                 id_us_plus((ID *)ob->dup_group);
1381                                 }
1382                                 else if(event==7) {     /* mass */
1383                                         base->object->mass= ob->mass;
1384                                 }
1385                                 else if(event==8) {     /* damping */
1386                                         base->object->damping= ob->damping;
1387                                         base->object->rdamping= ob->rdamping;
1388                                 }
1389                                 else if(event==11) {    /* all physical attributes */
1390                                         base->object->gameflag = ob->gameflag;
1391                                         base->object->inertia = ob->inertia;
1392                                         base->object->formfactor = ob->formfactor;
1393                                         base->object->damping= ob->damping;
1394                                         base->object->rdamping= ob->rdamping;
1395                                         base->object->min_vel= ob->min_vel;
1396                                         base->object->max_vel= ob->max_vel;
1397                                         if (ob->gameflag & OB_BOUNDS) {
1398                                                 base->object->boundtype = ob->boundtype;
1399                                         }
1400                                         base->object->margin= ob->margin;
1401                                         base->object->bsoft= copy_bulletsoftbody(ob->bsoft);
1402
1403                                 }
1404                                 else if(event==17) {    /* tex space */
1405                                         copy_texture_space(base->object, ob);
1406                                 }
1407                                 else if(event==18) {    /* font settings */
1408                                         
1409                                         if(base->object->type==ob->type) {
1410                                                 cu= ob->data;
1411                                                 cu1= base->object->data;
1412                                                 
1413                                                 cu1->spacemode= cu->spacemode;
1414                                                 cu1->spacing= cu->spacing;
1415                                                 cu1->linedist= cu->linedist;
1416                                                 cu1->shear= cu->shear;
1417                                                 cu1->fsize= cu->fsize;
1418                                                 cu1->xof= cu->xof;
1419                                                 cu1->yof= cu->yof;
1420                                                 cu1->textoncurve= cu->textoncurve;
1421                                                 cu1->wordspace= cu->wordspace;
1422                                                 cu1->ulpos= cu->ulpos;
1423                                                 cu1->ulheight= cu->ulheight;
1424                                                 if(cu1->vfont) cu1->vfont->id.us--;
1425                                                 cu1->vfont= cu->vfont;
1426                                                 id_us_plus((ID *)cu1->vfont);
1427                                                 if(cu1->vfontb) cu1->vfontb->id.us--;
1428                                                 cu1->vfontb= cu->vfontb;
1429                                                 id_us_plus((ID *)cu1->vfontb);
1430                                                 if(cu1->vfonti) cu1->vfonti->id.us--;
1431                                                 cu1->vfonti= cu->vfonti;
1432                                                 id_us_plus((ID *)cu1->vfonti);
1433                                                 if(cu1->vfontbi) cu1->vfontbi->id.us--;
1434                                                 cu1->vfontbi= cu->vfontbi;
1435                                                 id_us_plus((ID *)cu1->vfontbi);                                         
1436
1437                                                 BKE_text_to_curve(scene, base->object, 0);              /* needed? */
1438
1439                                                 
1440                                                 strcpy(cu1->family, cu->family);
1441                                                 
1442                                                 base->object->recalc |= OB_RECALC_DATA;
1443                                         }
1444                                 }
1445                                 else if(event==19) {    /* bevel settings */
1446                                         
1447                                         if(ELEM(base->object->type, OB_CURVE, OB_FONT)) {
1448                                                 cu= ob->data;
1449                                                 cu1= base->object->data;
1450                                                 
1451                                                 cu1->bevobj= cu->bevobj;
1452                                                 cu1->taperobj= cu->taperobj;
1453                                                 cu1->width= cu->width;
1454                                                 cu1->bevresol= cu->bevresol;
1455                                                 cu1->ext1= cu->ext1;
1456                                                 cu1->ext2= cu->ext2;
1457                                                 
1458                                                 base->object->recalc |= OB_RECALC_DATA;
1459                                         }
1460                                 }
1461                                 else if(event==25) {    /* curve resolution */
1462
1463                                         if(ELEM(base->object->type, OB_CURVE, OB_FONT)) {
1464                                                 cu= ob->data;
1465                                                 cu1= base->object->data;
1466                                                 
1467                                                 cu1->resolu= cu->resolu;
1468                                                 cu1->resolu_ren= cu->resolu_ren;
1469                                                 
1470                                                 nu= cu1->nurb.first;
1471                                                 
1472                                                 while(nu) {
1473                                                         nu->resolu= cu1->resolu;
1474                                                         nu= nu->next;
1475                                                 }
1476                                                 
1477                                                 base->object->recalc |= OB_RECALC_DATA;
1478                                         }
1479                                 }
1480                                 else if(event==21){
1481                                         if (base->object->type==OB_MESH) {
1482                                                 ModifierData *md = modifiers_findByType(ob, eModifierType_Subsurf);
1483
1484                                                 if (md) {
1485                                                         ModifierData *tmd = modifiers_findByType(base->object, eModifierType_Subsurf);
1486
1487                                                         if (!tmd) {
1488                                                                 tmd = modifier_new(eModifierType_Subsurf);
1489                                                                 BLI_addtail(&base->object->modifiers, tmd);
1490                                                         }
1491
1492                                                         modifier_copyData(md, tmd);
1493                                                         base->object->recalc |= OB_RECALC_DATA;
1494                                                 }
1495                                         }
1496                                 }
1497                                 else if(event==22) {
1498                                         /* Copy the constraint channels over */
1499                                         copy_constraints(&base->object->constraints, &ob->constraints);
1500                                         
1501                                         do_scene_sort= 1;
1502                                 }
1503                                 else if(event==23) {
1504                                         base->object->softflag= ob->softflag;
1505                                         if(base->object->soft) sbFree(base->object->soft);
1506                                         
1507                                         base->object->soft= copy_softbody(ob->soft);
1508
1509                                         if (!modifiers_findByType(base->object, eModifierType_Softbody)) {
1510                                                 BLI_addhead(&base->object->modifiers, modifier_new(eModifierType_Softbody));
1511                                         }
1512                                 }
1513                                 else if(event==26) {
1514 #if 0 // XXX old animation system
1515                                         copy_nlastrips(&base->object->nlastrips, &ob->nlastrips);
1516 #endif // XXX old animation system
1517                                 }
1518                                 else if(event==27) {    /* autosmooth */
1519                                         if (base->object->type==OB_MESH) {
1520                                                 Mesh *me= ob->data;
1521                                                 Mesh *cme= base->object->data;
1522                                                 cme->smoothresh= me->smoothresh;
1523                                                 if(me->flag & ME_AUTOSMOOTH)
1524                                                         cme->flag |= ME_AUTOSMOOTH;
1525                                                 else
1526                                                         cme->flag &= ~ME_AUTOSMOOTH;
1527                                         }
1528                                 }
1529                                 else if(event==28) { /* UV orco */
1530                                         if(ELEM(base->object->type, OB_CURVE, OB_SURF)) {
1531                                                 cu= ob->data;
1532                                                 cu1= base->object->data;
1533                                                 
1534                                                 if(cu->flag & CU_UV_ORCO)
1535                                                         cu1->flag |= CU_UV_ORCO;
1536                                                 else
1537                                                         cu1->flag &= ~CU_UV_ORCO;
1538                                         }               
1539                                 }
1540                                 else if(event==29) { /* protected bits */
1541                                         base->object->protectflag= ob->protectflag;
1542                                 }
1543                                 else if(event==30) { /* index object */
1544                                         base->object->index= ob->index;
1545                                 }
1546                                 else if(event==31) { /* object color */
1547                                         QUATCOPY(base->object->col, ob->col);
1548                                 }
1549                         }
1550                 }
1551         }
1552         
1553         if(do_scene_sort)
1554                 DAG_scene_sort(scene);
1555
1556         DAG_ids_flush_update(0);
1557 }
1558
1559 void copy_attr_menu(Scene *scene, View3D *v3d)
1560 {
1561         Object *ob;
1562         short event;
1563         char str[512];
1564         
1565         if(!(ob=OBACT)) return;
1566         
1567         if (scene->obedit) { // XXX get from context
1568 //              if (ob->type == OB_MESH)
1569 // XXX                  mesh_copy_menu();
1570                 return;
1571         }
1572         
1573         /* Object Mode */
1574         
1575         /* If you change this menu, don't forget to update the menu in header_view3d.c
1576          * view3d_edit_object_copyattrmenu() and in toolbox.c
1577          */
1578         
1579         strcpy(str, "Copy Attributes %t|Location%x1|Rotation%x2|Size%x3|Draw Options%x4|Time Offset%x5|Dupli%x6|Object Color%x31|%l|Mass%x7|Damping%x8|All Physical Attributes%x11|Properties%x9|Logic Bricks%x10|Protected Transform%x29|%l");
1580         
1581         strcat (str, "|Object Constraints%x22");
1582         strcat (str, "|NLA Strips%x26");
1583         
1584 // XXX  if (OB_SUPPORT_MATERIAL(ob)) {
1585 //              strcat(str, "|Texture Space%x17");
1586 //      }       
1587         
1588         if(ob->type == OB_FONT) strcat(str, "|Font Settings%x18|Bevel Settings%x19");
1589         if(ob->type == OB_CURVE) strcat(str, "|Bevel Settings%x19|UV Orco%x28");
1590         
1591         if((ob->type == OB_FONT) || (ob->type == OB_CURVE)) {
1592                         strcat(str, "|Curve Resolution%x25");
1593         }
1594
1595         if(ob->type==OB_MESH){
1596                 strcat(str, "|Subsurf Settings%x21|AutoSmooth%x27");
1597         }
1598
1599         if(ob->soft) strcat(str, "|Soft Body Settings%x23");
1600         
1601         strcat(str, "|Pass Index%x30");
1602         
1603         if(ob->type==OB_MESH || ob->type==OB_CURVE || ob->type==OB_LATTICE || ob->type==OB_SURF){
1604                 strcat(str, "|Modifiers ...%x24");
1605         }
1606
1607         event= pupmenu(str);
1608         if(event<= 0) return;
1609         
1610         copy_attr(scene, v3d, event);
1611 }
1612
1613 /* ********************************************** */
1614 /* Motion Paths */
1615
1616 /* For the object with pose/action: update paths for those that have got them
1617  * This should selectively update paths that exist...
1618  *
1619  * To be called from various tools that do incremental updates 
1620  */
1621 void ED_objects_recalculate_paths(bContext *C, Scene *scene)
1622 {
1623         ListBase targets = {NULL, NULL};
1624         
1625         /* loop over objects in scene */
1626         CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) 
1627         {
1628                 /* set flag to force recalc, then grab the relevant bones to target */
1629                 ob->avs.recalc |= ANIMVIZ_RECALC_PATHS;
1630                 animviz_get_object_motionpaths(ob, &targets);
1631         }
1632         CTX_DATA_END;
1633         
1634         /* recalculate paths, then free */
1635         animviz_calc_motionpaths(scene, &targets);
1636         BLI_freelistN(&targets);
1637 }
1638
1639 /* For the object with pose/action: create path curves for selected bones 
1640  * This recalculates the WHOLE path within the pchan->pathsf and pchan->pathef range
1641  */
1642 static int object_calculate_paths_exec (bContext *C, wmOperator *op)
1643 {
1644         Scene *scene= CTX_data_scene(C);
1645         
1646         /* set up path data for bones being calculated */
1647         CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects)  
1648         {
1649                 /* verify makes sure that the selected bone has a bone with the appropriate settings */
1650                 animviz_verify_motionpaths(scene, ob, NULL);
1651         }
1652         CTX_DATA_END;
1653         
1654         /* calculate the bones that now have motionpaths... */
1655         // TODO: only make for the selected bones?
1656         ED_objects_recalculate_paths(C, scene);
1657         
1658         /* notifiers for updates */
1659         WM_event_add_notifier(C, NC_OBJECT|ND_POSE, NULL);
1660         
1661         return OPERATOR_FINISHED; 
1662 }
1663
1664 void OBJECT_OT_paths_calculate (wmOperatorType *ot)
1665 {
1666         /* identifiers */
1667         ot->name= "Calculate Object Paths";
1668         ot->idname= "OBJECT_OT_paths_calculate";
1669         ot->description= "Calculate paths for the selected bones.";
1670         
1671         /* api callbacks */
1672         ot->exec= object_calculate_paths_exec;
1673         ot->poll= ED_operator_object_active_editable;
1674         
1675         /* flags */
1676         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1677 }
1678
1679 /* --------- */
1680
1681 /* for the object with pose/action: clear path curves for selected bones only */
1682 void ED_objects_clear_paths(bContext *C, Scene *scene)
1683 {
1684         /* loop over objects in scene */
1685         CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) 
1686         {
1687                 if (ob->mpath) {
1688                         animviz_free_motionpath(ob->mpath);
1689                         ob->mpath= NULL;
1690                 }
1691         }
1692         CTX_DATA_END;
1693 }
1694
1695 /* operator callback for this */
1696 static int object_clear_paths_exec (bContext *C, wmOperator *op)
1697 {
1698         Scene *scene= CTX_data_scene(C);
1699         
1700         /* use the backend function for this */
1701         ED_objects_clear_paths(C, scene);
1702         
1703         /* notifiers for updates */
1704         WM_event_add_notifier(C, NC_OBJECT|ND_POSE, NULL);
1705         
1706         return OPERATOR_FINISHED; 
1707 }
1708
1709 void OBJECT_OT_paths_clear (wmOperatorType *ot)
1710 {
1711         /* identifiers */
1712         ot->name= "Clear Object Paths";
1713         ot->idname= "OBJECT_OT_paths_clear";
1714         ot->description= "Clear path caches for selected bones.";
1715         
1716         /* api callbacks */
1717         ot->exec= object_clear_paths_exec;
1718         ot->poll= ED_operator_object_active_editable;
1719         
1720         /* flags */
1721         ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
1722 }
1723
1724
1725 /********************** Smooth/Flat *********************/
1726
1727 static int shade_smooth_exec(bContext *C, wmOperator *op)
1728 {
1729         Curve *cu;
1730         Nurb *nu;
1731         int clear= (strcmp(op->idname, "OBJECT_OT_shade_flat") == 0);
1732         int done= 0;
1733
1734         CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) {
1735
1736                 if(ob->type==OB_MESH) {
1737                         mesh_set_smooth_flag(ob, !clear);
1738
1739                         DAG_id_flush_update(&ob->id, OB_RECALC_DATA);
1740                         WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, ob);
1741
1742                         done= 1;
1743                 }
1744                 else if ELEM(ob->type, OB_SURF, OB_CURVE) {
1745                         cu= ob->data;
1746
1747                         for(nu=cu->nurb.first; nu; nu=nu->next) {
1748                                 if(!clear) nu->flag |= ME_SMOOTH;
1749                                 else nu->flag &= ~ME_SMOOTH;
1750                         }
1751
1752                         DAG_id_flush_update(&ob->id, OB_RECALC_DATA);
1753                         WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, ob);
1754
1755                         done= 1;
1756                 }
1757         }
1758         CTX_DATA_END;
1759
1760         return (done)? OPERATOR_FINISHED: OPERATOR_CANCELLED;
1761 }
1762
1763 void OBJECT_OT_shade_flat(wmOperatorType *ot)
1764 {
1765         /* identifiers */
1766         ot->name= "Shade Flat";
1767         ot->idname= "OBJECT_OT_shade_flat";
1768         
1769         /* api callbacks */
1770         ot->poll= ED_operator_object_active_editable;
1771         ot->exec= shade_smooth_exec;
1772
1773         /* flags */
1774         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1775 }
1776
1777 void OBJECT_OT_shade_smooth(wmOperatorType *ot)
1778 {
1779         /* identifiers */
1780         ot->name= "Shade Smooth";
1781         ot->idname= "OBJECT_OT_shade_smooth";
1782         
1783         /* api callbacks */
1784         ot->poll= ED_operator_object_active_editable;
1785         ot->exec= shade_smooth_exec;
1786         
1787         /* flags */
1788         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1789 }
1790
1791 /* ********************** */
1792
1793 void image_aspect(Scene *scene, View3D *v3d)
1794 {
1795         /* all selected objects with an image map: scale in image aspect */
1796         Base *base;
1797         Object *ob;
1798         Material *ma;
1799         Tex *tex;
1800         float x, y, space;
1801         int a, b, done;
1802         
1803         if(scene->obedit) return; // XXX get from context
1804         if(scene->id.lib) return;
1805         
1806         for(base= FIRSTBASE; base; base= base->next) {
1807                 if(TESTBASELIB(v3d, base)) {
1808                         ob= base->object;
1809                         done= 0;
1810                         
1811                         for(a=1; a<=ob->totcol; a++) {
1812                                 ma= give_current_material(ob, a);
1813                                 if(ma) {
1814                                         for(b=0; b<MAX_MTEX; b++) {
1815                                                 if(ma->mtex[b] && ma->mtex[b]->tex) {
1816                                                         tex= ma->mtex[b]->tex;
1817                                                         if(tex->type==TEX_IMAGE && tex->ima) {
1818                                                                 ImBuf *ibuf= BKE_image_get_ibuf(tex->ima, NULL);
1819                                                                 
1820                                                                 /* texturespace */
1821                                                                 space= 1.0;
1822                                                                 if(ob->type==OB_MESH) {
1823                                                                         float size[3];
1824                                                                         mesh_get_texspace(ob->data, NULL, NULL, size);
1825                                                                         space= size[0]/size[1];
1826                                                                 }
1827                                                                 else if(ELEM3(ob->type, OB_CURVE, OB_FONT, OB_SURF)) {
1828                                                                         Curve *cu= ob->data;
1829                                                                         space= cu->size[0]/cu->size[1];
1830                                                                 }
1831                                                         
1832                                                                 x= ibuf->x/space;
1833                                                                 y= ibuf->y;
1834                                                                 
1835                                                                 if(x>y) ob->size[0]= ob->size[1]*x/y;
1836                                                                 else ob->size[1]= ob->size[0]*y/x;
1837                                                                 
1838                                                                 done= 1;
1839                                                                 DAG_id_flush_update(&ob->id, OB_RECALC_OB);                                                             
1840                                                         }
1841                                                 }
1842                                                 if(done) break;
1843                                         }
1844                                 }
1845                                 if(done) break;
1846                         }
1847                 }
1848         }
1849         
1850 }
1851
1852 int vergbaseco(const void *a1, const void *a2)
1853 {
1854         Base **x1, **x2;
1855         
1856         x1= (Base **) a1;
1857         x2= (Base **) a2;
1858         
1859         if( (*x1)->sy > (*x2)->sy ) return 1;
1860         else if( (*x1)->sy < (*x2)->sy) return -1;
1861         else if( (*x1)->sx > (*x2)->sx ) return 1;
1862         else if( (*x1)->sx < (*x2)->sx ) return -1;
1863
1864         return 0;
1865 }
1866
1867
1868 void auto_timeoffs(Scene *scene, View3D *v3d)
1869 {
1870         Base *base, **basesort, **bs;
1871         float start, delta;
1872         int tot=0, a;
1873         short offset=25;
1874
1875         if(BASACT==0 || v3d==NULL) return;
1876 // XXX  if(button(&offset, 0, 1000,"Total time")==0) return;
1877
1878         /* make array of all bases, xco yco (screen) */
1879         for(base= FIRSTBASE; base; base= base->next) {
1880                 if(TESTBASELIB(v3d, base)) {
1881                         tot++;
1882                 }
1883         }
1884
1885         delta= (float)offset/(float)tot;
1886         start= OBACT->sf;
1887
1888         bs= basesort= MEM_mallocN(sizeof(void *)*tot,"autotimeoffs");
1889         for(base= FIRSTBASE; base; base= base->next) {
1890                 if(TESTBASELIB(v3d, base)) {
1891                         *bs= base;
1892                         bs++;
1893                 }
1894         }
1895         qsort(basesort, tot, sizeof(void *), vergbaseco);
1896
1897         bs= basesort;
1898         for(a=0; a<tot; a++) {
1899                 
1900                 (*bs)->object->sf= start;
1901                 start+= delta;
1902
1903                 bs++;
1904         }
1905         MEM_freeN(basesort);
1906
1907 }
1908
1909 void ofs_timeoffs(Scene *scene, View3D *v3d)
1910 {
1911         float offset=0.0f;
1912
1913         if(BASACT==0 || v3d==NULL) return;
1914         
1915 // XXX  if(fbutton(&offset, -10000.0f, 10000.0f, 10, 10, "Offset")==0) return;
1916
1917         /* make array of all bases, xco yco (screen) */
1918         CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) {
1919                 ob->sf += offset;
1920                 if (ob->sf < -MAXFRAMEF)                ob->sf = -MAXFRAMEF;
1921                 else if (ob->sf > MAXFRAMEF)    ob->sf = MAXFRAMEF;
1922         }
1923         CTX_DATA_END;
1924
1925 }
1926
1927
1928 void rand_timeoffs(Scene *scene, View3D *v3d)
1929 {
1930         Base *base;
1931         float rand=0.0f;
1932
1933         if(BASACT==0 || v3d==NULL) return;
1934         
1935 // XXX  if(fbutton(&rand, 0.0f, 10000.0f, 10, 10, "Randomize")==0) return;
1936         
1937         rand *= 2;
1938         
1939         for(base= FIRSTBASE; base; base= base->next) {
1940                 if(TESTBASELIB(v3d, base)) {
1941                         base->object->sf += (BLI_drand()-0.5) * rand;
1942                         if (base->object->sf < -MAXFRAMEF)              base->object->sf = -MAXFRAMEF;
1943                         else if (base->object->sf > MAXFRAMEF)  base->object->sf = MAXFRAMEF;
1944                 }
1945         }
1946
1947 }
1948
1949 static EnumPropertyItem *object_mode_set_itemsf(bContext *C, PointerRNA *ptr, int *free)
1950 {       
1951         EnumPropertyItem *input = object_mode_items;
1952         EnumPropertyItem *item= NULL;
1953         Object *ob;
1954         int totitem= 0;
1955         
1956         if(!C) /* needed for docs */
1957                 return object_mode_items;
1958
1959         ob = CTX_data_active_object(C);
1960         while(ob && input->identifier) {
1961                 if((input->value == OB_MODE_EDIT && ((ob->type == OB_MESH) || (ob->type == OB_ARMATURE) ||
1962                                                     (ob->type == OB_CURVE) || (ob->type == OB_SURF) ||
1963                                                      (ob->type == OB_FONT) || (ob->type == OB_MBALL) || (ob->type == OB_LATTICE))) ||
1964                    (input->value == OB_MODE_POSE && (ob->type == OB_ARMATURE)) ||
1965                    (input->value == OB_MODE_PARTICLE_EDIT && ob->particlesystem.first) ||
1966                    ((input->value == OB_MODE_SCULPT || input->value == OB_MODE_VERTEX_PAINT ||
1967                      input->value == OB_MODE_WEIGHT_PAINT || input->value == OB_MODE_TEXTURE_PAINT) && (ob->type == OB_MESH)) ||
1968                    (input->value == OB_MODE_OBJECT))
1969                         RNA_enum_item_add(&item, &totitem, input);
1970                 ++input;
1971         }
1972
1973         RNA_enum_item_end(&item, &totitem);
1974
1975         *free= 1;
1976
1977         return item;
1978 }
1979
1980 static const char *object_mode_op_string(int mode)
1981 {
1982         if(mode & OB_MODE_EDIT)
1983                 return "OBJECT_OT_editmode_toggle";
1984         if(mode == OB_MODE_SCULPT)
1985                 return "SCULPT_OT_sculptmode_toggle";
1986         if(mode == OB_MODE_VERTEX_PAINT)
1987                 return "PAINT_OT_vertex_paint_toggle";
1988         if(mode == OB_MODE_WEIGHT_PAINT)
1989                 return "PAINT_OT_weight_paint_toggle";
1990         if(mode == OB_MODE_TEXTURE_PAINT)
1991                 return "PAINT_OT_texture_paint_toggle";
1992         if(mode == OB_MODE_PARTICLE_EDIT)
1993                 return "PARTICLE_OT_particle_edit_toggle";
1994         if(mode == OB_MODE_POSE)
1995                 return "OBJECT_OT_posemode_toggle";
1996         return NULL;
1997 }
1998
1999 /* checks the mode to be set is compatible with the object
2000  * should be made into a generic function */
2001 static int object_mode_set_compat(bContext *C, wmOperator *op, Object *ob)
2002 {
2003         ObjectMode mode = RNA_enum_get(op->ptr, "mode");
2004
2005         if(ob) {
2006                 if(mode == OB_MODE_OBJECT)
2007                         return 1;
2008
2009                 switch(ob->type) {
2010                 case OB_MESH:
2011                         if(mode & (OB_MODE_EDIT|OB_MODE_SCULPT|OB_MODE_VERTEX_PAINT|OB_MODE_WEIGHT_PAINT|OB_MODE_TEXTURE_PAINT|OB_MODE_PARTICLE_EDIT))
2012                                 return 1;
2013                         return 0;
2014                 case OB_CURVE:
2015                 case OB_SURF:
2016                 case OB_FONT:
2017                 case OB_MBALL:
2018                         if(mode & (OB_MODE_EDIT))
2019                                 return 1;
2020                         return 0;
2021                 case OB_LATTICE:
2022                         if(mode & (OB_MODE_EDIT|OB_MODE_WEIGHT_PAINT))
2023                                 return 1;
2024                 case OB_ARMATURE:
2025                         if(mode & (OB_MODE_EDIT|OB_MODE_POSE))
2026                                 return 1;
2027                 }
2028         }
2029
2030         return 0;
2031 }
2032
2033 static int object_mode_set_exec(bContext *C, wmOperator *op)
2034 {
2035         Object *ob= CTX_data_active_object(C);
2036         ObjectMode mode = RNA_enum_get(op->ptr, "mode");
2037         ObjectMode restore_mode = ob->mode;
2038         int toggle = RNA_boolean_get(op->ptr, "toggle");
2039
2040         if(!ob || !object_mode_set_compat(C, op, ob))
2041                 return OPERATOR_PASS_THROUGH;
2042
2043         /* Exit current mode if it's not the mode we're setting */
2044         if(ob->mode != OB_MODE_OBJECT && ob->mode != mode)
2045                 WM_operator_name_call(C, object_mode_op_string(ob->mode), WM_OP_EXEC_REGION_WIN, NULL);
2046
2047         if(mode != OB_MODE_OBJECT) {
2048                 /* Enter new mode */
2049                 if(ob->mode != mode || toggle)
2050                         WM_operator_name_call(C, object_mode_op_string(mode), WM_OP_EXEC_REGION_WIN, NULL);
2051
2052                 if(toggle) {
2053                         if(ob->mode == mode)
2054                                 /* For toggling, store old mode so we know what to go back to */
2055                                 ob->restore_mode = restore_mode;
2056                         else if(ob->restore_mode != OB_MODE_OBJECT && ob->restore_mode != mode) {
2057                                 WM_operator_name_call(C, object_mode_op_string(ob->restore_mode), WM_OP_EXEC_REGION_WIN, NULL);
2058                         }
2059                 }
2060         }
2061
2062         return OPERATOR_FINISHED;
2063 }
2064
2065 void OBJECT_OT_mode_set(wmOperatorType *ot)
2066 {
2067         PropertyRNA *prop;
2068
2069         /* identifiers */
2070         ot->name= "Set Object Mode";
2071         ot->description = "Sets the object interaction mode.";
2072         ot->idname= "OBJECT_OT_mode_set";
2073         
2074         /* api callbacks */
2075         ot->exec= object_mode_set_exec;
2076         
2077         ot->poll= ED_operator_object_active_editable;
2078         
2079         /* flags */
2080         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2081         
2082         prop= RNA_def_enum(ot->srna, "mode", object_mode_items, OB_MODE_OBJECT, "Mode", "");
2083         RNA_def_enum_funcs(prop, object_mode_set_itemsf);
2084
2085         RNA_def_boolean(ot->srna, "toggle", 0, "Toggle", "");
2086 }
2087
2088
2089
2090 void ED_object_toggle_modes(bContext *C, int mode)
2091 {
2092         if(mode & OB_MODE_SCULPT)
2093                 WM_operator_name_call(C, "SCULPT_OT_sculptmode_toggle", WM_OP_EXEC_REGION_WIN, NULL);
2094         if(mode & OB_MODE_VERTEX_PAINT)
2095                 WM_operator_name_call(C, "PAINT_OT_vertex_paint_toggle", WM_OP_EXEC_REGION_WIN, NULL);
2096         if(mode & OB_MODE_WEIGHT_PAINT)
2097                 WM_operator_name_call(C, "PAINT_OT_weight_paint_toggle", WM_OP_EXEC_REGION_WIN, NULL);
2098         if(mode & OB_MODE_TEXTURE_PAINT)
2099                 WM_operator_name_call(C, "PAINT_OT_texture_paint_toggle", WM_OP_EXEC_REGION_WIN, NULL);
2100         if(mode & OB_MODE_PARTICLE_EDIT)
2101                 WM_operator_name_call(C, "PARTICLE_OT_particle_edit_toggle", WM_OP_EXEC_REGION_WIN, NULL);
2102         if(mode & OB_MODE_POSE)
2103                 WM_operator_name_call(C, "OBJECT_OT_posemode_toggle", WM_OP_EXEC_REGION_WIN, NULL);
2104 }
2105
2106 /************************ Game Properties ***********************/
2107
2108 static int game_property_new(bContext *C, wmOperator *op)
2109 {
2110         Object *ob= CTX_data_active_object(C);
2111         bProperty *prop;
2112
2113         if(!ob)
2114                 return OPERATOR_CANCELLED;
2115
2116         prop= new_property(PROP_FLOAT);
2117         BLI_addtail(&ob->prop, prop);
2118         unique_property(NULL, prop, 0); // make_unique_prop_names(prop->name);
2119
2120         return OPERATOR_FINISHED;
2121 }
2122
2123
2124 void OBJECT_OT_game_property_new(wmOperatorType *ot)
2125 {
2126         /* identifiers */
2127         ot->name= "New Game Property";
2128         ot->idname= "OBJECT_OT_game_property_new";
2129
2130         /* api callbacks */
2131         ot->exec= game_property_new;
2132         ot->poll= ED_operator_object_active_editable;
2133
2134         /* flags */
2135         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2136 }
2137
2138 static int game_property_remove(bContext *C, wmOperator *op)
2139 {
2140         Object *ob= CTX_data_active_object(C);
2141         bProperty *prop;
2142         int index;
2143
2144         if(!ob)
2145                 return OPERATOR_CANCELLED;
2146
2147         index = RNA_int_get(op->ptr, "index");
2148
2149     prop= BLI_findlink(&ob->prop, index);
2150
2151     if(prop) {
2152                 BLI_remlink(&ob->prop, prop);
2153                 free_property(prop);
2154                 return OPERATOR_FINISHED;
2155     }
2156     else {
2157         return OPERATOR_CANCELLED;
2158     }
2159 }
2160
2161 void OBJECT_OT_game_property_remove(wmOperatorType *ot)
2162 {
2163         /* identifiers */
2164         ot->name= "Remove Game Property";
2165         ot->idname= "OBJECT_OT_game_property_remove";
2166
2167         /* api callbacks */
2168         ot->exec= game_property_remove;
2169         ot->poll= ED_operator_object_active_editable;
2170
2171         /* flags */
2172         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2173
2174         RNA_def_int(ot->srna, "index", 0, 0, INT_MAX, "Index", "Property index to remove ", 0, INT_MAX);
2175 }