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