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