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