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