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