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