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