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