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