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