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