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