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