Cleanup: style
[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_scene_types.h"
55 #include "DNA_object_types.h"
56 #include "DNA_object_force_types.h"
57 #include "DNA_meshdata_types.h"
58 #include "DNA_vfont_types.h"
59 #include "DNA_mesh_types.h"
60 #include "DNA_lattice_types.h"
61 #include "DNA_workspace_types.h"
62
63 #include "IMB_imbuf_types.h"
64
65 #include "BKE_anim.h"
66 #include "BKE_collection.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_softbody.h"
83 #include "BKE_modifier.h"
84 #include "BKE_editlattice.h"
85 #include "BKE_editmesh.h"
86 #include "BKE_report.h"
87 #include "BKE_workspace.h"
88 #include "BKE_layer.h"
89
90 #include "DEG_depsgraph.h"
91 #include "DEG_depsgraph_build.h"
92
93 #include "ED_armature.h"
94 #include "ED_curve.h"
95 #include "ED_mesh.h"
96 #include "ED_mball.h"
97 #include "ED_lattice.h"
98 #include "ED_object.h"
99 #include "ED_screen.h"
100 #include "ED_undo.h"
101 #include "ED_image.h"
102
103 #include "RNA_access.h"
104 #include "RNA_define.h"
105 #include "RNA_enum_types.h"
106
107 /* for menu/popup icons etc etc*/
108
109 #include "UI_interface.h"
110 #include "UI_resources.h"
111 #include "WM_api.h"
112 #include "WM_types.h"
113
114 #include "object_intern.h"  // own include
115
116 /* prototypes */
117 typedef struct MoveToCollectionData MoveToCollectionData;
118 static void move_to_collection_menus_items(struct uiLayout *layout, struct MoveToCollectionData *menu);
119
120 /* ************* XXX **************** */
121 static void error(const char *UNUSED(arg)) {}
122 static void waitcursor(int UNUSED(val)) {}
123 static int pupmenu(const char *UNUSED(msg)) { return 0; }
124
125 /* port over here */
126 static void error_libdata(void) {}
127
128 Object *ED_object_context(bContext *C)
129 {
130         return CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
131 }
132
133 /* find the correct active object per context
134  * note: context can be NULL when called from a enum with PROP_ENUM_NO_CONTEXT */
135 Object *ED_object_active_context(bContext *C)
136 {
137         Object *ob = NULL;
138         if (C) {
139                 ob = ED_object_context(C);
140                 if (!ob) ob = CTX_data_active_object(C);
141         }
142         return ob;
143 }
144
145
146 /* ******************* toggle editmode operator  ***************** */
147
148 static bool mesh_needs_keyindex(const Mesh *me)
149 {
150         if (me->key) {
151                 return false;  /* will be added */
152         }
153
154         for (const Object *ob = G.main->object.first; ob; ob = ob->id.next) {
155                 if ((ob->parent) && (ob->parent->data == me) && ELEM(ob->partype, PARVERT1, PARVERT3)) {
156                         return true;
157                 }
158                 if (ob->data == me) {
159                         for (const ModifierData *md = ob->modifiers.first; md; md = md->next) {
160                                 if (md->type == eModifierType_Hook) {
161                                         return true;
162                                 }
163                         }
164                 }
165         }
166         return false;
167 }
168
169 /**
170  * Load EditMode data back into the object,
171  * optionally freeing the editmode data.
172  */
173 static bool ED_object_editmode_load_ex(Main *bmain, Object *obedit, const bool freedata)
174 {
175         if (obedit == NULL) {
176                 return false;
177         }
178
179         if (obedit->type == OB_MESH) {
180                 Mesh *me = obedit->data;
181                 if (me->edit_btmesh == NULL) {
182                         return false;
183                 }
184
185                 if (me->edit_btmesh->bm->totvert > MESH_MAX_VERTS) {
186                         error("Too many vertices");
187                         return false;
188                 }
189
190                 EDBM_mesh_load(obedit);
191
192                 if (freedata) {
193                         EDBM_mesh_free(me->edit_btmesh);
194                         MEM_freeN(me->edit_btmesh);
195                         me->edit_btmesh = NULL;
196                 }
197                 /* will be recalculated as needed. */
198                 {
199                         ED_mesh_mirror_spatial_table(NULL, NULL, NULL, NULL, 'e');
200                         ED_mesh_mirror_topo_table(NULL, NULL, 'e');
201                 }
202         }
203         else if (obedit->type == OB_ARMATURE) {
204                 const bArmature *arm = obedit->data;
205                 if (arm->edbo == NULL) {
206                         return false;
207                 }
208                 ED_armature_from_edit(obedit->data);
209                 if (freedata) {
210                         ED_armature_edit_free(obedit->data);
211                 }
212                 /* TODO(sergey): Pose channels might have been changed, so need
213                  * to inform dependency graph about this. But is it really the
214                  * best place to do this?
215                  */
216                 DEG_relations_tag_update(bmain);
217         }
218         else if (ELEM(obedit->type, OB_CURVE, OB_SURF)) {
219                 const Curve *cu = obedit->data;
220                 if (cu->editnurb == NULL) {
221                         return false;
222                 }
223                 ED_curve_editnurb_load(obedit);
224                 if (freedata) {
225                         ED_curve_editnurb_free(obedit);
226                 }
227         }
228         else if (obedit->type == OB_FONT) {
229                 const Curve *cu = obedit->data;
230                 if (cu->editfont == NULL) {
231                         return false;
232                 }
233                 ED_curve_editfont_load(obedit);
234                 if (freedata) {
235                         ED_curve_editfont_free(obedit);
236                 }
237         }
238         else if (obedit->type == OB_LATTICE) {
239                 const Lattice *lt = obedit->data;
240                 if (lt->editlatt == NULL) {
241                         return false;
242                 }
243                 BKE_editlattice_load(obedit);
244                 if (freedata) {
245                         BKE_editlattice_free(obedit);
246                 }
247         }
248         else if (obedit->type == OB_MBALL) {
249                 const MetaBall *mb = obedit->data;
250                 if (mb->editelems == NULL) {
251                         return false;
252                 }
253                 ED_mball_editmball_load(obedit);
254                 if (freedata) {
255                         ED_mball_editmball_free(obedit);
256                 }
257         }
258
259         return true;
260 }
261
262 bool ED_object_editmode_load(Object *obedit)
263 {
264         /* TODO(sergey): use proper main here? */
265         return ED_object_editmode_load_ex(G.main, obedit, false);
266 }
267
268 /**
269  * \param C: Can be NULL, only if #EM_DO_UNDO isn't set.
270  * \param flag:
271  * - Only in exceptional cases should #EM_DO_UNDO NOT be in the flag.
272  * - If #EM_FREEDATA isn't in the flag, use ED_object_editmode_load directly.
273  */
274 void ED_object_editmode_exit_ex(bContext *C, Scene *scene, Object *obedit, int flag)
275 {
276         BLI_assert(C || !(flag & EM_DO_UNDO));
277         const bool freedata = (flag & EM_FREEDATA) != 0;
278
279         if (flag & EM_WAITCURSOR) waitcursor(1);
280
281         if (ED_object_editmode_load_ex(G.main, obedit, freedata) == false) {
282                 /* in rare cases (background mode) its possible active object
283                  * is flagged for editmode, without 'obedit' being set [#35489] */
284                 if (UNLIKELY(obedit && obedit->mode & OB_MODE_EDIT)) {
285                         obedit->mode &= ~OB_MODE_EDIT;
286                 }
287                 if (flag & EM_WAITCURSOR) waitcursor(0);
288                 return;
289         }
290
291         /* freedata only 0 now on file saves and render */
292         if (freedata) {
293                 ListBase pidlist;
294                 PTCacheID *pid;
295
296                 /* flag object caches as outdated */
297                 BKE_ptcache_ids_from_object(&pidlist, obedit, scene, 0);
298                 for (pid = pidlist.first; pid; pid = pid->next) {
299                         if (pid->type != PTCACHE_TYPE_PARTICLES) /* particles don't need reset on geometry change */
300                                 pid->cache->flag |= PTCACHE_OUTDATED;
301                 }
302                 BLI_freelistN(&pidlist);
303                 
304                 BKE_ptcache_object_reset(scene, obedit, PTCACHE_RESET_OUTDATED);
305
306                 /* also flush ob recalc, doesn't take much overhead, but used for particles */
307                 DEG_id_tag_update(&obedit->id, OB_RECALC_OB | OB_RECALC_DATA);
308         
309                 if (flag & EM_DO_UNDO)
310                         ED_undo_push(C, "Editmode");
311
312                 if (C != NULL) {
313                         WM_event_add_notifier(C, NC_SCENE | ND_MODE | NS_MODE_OBJECT, scene);
314                 }
315                 else {
316                         WM_main_add_notifier(NC_SCENE | ND_MODE | NS_MODE_OBJECT, scene);
317                 }
318
319
320                 obedit->mode &= ~OB_MODE_EDIT;
321         }
322
323         if (flag & EM_WAITCURSOR) waitcursor(0);
324 }
325
326 void ED_object_editmode_exit(bContext *C, int flag)
327 {
328         Scene *scene = CTX_data_scene(C);
329         Object *obedit = CTX_data_edit_object(C);
330         ED_object_editmode_exit_ex(C, scene, obedit, flag);
331 }
332
333 void ED_object_editmode_enter_ex(Scene *scene, Object *ob, int flag)
334 {
335         bool ok = false;
336
337         if (ELEM(NULL, ob, ob->data)) return;
338         if (ID_IS_LINKED(ob)) return;
339
340         /* this checks actual object->data, for cases when other scenes have it in editmode context */
341         if (BKE_object_is_in_editmode(ob))
342                 return;
343         
344         if (BKE_object_obdata_is_libdata(ob)) {
345                 error_libdata();
346                 return;
347         }
348
349         if (flag & EM_WAITCURSOR) waitcursor(1);
350
351         ob->restore_mode = ob->mode;
352
353         ob->mode = OB_MODE_EDIT;
354
355         if (ob->type == OB_MESH) {
356                 BMEditMesh *em;
357                 ok = 1;
358                 const bool use_key_index = mesh_needs_keyindex(ob->data);
359
360                 EDBM_mesh_make(ob, scene->toolsettings->selectmode, use_key_index);
361
362                 em = BKE_editmesh_from_object(ob);
363                 if (LIKELY(em)) {
364                         /* order doesn't matter */
365                         EDBM_mesh_normals_update(em);
366                         BKE_editmesh_tessface_calc(em);
367                 }
368
369                 WM_main_add_notifier(NC_SCENE | ND_MODE | NS_EDITMODE_MESH, NULL);
370         }
371         else if (ob->type == OB_ARMATURE) {
372                 bArmature *arm = ob->data;
373                 if (!arm) return;
374                 /*
375                  * The function BKE_object_obdata_is_libdata make a problem here, the
376                  * check for ob->proxy return 0 and let blender enter to edit mode
377                  * this causes a crash when you try leave the edit mode.
378                  * The problem is that i can't remove the ob->proxy check from
379                  * BKE_object_obdata_is_libdata that prevent the bugfix #6614, so
380                  * i add this little hack here.
381                  */
382                 if (ID_IS_LINKED(arm)) {
383                         error_libdata();
384                         return;
385                 }
386                 ok = 1;
387                 ED_armature_to_edit(arm);
388                 /* to ensure all goes in restposition and without striding */
389                 DEG_id_tag_update(&ob->id, OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME); /* XXX: should this be OB_RECALC_DATA? */
390
391                 WM_main_add_notifier(NC_SCENE | ND_MODE | NS_EDITMODE_ARMATURE, scene);
392         }
393         else if (ob->type == OB_FONT) {
394                 ok = 1;
395                 ED_curve_editfont_make(ob);
396
397                 WM_main_add_notifier(NC_SCENE | ND_MODE | NS_EDITMODE_TEXT, scene);
398         }
399         else if (ob->type == OB_MBALL) {
400                 ok = 1;
401                 ED_mball_editmball_make(ob);
402
403                 WM_main_add_notifier(NC_SCENE | ND_MODE | NS_EDITMODE_MBALL, scene);
404         }
405         else if (ob->type == OB_LATTICE) {
406                 ok = 1;
407                 BKE_editlattice_make(ob);
408
409                 WM_main_add_notifier(NC_SCENE | ND_MODE | NS_EDITMODE_LATTICE, scene);
410         }
411         else if (ob->type == OB_SURF || ob->type == OB_CURVE) {
412                 ok = 1;
413                 ED_curve_editnurb_make(ob);
414
415                 WM_main_add_notifier(NC_SCENE | ND_MODE | NS_EDITMODE_CURVE, scene);
416         }
417
418         if (ok) {
419                 DEG_id_tag_update(&ob->id, OB_RECALC_DATA);
420         }
421         else {
422                 if ((flag & EM_NO_CONTEXT) == 0) {
423                         ob->mode &= ~OB_MODE_EDIT;
424                 }
425                 WM_main_add_notifier(NC_SCENE | ND_MODE | NS_MODE_OBJECT, scene);
426         }
427
428         if (flag & EM_WAITCURSOR) waitcursor(0);
429         BLI_assert((flag & EM_DO_UNDO) == 0);
430 }
431
432 void ED_object_editmode_enter(bContext *C, int flag)
433 {
434         Scene *scene = CTX_data_scene(C);
435         ViewLayer *view_layer = CTX_data_view_layer(C);
436         Object *ob;
437
438         if ((flag & EM_IGNORE_LAYER) == 0) {
439                 ob = CTX_data_active_object(C); /* active layer checked here for view3d */
440         }
441         else {
442                 ob = view_layer->basact->object;
443         }
444         if (ob == NULL) return;
445         if (ID_IS_LINKED(ob)) return;
446
447         ED_object_editmode_enter_ex(scene, ob, flag & ~EM_DO_UNDO);
448         if (flag & EM_DO_UNDO) ED_undo_push(C, "Enter Editmode");
449 }
450
451 static int editmode_toggle_exec(bContext *C, wmOperator *op)
452 {
453         const int mode_flag = OB_MODE_EDIT;
454         const bool is_mode_set = (CTX_data_edit_object(C) != NULL);
455         Scene *scene =  CTX_data_scene(C);
456         ViewLayer *view_layer = CTX_data_view_layer(C);
457         Object *obact = OBACT(view_layer);
458
459         if (!is_mode_set) {
460                 if (!ED_object_mode_compat_set(C, obact, 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                 if (obact->mode & mode_flag) {
468                         FOREACH_SELECTED_OBJECT_BEGIN(view_layer, ob)
469                         {
470                                 if ((ob != obact) && (ob->type == obact->type)) {
471                                         if (ob->flag & SELECT) {
472                                                 ED_object_editmode_enter_ex(scene, ob, EM_WAITCURSOR | EM_NO_CONTEXT);
473                                         }
474                                 }
475                         }
476                         FOREACH_SELECTED_OBJECT_END;
477                 }
478         }
479         else {
480                 ED_object_editmode_exit(C, EM_FREEDATA | EM_WAITCURSOR);  /* had EM_DO_UNDO but op flag calls undo too [#24685] */
481                 if ((obact->mode & mode_flag) == 0) {
482                         FOREACH_SELECTED_OBJECT_BEGIN(view_layer, ob)
483                         {
484                                 if ((ob != obact) && (ob->type == obact->type)) {
485                                         if (ob->flag & SELECT) {
486                                                 ED_object_editmode_exit_ex(NULL, scene, ob, EM_FREEDATA | EM_WAITCURSOR);
487                                         }
488                                 }
489                         }
490                         FOREACH_SELECTED_OBJECT_END;
491                 }
492         }
493         
494         ED_space_image_uv_sculpt_update(CTX_wm_manager(C), scene);
495
496         return OPERATOR_FINISHED;
497 }
498
499 static int editmode_toggle_poll(bContext *C)
500 {
501         Object *ob = CTX_data_active_object(C);
502
503         /* covers proxies too */
504         if (ELEM(NULL, ob, ob->data) || ID_IS_LINKED(ob->data))
505                 return 0;
506
507         /* if hidden but in edit mode, we still display */
508         if ((ob->restrictflag & OB_RESTRICT_VIEW) && !(ob->mode & OB_MODE_EDIT)) {
509                 return 0;
510         }
511
512         return OB_TYPE_SUPPORT_EDITMODE(ob->type);
513 }
514
515 void OBJECT_OT_editmode_toggle(wmOperatorType *ot)
516 {
517         
518         /* identifiers */
519         ot->name = "Toggle Editmode";
520         ot->description = "Toggle object's editmode";
521         ot->idname = "OBJECT_OT_editmode_toggle";
522         
523         /* api callbacks */
524         ot->exec = editmode_toggle_exec;
525         ot->poll = editmode_toggle_poll;
526         
527         /* flags */
528         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
529 }
530
531 /* *************************** */
532
533 static int posemode_exec(bContext *C, wmOperator *op)
534 {
535         Base *base = CTX_data_active_base(C);
536         Object *obact = base->object;
537         const int mode_flag = OB_MODE_POSE;
538         bool is_mode_set = (obact->mode & mode_flag) != 0;
539         
540         if (!is_mode_set) {
541                 if (!ED_object_mode_compat_set(C, obact, mode_flag, op->reports)) {
542                         return OPERATOR_CANCELLED;
543                 }
544         }
545
546         if (obact->type == OB_ARMATURE) {
547                 if (obact == CTX_data_edit_object(C)) {
548                         ED_object_editmode_exit(C, EM_FREEDATA | EM_DO_UNDO);
549                         is_mode_set = false;
550                 }
551
552                 if (is_mode_set) {
553                         bool ok = ED_object_posemode_exit(C, obact);
554                         if (ok) {
555                                 struct Main *bmain = CTX_data_main(C);
556                                 ViewLayer *view_layer = CTX_data_view_layer(C);
557                                 FOREACH_SELECTED_OBJECT_BEGIN(view_layer, ob)
558                                 {
559                                         if ((ob != obact) &&
560                                             (ob->type == OB_ARMATURE) &&
561                                             (ob->mode & mode_flag))
562                                         {
563                                                 if (ob->flag & SELECT) {
564                                                         ED_object_posemode_exit_ex(bmain, ob);
565                                                 }
566                                         }
567                                 }
568                                 FOREACH_SELECTED_OBJECT_END;
569                         }
570                 }
571                 else {
572                         bool ok = ED_object_posemode_enter(C, obact);
573                         if (ok) {
574                                 struct Main *bmain = CTX_data_main(C);
575                                 ViewLayer *view_layer = CTX_data_view_layer(C);
576                                 FOREACH_SELECTED_OBJECT_BEGIN(view_layer, ob)
577                                 {
578                                         if ((ob != obact) &&
579                                             (ob->type == OB_ARMATURE) &&
580                                             (ob->mode == OB_MODE_OBJECT) &&
581                                             (!ID_IS_LINKED(ob)))
582                                         {
583                                                 if (ob->flag & SELECT) {
584                                                         ED_object_posemode_enter_ex(bmain, ob);
585                                                 }
586                                         }
587                                 }
588                                 FOREACH_SELECTED_OBJECT_END;
589                         }
590                 }
591                 return OPERATOR_FINISHED;
592         }
593         
594         return OPERATOR_PASS_THROUGH;
595 }
596
597 void OBJECT_OT_posemode_toggle(wmOperatorType *ot) 
598 {
599         /* identifiers */
600         ot->name = "Toggle Pose Mode";
601         ot->idname = "OBJECT_OT_posemode_toggle";
602         ot->description = "Enable or disable posing/selecting bones";
603         
604         /* api callbacks */
605         ot->exec = posemode_exec;
606         ot->poll = ED_operator_object_active_editable;
607         
608         /* flag */
609         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
610 }
611
612 /* both pointers should exist */
613 static void copy_texture_space(Object *to, Object *ob)
614 {
615         float *poin1 = NULL, *poin2 = NULL;
616         short texflag = 0;
617         
618         if (ob->type == OB_MESH) {
619                 texflag = ((Mesh *)ob->data)->texflag;
620                 poin2 = ((Mesh *)ob->data)->loc;
621         }
622         else if (ELEM(ob->type, OB_CURVE, OB_SURF, OB_FONT)) {
623                 texflag = ((Curve *)ob->data)->texflag;
624                 poin2 = ((Curve *)ob->data)->loc;
625         }
626         else if (ob->type == OB_MBALL) {
627                 texflag = ((MetaBall *)ob->data)->texflag;
628                 poin2 = ((MetaBall *)ob->data)->loc;
629         }
630         else
631                 return;
632                 
633         if (to->type == OB_MESH) {
634                 ((Mesh *)to->data)->texflag = texflag;
635                 poin1 = ((Mesh *)to->data)->loc;
636         }
637         else if (ELEM(to->type, OB_CURVE, OB_SURF, OB_FONT)) {
638                 ((Curve *)to->data)->texflag = texflag;
639                 poin1 = ((Curve *)to->data)->loc;
640         }
641         else if (to->type == OB_MBALL) {
642                 ((MetaBall *)to->data)->texflag = texflag;
643                 poin1 = ((MetaBall *)to->data)->loc;
644         }
645         else
646                 return;
647         
648         memcpy(poin1, poin2, 9 * sizeof(float));  /* this was noted in DNA_mesh, curve, mball */
649         
650         if (to->type == OB_MESH) {
651                 /* pass */
652         }
653         else if (to->type == OB_MBALL) {
654                 BKE_mball_texspace_calc(to);
655         }
656         else {
657                 BKE_curve_texspace_calc(to->data);
658         }
659         
660 }
661
662 /* UNUSED, keep in case we want to copy functionality for use elsewhere */
663 static void copy_attr(Main *bmain, Scene *scene, ViewLayer *view_layer, short event)
664 {
665         Object *ob;
666         Base *base;
667         Curve *cu, *cu1;
668         Nurb *nu;
669         bool do_depgraph_update = false;
670         
671         if (ID_IS_LINKED(scene)) return;
672
673         if (!(ob = OBACT(view_layer))) return;
674         
675         if (BKE_object_is_in_editmode(ob)) {
676                 /* obedit_copymenu(); */
677                 return;
678         }
679
680         if (event == 24) {
681                 /* moved to BKE_object_link_modifiers */
682                 /* copymenu_modifiers(bmain, scene, v3d, ob); */
683                 return;
684         }
685
686         for (base = FIRSTBASE(view_layer); base; base = base->next) {
687                 if (base != BASACT(view_layer)) {
688                         if (TESTBASELIB(base)) {
689                                 DEG_id_tag_update(&base->object->id, OB_RECALC_DATA);
690                                 
691                                 if (event == 1) {  /* loc */
692                                         copy_v3_v3(base->object->loc, ob->loc);
693                                         copy_v3_v3(base->object->dloc, ob->dloc);
694                                 }
695                                 else if (event == 2) {  /* rot */
696                                         copy_v3_v3(base->object->rot, ob->rot);
697                                         copy_v3_v3(base->object->drot, ob->drot);
698
699                                         copy_qt_qt(base->object->quat, ob->quat);
700                                         copy_qt_qt(base->object->dquat, ob->dquat);
701                                 }
702                                 else if (event == 3) {  /* size */
703                                         copy_v3_v3(base->object->size, ob->size);
704                                         copy_v3_v3(base->object->dscale, ob->dscale);
705                                 }
706                                 else if (event == 4) {  /* drawtype */
707                                         base->object->dt = ob->dt;
708                                         base->object->dtx = ob->dtx;
709                                         base->object->empty_drawtype = ob->empty_drawtype;
710                                         base->object->empty_drawsize = ob->empty_drawsize;
711                                 }
712                                 else if (event == 5) {  /* time offs */
713                                         base->object->sf = ob->sf;
714                                 }
715                                 else if (event == 6) {  /* dupli */
716                                         base->object->dupon = ob->dupon;
717                                         base->object->dupoff = ob->dupoff;
718                                         base->object->dupsta = ob->dupsta;
719                                         base->object->dupend = ob->dupend;
720                                         
721                                         base->object->transflag &= ~OB_DUPLI;
722                                         base->object->transflag |= (ob->transflag & OB_DUPLI);
723
724                                         base->object->dup_group = ob->dup_group;
725                                         if (ob->dup_group)
726                                                 id_us_plus(&ob->dup_group->id);
727                                 }
728                                 else if (event == 17) {   /* tex space */
729                                         copy_texture_space(base->object, ob);
730                                 }
731                                 else if (event == 18) {   /* font settings */
732                                         
733                                         if (base->object->type == ob->type) {
734                                                 cu = ob->data;
735                                                 cu1 = base->object->data;
736
737                                                 cu1->spacemode = cu->spacemode;
738                                                 cu1->align_y = cu->align_y;
739                                                 cu1->spacing = cu->spacing;
740                                                 cu1->linedist = cu->linedist;
741                                                 cu1->shear = cu->shear;
742                                                 cu1->fsize = cu->fsize;
743                                                 cu1->xof = cu->xof;
744                                                 cu1->yof = cu->yof;
745                                                 cu1->textoncurve = cu->textoncurve;
746                                                 cu1->wordspace = cu->wordspace;
747                                                 cu1->ulpos = cu->ulpos;
748                                                 cu1->ulheight = cu->ulheight;
749                                                 if (cu1->vfont)
750                                                         id_us_min(&cu1->vfont->id);
751                                                 cu1->vfont = cu->vfont;
752                                                 id_us_plus((ID *)cu1->vfont);
753                                                 if (cu1->vfontb)
754                                                         id_us_min(&cu1->vfontb->id);
755                                                 cu1->vfontb = cu->vfontb;
756                                                 id_us_plus((ID *)cu1->vfontb);
757                                                 if (cu1->vfonti)
758                                                         id_us_min(&cu1->vfonti->id);
759                                                 cu1->vfonti = cu->vfonti;
760                                                 id_us_plus((ID *)cu1->vfonti);
761                                                 if (cu1->vfontbi)
762                                                         id_us_min(&cu1->vfontbi->id);
763                                                 cu1->vfontbi = cu->vfontbi;
764                                                 id_us_plus((ID *)cu1->vfontbi);
765                                                 
766                                                 BLI_strncpy(cu1->family, cu->family, sizeof(cu1->family));
767                                                 
768                                                 DEG_id_tag_update(&base->object->id, OB_RECALC_DATA);
769                                         }
770                                 }
771                                 else if (event == 19) {   /* bevel settings */
772                                         
773                                         if (ELEM(base->object->type, OB_CURVE, OB_FONT)) {
774                                                 cu = ob->data;
775                                                 cu1 = base->object->data;
776                                                 
777                                                 cu1->bevobj = cu->bevobj;
778                                                 cu1->taperobj = cu->taperobj;
779                                                 cu1->width = cu->width;
780                                                 cu1->bevresol = cu->bevresol;
781                                                 cu1->ext1 = cu->ext1;
782                                                 cu1->ext2 = cu->ext2;
783                                                 
784                                                 DEG_id_tag_update(&base->object->id, OB_RECALC_DATA);
785                                         }
786                                 }
787                                 else if (event == 25) {   /* curve resolution */
788
789                                         if (ELEM(base->object->type, OB_CURVE, OB_FONT)) {
790                                                 cu = ob->data;
791                                                 cu1 = base->object->data;
792                                                 
793                                                 cu1->resolu = cu->resolu;
794                                                 cu1->resolu_ren = cu->resolu_ren;
795                                                 
796                                                 nu = cu1->nurb.first;
797                                                 
798                                                 while (nu) {
799                                                         nu->resolu = cu1->resolu;
800                                                         nu = nu->next;
801                                                 }
802                                                 
803                                                 DEG_id_tag_update(&base->object->id, OB_RECALC_DATA);
804                                         }
805                                 }
806                                 else if (event == 21) {
807                                         if (base->object->type == OB_MESH) {
808                                                 ModifierData *md = modifiers_findByType(ob, eModifierType_Subsurf);
809
810                                                 if (md) {
811                                                         ModifierData *tmd = modifiers_findByType(base->object, eModifierType_Subsurf);
812
813                                                         if (!tmd) {
814                                                                 tmd = modifier_new(eModifierType_Subsurf);
815                                                                 BLI_addtail(&base->object->modifiers, tmd);
816                                                         }
817
818                                                         modifier_copyData(md, tmd);
819                                                         DEG_id_tag_update(&base->object->id, OB_RECALC_DATA);
820                                                 }
821                                         }
822                                 }
823                                 else if (event == 22) {
824                                         /* Copy the constraint channels over */
825                                         BKE_constraints_copy(&base->object->constraints, &ob->constraints, true);
826                                         
827                                         do_depgraph_update = true;
828                                 }
829                                 else if (event == 23) {
830                                         base->object->softflag = ob->softflag;
831                                         if (base->object->soft) sbFree(base->object->soft);
832                                         
833                                         base->object->soft = copy_softbody(ob->soft, 0);
834
835                                         if (!modifiers_findByType(base->object, eModifierType_Softbody)) {
836                                                 BLI_addhead(&base->object->modifiers, modifier_new(eModifierType_Softbody));
837                                         }
838                                 }
839                                 else if (event == 26) {
840 #if 0 // XXX old animation system
841                                         BKE_nlastrip_copy(s(&base->object->nlastrips, &ob->nlastrips);
842 #endif // XXX old animation system
843                                 }
844                                 else if (event == 27) {   /* autosmooth */
845                                         if (base->object->type == OB_MESH) {
846                                                 Mesh *me = ob->data;
847                                                 Mesh *cme = base->object->data;
848                                                 cme->smoothresh = me->smoothresh;
849                                                 if (me->flag & ME_AUTOSMOOTH)
850                                                         cme->flag |= ME_AUTOSMOOTH;
851                                                 else
852                                                         cme->flag &= ~ME_AUTOSMOOTH;
853                                         }
854                                 }
855                                 else if (event == 28) { /* UV orco */
856                                         if (ELEM(base->object->type, OB_CURVE, OB_SURF)) {
857                                                 cu = ob->data;
858                                                 cu1 = base->object->data;
859                                                 
860                                                 if (cu->flag & CU_UV_ORCO)
861                                                         cu1->flag |= CU_UV_ORCO;
862                                                 else
863                                                         cu1->flag &= ~CU_UV_ORCO;
864                                         }
865                                 }
866                                 else if (event == 29) { /* protected bits */
867                                         base->object->protectflag = ob->protectflag;
868                                 }
869                                 else if (event == 30) { /* index object */
870                                         base->object->index = ob->index;
871                                 }
872                                 else if (event == 31) { /* object color */
873                                         copy_v4_v4(base->object->col, ob->col);
874                                 }
875                         }
876                 }
877         }
878         
879         if (do_depgraph_update)
880                 DEG_relations_tag_update(bmain);
881 }
882
883 static void UNUSED_FUNCTION(copy_attr_menu) (Main *bmain, Scene *scene, ViewLayer *view_layer, Object *obedit)
884 {
885         Object *ob;
886         short event;
887         char str[512];
888         
889         if (!(ob = OBACT(view_layer))) return;
890         
891         if (obedit) {
892 /*              if (ob->type == OB_MESH) */
893 /* XXX                  mesh_copy_menu(); */
894                 return;
895         }
896         
897         /* Object Mode */
898         
899         /* If you change this menu, don't forget to update the menu in header_view3d.c
900          * view3d_edit_object_copyattrmenu() and in toolbox.c
901          */
902         
903         strcpy(str,
904                "Copy Attributes %t|Location %x1|Rotation %x2|Size %x3|Draw Options %x4|"
905                "Time Offset %x5|Dupli %x6|Object Color %x31|%l|Mass %x7|Damping %x8|All Physical Attributes %x11|Properties %x9|"
906                "Logic Bricks %x10|Protected Transform %x29|%l");
907         
908         strcat(str, "|Object Constraints %x22");
909         strcat(str, "|NLA Strips %x26");
910         
911 /* XXX  if (OB_TYPE_SUPPORT_MATERIAL(ob->type)) { */
912 /*              strcat(str, "|Texture Space %x17"); */
913 /*      } */
914         
915         if (ob->type == OB_FONT) strcat(str, "|Font Settings %x18|Bevel Settings %x19");
916         if (ob->type == OB_CURVE) strcat(str, "|Bevel Settings %x19|UV Orco %x28");
917         
918         if ((ob->type == OB_FONT) || (ob->type == OB_CURVE)) {
919                 strcat(str, "|Curve Resolution %x25");
920         }
921
922         if (ob->type == OB_MESH) {
923                 strcat(str, "|Subsurf Settings %x21|AutoSmooth %x27");
924         }
925
926         if (ob->soft) strcat(str, "|Soft Body Settings %x23");
927         
928         strcat(str, "|Pass Index %x30");
929         
930         if (ob->type == OB_MESH || ob->type == OB_CURVE || ob->type == OB_LATTICE || ob->type == OB_SURF) {
931                 strcat(str, "|Modifiers ... %x24");
932         }
933
934         event = pupmenu(str);
935         if (event <= 0) return;
936         
937         copy_attr(bmain, scene, view_layer, event);
938 }
939
940 /* ******************* force field toggle operator ***************** */
941
942 void ED_object_check_force_modifiers(Main *bmain, Scene *scene, Object *object)
943 {
944         PartDeflect *pd = object->pd;
945         ModifierData *md = modifiers_findByType(object, eModifierType_Surface);
946
947         /* add/remove modifier as needed */
948         if (!md) {
949                 if (pd && (pd->shape == PFIELD_SHAPE_SURFACE) && !ELEM(pd->forcefield, 0, PFIELD_GUIDE, PFIELD_TEXTURE)) {
950                         if (ELEM(object->type, OB_MESH, OB_SURF, OB_FONT, OB_CURVE)) {
951                                 ED_object_modifier_add(NULL, bmain, scene, object, NULL, eModifierType_Surface);
952                         }
953                 }
954         }
955         else {
956                 if (!pd || (pd->shape != PFIELD_SHAPE_SURFACE) || ELEM(pd->forcefield, 0, PFIELD_GUIDE, PFIELD_TEXTURE)) {
957                         ED_object_modifier_remove(NULL, bmain, object, md);
958                 }
959         }
960 }
961
962 static int forcefield_toggle_exec(bContext *C, wmOperator *UNUSED(op))
963 {
964         Object *ob = CTX_data_active_object(C);
965
966         if (ob->pd == NULL)
967                 ob->pd = object_add_collision_fields(PFIELD_FORCE);
968         else if (ob->pd->forcefield == 0)
969                 ob->pd->forcefield = PFIELD_FORCE;
970         else
971                 ob->pd->forcefield = 0;
972
973         ED_object_check_force_modifiers(CTX_data_main(C), CTX_data_scene(C), ob);
974         WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, ob);
975         WM_event_add_notifier(C, NC_OBJECT | ND_MODIFIER, ob);
976
977         return OPERATOR_FINISHED;
978 }
979
980 void OBJECT_OT_forcefield_toggle(wmOperatorType *ot)
981 {
982         
983         /* identifiers */
984         ot->name = "Toggle Force Field";
985         ot->description = "Toggle object's force field";
986         ot->idname = "OBJECT_OT_forcefield_toggle";
987         
988         /* api callbacks */
989         ot->exec = forcefield_toggle_exec;
990         ot->poll = ED_operator_object_active_editable;
991         
992         /* flags */
993         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
994 }
995
996 /* ********************************************** */
997 /* Motion Paths */
998
999 /* For the objects with animation: update paths for those that have got them
1000  * This should selectively update paths that exist...
1001  *
1002  * To be called from various tools that do incremental updates 
1003  */
1004 void ED_objects_recalculate_paths(bContext *C, Scene *scene)
1005 {
1006         struct Main *bmain = CTX_data_main(C);
1007         Depsgraph *depsgraph = CTX_data_depsgraph(C);
1008         ListBase targets = {NULL, NULL};
1009         
1010         /* loop over objects in scene */
1011         CTX_DATA_BEGIN(C, Object *, ob, selected_editable_objects)
1012         {
1013                 /* set flag to force recalc, then grab path(s) from object */
1014                 ob->avs.recalc |= ANIMVIZ_RECALC_PATHS;
1015                 animviz_get_object_motionpaths(ob, &targets);
1016         }
1017         CTX_DATA_END;
1018         
1019         /* recalculate paths, then free */
1020         animviz_calc_motionpaths(depsgraph, bmain, scene, &targets);
1021         BLI_freelistN(&targets);
1022 }
1023
1024
1025 /* show popup to determine settings */
1026 static int object_calculate_paths_invoke(bContext *C, wmOperator *op, const wmEvent *UNUSED(event))
1027 {
1028         Object *ob = CTX_data_active_object(C);
1029         
1030         if (ob == NULL)
1031                 return OPERATOR_CANCELLED;
1032         
1033         /* set default settings from existing/stored settings */
1034         {
1035                 bAnimVizSettings *avs = &ob->avs;
1036                 
1037                 RNA_int_set(op->ptr, "start_frame", avs->path_sf);
1038                 RNA_int_set(op->ptr, "end_frame", avs->path_ef);
1039         }
1040         
1041         /* show popup dialog to allow editing of range... */
1042         /* FIXME: hardcoded dimensions here are just arbitrary */
1043         return WM_operator_props_dialog_popup(C, op, 10 * UI_UNIT_X, 10 * UI_UNIT_Y);
1044 }
1045
1046 /* Calculate/recalculate whole paths (avs.path_sf to avs.path_ef) */
1047 static int object_calculate_paths_exec(bContext *C, wmOperator *op)
1048 {
1049         Scene *scene = CTX_data_scene(C);
1050         int start = RNA_int_get(op->ptr, "start_frame");
1051         int end = RNA_int_get(op->ptr, "end_frame");
1052         
1053         /* set up path data for bones being calculated */
1054         CTX_DATA_BEGIN(C, Object *, ob, selected_editable_objects)
1055         {
1056                 bAnimVizSettings *avs = &ob->avs;
1057                 
1058                 /* grab baking settings from operator settings */
1059                 avs->path_sf = start;
1060                 avs->path_ef = end;
1061                 
1062                 /* verify that the selected object has the appropriate settings */
1063                 animviz_verify_motionpaths(op->reports, scene, ob, NULL);
1064         }
1065         CTX_DATA_END;
1066         
1067         /* calculate the paths for objects that have them (and are tagged to get refreshed) */
1068         ED_objects_recalculate_paths(C, scene);
1069         
1070         /* notifiers for updates */
1071         WM_event_add_notifier(C, NC_OBJECT | ND_TRANSFORM, NULL);
1072         
1073         return OPERATOR_FINISHED; 
1074 }
1075
1076 void OBJECT_OT_paths_calculate(wmOperatorType *ot)
1077 {
1078         /* identifiers */
1079         ot->name = "Calculate Object Paths";
1080         ot->idname = "OBJECT_OT_paths_calculate";
1081         ot->description = "Calculate motion paths for the selected objects";
1082         
1083         /* api callbacks */
1084         ot->invoke = object_calculate_paths_invoke;
1085         ot->exec = object_calculate_paths_exec;
1086         ot->poll = ED_operator_object_active_editable;
1087         
1088         /* flags */
1089         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1090         
1091         /* properties */
1092         RNA_def_int(ot->srna, "start_frame", 1, MINAFRAME, MAXFRAME, "Start", 
1093                     "First frame to calculate object paths on", MINFRAME, MAXFRAME / 2.0);
1094         RNA_def_int(ot->srna, "end_frame", 250, MINAFRAME, MAXFRAME, "End", 
1095                     "Last frame to calculate object paths on", MINFRAME, MAXFRAME / 2.0);
1096 }
1097
1098 /* --------- */
1099
1100 static int object_update_paths_poll(bContext *C)
1101 {
1102         if (ED_operator_object_active_editable(C)) {
1103                 Object *ob = ED_object_active_context(C);
1104                 return (ob->avs.path_bakeflag & MOTIONPATH_BAKE_HAS_PATHS) != 0;
1105         }
1106         
1107         return false;
1108 }
1109
1110 static int object_update_paths_exec(bContext *C, wmOperator *UNUSED(op))
1111 {
1112         Scene *scene = CTX_data_scene(C);
1113         
1114         if (scene == NULL)
1115                 return OPERATOR_CANCELLED;
1116                 
1117         /* calculate the paths for objects that have them (and are tagged to get refreshed) */
1118         ED_objects_recalculate_paths(C, scene);
1119         
1120         /* notifiers for updates */
1121         WM_event_add_notifier(C, NC_OBJECT | ND_TRANSFORM, NULL);
1122         
1123         return OPERATOR_FINISHED;
1124 }
1125
1126 void OBJECT_OT_paths_update(wmOperatorType *ot)
1127 {
1128         /* identifiers */
1129         ot->name = "Update Object Paths";
1130         ot->idname = "OBJECT_OT_paths_update";
1131         ot->description = "Recalculate paths for selected objects";
1132         
1133         /* api callbakcs */
1134         ot->exec = object_update_paths_exec;
1135         ot->poll = object_update_paths_poll;
1136         
1137         /* flags */
1138         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1139 }
1140
1141 /* --------- */
1142
1143 /* Helper for ED_objects_clear_paths() */
1144 static void object_clear_mpath(Object *ob)
1145 {
1146         if (ob->mpath) {
1147                 animviz_free_motionpath(ob->mpath);
1148                 ob->mpath = NULL;
1149                 ob->avs.path_bakeflag &= ~MOTIONPATH_BAKE_HAS_PATHS;
1150         }
1151 }
1152
1153 /* Clear motion paths for all objects */
1154 void ED_objects_clear_paths(bContext *C, bool only_selected)
1155 {
1156         if (only_selected) {
1157                 /* loop over all selected + sedtiable objects in scene */
1158                 CTX_DATA_BEGIN(C, Object *, ob, selected_editable_objects)
1159                 {
1160                         object_clear_mpath(ob);
1161                 }
1162                 CTX_DATA_END;
1163         }
1164         else {
1165                 /* loop over all edtiable objects in scene */
1166                 CTX_DATA_BEGIN(C, Object *, ob, editable_objects)
1167                 {
1168                         object_clear_mpath(ob);
1169                 }
1170                 CTX_DATA_END;
1171         }
1172 }
1173
1174 /* operator callback for this */
1175 static int object_clear_paths_exec(bContext *C, wmOperator *op)
1176 {
1177         bool only_selected = RNA_boolean_get(op->ptr, "only_selected");
1178         
1179         /* use the backend function for this */
1180         ED_objects_clear_paths(C, only_selected);
1181         
1182         /* notifiers for updates */
1183         WM_event_add_notifier(C, NC_OBJECT | ND_TRANSFORM, NULL);
1184         
1185         return OPERATOR_FINISHED; 
1186 }
1187
1188 /* operator callback/wrapper */
1189 static int object_clear_paths_invoke(bContext *C, wmOperator *op, const wmEvent *evt)
1190 {
1191         if ((evt->shift) && !RNA_struct_property_is_set(op->ptr, "only_selected")) {
1192                 RNA_boolean_set(op->ptr, "only_selected", true);
1193         }
1194         return object_clear_paths_exec(C, op);
1195 }
1196
1197 void OBJECT_OT_paths_clear(wmOperatorType *ot)
1198 {
1199         /* identifiers */
1200         ot->name = "Clear Object Paths";
1201         ot->idname = "OBJECT_OT_paths_clear";
1202         ot->description = "Clear path caches for all objects, hold Shift key for selected objects only";
1203         
1204         /* api callbacks */
1205         ot->invoke = object_clear_paths_invoke;
1206         ot->exec = object_clear_paths_exec;
1207         ot->poll = ED_operator_object_active_editable;
1208         
1209         /* flags */
1210         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1211         
1212         /* properties */
1213         ot->prop = RNA_def_boolean(ot->srna, "only_selected", false, "Only Selected",
1214                                    "Only clear paths from selected objects");
1215         RNA_def_property_flag(ot->prop, PROP_SKIP_SAVE);
1216 }
1217
1218
1219 /********************** Smooth/Flat *********************/
1220
1221 static int shade_smooth_exec(bContext *C, wmOperator *op)
1222 {
1223         ID *data;
1224         Curve *cu;
1225         Nurb *nu;
1226         int clear = (STREQ(op->idname, "OBJECT_OT_shade_flat"));
1227         bool done = false, linked_data = false;
1228
1229         CTX_DATA_BEGIN(C, Object *, ob, selected_editable_objects)
1230         {
1231                 data = ob->data;
1232
1233                 if (data && ID_IS_LINKED(data)) {
1234                         linked_data = true;
1235                         continue;
1236                 }
1237
1238                 if (ob->type == OB_MESH) {
1239                         BKE_mesh_smooth_flag_set(ob, !clear);
1240
1241                         BKE_mesh_batch_cache_dirty(ob->data, BKE_MESH_BATCH_DIRTY_ALL);
1242                         DEG_id_tag_update(&ob->id, OB_RECALC_DATA);
1243                         WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, ob);
1244
1245                         done = true;
1246                 }
1247                 else if (ELEM(ob->type, OB_SURF, OB_CURVE)) {
1248                         cu = ob->data;
1249
1250                         for (nu = cu->nurb.first; nu; nu = nu->next) {
1251                                 if (!clear) nu->flag |= ME_SMOOTH;
1252                                 else nu->flag &= ~ME_SMOOTH;
1253                         }
1254
1255                         DEG_id_tag_update(&ob->id, OB_RECALC_DATA);
1256                         WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, ob);
1257
1258                         done = true;
1259                 }
1260         }
1261         CTX_DATA_END;
1262
1263         if (linked_data)
1264                 BKE_report(op->reports, RPT_WARNING, "Can't edit linked mesh or curve data");
1265
1266         return (done) ? OPERATOR_FINISHED : OPERATOR_CANCELLED;
1267 }
1268
1269 static int shade_poll(bContext *C)
1270 {
1271         return (CTX_data_edit_object(C) == NULL);
1272 }
1273
1274 void OBJECT_OT_shade_flat(wmOperatorType *ot)
1275 {
1276         /* identifiers */
1277         ot->name = "Shade Flat";
1278         ot->description = "Render and display faces uniform, using Face Normals";
1279         ot->idname = "OBJECT_OT_shade_flat";
1280         
1281         /* api callbacks */
1282         ot->poll = shade_poll;
1283         ot->exec = shade_smooth_exec;
1284
1285         /* flags */
1286         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1287 }
1288
1289 void OBJECT_OT_shade_smooth(wmOperatorType *ot)
1290 {
1291         /* identifiers */
1292         ot->name = "Shade Smooth";
1293         ot->description = "Render and display faces smooth, using interpolated Vertex Normals";
1294         ot->idname = "OBJECT_OT_shade_smooth";
1295         
1296         /* api callbacks */
1297         ot->poll = shade_poll;
1298         ot->exec = shade_smooth_exec;
1299         
1300         /* flags */
1301         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1302 }
1303
1304 /* ********************** */
1305
1306 static void UNUSED_FUNCTION(image_aspect) (Scene *scene, ViewLayer *view_layer, Object *obedit)
1307 {
1308         /* all selected objects with an image map: scale in image aspect */
1309         Base *base;
1310         Object *ob;
1311         Material *ma;
1312         Tex *tex;
1313         float x, y, space;
1314         int a, b, done;
1315         
1316         if (obedit) return;
1317         if (ID_IS_LINKED(scene)) return;
1318         
1319         for (base = FIRSTBASE(view_layer); base; base = base->next) {
1320                 if (TESTBASELIB(base)) {
1321                         ob = base->object;
1322                         done = false;
1323                         
1324                         for (a = 1; a <= ob->totcol; a++) {
1325                                 ma = give_current_material(ob, a);
1326                                 if (ma) {
1327                                         for (b = 0; b < MAX_MTEX; b++) {
1328                                                 if (ma->mtex[b] && ma->mtex[b]->tex) {
1329                                                         tex = ma->mtex[b]->tex;
1330                                                         if (tex->type == TEX_IMAGE && tex->ima) {
1331                                                                 ImBuf *ibuf = BKE_image_acquire_ibuf(tex->ima, NULL, NULL);
1332                                                                 
1333                                                                 /* texturespace */
1334                                                                 space = 1.0;
1335                                                                 if (ob->type == OB_MESH) {
1336                                                                         float size[3];
1337                                                                         BKE_mesh_texspace_get(ob->data, NULL, NULL, size);
1338                                                                         space = size[0] / size[1];
1339                                                                 }
1340                                                                 else if (ELEM(ob->type, OB_CURVE, OB_FONT, OB_SURF)) {
1341                                                                         float size[3];
1342                                                                         BKE_curve_texspace_get(ob->data, NULL, NULL, size);
1343                                                                         space = size[0] / size[1];
1344                                                                 }
1345                                                         
1346                                                                 x = ibuf->x / space;
1347                                                                 y = ibuf->y;
1348                                                                 
1349                                                                 if (x > y) ob->size[0] = ob->size[1] * x / y;
1350                                                                 else ob->size[1] = ob->size[0] * y / x;
1351                                                                 
1352                                                                 done = true;
1353                                                                 DEG_id_tag_update(&ob->id, OB_RECALC_OB);
1354
1355                                                                 BKE_image_release_ibuf(tex->ima, ibuf, NULL);
1356                                                         }
1357                                                 }
1358                                                 if (done) break;
1359                                         }
1360                                 }
1361                                 if (done) break;
1362                         }
1363                 }
1364         }
1365         
1366 }
1367
1368 static const EnumPropertyItem *object_mode_set_itemsf(
1369         bContext *C, PointerRNA *UNUSED(ptr), PropertyRNA *UNUSED(prop), bool *r_free)
1370 {
1371         const EnumPropertyItem *input = rna_enum_object_mode_items;
1372         EnumPropertyItem *item = NULL;
1373         Object *ob;
1374         bGPdata *gpd;
1375         int totitem = 0;
1376
1377         if (!C) /* needed for docs */
1378                 return rna_enum_object_mode_items;
1379
1380         ob = CTX_data_active_object(C);
1381         if (ob) {
1382                 const bool use_mode_particle_edit = (BLI_listbase_is_empty(&ob->particlesystem) == false) ||
1383                                                     (ob->soft != NULL) ||
1384                                                     (modifiers_findByType(ob, eModifierType_Cloth) != NULL);
1385                 while (input->identifier) {
1386                         if ((input->value == OB_MODE_EDIT && OB_TYPE_SUPPORT_EDITMODE(ob->type)) ||
1387                             (input->value == OB_MODE_POSE && (ob->type == OB_ARMATURE)) ||
1388                             (input->value == OB_MODE_PARTICLE_EDIT && use_mode_particle_edit) ||
1389                             (ELEM(input->value, OB_MODE_SCULPT, OB_MODE_VERTEX_PAINT,
1390                                    OB_MODE_WEIGHT_PAINT, OB_MODE_TEXTURE_PAINT) && (ob->type == OB_MESH)) ||
1391                             (input->value == OB_MODE_OBJECT))
1392                         {
1393                                 RNA_enum_item_add(&item, &totitem, input);
1394                         }
1395                         input++;
1396                 }
1397         }
1398         else {
1399                 /* We need at least this one! */
1400                 RNA_enum_items_add_value(&item, &totitem, input, OB_MODE_OBJECT);
1401         }
1402         
1403         /* On top of all the rest, GPencil Stroke Edit Mode
1404          * is available if there's a valid gp datablock...
1405          */
1406         gpd = CTX_data_gpencil_data(C);
1407         if (gpd) {
1408                 RNA_enum_items_add_value(&item, &totitem, rna_enum_object_mode_items, OB_MODE_GPENCIL);
1409         }
1410
1411         RNA_enum_item_end(&item, &totitem);
1412
1413         *r_free = true;
1414
1415         return item;
1416 }
1417
1418 static int object_mode_set_poll(bContext *C)
1419 {
1420         /* Since Grease Pencil editmode is also handled here,
1421          * we have a special exception for allowing this operator
1422          * to still work in that case when there's no active object
1423          * so that users can exit editmode this way as per normal.
1424          */
1425         if (ED_operator_object_active_editable(C))
1426                 return true;
1427         else
1428                 return (CTX_data_gpencil_data(C) != NULL);
1429 }
1430
1431 static int object_mode_set_exec(bContext *C, wmOperator *op)
1432 {
1433         Object *ob = CTX_data_active_object(C);
1434         bGPdata *gpd = CTX_data_gpencil_data(C);
1435         eObjectMode mode = RNA_enum_get(op->ptr, "mode");
1436         eObjectMode restore_mode = (ob) ? ob->mode : OB_MODE_OBJECT;
1437         const bool toggle = RNA_boolean_get(op->ptr, "toggle");
1438         
1439         if (gpd) {
1440                 /* GP Mode is not bound to a specific object. Therefore,
1441                  * we don't want it to be actually saved on any objects,
1442                  * as weirdness can happen if you select other objects,
1443                  * or load old files.
1444                  *
1445                  * Instead, we use the following 2 rules to ensure that
1446                  * the mode selector works as expected:
1447                  *  1) If there's no object, we want to enter editmode.
1448                  *     (i.e. with no object, we're in object mode)
1449                  *  2) Otherwise, exit stroke editmode, so that we can
1450                  *     enter another mode...
1451                  */
1452                 if (!ob || (gpd->flag & GP_DATA_STROKE_EDITMODE)) {
1453                         WM_operator_name_call(C, "GPENCIL_OT_editmode_toggle", WM_OP_EXEC_REGION_WIN, NULL);
1454                 }
1455         }
1456         
1457         if (!ob || !ED_object_mode_compat_test(ob, mode))
1458                 return OPERATOR_PASS_THROUGH;
1459
1460         if (ob->mode != mode) {
1461                 /* we should be able to remove this call, each operator calls  */
1462                 ED_object_mode_compat_set(C, ob, mode, op->reports);
1463         }
1464
1465         /* Exit current mode if it's not the mode we're setting */
1466         if (mode != OB_MODE_OBJECT && (ob->mode != mode || toggle)) {
1467                 /* Enter new mode */
1468                 ED_object_mode_toggle(C, mode);
1469         }
1470
1471         if (toggle) {
1472                 /* Special case for Object mode! */
1473                 if (mode == OB_MODE_OBJECT && restore_mode == OB_MODE_OBJECT && ob->restore_mode != OB_MODE_OBJECT) {
1474                         ED_object_mode_toggle(C, ob->restore_mode);
1475                 }
1476                 else if (ob->mode == mode) {
1477                         /* For toggling, store old mode so we know what to go back to */
1478                         ob->restore_mode = restore_mode;
1479                 }
1480                 else if (ob->restore_mode != OB_MODE_OBJECT && ob->restore_mode != mode) {
1481                         ED_object_mode_toggle(C, ob->restore_mode);
1482                 }
1483         }
1484
1485         return OPERATOR_FINISHED;
1486 }
1487
1488 void OBJECT_OT_mode_set(wmOperatorType *ot)
1489 {
1490         PropertyRNA *prop;
1491         
1492         /* identifiers */
1493         ot->name = "Set Object Mode";
1494         ot->description = "Sets the object interaction mode";
1495         ot->idname = "OBJECT_OT_mode_set";
1496         
1497         /* api callbacks */
1498         ot->exec = object_mode_set_exec;
1499         
1500         ot->poll = object_mode_set_poll; //ED_operator_object_active_editable;
1501         
1502         /* flags */
1503         ot->flag = 0; /* no register/undo here, leave it to operators being called */
1504         
1505         ot->prop = RNA_def_enum(ot->srna, "mode", rna_enum_object_mode_items, OB_MODE_OBJECT, "Mode", "");
1506         RNA_def_enum_funcs(ot->prop, object_mode_set_itemsf);
1507         RNA_def_property_flag(ot->prop, PROP_SKIP_SAVE);
1508
1509         prop = RNA_def_boolean(ot->srna, "toggle", 0, "Toggle", "");
1510         RNA_def_property_flag(prop, PROP_SKIP_SAVE);
1511 }
1512
1513 bool ED_object_editmode_calc_active_center(Object *obedit, const bool select_only, float r_center[3])
1514 {
1515         switch (obedit->type) {
1516                 case OB_MESH:
1517                 {
1518                         BMEditMesh *em = BKE_editmesh_from_object(obedit);
1519                         BMEditSelection ese;
1520
1521                         if (BM_select_history_active_get(em->bm, &ese)) {
1522                                 BM_editselection_center(&ese, r_center);
1523                                 return true;
1524                         }
1525                         break;
1526                 }
1527                 case OB_ARMATURE:
1528                 {
1529                         bArmature *arm = obedit->data;
1530                         EditBone *ebo = arm->act_edbone;
1531
1532                         if (ebo && (!select_only || (ebo->flag & (BONE_SELECTED | BONE_ROOTSEL)))) {
1533                                 copy_v3_v3(r_center, ebo->head);
1534                                 return true;
1535                         }
1536
1537                         break;
1538                 }
1539                 case OB_CURVE:
1540                 case OB_SURF:
1541                 {
1542                         Curve *cu = obedit->data;
1543
1544                         if (ED_curve_active_center(cu, r_center)) {
1545                                 return true;
1546                         }
1547                         break;
1548                 }
1549                 case OB_MBALL:
1550                 {
1551                         MetaBall *mb = obedit->data;
1552                         MetaElem *ml_act = mb->lastelem;
1553
1554                         if (ml_act && (!select_only || (ml_act->flag & SELECT))) {
1555                                 copy_v3_v3(r_center, &ml_act->x);
1556                                 return true;
1557                         }
1558                         break;
1559                 }
1560                 case OB_LATTICE:
1561                 {
1562                         BPoint *actbp = BKE_lattice_active_point_get(obedit->data);
1563
1564                         if (actbp) {
1565                                 copy_v3_v3(r_center, actbp->vec);
1566                                 return true;
1567                         }
1568                         break;
1569                 }
1570         }
1571
1572         return false;
1573 }
1574
1575 #define COLLECTION_INVALID_INDEX -1
1576
1577 static int move_to_collection_exec(bContext *C, wmOperator *op)
1578 {
1579         Scene *scene = CTX_data_scene(C);
1580         PropertyRNA *prop = RNA_struct_find_property(op->ptr, "collection_index");
1581         const bool is_add = RNA_boolean_get(op->ptr, "is_add");
1582         const bool is_new = RNA_boolean_get(op->ptr, "is_new");
1583         SceneCollection *scene_collection;
1584
1585         if (!RNA_property_is_set(op->ptr, prop)) {
1586                 BKE_report(op->reports, RPT_ERROR, "No collection selected");
1587                 return OPERATOR_CANCELLED;
1588         }
1589
1590         int collection_index = RNA_property_int_get(op->ptr, prop);
1591         scene_collection = BKE_collection_from_index(CTX_data_scene(C), collection_index);
1592         if (scene_collection == NULL) {
1593                 BKE_report(op->reports, RPT_ERROR, "Unexpected error, collection not found");
1594                 return OPERATOR_CANCELLED;
1595         }
1596
1597         Object *single_object = NULL;
1598         CTX_DATA_BEGIN (C, Object *, ob, selected_objects)
1599         {
1600                 if (single_object != NULL) {
1601                         single_object = NULL;
1602                         break;
1603                 }
1604                 else {
1605                         single_object = ob;
1606                 }
1607         }
1608         CTX_DATA_END;
1609
1610         if (is_new) {
1611                 char new_collection_name[MAX_NAME];
1612                 RNA_string_get(op->ptr, "new_collection_name", new_collection_name);
1613                 scene_collection = BKE_collection_add(&scene->id, scene_collection, COLLECTION_TYPE_NONE, new_collection_name);
1614         }
1615
1616         if ((single_object != NULL) &&
1617             is_add &&
1618             BLI_findptr(&scene_collection->objects, single_object, offsetof(LinkData, data)))
1619         {
1620                 BKE_reportf(op->reports, RPT_ERROR, "%s already in %s", single_object->id.name + 2, scene_collection->name);
1621                 return OPERATOR_CANCELLED;
1622         }
1623
1624         CTX_DATA_BEGIN (C, Object *, ob, selected_objects)
1625         {
1626                 if (!is_add) {
1627                         BKE_collection_object_move(&scene->id, scene_collection, NULL, ob);
1628                 }
1629                 else {
1630                         BKE_collection_object_add(&scene->id, scene_collection, ob);
1631                 }
1632         }
1633         CTX_DATA_END;
1634
1635         BKE_reportf(op->reports,
1636                     RPT_INFO,
1637                     "%s %s to %s",
1638                     (single_object != NULL) ? single_object->id.name + 2 : "Objects",
1639                     is_add ? "added" : "moved",
1640                     scene_collection->name);
1641
1642         DEG_relations_tag_update(CTX_data_main(C));
1643         DEG_id_tag_update(&scene->id, 0);
1644
1645         WM_event_add_notifier(C, NC_SCENE | ND_LAYER, scene);
1646         WM_event_add_notifier(C, NC_SCENE | ND_OB_ACTIVE, scene);
1647         WM_event_add_notifier(C, NC_SCENE | ND_LAYER_CONTENT, scene);
1648
1649         return OPERATOR_FINISHED;
1650 }
1651
1652 typedef struct MoveToCollectionData {
1653         struct MoveToCollectionData *next, *prev;
1654         int index;
1655         struct SceneCollection *collection;
1656         struct ListBase submenus;
1657         PointerRNA ptr;
1658         struct wmOperatorType *ot;
1659 } MoveToCollectionData;
1660
1661 static int move_to_collection_menus_create(wmOperator *op, MoveToCollectionData *menu)
1662 {
1663         int index = menu->index;
1664         for (SceneCollection *scene_collection = menu->collection->scene_collections.first;
1665              scene_collection != NULL;
1666              scene_collection = scene_collection->next)
1667         {
1668                 MoveToCollectionData *submenu = MEM_callocN(sizeof(MoveToCollectionData),
1669                                                             "MoveToCollectionData submenu - expected memleak");
1670                 BLI_addtail(&menu->submenus, submenu);
1671                 submenu->collection = scene_collection;
1672                 submenu->index = ++index;
1673                 index = move_to_collection_menus_create(op, submenu);
1674                 submenu->ot = op->type;
1675         }
1676         return index;
1677 }
1678
1679 static void move_to_collection_menus_free_recursive(MoveToCollectionData *menu)
1680 {
1681         for (MoveToCollectionData *submenu = menu->submenus.first;
1682              submenu != NULL;
1683              submenu = submenu->next)
1684         {
1685                 move_to_collection_menus_free_recursive(submenu);
1686         }
1687         BLI_freelistN(&menu->submenus);
1688 }
1689
1690 static void move_to_collection_menus_free(MoveToCollectionData **menu)
1691 {
1692         if (*menu == NULL) {
1693                 return;
1694         }
1695
1696         move_to_collection_menus_free_recursive(*menu);
1697         MEM_freeN(*menu);
1698         *menu = NULL;
1699 }
1700
1701 static void move_to_collection_menu_create(bContext *UNUSED(C), uiLayout *layout, void *menu_v)
1702 {
1703         MoveToCollectionData *menu = menu_v;
1704
1705         uiItemIntO(layout,
1706                    menu->collection->name,
1707                    ICON_NONE,
1708                    "OBJECT_OT_move_to_collection",
1709                    "collection_index",
1710                    menu->index);
1711         uiItemS(layout);
1712
1713         for (MoveToCollectionData *submenu = menu->submenus.first;
1714              submenu != NULL;
1715              submenu = submenu->next)
1716         {
1717                 move_to_collection_menus_items(layout, submenu);
1718         }
1719
1720         uiItemS(layout);
1721
1722         WM_operator_properties_create_ptr(&menu->ptr, menu->ot);
1723         RNA_int_set(&menu->ptr, "collection_index", menu->index);
1724         RNA_boolean_set(&menu->ptr, "is_new", true);
1725
1726         uiItemFullO_ptr(layout,
1727                         menu->ot,
1728                         "New Collection",
1729                         ICON_ZOOMIN,
1730                         menu->ptr.data,
1731                         /* We use invoke here so we can read ctrl from event. */
1732                         WM_OP_INVOKE_DEFAULT,
1733                         0,
1734                         NULL);
1735 }
1736
1737 static void move_to_collection_menus_items(uiLayout *layout, MoveToCollectionData *menu)
1738 {
1739         if (BLI_listbase_is_empty(&menu->submenus)) {
1740                 uiItemIntO(layout,
1741                            menu->collection->name,
1742                            ICON_NONE,
1743                            "OBJECT_OT_move_to_collection",
1744                            "collection_index",
1745                            menu->index);
1746         }
1747         else {
1748                 uiItemMenuF(layout,
1749                             menu->collection->name,
1750                             ICON_NONE,
1751                             move_to_collection_menu_create,
1752                             menu);
1753         }
1754 }
1755
1756 /* This is allocated statically because we need this available for the menus creation callback. */
1757 static MoveToCollectionData *master_collection_menu = NULL;
1758
1759 static int move_to_collection_invoke(bContext *C, wmOperator *op, const wmEvent *event)
1760 {
1761         /* Reset the menus data for the current master collection, and free previously allocated data. */
1762         move_to_collection_menus_free(&master_collection_menu);
1763
1764         PropertyRNA *prop;
1765         prop = RNA_struct_find_property(op->ptr, "collection_index");
1766         if (RNA_property_is_set(op->ptr, prop)) {
1767                 int collection_index = RNA_property_int_get(op->ptr, prop);
1768                 RNA_boolean_set(op->ptr, "is_add", event->ctrl);
1769
1770                 if (RNA_boolean_get(op->ptr, "is_new")) {
1771                         prop = RNA_struct_find_property(op->ptr, "new_collection_name");
1772                         if (!RNA_property_is_set(op->ptr, prop)) {
1773                                 char name[MAX_NAME];
1774                                 SceneCollection *scene_collection;
1775
1776                                 scene_collection = BKE_collection_from_index(CTX_data_scene(C), collection_index);
1777                                 BKE_collection_new_name_get(&CTX_data_scene(C)->id, scene_collection, name);
1778
1779                                 RNA_property_string_set(op->ptr, prop, name);
1780                                 return WM_operator_props_dialog_popup(C, op, 10 * UI_UNIT_X, 5 * UI_UNIT_Y);
1781                         }
1782                 }
1783                 return move_to_collection_exec(C, op);
1784         }
1785
1786         SceneCollection *master_collection = BKE_collection_master(&CTX_data_scene(C)->id);
1787
1788         /* We need the data to be allocated so it's available during menu drawing.
1789          * Technically we could use wmOperator->customdata. However there is no free callback
1790          * called to an operator that exit with OPERATOR_INTERFACE to launch a menu.
1791          *
1792          * So we are left with a memory that will necessarily leak. It's a small leak though.*/
1793         if (master_collection_menu == NULL) {
1794                 master_collection_menu = MEM_callocN(sizeof(MoveToCollectionData),
1795                                                      "MoveToCollectionData menu - expected eventual memleak");
1796         }
1797
1798         master_collection_menu->collection = master_collection;
1799         master_collection_menu->ot = op->type;
1800         move_to_collection_menus_create(op, master_collection_menu);
1801
1802         uiPopupMenu *pup;
1803         uiLayout *layout;
1804
1805         /* Build the menus. */
1806         pup = UI_popup_menu_begin(C, IFACE_("Move to Collection"), ICON_NONE);
1807         layout = UI_popup_menu_layout(pup);
1808
1809         /* We use invoke here so we can read ctrl from event. */
1810         uiLayoutSetOperatorContext(layout, WM_OP_INVOKE_DEFAULT);
1811
1812         move_to_collection_menu_create(C, layout, master_collection_menu);
1813
1814         UI_popup_menu_end(C, pup);
1815
1816         return OPERATOR_INTERFACE;
1817 }
1818
1819 void OBJECT_OT_move_to_collection(wmOperatorType *ot)
1820 {
1821         PropertyRNA *prop;
1822
1823         /* identifiers */
1824         ot->name = "Move to Collection";
1825         ot->description = "Move to a collection only (Ctrl to add)";
1826         ot->idname = "OBJECT_OT_move_to_collection";
1827
1828         /* api callbacks */
1829         ot->exec = move_to_collection_exec;
1830         ot->invoke = move_to_collection_invoke;
1831         ot->poll = ED_operator_object_active_editable;
1832
1833         /* flags */
1834         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1835
1836         prop = RNA_def_int(ot->srna, "collection_index", COLLECTION_INVALID_INDEX, COLLECTION_INVALID_INDEX, INT_MAX,
1837                            "Collection Index", "Index of the collection to move to", 0, INT_MAX);
1838         RNA_def_property_flag(prop, PROP_SKIP_SAVE | PROP_HIDDEN);
1839         prop = RNA_def_boolean(ot->srna, "is_add", false, "Add", "Keep object in original collections as well");
1840         RNA_def_property_flag(prop, PROP_SKIP_SAVE | PROP_HIDDEN);
1841         prop = RNA_def_boolean(ot->srna, "is_new", false, "New", "Move objects to a new collection");
1842         RNA_def_property_flag(prop, PROP_SKIP_SAVE | PROP_HIDDEN);
1843         prop = RNA_def_string(ot->srna, "new_collection_name", NULL, MAX_NAME, "Name",
1844                               "Name of the newly added collection");
1845         RNA_def_property_flag(prop, PROP_SKIP_SAVE);
1846 }
1847
1848 #undef COLLECTION_INVALID_INDEX