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