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