Edit Mode: add success return value
[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_ghash.h"
44 #include "BLI_string_utils.h"
45
46 #include "BLT_translation.h"
47
48 #include "DNA_armature_types.h"
49 #include "DNA_curve_types.h"
50 #include "DNA_gpencil_types.h"
51 #include "DNA_group_types.h"
52 #include "DNA_material_types.h"
53 #include "DNA_meta_types.h"
54 #include "DNA_property_types.h"
55 #include "DNA_scene_types.h"
56 #include "DNA_object_types.h"
57 #include "DNA_object_force_types.h"
58 #include "DNA_meshdata_types.h"
59 #include "DNA_vfont_types.h"
60 #include "DNA_mesh_types.h"
61 #include "DNA_lattice_types.h"
62
63 #include "IMB_imbuf_types.h"
64
65 #include "BKE_anim.h"
66 #include "BKE_constraint.h"
67 #include "BKE_context.h"
68 #include "BKE_curve.h"
69 #include "BKE_effect.h"
70 #include "BKE_depsgraph.h"
71 #include "BKE_global.h"
72 #include "BKE_image.h"
73 #include "BKE_lattice.h"
74 #include "BKE_library.h"
75 #include "BKE_main.h"
76 #include "BKE_material.h"
77 #include "BKE_mball.h"
78 #include "BKE_mesh.h"
79 #include "BKE_object.h"
80 #include "BKE_pointcache.h"
81 #include "BKE_property.h"
82 #include "BKE_sca.h"
83 #include "BKE_softbody.h"
84 #include "BKE_modifier.h"
85 #include "BKE_editlattice.h"
86 #include "BKE_editmesh.h"
87 #include "BKE_report.h"
88 #include "BKE_undo_system.h"
89
90 #include "ED_armature.h"
91 #include "ED_curve.h"
92 #include "ED_mesh.h"
93 #include "ED_mball.h"
94 #include "ED_lattice.h"
95 #include "ED_object.h"
96 #include "ED_screen.h"
97 #include "ED_undo.h"
98 #include "ED_image.h"
99
100 #include "RNA_access.h"
101 #include "RNA_define.h"
102 #include "RNA_enum_types.h"
103
104 /* for menu/popup icons etc etc*/
105
106 #include "UI_interface.h"
107 #include "WM_api.h"
108 #include "WM_types.h"
109
110 #include "object_intern.h"  // own include
111
112 /* ************* XXX **************** */
113 static void error(const char *UNUSED(arg)) {}
114 static void waitcursor(int UNUSED(val)) {}
115 static int pupmenu(const char *UNUSED(msg)) { return 0; }
116
117 /* port over here */
118 static void error_libdata(void) {}
119
120 Object *ED_object_context(bContext *C)
121 {
122         return CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
123 }
124
125 /* find the correct active object per context
126  * note: context can be NULL when called from a enum with PROP_ENUM_NO_CONTEXT */
127 Object *ED_object_active_context(bContext *C)
128 {
129         Object *ob = NULL;
130         if (C) {
131                 ob = ED_object_context(C);
132                 if (!ob) ob = CTX_data_active_object(C);
133         }
134         return ob;
135 }
136
137
138 /* ********* clear/set restrict view *********/
139 static int object_hide_view_clear_exec(bContext *C, wmOperator *op)
140 {
141         Main *bmain = CTX_data_main(C);
142         ScrArea *sa = CTX_wm_area(C);
143         View3D *v3d = sa->spacedata.first;
144         Scene *scene = CTX_data_scene(C);
145         Base *base;
146         bool changed = false;
147         const bool select = RNA_boolean_get(op->ptr, "select");
148         
149         /* XXX need a context loop to handle such cases */
150         for (base = FIRSTBASE; base; base = base->next) {
151                 if ((base->lay & v3d->lay) && base->object->restrictflag & OB_RESTRICT_VIEW) {
152                         if (!(base->object->restrictflag & OB_RESTRICT_SELECT)) {
153                                 SET_FLAG_FROM_TEST(base->flag, select, SELECT);
154                         }
155                         base->object->flag = base->flag;
156                         base->object->restrictflag &= ~OB_RESTRICT_VIEW; 
157                         changed = true;
158                 }
159         }
160         if (changed) {
161                 DAG_id_type_tag(bmain, ID_OB);
162                 DAG_relations_tag_update(bmain);
163                 WM_event_add_notifier(C, NC_SCENE | ND_OB_SELECT, scene);
164         }
165
166         return OPERATOR_FINISHED;
167 }
168
169 void OBJECT_OT_hide_view_clear(wmOperatorType *ot)
170 {
171         
172         /* identifiers */
173         ot->name = "Clear Restrict View";
174         ot->description = "Reveal the object by setting the hide flag";
175         ot->idname = "OBJECT_OT_hide_view_clear";
176         
177         /* api callbacks */
178         ot->exec = object_hide_view_clear_exec;
179         ot->poll = ED_operator_view3d_active;
180         
181         /* flags */
182         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
183
184         RNA_def_boolean(ot->srna, "select", true, "Select", "");
185 }
186
187 static int object_hide_view_set_exec(bContext *C, wmOperator *op)
188 {
189         Main *bmain = CTX_data_main(C);
190         Scene *scene = CTX_data_scene(C);
191         bool changed = false;
192         const bool unselected = RNA_boolean_get(op->ptr, "unselected");
193         
194         CTX_DATA_BEGIN(C, Base *, base, visible_bases)
195         {
196                 if (!unselected) {
197                         if (base->flag & SELECT) {
198                                 base->flag &= ~SELECT;
199                                 base->object->flag = base->flag;
200                                 base->object->restrictflag |= OB_RESTRICT_VIEW;
201                                 changed = true;
202                                 if (base == BASACT) {
203                                         ED_base_object_activate(C, NULL);
204                                 }
205                         }
206                 }
207                 else {
208                         if (!(base->flag & SELECT)) {
209                                 base->object->restrictflag |= OB_RESTRICT_VIEW;
210                                 changed = true;
211                                 if (base == BASACT) {
212                                         ED_base_object_activate(C, NULL);
213                                 }
214                         }
215                 }
216         }
217         CTX_DATA_END;
218
219         if (changed) {
220                 DAG_id_type_tag(bmain, ID_OB);
221                 DAG_relations_tag_update(bmain);
222                 
223                 WM_event_add_notifier(C, NC_SCENE | ND_OB_SELECT, scene);
224                 
225         }
226
227         return OPERATOR_FINISHED;
228 }
229
230 void OBJECT_OT_hide_view_set(wmOperatorType *ot)
231 {
232         /* identifiers */
233         ot->name = "Set Restrict View";
234         ot->description = "Hide the object by setting the hide flag";
235         ot->idname = "OBJECT_OT_hide_view_set";
236         
237         /* api callbacks */
238         ot->exec = object_hide_view_set_exec;
239         ot->poll = ED_operator_view3d_active;
240         
241         /* flags */
242         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
243         
244         RNA_def_boolean(ot->srna, "unselected", 0, "Unselected", "Hide unselected rather than selected objects");
245         
246 }
247
248 /* 99% same as above except no need for scene refreshing (TODO, update render preview) */
249 static int object_hide_render_clear_exec(bContext *C, wmOperator *UNUSED(op))
250 {
251         bool changed = false;
252
253         /* XXX need a context loop to handle such cases */
254         CTX_DATA_BEGIN(C, Object *, ob, selected_editable_objects)
255         {
256                 if (ob->restrictflag & OB_RESTRICT_RENDER) {
257                         ob->restrictflag &= ~OB_RESTRICT_RENDER;
258                         changed = true;
259                 }
260         }
261         CTX_DATA_END;
262
263         if (changed)
264                 WM_event_add_notifier(C, NC_SPACE | ND_SPACE_OUTLINER, NULL);
265
266         return OPERATOR_FINISHED;
267 }
268
269 void OBJECT_OT_hide_render_clear(wmOperatorType *ot)
270 {
271
272         /* identifiers */
273         ot->name = "Clear Restrict Render";
274         ot->description = "Reveal the render object by setting the hide render flag";
275         ot->idname = "OBJECT_OT_hide_render_clear";
276
277         /* api callbacks */
278         ot->exec = object_hide_render_clear_exec;
279         ot->poll = ED_operator_view3d_active;
280
281         /* flags */
282         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
283 }
284
285 static int object_hide_render_set_exec(bContext *C, wmOperator *op)
286 {
287         const bool unselected = RNA_boolean_get(op->ptr, "unselected");
288
289         CTX_DATA_BEGIN(C, Base *, base, visible_bases)
290         {
291                 if (!unselected) {
292                         if (base->flag & SELECT) {
293                                 base->object->restrictflag |= OB_RESTRICT_RENDER;
294                         }
295                 }
296                 else {
297                         if (!(base->flag & SELECT)) {
298                                 base->object->restrictflag |= OB_RESTRICT_RENDER;
299                         }
300                 }
301         }
302         CTX_DATA_END;
303         WM_event_add_notifier(C, NC_SPACE | ND_SPACE_OUTLINER, NULL);
304         return OPERATOR_FINISHED;
305 }
306
307 void OBJECT_OT_hide_render_set(wmOperatorType *ot)
308 {
309         /* identifiers */
310         ot->name = "Set Restrict Render";
311         ot->description = "Hide the render object by setting the hide render flag";
312         ot->idname = "OBJECT_OT_hide_render_set";
313
314         /* api callbacks */
315         ot->exec = object_hide_render_set_exec;
316         ot->poll = ED_operator_view3d_active;
317
318         /* flags */
319         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
320
321         RNA_def_boolean(ot->srna, "unselected", 0, "Unselected", "Hide unselected rather than selected objects");
322 }
323
324 /* ******************* toggle editmode operator  ***************** */
325
326 static bool mesh_needs_keyindex(const Mesh *me)
327 {
328         if (me->key) {
329                 return false;  /* will be added */
330         }
331
332         for (const Object *ob = G.main->object.first; ob; ob = ob->id.next) {
333                 if ((ob->parent) && (ob->parent->data == me) && ELEM(ob->partype, PARVERT1, PARVERT3)) {
334                         return true;
335                 }
336                 if (ob->data == me) {
337                         for (const ModifierData *md = ob->modifiers.first; md; md = md->next) {
338                                 if (md->type == eModifierType_Hook) {
339                                         return true;
340                                 }
341                         }
342                 }
343         }
344         return false;
345 }
346
347 /**
348  * Load EditMode data back into the object,
349  * optionally freeing the editmode data.
350  */
351 static bool ED_object_editmode_load_ex(Main *bmain, Object *obedit, const bool freedata)
352 {
353         if (obedit == NULL) {
354                 return false;
355         }
356
357         if (obedit->type == OB_MESH) {
358                 Mesh *me = obedit->data;
359                 if (me->edit_btmesh == NULL) {
360                         return false;
361                 }
362
363                 if (me->edit_btmesh->bm->totvert > MESH_MAX_VERTS) {
364                         error("Too many vertices");
365                         return false;
366                 }
367
368                 EDBM_mesh_load(obedit);
369
370                 if (freedata) {
371                         EDBM_mesh_free(me->edit_btmesh);
372                         MEM_freeN(me->edit_btmesh);
373                         me->edit_btmesh = NULL;
374                 }
375                 /* will be recalculated as needed. */
376                 {
377                         ED_mesh_mirror_spatial_table(NULL, NULL, NULL, NULL, 'e');
378                         ED_mesh_mirror_topo_table(NULL, NULL, 'e');
379                 }
380         }
381         else if (obedit->type == OB_ARMATURE) {
382                 const bArmature *arm = obedit->data;
383                 if (arm->edbo == NULL) {
384                         return false;
385                 }
386                 ED_armature_from_edit(obedit->data);
387                 if (freedata) {
388                         ED_armature_edit_free(obedit->data);
389                 }
390                 /* TODO(sergey): Pose channels might have been changed, so need
391                  * to inform dependency graph about this. But is it really the
392                  * best place to do this?
393                  */
394                 DAG_relations_tag_update(bmain);
395         }
396         else if (ELEM(obedit->type, OB_CURVE, OB_SURF)) {
397                 const Curve *cu = obedit->data;
398                 if (cu->editnurb == NULL) {
399                         return false;
400                 }
401                 ED_curve_editnurb_load(obedit);
402                 if (freedata) {
403                         ED_curve_editnurb_free(obedit);
404                 }
405         }
406         else if (obedit->type == OB_FONT) {
407                 const Curve *cu = obedit->data;
408                 if (cu->editfont == NULL) {
409                         return false;
410                 }
411                 ED_curve_editfont_load(obedit);
412                 if (freedata) {
413                         ED_curve_editfont_free(obedit);
414                 }
415         }
416         else if (obedit->type == OB_LATTICE) {
417                 const Lattice *lt = obedit->data;
418                 if (lt->editlatt == NULL) {
419                         return false;
420                 }
421                 BKE_editlattice_load(obedit);
422                 if (freedata) {
423                         BKE_editlattice_free(obedit);
424                 }
425         }
426         else if (obedit->type == OB_MBALL) {
427                 const MetaBall *mb = obedit->data;
428                 if (mb->editelems == NULL) {
429                         return false;
430                 }
431                 ED_mball_editmball_load(obedit);
432                 if (freedata) {
433                         ED_mball_editmball_free(obedit);
434                 }
435         }
436
437         return true;
438 }
439
440 bool ED_object_editmode_load(Object *obedit)
441 {
442         /* TODO(sergey): use proper main here? */
443         return ED_object_editmode_load_ex(G.main, obedit, false);
444 }
445
446 /**
447  * \param flag:
448  * - If #EM_FREEDATA isn't in the flag, use ED_object_editmode_load directly.
449  */
450 bool ED_object_editmode_exit_ex(Scene *scene, Object *obedit, int flag)
451 {
452         const bool freedata = (flag & EM_FREEDATA) != 0;
453
454         if (flag & EM_WAITCURSOR) waitcursor(1);
455
456         if (ED_object_editmode_load_ex(G.main, obedit, freedata) == false) {
457                 /* in rare cases (background mode) its possible active object
458                  * is flagged for editmode, without 'obedit' being set [#35489] */
459                 if (UNLIKELY(scene->basact && (scene->basact->object->mode & OB_MODE_EDIT))) {
460                         scene->basact->object->mode &= ~OB_MODE_EDIT;
461                 }
462                 if (flag & EM_WAITCURSOR) waitcursor(0);
463                 return true;
464         }
465
466         /* freedata only 0 now on file saves and render */
467         if (freedata) {
468                 ListBase pidlist;
469                 PTCacheID *pid;
470
471                 /* for example; displist make is different in editmode */
472                 scene->obedit = NULL; // XXX for context
473
474                 /* flag object caches as outdated */
475                 BKE_ptcache_ids_from_object(&pidlist, obedit, scene, 0);
476                 for (pid = pidlist.first; pid; pid = pid->next) {
477                         if (pid->type != PTCACHE_TYPE_PARTICLES) /* particles don't need reset on geometry change */
478                                 pid->cache->flag |= PTCACHE_OUTDATED;
479                 }
480                 BLI_freelistN(&pidlist);
481
482                 BKE_ptcache_object_reset(scene, obedit, PTCACHE_RESET_OUTDATED);
483
484                 /* also flush ob recalc, doesn't take much overhead, but used for particles */
485                 DAG_id_tag_update(&obedit->id, OB_RECALC_OB | OB_RECALC_DATA);
486
487                 WM_main_add_notifier(NC_SCENE | ND_MODE | NS_MODE_OBJECT, scene);
488
489                 obedit->mode &= ~OB_MODE_EDIT;
490         }
491
492         if (flag & EM_WAITCURSOR) waitcursor(0);
493
494         return (obedit->mode & OB_MODE_EDIT) == 0;
495 }
496
497 bool ED_object_editmode_exit(bContext *C, int flag)
498 {
499         Scene *scene = CTX_data_scene(C);
500         Object *obedit = CTX_data_edit_object(C);
501         return ED_object_editmode_exit_ex(scene, obedit, flag);
502 }
503
504 bool ED_object_editmode_enter(bContext *C, int flag)
505 {
506         Scene *scene = CTX_data_scene(C);
507         Base *base = NULL;
508         Object *ob;
509         ScrArea *sa = CTX_wm_area(C);
510         View3D *v3d = NULL;
511         bool ok = false;
512
513         if (ID_IS_LINKED(scene)) {
514                 return false;
515         }
516
517         if (sa && sa->spacetype == SPACE_VIEW3D)
518                 v3d = sa->spacedata.first;
519
520         if ((flag & EM_IGNORE_LAYER) == 0) {
521                 base = CTX_data_active_base(C); /* active layer checked here for view3d */
522
523                 if ((base == NULL) ||
524                     (v3d && (base->lay & v3d->lay) == 0) ||
525                     (!v3d && (base->lay & scene->lay) == 0))
526                 {
527                         return false;
528                 }
529         }
530         else {
531                 base = scene->basact;
532         }
533
534         if (ELEM(NULL, base, base->object, base->object->data)) {
535                 return false;
536         }
537
538         ob = base->object;
539
540         /* this checks actual object->data, for cases when other scenes have it in editmode context */
541         if (BKE_object_is_in_editmode(ob)) {
542                 return true;
543         }
544
545         if (BKE_object_obdata_is_libdata(ob)) {
546                 error_libdata();
547                 return false;
548         }
549
550         if (flag & EM_WAITCURSOR) waitcursor(1);
551
552         ob->restore_mode = ob->mode;
553
554         /* note, when switching scenes the object can have editmode data but
555          * not be scene->obedit: bug 22954, this avoids calling self eternally */
556         if ((ob->restore_mode & OB_MODE_EDIT) == 0)
557                 ED_object_mode_toggle(C, ob->mode);
558
559         ob->mode = OB_MODE_EDIT;
560
561         if (ob->type == OB_MESH) {
562                 BMEditMesh *em;
563                 ok = 1;
564                 scene->obedit = ob;  /* context sees this */
565
566                 const bool use_key_index = mesh_needs_keyindex(ob->data);
567
568                 EDBM_mesh_make(ob, scene->toolsettings->selectmode, use_key_index);
569
570                 em = BKE_editmesh_from_object(ob);
571                 if (LIKELY(em)) {
572                         /* order doesn't matter */
573                         EDBM_mesh_normals_update(em);
574                         BKE_editmesh_tessface_calc(em);
575                 }
576
577                 WM_event_add_notifier(C, NC_SCENE | ND_MODE | NS_EDITMODE_MESH, scene);
578         }
579         else if (ob->type == OB_ARMATURE) {
580                 ok = 1;
581                 scene->obedit = ob;
582                 ED_armature_to_edit(ob->data);
583                 /* to ensure all goes in restposition and without striding */
584                 DAG_id_tag_update(&ob->id, OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME); /* XXX: should this be OB_RECALC_DATA? */
585
586                 WM_event_add_notifier(C, NC_SCENE | ND_MODE | NS_EDITMODE_ARMATURE, scene);
587         }
588         else if (ob->type == OB_FONT) {
589                 scene->obedit = ob; /* XXX for context */
590                 ok = 1;
591                 ED_curve_editfont_make(ob);
592
593                 WM_event_add_notifier(C, NC_SCENE | ND_MODE | NS_EDITMODE_TEXT, scene);
594         }
595         else if (ob->type == OB_MBALL) {
596                 scene->obedit = ob; /* XXX for context */
597                 ok = 1;
598                 ED_mball_editmball_make(ob);
599
600                 WM_event_add_notifier(C, NC_SCENE | ND_MODE | NS_EDITMODE_MBALL, scene);
601         }
602         else if (ob->type == OB_LATTICE) {
603                 scene->obedit = ob; /* XXX for context */
604                 ok = 1;
605                 BKE_editlattice_make(ob);
606
607                 WM_event_add_notifier(C, NC_SCENE | ND_MODE | NS_EDITMODE_LATTICE, scene);
608         }
609         else if (ob->type == OB_SURF || ob->type == OB_CURVE) {
610                 ok = 1;
611                 scene->obedit = ob; /* XXX for context */
612                 ED_curve_editnurb_make(ob);
613
614                 WM_event_add_notifier(C, NC_SCENE | ND_MODE | NS_EDITMODE_CURVE, scene);
615         }
616
617         if (ok) {
618                 DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
619         }
620         else {
621                 scene->obedit = NULL; /* XXX for context */
622                 ob->mode &= ~OB_MODE_EDIT;
623                 WM_event_add_notifier(C, NC_SCENE | ND_MODE | NS_MODE_OBJECT, scene);
624         }
625
626         if (flag & EM_WAITCURSOR) waitcursor(0);
627
628         return (ob->mode & OB_MODE_EDIT) != 0;
629 }
630
631 static int editmode_toggle_exec(bContext *C, wmOperator *op)
632 {
633         const int mode_flag = OB_MODE_EDIT;
634         const bool is_mode_set = (CTX_data_edit_object(C) != NULL);
635         Scene *scene =  CTX_data_scene(C);
636
637         if (!is_mode_set) {
638                 Object *ob = CTX_data_active_object(C);
639                 if (!ED_object_mode_compat_set(C, ob, mode_flag, op->reports)) {
640                         return OPERATOR_CANCELLED;
641                 }
642         }
643
644         if (!is_mode_set) {
645                 ED_object_editmode_enter(C, EM_WAITCURSOR);
646         }
647         else {
648                 ED_object_editmode_exit(C, EM_FREEDATA | EM_WAITCURSOR);
649         }
650         ED_space_image_uv_sculpt_update(CTX_wm_manager(C), scene);
651
652         return OPERATOR_FINISHED;
653 }
654
655 static int editmode_toggle_poll(bContext *C)
656 {
657         Object *ob = CTX_data_active_object(C);
658
659         /* covers proxies too */
660         if (ELEM(NULL, ob, ob->data) || ID_IS_LINKED(ob->data))
661                 return 0;
662
663         /* if hidden but in edit mode, we still display */
664         if ((ob->restrictflag & OB_RESTRICT_VIEW) && !(ob->mode & OB_MODE_EDIT))
665                 return 0;
666
667         return OB_TYPE_SUPPORT_EDITMODE(ob->type);
668 }
669
670 void OBJECT_OT_editmode_toggle(wmOperatorType *ot)
671 {
672         
673         /* identifiers */
674         ot->name = "Toggle Editmode";
675         ot->description = "Toggle object's editmode";
676         ot->idname = "OBJECT_OT_editmode_toggle";
677         
678         /* api callbacks */
679         ot->exec = editmode_toggle_exec;
680         ot->poll = editmode_toggle_poll;
681         
682         /* flags */
683         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
684 }
685
686 /* *************************** */
687
688 static int posemode_exec(bContext *C, wmOperator *op)
689 {
690         Base *base = CTX_data_active_base(C);
691         Object *ob = base->object;
692         const int mode_flag = OB_MODE_POSE;
693         bool is_mode_set = (ob->mode & mode_flag) != 0;
694         
695         if (!is_mode_set) {
696                 if (!ED_object_mode_compat_set(C, ob, mode_flag, op->reports)) {
697                         return OPERATOR_CANCELLED;
698                 }
699         }
700
701         if (ob->type == OB_ARMATURE) {
702                 if (ob == CTX_data_edit_object(C)) {
703                         ED_object_editmode_exit(C, EM_FREEDATA);
704                         is_mode_set = false;
705                 }
706
707                 if (is_mode_set) {
708                         ED_object_posemode_exit(C, ob);
709                 }
710                 else {
711                         ED_object_posemode_enter(C, ob);
712                 }
713                 
714                 return OPERATOR_FINISHED;
715         }
716         
717         return OPERATOR_PASS_THROUGH;
718 }
719
720 void OBJECT_OT_posemode_toggle(wmOperatorType *ot) 
721 {
722         /* identifiers */
723         ot->name = "Toggle Pose Mode";
724         ot->idname = "OBJECT_OT_posemode_toggle";
725         ot->description = "Enable or disable posing/selecting bones";
726         
727         /* api callbacks */
728         ot->exec = posemode_exec;
729         ot->poll = ED_operator_object_active_editable;
730         
731         /* flag */
732         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
733 }
734
735 static void copymenu_properties(Scene *scene, View3D *v3d, Object *ob)
736 {       
737 //XXX no longer used - to be removed - replaced by game_properties_copy_exec
738         bProperty *prop;
739         Base *base;
740         int nr, tot = 0;
741         char *str;
742         
743         prop = ob->prop.first;
744         while (prop) {
745                 tot++;
746                 prop = prop->next;
747         }
748         
749         str = MEM_callocN(50 + 33 * tot, "copymenu prop");
750         
751         if (tot)
752                 strcpy(str, "Copy Property %t|Replace All|Merge All|%l");
753         else
754                 strcpy(str, "Copy Property %t|Clear All (no properties on active)");
755         
756         tot = 0;
757         prop = ob->prop.first;
758         while (prop) {
759                 tot++;
760                 strcat(str, "|");
761                 strcat(str, prop->name);
762                 prop = prop->next;
763         }
764
765         nr = pupmenu(str);
766         
767         if (nr == 1 || nr == 2) {
768                 for (base = FIRSTBASE; base; base = base->next) {
769                         if ((base != BASACT) && (TESTBASELIB(v3d, base))) {
770                                 if (nr == 1) { /* replace */
771                                         BKE_bproperty_copy_list(&base->object->prop, &ob->prop);
772                                 }
773                                 else {
774                                         for (prop = ob->prop.first; prop; prop = prop->next) {
775                                                 BKE_bproperty_object_set(base->object, prop);
776                                         }
777                                 }
778                         }
779                 }
780         }
781         else if (nr > 0) {
782                 prop = BLI_findlink(&ob->prop, nr - 4); /* account for first 3 menu items & menu index starting at 1*/
783                 
784                 if (prop) {
785                         for (base = FIRSTBASE; base; base = base->next) {
786                                 if ((base != BASACT) && (TESTBASELIB(v3d, base))) {
787                                         BKE_bproperty_object_set(base->object, prop);
788                                 }
789                         }
790                 }
791         }
792         MEM_freeN(str);
793         
794 }
795
796 static void copymenu_logicbricks(Scene *scene, View3D *v3d, Object *ob)
797 {
798 //XXX no longer used - to be removed - replaced by logicbricks_copy_exec
799         Base *base;
800         
801         for (base = FIRSTBASE; base; base = base->next) {
802                 if (base->object != ob) {
803                         if (TESTBASELIB(v3d, base)) {
804                                 
805                                 /* first: free all logic */
806                                 free_sensors(&base->object->sensors);
807                                 unlink_controllers(&base->object->controllers);
808                                 free_controllers(&base->object->controllers);
809                                 unlink_actuators(&base->object->actuators);
810                                 free_actuators(&base->object->actuators);
811                                 
812                                 /* now copy it, this also works without logicbricks! */
813                                 clear_sca_new_poins_ob(ob);
814                                 copy_sensors(&base->object->sensors, &ob->sensors, 0);
815                                 copy_controllers(&base->object->controllers, &ob->controllers, 0);
816                                 copy_actuators(&base->object->actuators, &ob->actuators, 0);
817                                 set_sca_new_poins_ob(base->object);
818                                 
819                                 /* some menu settings */
820                                 base->object->scavisflag = ob->scavisflag;
821                                 base->object->scaflag = ob->scaflag;
822                                 
823                                 /* set the initial state */
824                                 base->object->state = ob->state;
825                                 base->object->init_state = ob->init_state;
826                         }
827                 }
828         }
829 }
830
831 /* both pointers should exist */
832 static void copy_texture_space(Object *to, Object *ob)
833 {
834         float *poin1 = NULL, *poin2 = NULL;
835         short texflag = 0;
836         
837         if (ob->type == OB_MESH) {
838                 texflag = ((Mesh *)ob->data)->texflag;
839                 poin2 = ((Mesh *)ob->data)->loc;
840         }
841         else if (ELEM(ob->type, OB_CURVE, OB_SURF, OB_FONT)) {
842                 texflag = ((Curve *)ob->data)->texflag;
843                 poin2 = ((Curve *)ob->data)->loc;
844         }
845         else if (ob->type == OB_MBALL) {
846                 texflag = ((MetaBall *)ob->data)->texflag;
847                 poin2 = ((MetaBall *)ob->data)->loc;
848         }
849         else
850                 return;
851                 
852         if (to->type == OB_MESH) {
853                 ((Mesh *)to->data)->texflag = texflag;
854                 poin1 = ((Mesh *)to->data)->loc;
855         }
856         else if (ELEM(to->type, OB_CURVE, OB_SURF, OB_FONT)) {
857                 ((Curve *)to->data)->texflag = texflag;
858                 poin1 = ((Curve *)to->data)->loc;
859         }
860         else if (to->type == OB_MBALL) {
861                 ((MetaBall *)to->data)->texflag = texflag;
862                 poin1 = ((MetaBall *)to->data)->loc;
863         }
864         else
865                 return;
866         
867         memcpy(poin1, poin2, 9 * sizeof(float));  /* this was noted in DNA_mesh, curve, mball */
868         
869         if (to->type == OB_MESH) {
870                 /* pass */
871         }
872         else if (to->type == OB_MBALL) {
873                 BKE_mball_texspace_calc(to);
874         }
875         else {
876                 BKE_curve_texspace_calc(to->data);
877         }
878         
879 }
880
881 /* UNUSED, keep in case we want to copy functionality for use elsewhere */
882 static void copy_attr(Main *bmain, Scene *scene, View3D *v3d, short event)
883 {
884         Object *ob;
885         Base *base;
886         Curve *cu, *cu1;
887         Nurb *nu;
888         bool do_depgraph_update = false;
889         
890         if (ID_IS_LINKED(scene)) return;
891
892         if (!(ob = OBACT)) return;
893         
894         if (scene->obedit) { // XXX get from context
895                 /* obedit_copymenu(); */
896                 return;
897         }
898         if (event == 9) {
899                 copymenu_properties(scene, v3d, ob);
900                 return;
901         }
902         else if (event == 10) {
903                 copymenu_logicbricks(scene, v3d, ob);
904                 return;
905         }
906         else if (event == 24) {
907                 /* moved to BKE_object_link_modifiers */
908                 /* copymenu_modifiers(bmain, scene, v3d, ob); */
909                 return;
910         }
911
912         for (base = FIRSTBASE; base; base = base->next) {
913                 if (base != BASACT) {
914                         if (TESTBASELIB(v3d, base)) {
915                                 DAG_id_tag_update(&base->object->id, OB_RECALC_DATA);
916                                 
917                                 if (event == 1) {  /* loc */
918                                         copy_v3_v3(base->object->loc, ob->loc);
919                                         copy_v3_v3(base->object->dloc, ob->dloc);
920                                 }
921                                 else if (event == 2) {  /* rot */
922                                         copy_v3_v3(base->object->rot, ob->rot);
923                                         copy_v3_v3(base->object->drot, ob->drot);
924
925                                         copy_qt_qt(base->object->quat, ob->quat);
926                                         copy_qt_qt(base->object->dquat, ob->dquat);
927                                 }
928                                 else if (event == 3) {  /* size */
929                                         copy_v3_v3(base->object->size, ob->size);
930                                         copy_v3_v3(base->object->dscale, ob->dscale);
931                                 }
932                                 else if (event == 4) {  /* drawtype */
933                                         base->object->dt = ob->dt;
934                                         base->object->dtx = ob->dtx;
935                                         base->object->empty_drawtype = ob->empty_drawtype;
936                                         base->object->empty_drawsize = ob->empty_drawsize;
937                                 }
938                                 else if (event == 5) {  /* time offs */
939                                         base->object->sf = ob->sf;
940                                 }
941                                 else if (event == 6) {  /* dupli */
942                                         base->object->dupon = ob->dupon;
943                                         base->object->dupoff = ob->dupoff;
944                                         base->object->dupsta = ob->dupsta;
945                                         base->object->dupend = ob->dupend;
946                                         
947                                         base->object->transflag &= ~OB_DUPLI;
948                                         base->object->transflag |= (ob->transflag & OB_DUPLI);
949
950                                         base->object->dup_group = ob->dup_group;
951                                         if (ob->dup_group)
952                                                 id_us_plus(&ob->dup_group->id);
953                                 }
954                                 else if (event == 7) {    /* mass */
955                                         base->object->mass = ob->mass;
956                                 }
957                                 else if (event == 8) {    /* damping */
958                                         base->object->damping = ob->damping;
959                                         base->object->rdamping = ob->rdamping;
960                                 }
961                                 else if (event == 11) {   /* all physical attributes */
962                                         base->object->gameflag = ob->gameflag;
963                                         base->object->inertia = ob->inertia;
964                                         base->object->formfactor = ob->formfactor;
965                                         base->object->damping = ob->damping;
966                                         base->object->rdamping = ob->rdamping;
967                                         base->object->min_vel = ob->min_vel;
968                                         base->object->max_vel = ob->max_vel;
969                                         base->object->min_angvel = ob->min_angvel;
970                                         base->object->max_angvel = ob->max_angvel;
971                                         if (ob->gameflag & OB_BOUNDS) {
972                                                 base->object->collision_boundtype = ob->collision_boundtype;
973                                         }
974                                         base->object->margin = ob->margin;
975                                         base->object->bsoft = copy_bulletsoftbody(ob->bsoft, 0);
976
977                                 }
978                                 else if (event == 17) {   /* tex space */
979                                         copy_texture_space(base->object, ob);
980                                 }
981                                 else if (event == 18) {   /* font settings */
982                                         
983                                         if (base->object->type == ob->type) {
984                                                 cu = ob->data;
985                                                 cu1 = base->object->data;
986
987                                                 cu1->spacemode = cu->spacemode;
988                                                 cu1->align_y = cu->align_y;
989                                                 cu1->spacing = cu->spacing;
990                                                 cu1->linedist = cu->linedist;
991                                                 cu1->shear = cu->shear;
992                                                 cu1->fsize = cu->fsize;
993                                                 cu1->xof = cu->xof;
994                                                 cu1->yof = cu->yof;
995                                                 cu1->textoncurve = cu->textoncurve;
996                                                 cu1->wordspace = cu->wordspace;
997                                                 cu1->ulpos = cu->ulpos;
998                                                 cu1->ulheight = cu->ulheight;
999                                                 if (cu1->vfont)
1000                                                         id_us_min(&cu1->vfont->id);
1001                                                 cu1->vfont = cu->vfont;
1002                                                 id_us_plus((ID *)cu1->vfont);
1003                                                 if (cu1->vfontb)
1004                                                         id_us_min(&cu1->vfontb->id);
1005                                                 cu1->vfontb = cu->vfontb;
1006                                                 id_us_plus((ID *)cu1->vfontb);
1007                                                 if (cu1->vfonti)
1008                                                         id_us_min(&cu1->vfonti->id);
1009                                                 cu1->vfonti = cu->vfonti;
1010                                                 id_us_plus((ID *)cu1->vfonti);
1011                                                 if (cu1->vfontbi)
1012                                                         id_us_min(&cu1->vfontbi->id);
1013                                                 cu1->vfontbi = cu->vfontbi;
1014                                                 id_us_plus((ID *)cu1->vfontbi);
1015                                                 
1016                                                 BLI_strncpy(cu1->family, cu->family, sizeof(cu1->family));
1017                                                 
1018                                                 DAG_id_tag_update(&base->object->id, OB_RECALC_DATA);
1019                                         }
1020                                 }
1021                                 else if (event == 19) {   /* bevel settings */
1022                                         
1023                                         if (ELEM(base->object->type, OB_CURVE, OB_FONT)) {
1024                                                 cu = ob->data;
1025                                                 cu1 = base->object->data;
1026                                                 
1027                                                 cu1->bevobj = cu->bevobj;
1028                                                 cu1->taperobj = cu->taperobj;
1029                                                 cu1->width = cu->width;
1030                                                 cu1->bevresol = cu->bevresol;
1031                                                 cu1->ext1 = cu->ext1;
1032                                                 cu1->ext2 = cu->ext2;
1033                                                 
1034                                                 DAG_id_tag_update(&base->object->id, OB_RECALC_DATA);
1035                                         }
1036                                 }
1037                                 else if (event == 25) {   /* curve resolution */
1038
1039                                         if (ELEM(base->object->type, OB_CURVE, OB_FONT)) {
1040                                                 cu = ob->data;
1041                                                 cu1 = base->object->data;
1042                                                 
1043                                                 cu1->resolu = cu->resolu;
1044                                                 cu1->resolu_ren = cu->resolu_ren;
1045                                                 
1046                                                 nu = cu1->nurb.first;
1047                                                 
1048                                                 while (nu) {
1049                                                         nu->resolu = cu1->resolu;
1050                                                         nu = nu->next;
1051                                                 }
1052                                                 
1053                                                 DAG_id_tag_update(&base->object->id, OB_RECALC_DATA);
1054                                         }
1055                                 }
1056                                 else if (event == 21) {
1057                                         if (base->object->type == OB_MESH) {
1058                                                 ModifierData *md = modifiers_findByType(ob, eModifierType_Subsurf);
1059
1060                                                 if (md) {
1061                                                         ModifierData *tmd = modifiers_findByType(base->object, eModifierType_Subsurf);
1062
1063                                                         if (!tmd) {
1064                                                                 tmd = modifier_new(eModifierType_Subsurf);
1065                                                                 BLI_addtail(&base->object->modifiers, tmd);
1066                                                         }
1067
1068                                                         modifier_copyData(md, tmd);
1069                                                         DAG_id_tag_update(&base->object->id, OB_RECALC_DATA);
1070                                                 }
1071                                         }
1072                                 }
1073                                 else if (event == 22) {
1074                                         /* Copy the constraint channels over */
1075                                         BKE_constraints_copy(&base->object->constraints, &ob->constraints, true);
1076                                         
1077                                         do_depgraph_update = true;
1078                                 }
1079                                 else if (event == 23) {
1080                                         base->object->softflag = ob->softflag;
1081                                         if (base->object->soft) sbFree(base->object->soft);
1082                                         
1083                                         base->object->soft = copy_softbody(ob->soft, 0);
1084
1085                                         if (!modifiers_findByType(base->object, eModifierType_Softbody)) {
1086                                                 BLI_addhead(&base->object->modifiers, modifier_new(eModifierType_Softbody));
1087                                         }
1088                                 }
1089                                 else if (event == 26) {
1090 #if 0 // XXX old animation system
1091                                         BKE_nlastrip_copy(s(&base->object->nlastrips, &ob->nlastrips);
1092 #endif // XXX old animation system
1093                                 }
1094                                 else if (event == 27) {   /* autosmooth */
1095                                         if (base->object->type == OB_MESH) {
1096                                                 Mesh *me = ob->data;
1097                                                 Mesh *cme = base->object->data;
1098                                                 cme->smoothresh = me->smoothresh;
1099                                                 if (me->flag & ME_AUTOSMOOTH)
1100                                                         cme->flag |= ME_AUTOSMOOTH;
1101                                                 else
1102                                                         cme->flag &= ~ME_AUTOSMOOTH;
1103                                         }
1104                                 }
1105                                 else if (event == 28) { /* UV orco */
1106                                         if (ELEM(base->object->type, OB_CURVE, OB_SURF)) {
1107                                                 cu = ob->data;
1108                                                 cu1 = base->object->data;
1109                                                 
1110                                                 if (cu->flag & CU_UV_ORCO)
1111                                                         cu1->flag |= CU_UV_ORCO;
1112                                                 else
1113                                                         cu1->flag &= ~CU_UV_ORCO;
1114                                         }
1115                                 }
1116                                 else if (event == 29) { /* protected bits */
1117                                         base->object->protectflag = ob->protectflag;
1118                                 }
1119                                 else if (event == 30) { /* index object */
1120                                         base->object->index = ob->index;
1121                                 }
1122                                 else if (event == 31) { /* object color */
1123                                         copy_v4_v4(base->object->col, ob->col);
1124                                 }
1125                         }
1126                 }
1127         }
1128         
1129         if (do_depgraph_update)
1130                 DAG_relations_tag_update(bmain);
1131 }
1132
1133 static void UNUSED_FUNCTION(copy_attr_menu) (Main *bmain, Scene *scene, View3D *v3d)
1134 {
1135         Object *ob;
1136         short event;
1137         char str[512];
1138         
1139         if (!(ob = OBACT)) return;
1140         
1141         if (scene->obedit) { /* XXX get from context */
1142 /*              if (ob->type == OB_MESH) */
1143 /* XXX                  mesh_copy_menu(); */
1144                 return;
1145         }
1146         
1147         /* Object Mode */
1148         
1149         /* If you change this menu, don't forget to update the menu in header_view3d.c
1150          * view3d_edit_object_copyattrmenu() and in toolbox.c
1151          */
1152         
1153         strcpy(str,
1154                "Copy Attributes %t|Location %x1|Rotation %x2|Size %x3|Draw Options %x4|"
1155                "Time Offset %x5|Dupli %x6|Object Color %x31|%l|Mass %x7|Damping %x8|All Physical Attributes %x11|Properties %x9|"
1156                "Logic Bricks %x10|Protected Transform %x29|%l");
1157         
1158         strcat(str, "|Object Constraints %x22");
1159         strcat(str, "|NLA Strips %x26");
1160         
1161 /* XXX  if (OB_TYPE_SUPPORT_MATERIAL(ob->type)) { */
1162 /*              strcat(str, "|Texture Space %x17"); */
1163 /*      } */
1164         
1165         if (ob->type == OB_FONT) strcat(str, "|Font Settings %x18|Bevel Settings %x19");
1166         if (ob->type == OB_CURVE) strcat(str, "|Bevel Settings %x19|UV Orco %x28");
1167         
1168         if ((ob->type == OB_FONT) || (ob->type == OB_CURVE)) {
1169                 strcat(str, "|Curve Resolution %x25");
1170         }
1171
1172         if (ob->type == OB_MESH) {
1173                 strcat(str, "|Subsurf Settings %x21|AutoSmooth %x27");
1174         }
1175
1176         if (ob->soft) strcat(str, "|Soft Body Settings %x23");
1177         
1178         strcat(str, "|Pass Index %x30");
1179         
1180         if (ob->type == OB_MESH || ob->type == OB_CURVE || ob->type == OB_LATTICE || ob->type == OB_SURF) {
1181                 strcat(str, "|Modifiers ... %x24");
1182         }
1183
1184         event = pupmenu(str);
1185         if (event <= 0) return;
1186         
1187         copy_attr(bmain, scene, v3d, event);
1188 }
1189
1190 /* ******************* force field toggle operator ***************** */
1191
1192 void ED_object_check_force_modifiers(Main *bmain, Scene *scene, Object *object)
1193 {
1194         PartDeflect *pd = object->pd;
1195         ModifierData *md = modifiers_findByType(object, eModifierType_Surface);
1196
1197         /* add/remove modifier as needed */
1198         if (!md) {
1199                 if (pd && (pd->shape == PFIELD_SHAPE_SURFACE) && !ELEM(pd->forcefield, 0, PFIELD_GUIDE, PFIELD_TEXTURE)) {
1200                         if (ELEM(object->type, OB_MESH, OB_SURF, OB_FONT, OB_CURVE)) {
1201                                 ED_object_modifier_add(NULL, bmain, scene, object, NULL, eModifierType_Surface);
1202                         }
1203                 }
1204         }
1205         else {
1206                 if (!pd || (pd->shape != PFIELD_SHAPE_SURFACE) || ELEM(pd->forcefield, 0, PFIELD_GUIDE, PFIELD_TEXTURE)) {
1207                         ED_object_modifier_remove(NULL, bmain, object, md);
1208                 }
1209         }
1210 }
1211
1212 static int forcefield_toggle_exec(bContext *C, wmOperator *UNUSED(op))
1213 {
1214         Object *ob = CTX_data_active_object(C);
1215
1216         if (ob->pd == NULL)
1217                 ob->pd = object_add_collision_fields(PFIELD_FORCE);
1218         else if (ob->pd->forcefield == 0)
1219                 ob->pd->forcefield = PFIELD_FORCE;
1220         else
1221                 ob->pd->forcefield = 0;
1222         
1223         ED_object_check_force_modifiers(CTX_data_main(C), CTX_data_scene(C), ob);
1224         WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, ob);
1225         WM_event_add_notifier(C, NC_OBJECT | ND_MODIFIER, ob);
1226
1227         return OPERATOR_FINISHED;
1228 }
1229
1230 void OBJECT_OT_forcefield_toggle(wmOperatorType *ot)
1231 {
1232         
1233         /* identifiers */
1234         ot->name = "Toggle Force Field";
1235         ot->description = "Toggle object's force field";
1236         ot->idname = "OBJECT_OT_forcefield_toggle";
1237         
1238         /* api callbacks */
1239         ot->exec = forcefield_toggle_exec;
1240         ot->poll = ED_operator_object_active_editable;
1241         
1242         /* flags */
1243         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1244 }
1245
1246 /* ********************************************** */
1247 /* Motion Paths */
1248
1249 /* For the objects with animation: update paths for those that have got them
1250  * This should selectively update paths that exist...
1251  *
1252  * To be called from various tools that do incremental updates 
1253  */
1254 void ED_objects_recalculate_paths(bContext *C, Scene *scene)
1255 {
1256         ListBase targets = {NULL, NULL};
1257         
1258         /* loop over objects in scene */
1259         CTX_DATA_BEGIN(C, Object *, ob, selected_editable_objects)
1260         {
1261                 /* set flag to force recalc, then grab path(s) from object */
1262                 ob->avs.recalc |= ANIMVIZ_RECALC_PATHS;
1263                 animviz_get_object_motionpaths(ob, &targets);
1264         }
1265         CTX_DATA_END;
1266         
1267         /* recalculate paths, then free */
1268         animviz_calc_motionpaths(scene, &targets);
1269         BLI_freelistN(&targets);
1270 }
1271
1272
1273 /* show popup to determine settings */
1274 static int object_calculate_paths_invoke(bContext *C, wmOperator *op, const wmEvent *UNUSED(event))
1275 {
1276         Object *ob = CTX_data_active_object(C);
1277         
1278         if (ob == NULL)
1279                 return OPERATOR_CANCELLED;
1280         
1281         /* set default settings from existing/stored settings */
1282         {
1283                 bAnimVizSettings *avs = &ob->avs;
1284                 
1285                 RNA_int_set(op->ptr, "start_frame", avs->path_sf);
1286                 RNA_int_set(op->ptr, "end_frame", avs->path_ef);
1287         }
1288         
1289         /* show popup dialog to allow editing of range... */
1290         /* FIXME: hardcoded dimensions here are just arbitrary */
1291         return WM_operator_props_dialog_popup(C, op, 10 * UI_UNIT_X, 10 * UI_UNIT_Y);
1292 }
1293
1294 /* Calculate/recalculate whole paths (avs.path_sf to avs.path_ef) */
1295 static int object_calculate_paths_exec(bContext *C, wmOperator *op)
1296 {
1297         Scene *scene = CTX_data_scene(C);
1298         int start = RNA_int_get(op->ptr, "start_frame");
1299         int end = RNA_int_get(op->ptr, "end_frame");
1300         
1301         /* set up path data for bones being calculated */
1302         CTX_DATA_BEGIN(C, Object *, ob, selected_editable_objects)
1303         {
1304                 bAnimVizSettings *avs = &ob->avs;
1305                 
1306                 /* grab baking settings from operator settings */
1307                 avs->path_sf = start;
1308                 avs->path_ef = end;
1309                 
1310                 /* verify that the selected object has the appropriate settings */
1311                 animviz_verify_motionpaths(op->reports, scene, ob, NULL);
1312         }
1313         CTX_DATA_END;
1314         
1315         /* calculate the paths for objects that have them (and are tagged to get refreshed) */
1316         ED_objects_recalculate_paths(C, scene);
1317         
1318         /* notifiers for updates */
1319         WM_event_add_notifier(C, NC_OBJECT | ND_TRANSFORM, NULL);
1320         
1321         return OPERATOR_FINISHED; 
1322 }
1323
1324 void OBJECT_OT_paths_calculate(wmOperatorType *ot)
1325 {
1326         /* identifiers */
1327         ot->name = "Calculate Object Paths";
1328         ot->idname = "OBJECT_OT_paths_calculate";
1329         ot->description = "Calculate motion paths for the selected objects";
1330         
1331         /* api callbacks */
1332         ot->invoke = object_calculate_paths_invoke;
1333         ot->exec = object_calculate_paths_exec;
1334         ot->poll = ED_operator_object_active_editable;
1335         
1336         /* flags */
1337         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1338         
1339         /* properties */
1340         RNA_def_int(ot->srna, "start_frame", 1, MINAFRAME, MAXFRAME, "Start", 
1341                     "First frame to calculate object paths on", MINFRAME, MAXFRAME / 2.0);
1342         RNA_def_int(ot->srna, "end_frame", 250, MINAFRAME, MAXFRAME, "End", 
1343                     "Last frame to calculate object paths on", MINFRAME, MAXFRAME / 2.0);
1344 }
1345
1346 /* --------- */
1347
1348 static int object_update_paths_poll(bContext *C)
1349 {
1350         if (ED_operator_object_active_editable(C)) {
1351                 Object *ob = ED_object_active_context(C);
1352                 return (ob->avs.path_bakeflag & MOTIONPATH_BAKE_HAS_PATHS) != 0;
1353         }
1354         
1355         return false;
1356 }
1357
1358 static int object_update_paths_exec(bContext *C, wmOperator *UNUSED(op))
1359 {
1360         Scene *scene = CTX_data_scene(C);
1361         
1362         if (scene == NULL)
1363                 return OPERATOR_CANCELLED;
1364                 
1365         /* calculate the paths for objects that have them (and are tagged to get refreshed) */
1366         ED_objects_recalculate_paths(C, scene);
1367         
1368         /* notifiers for updates */
1369         WM_event_add_notifier(C, NC_OBJECT | ND_TRANSFORM, NULL);
1370         
1371         return OPERATOR_FINISHED;
1372 }
1373
1374 void OBJECT_OT_paths_update(wmOperatorType *ot)
1375 {
1376         /* identifiers */
1377         ot->name = "Update Object Paths";
1378         ot->idname = "OBJECT_OT_paths_update";
1379         ot->description = "Recalculate paths for selected objects";
1380         
1381         /* api callbakcs */
1382         ot->exec = object_update_paths_exec;
1383         ot->poll = object_update_paths_poll;
1384         
1385         /* flags */
1386         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1387 }
1388
1389 /* --------- */
1390
1391 /* Helper for ED_objects_clear_paths() */
1392 static void object_clear_mpath(Object *ob)
1393 {
1394         if (ob->mpath) {
1395                 animviz_free_motionpath(ob->mpath);
1396                 ob->mpath = NULL;
1397                 ob->avs.path_bakeflag &= ~MOTIONPATH_BAKE_HAS_PATHS;
1398         }
1399 }
1400
1401 /* Clear motion paths for all objects */
1402 void ED_objects_clear_paths(bContext *C, bool only_selected)
1403 {
1404         if (only_selected) {
1405                 /* loop over all selected + sedtiable objects in scene */
1406                 CTX_DATA_BEGIN(C, Object *, ob, selected_editable_objects)
1407                 {
1408                         object_clear_mpath(ob);
1409                 }
1410                 CTX_DATA_END;
1411         }
1412         else {
1413                 /* loop over all edtiable objects in scene */
1414                 CTX_DATA_BEGIN(C, Object *, ob, editable_objects)
1415                 {
1416                         object_clear_mpath(ob);
1417                 }
1418                 CTX_DATA_END;
1419         }
1420 }
1421
1422 /* operator callback for this */
1423 static int object_clear_paths_exec(bContext *C, wmOperator *op)
1424 {
1425         bool only_selected = RNA_boolean_get(op->ptr, "only_selected");
1426         
1427         /* use the backend function for this */
1428         ED_objects_clear_paths(C, only_selected);
1429         
1430         /* notifiers for updates */
1431         WM_event_add_notifier(C, NC_OBJECT | ND_TRANSFORM, NULL);
1432         
1433         return OPERATOR_FINISHED; 
1434 }
1435
1436 /* operator callback/wrapper */
1437 static int object_clear_paths_invoke(bContext *C, wmOperator *op, const wmEvent *evt)
1438 {
1439         if ((evt->shift) && !RNA_struct_property_is_set(op->ptr, "only_selected")) {
1440                 RNA_boolean_set(op->ptr, "only_selected", true);
1441         }
1442         return object_clear_paths_exec(C, op);
1443 }
1444
1445 void OBJECT_OT_paths_clear(wmOperatorType *ot)
1446 {
1447         /* identifiers */
1448         ot->name = "Clear Object Paths";
1449         ot->idname = "OBJECT_OT_paths_clear";
1450         ot->description = "Clear path caches for all objects, hold Shift key for selected objects only";
1451         
1452         /* api callbacks */
1453         ot->invoke = object_clear_paths_invoke;
1454         ot->exec = object_clear_paths_exec;
1455         ot->poll = ED_operator_object_active_editable;
1456         
1457         /* flags */
1458         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1459         
1460         /* properties */
1461         ot->prop = RNA_def_boolean(ot->srna, "only_selected", false, "Only Selected",
1462                                    "Only clear paths from selected objects");
1463         RNA_def_property_flag(ot->prop, PROP_SKIP_SAVE);
1464 }
1465
1466
1467 /********************** Smooth/Flat *********************/
1468
1469 static int shade_smooth_exec(bContext *C, wmOperator *op)
1470 {
1471         ID *data;
1472         Curve *cu;
1473         Nurb *nu;
1474         int clear = (STREQ(op->idname, "OBJECT_OT_shade_flat"));
1475         bool done = false, linked_data = false;
1476
1477         CTX_DATA_BEGIN(C, Object *, ob, selected_editable_objects)
1478         {
1479                 data = ob->data;
1480
1481                 if (data && ID_IS_LINKED(data)) {
1482                         linked_data = true;
1483                         continue;
1484                 }
1485
1486                 if (ob->type == OB_MESH) {
1487                         BKE_mesh_smooth_flag_set(ob, !clear);
1488
1489                         DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
1490                         WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, ob);
1491
1492                         done = true;
1493                 }
1494                 else if (ELEM(ob->type, OB_SURF, OB_CURVE)) {
1495                         cu = ob->data;
1496
1497                         for (nu = cu->nurb.first; nu; nu = nu->next) {
1498                                 if (!clear) nu->flag |= ME_SMOOTH;
1499                                 else nu->flag &= ~ME_SMOOTH;
1500                         }
1501
1502                         DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
1503                         WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, ob);
1504
1505                         done = true;
1506                 }
1507         }
1508         CTX_DATA_END;
1509
1510         if (linked_data)
1511                 BKE_report(op->reports, RPT_WARNING, "Can't edit linked mesh or curve data");
1512
1513         return (done) ? OPERATOR_FINISHED : OPERATOR_CANCELLED;
1514 }
1515
1516 static int shade_poll(bContext *C)
1517 {
1518         return (CTX_data_edit_object(C) == NULL);
1519 }
1520
1521 void OBJECT_OT_shade_flat(wmOperatorType *ot)
1522 {
1523         /* identifiers */
1524         ot->name = "Shade Flat";
1525         ot->description = "Render and display faces uniform, using Face Normals";
1526         ot->idname = "OBJECT_OT_shade_flat";
1527         
1528         /* api callbacks */
1529         ot->poll = shade_poll;
1530         ot->exec = shade_smooth_exec;
1531
1532         /* flags */
1533         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1534 }
1535
1536 void OBJECT_OT_shade_smooth(wmOperatorType *ot)
1537 {
1538         /* identifiers */
1539         ot->name = "Shade Smooth";
1540         ot->description = "Render and display faces smooth, using interpolated Vertex Normals";
1541         ot->idname = "OBJECT_OT_shade_smooth";
1542         
1543         /* api callbacks */
1544         ot->poll = shade_poll;
1545         ot->exec = shade_smooth_exec;
1546         
1547         /* flags */
1548         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1549 }
1550
1551 /* ********************** */
1552
1553 static void UNUSED_FUNCTION(image_aspect) (Scene *scene, View3D *v3d)
1554 {
1555         /* all selected objects with an image map: scale in image aspect */
1556         Base *base;
1557         Object *ob;
1558         Material *ma;
1559         Tex *tex;
1560         float x, y, space;
1561         int a, b, done;
1562         
1563         if (scene->obedit) return;  // XXX get from context
1564         if (ID_IS_LINKED(scene)) return;
1565         
1566         for (base = FIRSTBASE; base; base = base->next) {
1567                 if (TESTBASELIB(v3d, base)) {
1568                         ob = base->object;
1569                         done = false;
1570                         
1571                         for (a = 1; a <= ob->totcol; a++) {
1572                                 ma = give_current_material(ob, a);
1573                                 if (ma) {
1574                                         for (b = 0; b < MAX_MTEX; b++) {
1575                                                 if (ma->mtex[b] && ma->mtex[b]->tex) {
1576                                                         tex = ma->mtex[b]->tex;
1577                                                         if (tex->type == TEX_IMAGE && tex->ima) {
1578                                                                 ImBuf *ibuf = BKE_image_acquire_ibuf(tex->ima, NULL, NULL);
1579                                                                 
1580                                                                 /* texturespace */
1581                                                                 space = 1.0;
1582                                                                 if (ob->type == OB_MESH) {
1583                                                                         float size[3];
1584                                                                         BKE_mesh_texspace_get(ob->data, NULL, NULL, size);
1585                                                                         space = size[0] / size[1];
1586                                                                 }
1587                                                                 else if (ELEM(ob->type, OB_CURVE, OB_FONT, OB_SURF)) {
1588                                                                         float size[3];
1589                                                                         BKE_curve_texspace_get(ob->data, NULL, NULL, size);
1590                                                                         space = size[0] / size[1];
1591                                                                 }
1592                                                         
1593                                                                 x = ibuf->x / space;
1594                                                                 y = ibuf->y;
1595                                                                 
1596                                                                 if (x > y) ob->size[0] = ob->size[1] * x / y;
1597                                                                 else ob->size[1] = ob->size[0] * y / x;
1598                                                                 
1599                                                                 done = true;
1600                                                                 DAG_id_tag_update(&ob->id, OB_RECALC_OB);
1601
1602                                                                 BKE_image_release_ibuf(tex->ima, ibuf, NULL);
1603                                                         }
1604                                                 }
1605                                                 if (done) break;
1606                                         }
1607                                 }
1608                                 if (done) break;
1609                         }
1610                 }
1611         }
1612         
1613 }
1614
1615 static const EnumPropertyItem *object_mode_set_itemsf(
1616         bContext *C, PointerRNA *UNUSED(ptr), PropertyRNA *UNUSED(prop), bool *r_free)
1617 {
1618         const EnumPropertyItem *input = rna_enum_object_mode_items;
1619         EnumPropertyItem *item = NULL;
1620         Object *ob;
1621         bGPdata *gpd;
1622         int totitem = 0;
1623
1624         if (!C) /* needed for docs */
1625                 return rna_enum_object_mode_items;
1626
1627         ob = CTX_data_active_object(C);
1628         if (ob) {
1629                 const bool use_mode_particle_edit = (BLI_listbase_is_empty(&ob->particlesystem) == false) ||
1630                                                     (ob->soft != NULL) ||
1631                                                     (modifiers_findByType(ob, eModifierType_Cloth) != NULL);
1632                 while (input->identifier) {
1633                         if ((input->value == OB_MODE_EDIT && OB_TYPE_SUPPORT_EDITMODE(ob->type)) ||
1634                             (input->value == OB_MODE_POSE && (ob->type == OB_ARMATURE)) ||
1635                             (input->value == OB_MODE_PARTICLE_EDIT && use_mode_particle_edit) ||
1636                             (ELEM(input->value, OB_MODE_SCULPT, OB_MODE_VERTEX_PAINT,
1637                                    OB_MODE_WEIGHT_PAINT, OB_MODE_TEXTURE_PAINT) && (ob->type == OB_MESH)) ||
1638                             (input->value == OB_MODE_OBJECT))
1639                         {
1640                                 RNA_enum_item_add(&item, &totitem, input);
1641                         }
1642                         input++;
1643                 }
1644         }
1645         else {
1646                 /* We need at least this one! */
1647                 RNA_enum_items_add_value(&item, &totitem, input, OB_MODE_OBJECT);
1648         }
1649         
1650         /* On top of all the rest, GPencil Stroke Edit Mode
1651          * is available if there's a valid gp datablock...
1652          */
1653         gpd = CTX_data_gpencil_data(C);
1654         if (gpd) {
1655                 RNA_enum_items_add_value(&item, &totitem, rna_enum_object_mode_items, OB_MODE_GPENCIL);
1656         }
1657
1658         RNA_enum_item_end(&item, &totitem);
1659
1660         *r_free = true;
1661
1662         return item;
1663 }
1664
1665 static int object_mode_set_poll(bContext *C)
1666 {
1667         /* Since Grease Pencil editmode is also handled here,
1668          * we have a special exception for allowing this operator
1669          * to still work in that case when there's no active object
1670          * so that users can exit editmode this way as per normal.
1671          */
1672         if (ED_operator_object_active_editable(C))
1673                 return true;
1674         else
1675                 return (CTX_data_gpencil_data(C) != NULL);
1676 }
1677
1678 static int object_mode_set_exec(bContext *C, wmOperator *op)
1679 {
1680         Object *ob = CTX_data_active_object(C);
1681         bGPdata *gpd = CTX_data_gpencil_data(C);
1682         eObjectMode mode = RNA_enum_get(op->ptr, "mode");
1683         eObjectMode restore_mode = (ob) ? ob->mode : OB_MODE_OBJECT;
1684         const bool toggle = RNA_boolean_get(op->ptr, "toggle");
1685         
1686         if (gpd) {
1687                 /* GP Mode is not bound to a specific object. Therefore,
1688                  * we don't want it to be actually saved on any objects,
1689                  * as weirdness can happen if you select other objects,
1690                  * or load old files.
1691                  *
1692                  * Instead, we use the following 2 rules to ensure that
1693                  * the mode selector works as expected:
1694                  *  1) If there's no object, we want to enter editmode.
1695                  *     (i.e. with no object, we're in object mode)
1696                  *  2) Otherwise, exit stroke editmode, so that we can
1697                  *     enter another mode...
1698                  */
1699                 if (!ob || (gpd->flag & GP_DATA_STROKE_EDITMODE)) {
1700                         WM_operator_name_call(C, "GPENCIL_OT_editmode_toggle", WM_OP_EXEC_REGION_WIN, NULL);
1701                 }
1702         }
1703         
1704         if (!ob || !ED_object_mode_compat_test(ob, mode))
1705                 return OPERATOR_PASS_THROUGH;
1706
1707         if (ob->mode != mode) {
1708                 /* we should be able to remove this call, each operator calls  */
1709                 ED_object_mode_compat_set(C, ob, mode, op->reports);
1710         }
1711
1712         /* Exit current mode if it's not the mode we're setting */
1713         if (mode != OB_MODE_OBJECT && (ob->mode != mode || toggle)) {
1714                 /* Enter new mode */
1715                 ED_object_mode_toggle(C, mode);
1716         }
1717
1718         if (toggle) {
1719                 /* Special case for Object mode! */
1720                 if (mode == OB_MODE_OBJECT && restore_mode == OB_MODE_OBJECT && ob->restore_mode != OB_MODE_OBJECT) {
1721                         ED_object_mode_toggle(C, ob->restore_mode);
1722                 }
1723                 else if (ob->mode == mode) {
1724                         /* For toggling, store old mode so we know what to go back to */
1725                         ob->restore_mode = restore_mode;
1726                 }
1727                 else if (ob->restore_mode != OB_MODE_OBJECT && ob->restore_mode != mode) {
1728                         ED_object_mode_toggle(C, ob->restore_mode);
1729                 }
1730         }
1731
1732         return OPERATOR_FINISHED;
1733 }
1734
1735 void OBJECT_OT_mode_set(wmOperatorType *ot)
1736 {
1737         PropertyRNA *prop;
1738         
1739         /* identifiers */
1740         ot->name = "Set Object Mode";
1741         ot->description = "Sets the object interaction mode";
1742         ot->idname = "OBJECT_OT_mode_set";
1743         
1744         /* api callbacks */
1745         ot->exec = object_mode_set_exec;
1746         
1747         ot->poll = object_mode_set_poll; //ED_operator_object_active_editable;
1748         
1749         /* flags */
1750         ot->flag = 0; /* no register/undo here, leave it to operators being called */
1751         
1752         ot->prop = RNA_def_enum(ot->srna, "mode", rna_enum_object_mode_items, OB_MODE_OBJECT, "Mode", "");
1753         RNA_def_enum_funcs(ot->prop, object_mode_set_itemsf);
1754         RNA_def_property_flag(ot->prop, PROP_SKIP_SAVE);
1755
1756         prop = RNA_def_boolean(ot->srna, "toggle", 0, "Toggle", "");
1757         RNA_def_property_flag(prop, PROP_SKIP_SAVE);
1758 }
1759
1760 /************************ Game Properties ***********************/
1761
1762 static int game_property_new_exec(bContext *C, wmOperator *op)
1763 {
1764         Object *ob = CTX_data_active_object(C);
1765         bProperty *prop;
1766         char name[MAX_NAME];
1767         int type = RNA_enum_get(op->ptr, "type");
1768
1769         prop = BKE_bproperty_new(type);
1770         BLI_addtail(&ob->prop, prop);
1771
1772         RNA_string_get(op->ptr, "name", name);
1773         if (name[0] != '\0') {
1774                 BLI_strncpy(prop->name, name, sizeof(prop->name));
1775         }
1776
1777         BLI_uniquename(&ob->prop, prop, DATA_("Property"), '.', offsetof(bProperty, name), sizeof(prop->name));
1778
1779         WM_event_add_notifier(C, NC_LOGIC, NULL);
1780         return OPERATOR_FINISHED;
1781 }
1782
1783
1784 void OBJECT_OT_game_property_new(wmOperatorType *ot)
1785 {
1786         /* identifiers */
1787         ot->name = "New Game Property";
1788         ot->description = "Create a new property available to the game engine";
1789         ot->idname = "OBJECT_OT_game_property_new";
1790
1791         /* api callbacks */
1792         ot->exec = game_property_new_exec;
1793         ot->poll = ED_operator_object_active_editable;
1794
1795         /* flags */
1796         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1797
1798         RNA_def_enum(ot->srna, "type", rna_enum_gameproperty_type_items, GPROP_FLOAT, "Type", "Type of game property to add");
1799         RNA_def_string(ot->srna, "name", NULL, MAX_NAME, "Name", "Name of the game property to add");
1800 }
1801
1802 static int game_property_remove_exec(bContext *C, wmOperator *op)
1803 {
1804         Object *ob = CTX_data_active_object(C);
1805         bProperty *prop;
1806         int index = RNA_int_get(op->ptr, "index");
1807
1808         if (!ob)
1809                 return OPERATOR_CANCELLED;
1810
1811         prop = BLI_findlink(&ob->prop, index);
1812
1813         if (prop) {
1814                 BLI_remlink(&ob->prop, prop);
1815                 BKE_bproperty_free(prop);
1816
1817                 WM_event_add_notifier(C, NC_LOGIC, NULL);
1818                 return OPERATOR_FINISHED;
1819         }
1820         else {
1821                 return OPERATOR_CANCELLED;
1822         }
1823 }
1824
1825 void OBJECT_OT_game_property_remove(wmOperatorType *ot)
1826 {
1827         /* identifiers */
1828         ot->name = "Remove Game Property";
1829         ot->description = "Remove game property";
1830         ot->idname = "OBJECT_OT_game_property_remove";
1831
1832         /* api callbacks */
1833         ot->exec = game_property_remove_exec;
1834         ot->poll = ED_operator_object_active_editable;
1835
1836         /* flags */
1837         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1838
1839         RNA_def_int(ot->srna, "index", 0, 0, INT_MAX, "Index", "Property index to remove ", 0, INT_MAX);
1840 }
1841
1842 #define GAME_PROPERTY_MOVE_UP    1
1843 #define GAME_PROPERTY_MOVE_DOWN -1
1844
1845 static int game_property_move(bContext *C, wmOperator *op)
1846 {
1847         Object *ob = CTX_data_active_object(C);
1848         bProperty *prop;
1849         bProperty *otherprop = NULL;
1850         const int index = RNA_int_get(op->ptr, "index");
1851         const int dir = RNA_enum_get(op->ptr, "direction");
1852
1853         if (ob == NULL)
1854                 return OPERATOR_CANCELLED;
1855
1856         prop = BLI_findlink(&ob->prop, index);
1857         /* invalid index */
1858         if (prop == NULL)
1859                 return OPERATOR_CANCELLED;
1860
1861         if (dir == GAME_PROPERTY_MOVE_UP) {
1862                 otherprop = prop->prev;
1863         }
1864         else if (dir == GAME_PROPERTY_MOVE_DOWN) {
1865                 otherprop = prop->next;
1866         }
1867         else {
1868                 BLI_assert(0);
1869         }
1870
1871         if (prop && otherprop) {
1872                 BLI_listbase_swaplinks(&ob->prop, prop, otherprop);
1873
1874                 WM_event_add_notifier(C, NC_LOGIC, NULL);
1875                 return OPERATOR_FINISHED;
1876         }
1877         else {
1878                 return OPERATOR_CANCELLED;
1879         }
1880 }
1881
1882 void OBJECT_OT_game_property_move(wmOperatorType *ot)
1883 {
1884         static const EnumPropertyItem direction_property_move[] = {
1885                 {GAME_PROPERTY_MOVE_UP,   "UP",   0, "Up",   ""},
1886                 {GAME_PROPERTY_MOVE_DOWN, "DOWN", 0, "Down", ""},
1887                 {0, NULL, 0, NULL, NULL}
1888         };
1889         PropertyRNA *prop;
1890
1891         /* identifiers */
1892         ot->name = "Move Game Property";
1893         ot->description = "Move game property";
1894         ot->idname = "OBJECT_OT_game_property_move";
1895
1896         /* api callbacks */
1897         ot->exec = game_property_move;
1898         ot->poll = ED_operator_object_active_editable;
1899
1900         /* flags */
1901         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1902
1903         /* properties */
1904         prop = RNA_def_int(ot->srna, "index", 0, 0, INT_MAX, "Index", "Property index to move", 0, INT_MAX);
1905         RNA_def_property_flag(prop, PROP_HIDDEN);
1906         RNA_def_enum(ot->srna, "direction", direction_property_move, 0, "Direction",
1907                      "Direction for moving the property");
1908 }
1909
1910 #undef GAME_PROPERTY_MOVE_UP
1911 #undef GAME_PROPERTY_MOVE_DOWN
1912
1913 #define COPY_PROPERTIES_REPLACE 1
1914 #define COPY_PROPERTIES_MERGE   2
1915 #define COPY_PROPERTIES_COPY    3
1916
1917 static const EnumPropertyItem game_properties_copy_operations[] = {
1918         {COPY_PROPERTIES_REPLACE, "REPLACE", 0, "Replace Properties", ""},
1919         {COPY_PROPERTIES_MERGE, "MERGE", 0, "Merge Properties", ""},
1920         {COPY_PROPERTIES_COPY, "COPY", 0, "Copy a Property", ""},
1921         {0, NULL, 0, NULL, NULL}
1922 };
1923
1924 static const EnumPropertyItem *gameprops_itemf(bContext *C, PointerRNA *UNUSED(ptr), PropertyRNA *UNUSED(prop), bool *r_free)
1925 {       
1926         Object *ob = ED_object_active_context(C);
1927         EnumPropertyItem tmp = {0, "", 0, "", ""};
1928         EnumPropertyItem *item = NULL;
1929         bProperty *prop;
1930         int a, totitem = 0;
1931         
1932         if (!ob)
1933                 return DummyRNA_NULL_items;
1934
1935         for (a = 1, prop = ob->prop.first; prop; prop = prop->next, a++) {
1936                 tmp.value = a;
1937                 tmp.identifier = prop->name;
1938                 tmp.name = prop->name;
1939                 RNA_enum_item_add(&item, &totitem, &tmp);
1940         }
1941
1942         RNA_enum_item_end(&item, &totitem);
1943         *r_free = true;
1944
1945         return item;
1946 }
1947
1948 static int game_property_copy_exec(bContext *C, wmOperator *op)
1949 {
1950         Object *ob = ED_object_active_context(C);
1951         bProperty *prop;
1952         int type = RNA_enum_get(op->ptr, "operation");
1953         int propid = RNA_enum_get(op->ptr, "property");
1954
1955         if (propid > 0) { /* copy */
1956                 prop = BLI_findlink(&ob->prop, propid - 1);
1957                 
1958                 if (prop) {
1959                         CTX_DATA_BEGIN(C, Object *, ob_iter, selected_editable_objects)
1960                         {
1961                                 if (ob != ob_iter)
1962                                         BKE_bproperty_object_set(ob_iter, prop);
1963                         } CTX_DATA_END;
1964                 }
1965         }
1966
1967         else {
1968                 CTX_DATA_BEGIN(C, Object *, ob_iter, selected_editable_objects)
1969                 {
1970                         if (ob != ob_iter) {
1971                                 if (type == COPY_PROPERTIES_REPLACE) {
1972                                         BKE_bproperty_copy_list(&ob_iter->prop, &ob->prop);
1973                                 }
1974                                 else {
1975                                         /* merge - the default when calling with no argument */
1976                                         for (prop = ob->prop.first; prop; prop = prop->next) {
1977                                                 BKE_bproperty_object_set(ob_iter, prop);
1978                                         }
1979                                 }
1980                         }
1981                 }
1982                 CTX_DATA_END;
1983         }
1984
1985         return OPERATOR_FINISHED;
1986 }
1987
1988 void OBJECT_OT_game_property_copy(wmOperatorType *ot)
1989 {
1990         PropertyRNA *prop;
1991         /* identifiers */
1992         ot->name = "Copy Game Property";
1993         ot->idname = "OBJECT_OT_game_property_copy";
1994         ot->description = "Copy/merge/replace a game property from active object to all selected objects";
1995
1996         /* api callbacks */
1997         ot->exec = game_property_copy_exec;
1998         ot->poll = ED_operator_object_active_editable;
1999
2000         /* flags */
2001         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2002
2003         RNA_def_enum(ot->srna, "operation", game_properties_copy_operations, 3, "Operation", "");
2004         prop = RNA_def_enum(ot->srna, "property", DummyRNA_NULL_items, 0, "Property", "Properties to copy");
2005         RNA_def_property_flag(prop, PROP_SKIP_SAVE | PROP_ENUM_NO_TRANSLATE);
2006         RNA_def_enum_funcs(prop, gameprops_itemf);
2007         ot->prop = prop;
2008 }
2009
2010 static int game_property_clear_exec(bContext *C, wmOperator *UNUSED(op))
2011 {
2012         CTX_DATA_BEGIN(C, Object *, ob_iter, selected_editable_objects)
2013         {
2014                 BKE_bproperty_free_list(&ob_iter->prop);
2015         }
2016         CTX_DATA_END;
2017
2018         WM_event_add_notifier(C, NC_LOGIC, NULL);
2019         return OPERATOR_FINISHED;
2020 }
2021 void OBJECT_OT_game_property_clear(wmOperatorType *ot)
2022 {
2023         /* identifiers */
2024         ot->name = "Clear Game Properties";
2025         ot->idname = "OBJECT_OT_game_property_clear";
2026         ot->description = "Remove all game properties from all selected objects";
2027
2028         /* api callbacks */
2029         ot->exec = game_property_clear_exec;
2030         ot->poll = ED_operator_object_active_editable;
2031
2032         /* flags */
2033         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2034 }
2035
2036 /************************ Copy Logic Bricks ***********************/
2037
2038 static int logicbricks_copy_exec(bContext *C, wmOperator *UNUSED(op))
2039 {
2040         Object *ob = ED_object_active_context(C);
2041
2042         CTX_DATA_BEGIN(C, Object *, ob_iter, selected_editable_objects)
2043         {
2044                 if (ob != ob_iter) {
2045                         /* first: free all logic */
2046                         free_sensors(&ob_iter->sensors);
2047                         unlink_controllers(&ob_iter->controllers);
2048                         free_controllers(&ob_iter->controllers);
2049                         unlink_actuators(&ob_iter->actuators);
2050                         free_actuators(&ob_iter->actuators);
2051                 
2052                         /* now copy it, this also works without logicbricks! */
2053                         clear_sca_new_poins_ob(ob);
2054                         copy_sensors(&ob_iter->sensors, &ob->sensors, 0);
2055                         copy_controllers(&ob_iter->controllers, &ob->controllers, 0);
2056                         copy_actuators(&ob_iter->actuators, &ob->actuators, 0);
2057                         set_sca_new_poins_ob(ob_iter);
2058                 
2059                         /* some menu settings */
2060                         ob_iter->scavisflag = ob->scavisflag;
2061                         ob_iter->scaflag = ob->scaflag;
2062                 
2063                         /* set the initial state */
2064                         ob_iter->state = ob->state;
2065                         ob_iter->init_state = ob->init_state;
2066
2067                         if (ob_iter->totcol == ob->totcol) {
2068                                 ob_iter->actcol = ob->actcol;
2069                                 WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, ob_iter);
2070                         }
2071                 }
2072         }
2073         CTX_DATA_END;
2074
2075         WM_event_add_notifier(C, NC_LOGIC, NULL);
2076
2077         return OPERATOR_FINISHED;
2078 }
2079
2080 void OBJECT_OT_logic_bricks_copy(wmOperatorType *ot)
2081 {
2082         /* identifiers */
2083         ot->name = "Copy Logic Bricks to Selected";
2084         ot->description = "Copy logic bricks to other selected objects";
2085         ot->idname = "OBJECT_OT_logic_bricks_copy";
2086
2087         /* api callbacks */
2088         ot->exec = logicbricks_copy_exec;
2089         ot->poll = ED_operator_object_active_editable;
2090
2091         /* flags */
2092         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2093 }
2094
2095 static int game_physics_copy_exec(bContext *C, wmOperator *UNUSED(op))
2096 {
2097         Object *ob = ED_object_active_context(C);
2098         
2099         CTX_DATA_BEGIN(C, Object *, ob_iter, selected_editable_objects)
2100         {
2101                 if (ob != ob_iter) {
2102                         ob_iter->gameflag = ob->gameflag;
2103                         ob_iter->gameflag2 = ob->gameflag2;
2104                         ob_iter->inertia = ob->inertia;
2105                         ob_iter->formfactor = ob->formfactor;
2106                         ob_iter->damping = ob->damping;
2107                         ob_iter->rdamping = ob->rdamping;
2108                         ob_iter->min_vel = ob->min_vel;
2109                         ob_iter->max_vel = ob->max_vel;
2110                         ob_iter->min_angvel = ob->min_angvel;
2111                         ob_iter->max_angvel = ob->max_angvel;
2112                         ob_iter->obstacleRad = ob->obstacleRad;
2113                         ob_iter->mass = ob->mass;
2114                         copy_v3_v3(ob_iter->anisotropicFriction, ob->anisotropicFriction);
2115                         ob_iter->collision_boundtype = ob->collision_boundtype;
2116                         ob_iter->margin = ob->margin;
2117                         ob_iter->bsoft = copy_bulletsoftbody(ob->bsoft, 0);
2118                         if (ob->restrictflag & OB_RESTRICT_RENDER) 
2119                                 ob_iter->restrictflag |= OB_RESTRICT_RENDER;
2120                         else
2121                                 ob_iter->restrictflag &= ~OB_RESTRICT_RENDER;
2122
2123                         ob_iter->col_group = ob->col_group;
2124                         ob_iter->col_mask = ob->col_mask;
2125                 }
2126         }
2127         CTX_DATA_END;
2128         
2129         return OPERATOR_FINISHED;
2130 }
2131
2132 void OBJECT_OT_game_physics_copy(struct wmOperatorType *ot)
2133 {
2134         /* identifiers */
2135         ot->name = "Copy Game Physics Properties to Selected";
2136         ot->description = "Copy game physics properties to other selected objects";
2137         ot->idname = "OBJECT_OT_game_physics_copy";
2138         
2139         /* api callbacks */
2140         ot->exec = game_physics_copy_exec;
2141         ot->poll = ED_operator_object_active_editable;
2142         
2143         /* flags */
2144         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2145 }
2146
2147 /* generic utility function */
2148
2149 bool ED_object_editmode_calc_active_center(Object *obedit, const bool select_only, float r_center[3])
2150 {
2151         switch (obedit->type) {
2152                 case OB_MESH:
2153                 {
2154                         BMEditMesh *em = BKE_editmesh_from_object(obedit);
2155                         BMEditSelection ese;
2156
2157                         if (BM_select_history_active_get(em->bm, &ese)) {
2158                                 BM_editselection_center(&ese, r_center);
2159                                 return true;
2160                         }
2161                         break;
2162                 }
2163                 case OB_ARMATURE:
2164                 {
2165                         bArmature *arm = obedit->data;
2166                         EditBone *ebo = arm->act_edbone;
2167
2168                         if (ebo && (!select_only || (ebo->flag & (BONE_SELECTED | BONE_ROOTSEL)))) {
2169                                 copy_v3_v3(r_center, ebo->head);
2170                                 return true;
2171                         }
2172
2173                         break;
2174                 }
2175                 case OB_CURVE:
2176                 case OB_SURF:
2177                 {
2178                         Curve *cu = obedit->data;
2179
2180                         if (ED_curve_active_center(cu, r_center)) {
2181                                 return true;
2182                         }
2183                         break;
2184                 }
2185                 case OB_MBALL:
2186                 {
2187                         MetaBall *mb = obedit->data;
2188                         MetaElem *ml_act = mb->lastelem;
2189
2190                         if (ml_act && (!select_only || (ml_act->flag & SELECT))) {
2191                                 copy_v3_v3(r_center, &ml_act->x);
2192                                 return true;
2193                         }
2194                         break;
2195                 }
2196                 case OB_LATTICE:
2197                 {
2198                         BPoint *actbp = BKE_lattice_active_point_get(obedit->data);
2199
2200                         if (actbp) {
2201                                 copy_v3_v3(r_center, actbp->vec);
2202                                 return true;
2203                         }
2204                         break;
2205                 }
2206         }
2207
2208         return false;
2209 }