UI: Nicer report when trying to edit external libraries
[blender.git] / source / blender / editors / object / object_add.c
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
17  * All rights reserved.
18  */
19
20 /** \file
21  * \ingroup edobj
22  */
23
24
25 #include <stdlib.h>
26 #include <string.h>
27 #include <ctype.h>
28
29 #include "MEM_guardedalloc.h"
30
31 #include "DNA_anim_types.h"
32 #include "DNA_camera_types.h"
33 #include "DNA_collection_types.h"
34 #include "DNA_curve_types.h"
35 #include "DNA_light_types.h"
36 #include "DNA_key_types.h"
37 #include "DNA_material_types.h"
38 #include "DNA_mesh_types.h"
39 #include "DNA_meta_types.h"
40 #include "DNA_object_fluidsim_types.h"
41 #include "DNA_object_force_types.h"
42 #include "DNA_object_types.h"
43 #include "DNA_lightprobe_types.h"
44 #include "DNA_scene_types.h"
45 #include "DNA_vfont_types.h"
46 #include "DNA_gpencil_types.h"
47
48 #include "BLI_utildefines.h"
49 #include "BLI_ghash.h"
50 #include "BLI_listbase.h"
51 #include "BLI_math.h"
52 #include "BLI_string.h"
53
54 #include "BLT_translation.h"
55
56 #include "BKE_action.h"
57 #include "BKE_anim.h"
58 #include "BKE_animsys.h"
59 #include "BKE_armature.h"
60 #include "BKE_camera.h"
61 #include "BKE_collection.h"
62 #include "BKE_context.h"
63 #include "BKE_constraint.h"
64 #include "BKE_curve.h"
65 #include "BKE_displist.h"
66 #include "BKE_effect.h"
67 #include "BKE_font.h"
68 #include "BKE_gpencil.h"
69 #include "BKE_key.h"
70 #include "BKE_light.h"
71 #include "BKE_lattice.h"
72 #include "BKE_layer.h"
73 #include "BKE_library.h"
74 #include "BKE_library_query.h"
75 #include "BKE_library_remap.h"
76 #include "BKE_lightprobe.h"
77 #include "BKE_main.h"
78 #include "BKE_material.h"
79 #include "BKE_mball.h"
80 #include "BKE_mesh.h"
81 #include "BKE_mesh_runtime.h"
82 #include "BKE_nla.h"
83 #include "BKE_object.h"
84 #include "BKE_particle.h"
85 #include "BKE_report.h"
86 #include "BKE_scene.h"
87 #include "BKE_speaker.h"
88
89 #include "DEG_depsgraph.h"
90 #include "DEG_depsgraph_build.h"
91 #include "DEG_depsgraph_query.h"
92
93 #include "RNA_access.h"
94 #include "RNA_define.h"
95 #include "RNA_enum_types.h"
96
97 #include "WM_api.h"
98 #include "WM_types.h"
99
100 #include "ED_armature.h"
101 #include "ED_curve.h"
102 #include "ED_gpencil.h"
103 #include "ED_mball.h"
104 #include "ED_mesh.h"
105 #include "ED_node.h"
106 #include "ED_object.h"
107 #include "ED_physics.h"
108 #include "ED_render.h"
109 #include "ED_screen.h"
110 #include "ED_transform.h"
111 #include "ED_view3d.h"
112
113 #include "UI_resources.h"
114
115 #include "object_intern.h"
116
117 /* this is an exact copy of the define in rna_light.c
118  * kept here because of linking order.
119  * Icons are only defined here */
120 const EnumPropertyItem rna_enum_light_type_items[] = {
121         {LA_LOCAL, "POINT", ICON_LIGHT_POINT, "Point", "Omnidirectional point light source"},
122         {LA_SUN, "SUN", ICON_LIGHT_SUN, "Sun", "Constant direction parallel ray light source"},
123         {LA_SPOT, "SPOT", ICON_LIGHT_SPOT, "Spot", "Directional cone light source"},
124         {LA_AREA, "AREA", ICON_LIGHT_AREA, "Area", "Directional area light source"},
125         {0, NULL, 0, NULL, NULL},
126 };
127
128 /* copy from rna_object_force.c */
129 static const EnumPropertyItem field_type_items[] = {
130         {PFIELD_FORCE, "FORCE", ICON_FORCE_FORCE, "Force", ""},
131         {PFIELD_WIND, "WIND", ICON_FORCE_WIND, "Wind", ""},
132         {PFIELD_VORTEX, "VORTEX", ICON_FORCE_VORTEX, "Vortex", ""},
133         {PFIELD_MAGNET, "MAGNET", ICON_FORCE_MAGNETIC, "Magnetic", ""},
134         {PFIELD_HARMONIC, "HARMONIC", ICON_FORCE_HARMONIC, "Harmonic", ""},
135         {PFIELD_CHARGE, "CHARGE", ICON_FORCE_CHARGE, "Charge", ""},
136         {PFIELD_LENNARDJ, "LENNARDJ", ICON_FORCE_LENNARDJONES, "Lennard-Jones", ""},
137         {PFIELD_TEXTURE, "TEXTURE", ICON_FORCE_TEXTURE, "Texture", ""},
138         {PFIELD_GUIDE, "GUIDE", ICON_FORCE_CURVE, "Curve Guide", ""},
139         {PFIELD_BOID, "BOID", ICON_FORCE_BOID, "Boid", ""},
140         {PFIELD_TURBULENCE, "TURBULENCE", ICON_FORCE_TURBULENCE, "Turbulence", ""},
141         {PFIELD_DRAG, "DRAG", ICON_FORCE_DRAG, "Drag", ""},
142         {PFIELD_SMOKEFLOW, "SMOKE", ICON_FORCE_SMOKEFLOW, "Smoke Flow", ""},
143         {0, NULL, 0, NULL, NULL},
144 };
145
146 static EnumPropertyItem lightprobe_type_items[] = {
147         {LIGHTPROBE_TYPE_CUBE, "CUBEMAP", ICON_LIGHTPROBE_CUBEMAP, "Reflection Cubemap",
148      "Reflection probe with spherical or cubic attenuation"},
149         {LIGHTPROBE_TYPE_PLANAR, "PLANAR", ICON_LIGHTPROBE_PLANAR, "Reflection Plane",
150      "Planar reflection probe"},
151         {LIGHTPROBE_TYPE_GRID, "GRID", ICON_LIGHTPROBE_GRID, "Irradiance Volume",
152      "Irradiance probe to capture diffuse indirect lighting"},
153         {0, NULL, 0, NULL, NULL},
154 };
155
156 /************************** Exported *****************************/
157
158 void ED_object_location_from_view(bContext *C, float loc[3])
159 {
160         const Scene *scene = CTX_data_scene(C);
161         copy_v3_v3(loc, scene->cursor.location);
162 }
163
164 void ED_object_rotation_from_quat(float rot[3], const float viewquat[4], const char align_axis)
165 {
166         BLI_assert(align_axis >= 'X' && align_axis <= 'Z');
167
168         switch (align_axis) {
169                 case 'X':
170                 {
171                         /* Same as 'rv3d->viewinv[1]' */
172                         float axis_y[4] = {0.0f, 1.0f, 0.0f};
173                         float quat_y[4], quat[4];
174                         axis_angle_to_quat(quat_y, axis_y, M_PI_2);
175                         mul_qt_qtqt(quat, viewquat, quat_y);
176                         quat_to_eul(rot, quat);
177                         break;
178                 }
179                 case 'Y':
180                 {
181                         quat_to_eul(rot, viewquat);
182                         rot[0] -= (float)M_PI_2;
183                         break;
184                 }
185                 case 'Z':
186                 {
187                         quat_to_eul(rot, viewquat);
188                         break;
189                 }
190         }
191 }
192
193 void ED_object_rotation_from_view(bContext *C, float rot[3], const char align_axis)
194 {
195         RegionView3D *rv3d = CTX_wm_region_view3d(C);
196         BLI_assert(align_axis >= 'X' && align_axis <= 'Z');
197         if (rv3d) {
198                 float viewquat[4];
199                 copy_qt_qt(viewquat, rv3d->viewquat);
200                 viewquat[0] *= -1.0f;
201                 ED_object_rotation_from_quat(rot, viewquat, align_axis);
202         }
203         else {
204                 zero_v3(rot);
205         }
206 }
207
208 void ED_object_base_init_transform(bContext *C, Base *base, const float loc[3], const float rot[3])
209 {
210         Object *ob = base->object;
211         Scene *scene = CTX_data_scene(C);
212         Depsgraph *depsgraph = CTX_data_depsgraph(C);
213
214         if (!scene) return;
215
216         if (loc)
217                 copy_v3_v3(ob->loc, loc);
218
219         if (rot)
220                 copy_v3_v3(ob->rot, rot);
221
222         BKE_object_where_is_calc(depsgraph, scene, ob);
223 }
224
225 /* Uses context to figure out transform for primitive.
226  * Returns standard diameter. */
227 float ED_object_new_primitive_matrix(
228         bContext *C, Object *obedit,
229         const float loc[3], const float rot[3], float primmat[4][4])
230 {
231         Scene *scene = CTX_data_scene(C);
232         View3D *v3d = CTX_wm_view3d(C);
233         float mat[3][3], rmat[3][3], cmat[3][3], imat[3][3];
234
235         unit_m4(primmat);
236
237         eul_to_mat3(rmat, rot);
238         invert_m3(rmat);
239
240         /* inverse transform for initial rotation and object */
241         copy_m3_m4(mat, obedit->obmat);
242         mul_m3_m3m3(cmat, rmat, mat);
243         invert_m3_m3(imat, cmat);
244         copy_m4_m3(primmat, imat);
245
246         /* center */
247         copy_v3_v3(primmat[3], loc);
248         sub_v3_v3v3(primmat[3], primmat[3], obedit->obmat[3]);
249         invert_m3_m3(imat, mat);
250         mul_m3_v3(imat, primmat[3]);
251
252         {
253                 const float dia = v3d ? ED_view3d_grid_scale(scene, v3d, NULL) : ED_scene_grid_scale(scene, NULL);
254                 return dia;
255         }
256
257         // return 1.0f;
258 }
259
260 /********************* Add Object Operator ********************/
261
262 static void view_align_update(struct Main *UNUSED(main), struct Scene *UNUSED(scene), struct PointerRNA *ptr)
263 {
264         RNA_struct_idprops_unset(ptr, "rotation");
265 }
266
267 void ED_object_add_unit_props_size(wmOperatorType *ot)
268 {
269         RNA_def_float_distance(ot->srna, "size", 2.0f, 0.0, OBJECT_ADD_SIZE_MAXF, "Size", "", 0.001, 100.00);
270 }
271
272 void ED_object_add_unit_props_radius(wmOperatorType *ot)
273 {
274         RNA_def_float_distance(ot->srna, "radius", 1.0f, 0.0, OBJECT_ADD_SIZE_MAXF, "Radius", "", 0.001, 100.00);
275 }
276
277 void ED_object_add_generic_props(wmOperatorType *ot, bool do_editmode)
278 {
279         PropertyRNA *prop;
280
281         /* note: this property gets hidden for add-camera operator */
282         prop = RNA_def_boolean(ot->srna, "view_align", 0, "Align to View", "Align the new object to the view");
283         RNA_def_property_update_runtime(prop, view_align_update);
284
285         if (do_editmode) {
286                 prop = RNA_def_boolean(ot->srna, "enter_editmode", 0, "Enter Editmode",
287                                        "Enter editmode when adding this object");
288                 RNA_def_property_flag(prop, PROP_HIDDEN | PROP_SKIP_SAVE);
289         }
290
291         prop = RNA_def_float_vector_xyz(ot->srna, "location", 3, NULL, -OBJECT_ADD_SIZE_MAXF, OBJECT_ADD_SIZE_MAXF,
292                                         "Location", "Location for the newly added object", -1000.0f, 1000.0f);
293         RNA_def_property_flag(prop, PROP_SKIP_SAVE);
294         prop = RNA_def_float_rotation(ot->srna, "rotation", 3, NULL, -OBJECT_ADD_SIZE_MAXF, OBJECT_ADD_SIZE_MAXF,
295                                       "Rotation", "Rotation for the newly added object",
296                                       DEG2RADF(-360.0f), DEG2RADF(360.0f));
297         RNA_def_property_flag(prop, PROP_SKIP_SAVE);
298 }
299
300 void ED_object_add_mesh_props(wmOperatorType *ot)
301 {
302         RNA_def_boolean(ot->srna, "calc_uvs", true, "Generate UVs", "Generate a default UV map");
303 }
304
305 bool ED_object_add_generic_get_opts(
306         bContext *C, wmOperator *op, const char view_align_axis,
307         float loc[3], float rot[3],
308         bool *enter_editmode, ushort *local_view_bits, bool *is_view_aligned)
309 {
310         PropertyRNA *prop;
311
312         /* Switch to Edit mode? optional prop */
313         if ((prop = RNA_struct_find_property(op->ptr, "enter_editmode"))) {
314                 bool _enter_editmode;
315                 if (!enter_editmode)
316                         enter_editmode = &_enter_editmode;
317
318                 if (RNA_property_is_set(op->ptr, prop) && enter_editmode)
319                         *enter_editmode = RNA_property_boolean_get(op->ptr, prop);
320                 else {
321                         *enter_editmode = (U.flag & USER_ADD_EDITMODE) != 0;
322                         RNA_property_boolean_set(op->ptr, prop, *enter_editmode);
323                 }
324         }
325
326         if (local_view_bits) {
327                 View3D *v3d = CTX_wm_view3d(C);
328                 if (v3d && v3d->localvd) {
329                         *local_view_bits = v3d->local_view_uuid;
330                 }
331         }
332
333         /* Location! */
334         {
335                 float _loc[3];
336                 if (!loc)
337                         loc = _loc;
338
339                 if (RNA_struct_property_is_set(op->ptr, "location")) {
340                         RNA_float_get_array(op->ptr, "location", loc);
341                 }
342                 else {
343                         ED_object_location_from_view(C, loc);
344                         RNA_float_set_array(op->ptr, "location", loc);
345                 }
346         }
347
348         /* Rotation! */
349         {
350                 bool _is_view_aligned;
351                 float _rot[3];
352                 if (!is_view_aligned)
353                         is_view_aligned = &_is_view_aligned;
354                 if (!rot)
355                         rot = _rot;
356
357                 if (RNA_struct_property_is_set(op->ptr, "rotation"))
358                         *is_view_aligned = false;
359                 else if (RNA_struct_property_is_set(op->ptr, "view_align"))
360                         *is_view_aligned = RNA_boolean_get(op->ptr, "view_align");
361                 else {
362                         *is_view_aligned = (U.flag & USER_ADD_VIEWALIGNED) != 0;
363                         RNA_boolean_set(op->ptr, "view_align", *is_view_aligned);
364                 }
365
366                 if (*is_view_aligned) {
367                         ED_object_rotation_from_view(C, rot, view_align_axis);
368                         RNA_float_set_array(op->ptr, "rotation", rot);
369                 }
370                 else
371                         RNA_float_get_array(op->ptr, "rotation", rot);
372         }
373
374         return true;
375 }
376
377 /* For object add primitive operators.
378  * Do not call undo push in this function (users of this function have to). */
379 Object *ED_object_add_type(
380         bContext *C,
381         int type, const char *name,
382         const float loc[3], const float rot[3],
383         bool enter_editmode, ushort local_view_bits)
384 {
385         Main *bmain = CTX_data_main(C);
386         Scene *scene = CTX_data_scene(C);
387         ViewLayer *view_layer = CTX_data_view_layer(C);
388         Object *ob;
389
390         /* for as long scene has editmode... */
391         if (CTX_data_edit_object(C)) {
392                 ED_object_editmode_exit(C, EM_FREEDATA);
393         }
394
395         /* deselects all, sets active object */
396         ob = BKE_object_add(bmain, scene, view_layer, type, name);
397         BASACT(view_layer)->local_view_bits = local_view_bits;
398         /* editor level activate, notifiers */
399         ED_object_base_activate(C, view_layer->basact);
400
401         /* more editor stuff */
402         ED_object_base_init_transform(C, view_layer->basact, loc, rot);
403
404         /* TODO(sergey): This is weird to manually tag objects for update, better to
405          * use DEG_id_tag_update here perhaps.
406          */
407         DEG_id_type_tag(bmain, ID_OB);
408         DEG_relations_tag_update(bmain);
409         if (ob->data != NULL) {
410                 DEG_id_tag_update_ex(bmain, (ID *)ob->data, ID_RECALC_EDITORS);
411         }
412
413         if (enter_editmode) {
414                 ED_object_editmode_enter_ex(bmain, scene, ob, 0);
415         }
416
417         WM_event_add_notifier(C, NC_SCENE | ND_LAYER_CONTENT, scene);
418
419         /* TODO(sergey): Use proper flag for tagging here. */
420         DEG_id_tag_update(&scene->id, 0);
421
422         return ob;
423 }
424
425 /* for object add operator */
426 static int object_add_exec(bContext *C, wmOperator *op)
427 {
428         Object *ob;
429         bool enter_editmode;
430         ushort local_view_bits;
431         float loc[3], rot[3], radius;
432
433         WM_operator_view3d_unit_defaults(C, op);
434         if (!ED_object_add_generic_get_opts(C, op, 'Z', loc, rot, &enter_editmode, &local_view_bits, NULL)) {
435                 return OPERATOR_CANCELLED;
436         }
437         radius = RNA_float_get(op->ptr, "radius");
438         ob = ED_object_add_type(C, RNA_enum_get(op->ptr, "type"), NULL, loc, rot, enter_editmode, local_view_bits);
439
440         if (ob->type == OB_LATTICE) {
441                 /* lattice is a special case!
442                  * we never want to scale the obdata since that is the rest-state */
443                 copy_v3_fl(ob->scale, radius);
444         }
445         else {
446                 BKE_object_obdata_size_init(ob, radius);
447         }
448
449         return OPERATOR_FINISHED;
450 }
451
452 void OBJECT_OT_add(wmOperatorType *ot)
453 {
454         /* identifiers */
455         ot->name = "Add Object";
456         ot->description = "Add an object to the scene";
457         ot->idname = "OBJECT_OT_add";
458
459         /* api callbacks */
460         ot->exec = object_add_exec;
461         ot->poll = ED_operator_objectmode;
462
463         /* flags */
464         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
465
466         /* properties */
467         ED_object_add_unit_props_radius(ot);
468         PropertyRNA *prop = RNA_def_enum(ot->srna, "type", rna_enum_object_type_items, 0, "Type", "");
469         RNA_def_property_translation_context(prop, BLT_I18NCONTEXT_ID_ID);
470
471         ED_object_add_generic_props(ot, true);
472 }
473
474 /********************** Add Probe Operator **********************/
475
476 /* for object add operator */
477 static const char *get_lightprobe_defname(int type)
478 {
479         switch (type) {
480                 case LIGHTPROBE_TYPE_GRID: return CTX_DATA_(BLT_I18NCONTEXT_ID_LIGHT, "IrradianceVolume");
481                 case LIGHTPROBE_TYPE_PLANAR: return CTX_DATA_(BLT_I18NCONTEXT_ID_LIGHT, "ReflectionPlane");
482                 case LIGHTPROBE_TYPE_CUBE: return CTX_DATA_(BLT_I18NCONTEXT_ID_LIGHT, "ReflectionCubemap");
483                 default:
484                         return CTX_DATA_(BLT_I18NCONTEXT_ID_LIGHT, "LightProbe");
485         }
486 }
487
488 static int lightprobe_add_exec(bContext *C, wmOperator *op)
489 {
490         Object *ob;
491         LightProbe *probe;
492         int type;
493         bool enter_editmode;
494         ushort local_view_bits;
495         float loc[3], rot[3];
496         float radius;
497
498         WM_operator_view3d_unit_defaults(C, op);
499         if (!ED_object_add_generic_get_opts(C, op, 'Z', loc, rot, &enter_editmode, &local_view_bits, NULL)) {
500                 return OPERATOR_CANCELLED;
501         }
502         type = RNA_enum_get(op->ptr, "type");
503         radius = RNA_float_get(op->ptr, "radius");
504
505         ob = ED_object_add_type(C, OB_LIGHTPROBE, get_lightprobe_defname(type), loc, rot, false, local_view_bits);
506         BKE_object_obdata_size_init(ob, radius);
507
508         probe = (LightProbe *)ob->data;
509         probe->type = type;
510
511         switch (type) {
512                 case LIGHTPROBE_TYPE_GRID:
513                         probe->distinf = 0.3f;
514                         probe->falloff = 1.0f;
515                         probe->clipsta = 0.01f;
516                         break;
517                 case LIGHTPROBE_TYPE_PLANAR:
518                         probe->distinf = 0.1f;
519                         probe->falloff = 0.5f;
520                         probe->clipsta = 0.001f;
521                         ob->empty_drawsize = 0.5f;
522                         break;
523                 case LIGHTPROBE_TYPE_CUBE:
524                         probe->attenuation_type = LIGHTPROBE_SHAPE_ELIPSOID;
525                         break;
526                 default:
527                         BLI_assert(!"LightProbe type not configured.");
528                         break;
529         }
530
531         DEG_relations_tag_update(CTX_data_main(C));
532
533         return OPERATOR_FINISHED;
534 }
535
536 void OBJECT_OT_lightprobe_add(wmOperatorType *ot)
537 {
538         /* identifiers */
539         ot->name = "Add Light Probe";
540         ot->description = "Add a light probe object";
541         ot->idname = "OBJECT_OT_lightprobe_add";
542
543         /* api callbacks */
544         ot->exec = lightprobe_add_exec;
545         ot->poll = ED_operator_objectmode;
546
547         /* flags */
548         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
549
550         /* properties */
551         ot->prop = RNA_def_enum(ot->srna, "type", lightprobe_type_items, 0, "Type", "");
552
553         ED_object_add_unit_props_radius(ot);
554         ED_object_add_generic_props(ot, true);
555 }
556
557 /********************* Add Effector Operator ********************/
558
559 /* for object add operator */
560 static int effector_add_exec(bContext *C, wmOperator *op)
561 {
562         Object *ob;
563         int type;
564         bool enter_editmode;
565         ushort local_view_bits;
566         float loc[3], rot[3];
567         float mat[4][4];
568         float dia;
569
570         WM_operator_view3d_unit_defaults(C, op);
571         if (!ED_object_add_generic_get_opts(C, op, 'Z', loc, rot, &enter_editmode, &local_view_bits, NULL)) {
572                 return OPERATOR_CANCELLED;
573         }
574         type = RNA_enum_get(op->ptr, "type");
575         dia = RNA_float_get(op->ptr, "radius");
576
577         if (type == PFIELD_GUIDE) {
578                 Curve *cu;
579                 const char *name = CTX_DATA_(BLT_I18NCONTEXT_ID_OBJECT, "CurveGuide");
580                 ob = ED_object_add_type(C, OB_CURVE, name, loc, rot, false, local_view_bits);
581
582                 cu = ob->data;
583                 cu->flag |= CU_PATH | CU_3D;
584                 ED_object_editmode_enter(C, 0);
585                 ED_object_new_primitive_matrix(C, ob, loc, rot, mat);
586                 BLI_addtail(&cu->editnurb->nurbs, ED_curve_add_nurbs_primitive(C, ob, mat, CU_NURBS | CU_PRIM_PATH, dia));
587                 if (!enter_editmode)
588                         ED_object_editmode_exit(C, EM_FREEDATA);
589         }
590         else {
591                 const char *name = CTX_DATA_(BLT_I18NCONTEXT_ID_OBJECT, "Field");
592                 ob = ED_object_add_type(C, OB_EMPTY, name, loc, rot, false, local_view_bits);
593                 BKE_object_obdata_size_init(ob, dia);
594                 if (ELEM(type, PFIELD_WIND, PFIELD_VORTEX))
595                         ob->empty_drawtype = OB_SINGLE_ARROW;
596         }
597
598         ob->pd = BKE_partdeflect_new(type);
599
600         DEG_relations_tag_update(CTX_data_main(C));
601
602         return OPERATOR_FINISHED;
603 }
604
605 void OBJECT_OT_effector_add(wmOperatorType *ot)
606 {
607         /* identifiers */
608         ot->name = "Add Effector";
609         ot->description = "Add an empty object with a physics effector to the scene";
610         ot->idname = "OBJECT_OT_effector_add";
611
612         /* api callbacks */
613         ot->exec = effector_add_exec;
614         ot->poll = ED_operator_objectmode;
615
616         /* flags */
617         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
618
619         /* properties */
620         ot->prop = RNA_def_enum(ot->srna, "type", field_type_items, 0, "Type", "");
621
622         ED_object_add_unit_props_radius(ot);
623         ED_object_add_generic_props(ot, true);
624 }
625
626 /********************* Add Camera Operator ********************/
627
628 static int object_camera_add_exec(bContext *C, wmOperator *op)
629 {
630         View3D *v3d = CTX_wm_view3d(C);
631         Scene *scene = CTX_data_scene(C);
632         Object *ob;
633         Camera *cam;
634         bool enter_editmode;
635         ushort local_view_bits;
636         float loc[3], rot[3];
637
638         /* force view align for cameras */
639         RNA_boolean_set(op->ptr, "view_align", true);
640
641         if (!ED_object_add_generic_get_opts(C, op, 'Z', loc, rot, &enter_editmode, &local_view_bits, NULL)) {
642                 return OPERATOR_CANCELLED;
643         }
644         ob = ED_object_add_type(C, OB_CAMERA, NULL, loc, rot, false, local_view_bits);
645
646         if (v3d) {
647                 if (v3d->camera == NULL)
648                         v3d->camera = ob;
649                 if (v3d->scenelock && scene->camera == NULL) {
650                         scene->camera = ob;
651                 }
652         }
653
654         cam = ob->data;
655         cam->drawsize = v3d ? ED_view3d_grid_scale(scene, v3d, NULL) : ED_scene_grid_scale(scene, NULL);
656
657         return OPERATOR_FINISHED;
658 }
659
660 void OBJECT_OT_camera_add(wmOperatorType *ot)
661 {
662         PropertyRNA *prop;
663
664         /* identifiers */
665         ot->name = "Add Camera";
666         ot->description = "Add a camera object to the scene";
667         ot->idname = "OBJECT_OT_camera_add";
668
669         /* api callbacks */
670         ot->exec = object_camera_add_exec;
671         ot->poll = ED_operator_objectmode;
672
673         /* flags */
674         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
675
676         ED_object_add_generic_props(ot, true);
677
678         /* hide this for cameras, default */
679         prop = RNA_struct_type_find_property(ot->srna, "view_align");
680         RNA_def_property_flag(prop, PROP_HIDDEN);
681 }
682
683
684 /********************* Add Metaball Operator ********************/
685
686 static int object_metaball_add_exec(bContext *C, wmOperator *op)
687 {
688         Object *obedit = CTX_data_edit_object(C);
689         bool newob = false;
690         bool enter_editmode;
691         ushort local_view_bits;
692         float loc[3], rot[3];
693         float mat[4][4];
694         float dia;
695
696         WM_operator_view3d_unit_defaults(C, op);
697         if (!ED_object_add_generic_get_opts(C, op, 'Z', loc, rot, &enter_editmode, &local_view_bits, NULL)) {
698                 return OPERATOR_CANCELLED;
699         }
700         if (obedit == NULL || obedit->type != OB_MBALL) {
701                 obedit = ED_object_add_type(C, OB_MBALL, NULL, loc, rot, true, local_view_bits);
702                 newob = true;
703         }
704         else {
705                 DEG_id_tag_update(&obedit->id, ID_RECALC_GEOMETRY);
706         }
707
708         ED_object_new_primitive_matrix(C, obedit, loc, rot, mat);
709         dia = RNA_float_get(op->ptr, "radius");
710
711         ED_mball_add_primitive(C, obedit, mat, dia, RNA_enum_get(op->ptr, "type"));
712
713         /* userdef */
714         if (newob && !enter_editmode) {
715                 ED_object_editmode_exit(C, EM_FREEDATA);
716         }
717
718         WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, obedit);
719
720         return OPERATOR_FINISHED;
721 }
722
723 void OBJECT_OT_metaball_add(wmOperatorType *ot)
724 {
725         /* identifiers */
726         ot->name = "Add Metaball";
727         ot->description = "Add an metaball object to the scene";
728         ot->idname = "OBJECT_OT_metaball_add";
729
730         /* api callbacks */
731         ot->invoke = WM_menu_invoke;
732         ot->exec = object_metaball_add_exec;
733         ot->poll = ED_operator_scene_editable;
734
735         /* flags */
736         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
737
738         ot->prop = RNA_def_enum(ot->srna, "type", rna_enum_metaelem_type_items, 0, "Primitive", "");
739
740         ED_object_add_unit_props_radius(ot);
741         ED_object_add_generic_props(ot, true);
742 }
743
744 /********************* Add Text Operator ********************/
745
746 static int object_add_text_exec(bContext *C, wmOperator *op)
747 {
748         Object *obedit = CTX_data_edit_object(C);
749         bool enter_editmode;
750         ushort local_view_bits;
751         float loc[3], rot[3];
752
753         WM_operator_view3d_unit_defaults(C, op);
754         if (!ED_object_add_generic_get_opts(C, op, 'Z', loc, rot, &enter_editmode, &local_view_bits, NULL)) {
755                 return OPERATOR_CANCELLED;
756         }
757         if (obedit && obedit->type == OB_FONT)
758                 return OPERATOR_CANCELLED;
759
760         obedit = ED_object_add_type(C, OB_FONT, NULL, loc, rot, enter_editmode, local_view_bits);
761         BKE_object_obdata_size_init(obedit, RNA_float_get(op->ptr, "radius"));
762
763         WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, obedit);
764
765         return OPERATOR_FINISHED;
766 }
767
768 void OBJECT_OT_text_add(wmOperatorType *ot)
769 {
770         /* identifiers */
771         ot->name = "Add Text";
772         ot->description = "Add a text object to the scene";
773         ot->idname = "OBJECT_OT_text_add";
774
775         /* api callbacks */
776         ot->exec = object_add_text_exec;
777         ot->poll = ED_operator_objectmode;
778
779         /* flags */
780         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
781
782         /* properties */
783         ED_object_add_unit_props_radius(ot);
784         ED_object_add_generic_props(ot, true);
785 }
786
787 /********************* Add Armature Operator ********************/
788
789 static int object_armature_add_exec(bContext *C, wmOperator *op)
790 {
791         Object *obedit = CTX_data_edit_object(C);
792         RegionView3D *rv3d = CTX_wm_region_view3d(C);
793         bool newob = false;
794         bool enter_editmode;
795         ushort local_view_bits;
796         float loc[3], rot[3], dia;
797         bool view_aligned = rv3d && (U.flag & USER_ADD_VIEWALIGNED);
798
799         WM_operator_view3d_unit_defaults(C, op);
800         if (!ED_object_add_generic_get_opts(C, op, 'Z', loc, rot, &enter_editmode, &local_view_bits, NULL)) {
801                 return OPERATOR_CANCELLED;
802         }
803         if ((obedit == NULL) || (obedit->type != OB_ARMATURE)) {
804                 obedit = ED_object_add_type(C, OB_ARMATURE, NULL, loc, rot, true, local_view_bits);
805                 ED_object_editmode_enter(C, 0);
806                 newob = true;
807         }
808         else {
809                 DEG_id_tag_update(&obedit->id, ID_RECALC_GEOMETRY);
810         }
811
812         if (obedit == NULL) {
813                 BKE_report(op->reports, RPT_ERROR, "Cannot create editmode armature");
814                 return OPERATOR_CANCELLED;
815         }
816
817         dia = RNA_float_get(op->ptr, "radius");
818         ED_armature_ebone_add_primitive(obedit, dia, view_aligned);
819
820         /* userdef */
821         if (newob && !enter_editmode)
822                 ED_object_editmode_exit(C, EM_FREEDATA);
823
824         WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, obedit);
825
826         return OPERATOR_FINISHED;
827 }
828
829 void OBJECT_OT_armature_add(wmOperatorType *ot)
830 {
831         /* identifiers */
832         ot->name = "Add Armature";
833         ot->description = "Add an armature object to the scene";
834         ot->idname = "OBJECT_OT_armature_add";
835
836         /* api callbacks */
837         ot->exec = object_armature_add_exec;
838         ot->poll = ED_operator_objectmode;
839
840         /* flags */
841         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
842
843         /* properties */
844         ED_object_add_unit_props_radius(ot);
845         ED_object_add_generic_props(ot, true);
846 }
847
848 /********************* Add Empty Operator ********************/
849
850 static int object_empty_add_exec(bContext *C, wmOperator *op)
851 {
852         Object *ob;
853         int type = RNA_enum_get(op->ptr, "type");
854         ushort local_view_bits;
855         float loc[3], rot[3];
856
857         WM_operator_view3d_unit_defaults(C, op);
858         if (!ED_object_add_generic_get_opts(C, op, 'Z', loc, rot, NULL, &local_view_bits, NULL)) {
859                 return OPERATOR_CANCELLED;
860         }
861         ob = ED_object_add_type(C, OB_EMPTY, NULL, loc, rot, false, local_view_bits);
862
863         BKE_object_empty_draw_type_set(ob, type);
864         BKE_object_obdata_size_init(ob, RNA_float_get(op->ptr, "radius"));
865
866         return OPERATOR_FINISHED;
867 }
868
869 void OBJECT_OT_empty_add(wmOperatorType *ot)
870 {
871         /* identifiers */
872         ot->name = "Add Empty";
873         ot->description = "Add an empty object to the scene";
874         ot->idname = "OBJECT_OT_empty_add";
875
876         /* api callbacks */
877         ot->invoke = WM_menu_invoke;
878         ot->exec = object_empty_add_exec;
879         ot->poll = ED_operator_objectmode;
880
881         /* flags */
882         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
883
884         /* properties */
885         ot->prop = RNA_def_enum(ot->srna, "type", rna_enum_object_empty_drawtype_items, 0, "Type", "");
886
887         ED_object_add_unit_props_radius(ot);
888         ED_object_add_generic_props(ot, false);
889 }
890
891 static int empty_drop_named_image_invoke(bContext *C, wmOperator *op, const wmEvent *event)
892 {
893         Scene *scene = CTX_data_scene(C);
894
895         Image *ima = NULL;
896
897         ima = (Image *)WM_operator_drop_load_path(C, op, ID_IM);
898         if (!ima) {
899                 return OPERATOR_CANCELLED;
900         }
901         /* handled below */
902         id_us_min((ID *)ima);
903
904         Object *ob = NULL;
905         Object *ob_cursor = ED_view3d_give_object_under_cursor(C, event->mval);
906
907         /* either change empty under cursor or create a new empty */
908         if (ob_cursor && ob_cursor->type == OB_EMPTY) {
909                 WM_event_add_notifier(C, NC_SCENE | ND_OB_ACTIVE, scene);
910                 DEG_id_tag_update((ID *)ob_cursor, ID_RECALC_TRANSFORM);
911                 ob = ob_cursor;
912         }
913         else {
914                 /* add new empty */
915                 ushort local_view_bits;
916                 float rot[3];
917
918                 if (!ED_object_add_generic_get_opts(C, op, 'Z', NULL, rot, NULL, &local_view_bits, NULL)) {
919                         return OPERATOR_CANCELLED;
920                 }
921                 ob = ED_object_add_type(C, OB_EMPTY, NULL, NULL, rot, false, local_view_bits);
922
923                 ED_object_location_from_view(C, ob->loc);
924                 ED_view3d_cursor3d_position(C, event->mval, false, ob->loc);
925                 ED_object_rotation_from_view(C, ob->rot, 'Z');
926                 ob->empty_drawsize = 5.0f;
927         }
928
929         BKE_object_empty_draw_type_set(ob, OB_EMPTY_IMAGE);
930
931         id_us_min(ob->data);
932         ob->data = ima;
933         id_us_plus(ob->data);
934
935         return OPERATOR_FINISHED;
936 }
937
938 void OBJECT_OT_drop_named_image(wmOperatorType *ot)
939 {
940         PropertyRNA *prop;
941
942         /* identifiers */
943         ot->name = "Add Empty Image/Drop Image To Empty";
944         ot->description = "Add an empty image type to scene with data";
945         ot->idname = "OBJECT_OT_drop_named_image";
946
947         /* api callbacks */
948         ot->invoke = empty_drop_named_image_invoke;
949         ot->poll = ED_operator_objectmode;
950
951         /* flags */
952         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
953
954         /* properties */
955         prop = RNA_def_string(ot->srna, "filepath", NULL, FILE_MAX, "Filepath", "Path to image file");
956         RNA_def_property_flag(prop, PROP_HIDDEN | PROP_SKIP_SAVE);
957         RNA_def_boolean(ot->srna, "relative_path", true, "Relative Path", "Select the file relative to the blend file");
958         RNA_def_property_flag(prop, PROP_HIDDEN | PROP_SKIP_SAVE);
959         prop = RNA_def_string(ot->srna, "name", NULL, MAX_ID_NAME - 2, "Name", "Image name to assign");
960         RNA_def_property_flag(prop, PROP_HIDDEN | PROP_SKIP_SAVE);
961         ED_object_add_generic_props(ot, false);
962 }
963
964 /********************* Add Gpencil Operator ********************/
965 static bool object_gpencil_add_poll(bContext *C)
966 {
967         Scene *scene = CTX_data_scene(C);
968         Object *obact = CTX_data_active_object(C);
969
970         if ((scene == NULL) || (ID_IS_LINKED(scene))) {
971                 return false;
972         }
973
974         if (obact && obact->type == OB_GPENCIL) {
975                 if (obact->mode != OB_MODE_OBJECT) {
976                         return false;
977                 }
978         }
979
980         return true;
981 }
982
983 static int object_gpencil_add_exec(bContext *C, wmOperator *op)
984 {
985         Object *ob = CTX_data_active_object(C);
986         bGPdata *gpd = (ob && (ob->type == OB_GPENCIL)) ? ob->data : NULL;
987
988         const int type = RNA_enum_get(op->ptr, "type");
989
990         ushort local_view_bits;
991         float loc[3], rot[3];
992         bool newob = false;
993
994         /* Note: We use 'Y' here (not 'Z'), as */
995         WM_operator_view3d_unit_defaults(C, op);
996         if (!ED_object_add_generic_get_opts(C, op, 'Y', loc, rot, NULL, &local_view_bits, NULL)) {
997                 return OPERATOR_CANCELLED;
998         }
999         /* add new object if not currently editing a GP object,
1000          * or if "empty" was chosen (i.e. user wants a blank GP canvas)
1001          */
1002         if ((gpd == NULL) || (GPENCIL_ANY_MODE(gpd) == false) || (type == GP_EMPTY)) {
1003                 const char *ob_name = NULL;
1004                 switch (type) {
1005                         case GP_MONKEY:
1006                         {
1007                                 ob_name = "Suzanne";
1008                                 break;
1009                         }
1010                         case GP_STROKE:
1011                         {
1012                                 ob_name = "Stroke";
1013                                 break;
1014                         }
1015                         default:
1016                         {
1017                                 break;
1018                         }
1019                 }
1020
1021                 ob = ED_object_add_type(C, OB_GPENCIL, ob_name, loc, rot, true, local_view_bits);
1022                 gpd = ob->data;
1023                 newob = true;
1024         }
1025         else {
1026                 DEG_id_tag_update(&ob->id, ID_RECALC_GEOMETRY);
1027                 WM_event_add_notifier(C, NC_GPENCIL | ND_DATA | NA_ADDED, NULL);
1028         }
1029
1030         /* create relevant geometry */
1031         switch (type) {
1032                 case GP_STROKE:
1033                 {
1034                         float radius = RNA_float_get(op->ptr, "radius");
1035                         float mat[4][4];
1036
1037                         ED_object_new_primitive_matrix(C, ob, loc, rot, mat);
1038                         mul_v3_fl(mat[0], radius);
1039                         mul_v3_fl(mat[1], radius);
1040                         mul_v3_fl(mat[2], radius);
1041
1042                         ED_gpencil_create_stroke(C, ob, mat);
1043                         break;
1044                 }
1045                 case GP_MONKEY:
1046                 {
1047                         float radius = RNA_float_get(op->ptr, "radius");
1048                         float mat[4][4];
1049
1050                         ED_object_new_primitive_matrix(C, ob, loc, rot, mat);
1051                         mul_v3_fl(mat[0], radius);
1052                         mul_v3_fl(mat[1], radius);
1053                         mul_v3_fl(mat[2], radius);
1054
1055                         ED_gpencil_create_monkey(C, ob, mat);
1056                         break;
1057                 }
1058                 case GP_EMPTY:
1059                         /* do nothing */
1060                         break;
1061
1062                 default:
1063                         BKE_report(op->reports, RPT_WARNING, "Not implemented");
1064                         break;
1065         }
1066
1067         /* if this is a new object, initialise default stuff (colors, etc.) */
1068         if (newob) {
1069                 /* set default viewport color to black */
1070                 copy_v3_fl(ob->color, 0.0f);
1071
1072                 ED_gpencil_add_defaults(C, ob);
1073         }
1074
1075         return OPERATOR_FINISHED;
1076 }
1077
1078 void OBJECT_OT_gpencil_add(wmOperatorType *ot)
1079 {
1080         /* identifiers */
1081         ot->name = "Add Grease Pencil";
1082         ot->description = "Add a Grease Pencil object to the scene";
1083         ot->idname = "OBJECT_OT_gpencil_add";
1084
1085         /* api callbacks */
1086         ot->invoke = WM_menu_invoke;
1087         ot->exec = object_gpencil_add_exec;
1088         ot->poll = object_gpencil_add_poll;
1089
1090         /* flags */
1091         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1092
1093         /* properties */
1094         ED_object_add_unit_props_radius(ot);
1095         ED_object_add_generic_props(ot, false);
1096
1097         ot->prop = RNA_def_enum(ot->srna, "type", rna_enum_object_gpencil_type_items, 0, "Type", "");
1098 }
1099
1100 /********************* Add Light Operator ********************/
1101
1102 static const char *get_light_defname(int type)
1103 {
1104         switch (type) {
1105                 case LA_LOCAL: return CTX_DATA_(BLT_I18NCONTEXT_ID_LIGHT, "Point");
1106                 case LA_SUN: return CTX_DATA_(BLT_I18NCONTEXT_ID_LIGHT, "Sun");
1107                 case LA_SPOT: return CTX_DATA_(BLT_I18NCONTEXT_ID_LIGHT, "Spot");
1108                 case LA_AREA: return CTX_DATA_(BLT_I18NCONTEXT_ID_LIGHT, "Area");
1109                 default:
1110                         return CTX_DATA_(BLT_I18NCONTEXT_ID_LIGHT, "Light");
1111         }
1112 }
1113
1114 static int object_light_add_exec(bContext *C, wmOperator *op)
1115 {
1116         Scene *scene = CTX_data_scene(C);
1117         Object *ob;
1118         Light *la;
1119         int type = RNA_enum_get(op->ptr, "type");
1120         ushort local_view_bits;
1121         float loc[3], rot[3];
1122
1123         WM_operator_view3d_unit_defaults(C, op);
1124         if (!ED_object_add_generic_get_opts(C, op, 'Z', loc, rot, NULL, &local_view_bits, NULL)) {
1125                 return OPERATOR_CANCELLED;
1126         }
1127         ob = ED_object_add_type(C, OB_LAMP, get_light_defname(type), loc, rot, false, local_view_bits);
1128
1129         float size = RNA_float_get(op->ptr, "radius");
1130         /* Better defaults for light size. */
1131         switch (type) {
1132                 case LA_LOCAL:
1133                 case LA_SPOT:
1134                         break;
1135                 case LA_AREA:
1136                         size *= 4.0f;
1137                         break;
1138                 default:
1139                         size *= 0.5f;
1140                         break;
1141         }
1142         BKE_object_obdata_size_init(ob, size);
1143
1144         la = (Light *)ob->data;
1145         la->type = type;
1146
1147         if (BKE_scene_uses_cycles(scene)) {
1148                 ED_node_shader_default(C, &la->id);
1149                 la->use_nodes = true;
1150         }
1151
1152         return OPERATOR_FINISHED;
1153 }
1154
1155 void OBJECT_OT_light_add(wmOperatorType *ot)
1156 {
1157         /* identifiers */
1158         ot->name = "Add Light";
1159         ot->description = "Add a light object to the scene";
1160         ot->idname = "OBJECT_OT_light_add";
1161
1162         /* api callbacks */
1163         ot->invoke = WM_menu_invoke;
1164         ot->exec = object_light_add_exec;
1165         ot->poll = ED_operator_objectmode;
1166
1167         /* flags */
1168         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1169
1170         /* properties */
1171         ot->prop = RNA_def_enum(ot->srna, "type", rna_enum_light_type_items, 0, "Type", "");
1172         RNA_def_property_translation_context(ot->prop, BLT_I18NCONTEXT_ID_LIGHT);
1173
1174         ED_object_add_unit_props_radius(ot);
1175         ED_object_add_generic_props(ot, false);
1176 }
1177
1178 /********************* Add Collection Instance Operator ********************/
1179
1180 static int collection_instance_add_exec(bContext *C, wmOperator *op)
1181 {
1182         Main *bmain = CTX_data_main(C);
1183         Collection *collection;
1184         ushort local_view_bits;
1185         float loc[3], rot[3];
1186
1187         if (RNA_struct_property_is_set(op->ptr, "name")) {
1188                 char name[MAX_ID_NAME - 2];
1189
1190                 RNA_string_get(op->ptr, "name", name);
1191                 collection = (Collection *)BKE_libblock_find_name(bmain, ID_GR, name);
1192
1193                 if (0 == RNA_struct_property_is_set(op->ptr, "location")) {
1194                         const wmEvent *event = CTX_wm_window(C)->eventstate;
1195                         ARegion *ar = CTX_wm_region(C);
1196                         const int mval[2] = {event->x - ar->winrct.xmin,
1197                                              event->y - ar->winrct.ymin};
1198                         ED_object_location_from_view(C, loc);
1199                         ED_view3d_cursor3d_position(C, mval, false, loc);
1200                         RNA_float_set_array(op->ptr, "location", loc);
1201                 }
1202         }
1203         else
1204                 collection = BLI_findlink(&CTX_data_main(C)->collection, RNA_enum_get(op->ptr, "collection"));
1205
1206         if (!ED_object_add_generic_get_opts(C, op, 'Z', loc, rot, NULL, &local_view_bits, NULL)) {
1207                 return OPERATOR_CANCELLED;
1208         }
1209         if (collection) {
1210                 Scene *scene = CTX_data_scene(C);
1211                 ViewLayer *view_layer = CTX_data_view_layer(C);
1212
1213                 /* Avoid dependency cycles. */
1214                 LayerCollection *active_lc = BKE_layer_collection_get_active(view_layer);
1215                 while (BKE_collection_find_cycle(active_lc->collection, collection)) {
1216                         active_lc = BKE_layer_collection_activate_parent(view_layer, active_lc);
1217                 }
1218
1219                 Object *ob = ED_object_add_type(C, OB_EMPTY, collection->id.name + 2, loc, rot, false, local_view_bits);
1220                 ob->instance_collection = collection;
1221                 ob->transflag |= OB_DUPLICOLLECTION;
1222                 id_us_plus(&collection->id);
1223
1224                 /* works without this except if you try render right after, see: 22027 */
1225                 DEG_relations_tag_update(bmain);
1226                 DEG_id_tag_update(&scene->id, ID_RECALC_SELECT);
1227                 WM_event_add_notifier(C, NC_SCENE | ND_OB_ACTIVE, scene);
1228
1229                 return OPERATOR_FINISHED;
1230         }
1231
1232         return OPERATOR_CANCELLED;
1233 }
1234
1235 /* only used as menu */
1236 void OBJECT_OT_collection_instance_add(wmOperatorType *ot)
1237 {
1238         PropertyRNA *prop;
1239
1240         /* identifiers */
1241         ot->name = "Add Collection Instance";
1242         ot->description = "Add a collection instance";
1243         ot->idname = "OBJECT_OT_collection_instance_add";
1244
1245         /* api callbacks */
1246         ot->invoke = WM_enum_search_invoke;
1247         ot->exec = collection_instance_add_exec;
1248         ot->poll = ED_operator_objectmode;
1249
1250         /* flags */
1251         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1252
1253         /* properties */
1254         RNA_def_string(ot->srna, "name", "Collection", MAX_ID_NAME - 2, "Name", "Collection name to add");
1255         prop = RNA_def_enum(ot->srna, "collection", DummyRNA_NULL_items, 0, "Collection", "");
1256         RNA_def_enum_funcs(prop, RNA_collection_itemf);
1257         RNA_def_property_flag(prop, PROP_ENUM_NO_TRANSLATE);
1258         ot->prop = prop;
1259         ED_object_add_generic_props(ot, false);
1260 }
1261
1262 /********************* Add Speaker Operator ********************/
1263
1264 static int object_speaker_add_exec(bContext *C, wmOperator *op)
1265 {
1266         Object *ob;
1267         ushort local_view_bits;
1268         float loc[3], rot[3];
1269         Scene *scene = CTX_data_scene(C);
1270
1271         if (!ED_object_add_generic_get_opts(C, op, 'Z', loc, rot, NULL, &local_view_bits, NULL)) {
1272                 return OPERATOR_CANCELLED;
1273         }
1274         ob = ED_object_add_type(C, OB_SPEAKER, NULL, loc, rot, false, local_view_bits);
1275
1276         /* to make it easier to start using this immediately in NLA, a default sound clip is created
1277          * ready to be moved around to retime the sound and/or make new sound clips
1278          */
1279         {
1280                 /* create new data for NLA hierarchy */
1281                 AnimData *adt = BKE_animdata_add_id(&ob->id);
1282                 NlaTrack *nlt = BKE_nlatrack_add(adt, NULL);
1283                 NlaStrip *strip = BKE_nla_add_soundstrip(scene, ob->data);
1284                 strip->start = CFRA;
1285                 strip->end += strip->start;
1286
1287                 /* hook them up */
1288                 BKE_nlatrack_add_strip(nlt, strip);
1289
1290                 /* auto-name the strip, and give the track an interesting name  */
1291                 BLI_strncpy(nlt->name, DATA_("SoundTrack"), sizeof(nlt->name));
1292                 BKE_nlastrip_validate_name(adt, strip);
1293
1294                 WM_event_add_notifier(C, NC_ANIMATION | ND_NLA | NA_EDITED, NULL);
1295         }
1296
1297         return OPERATOR_FINISHED;
1298 }
1299
1300 void OBJECT_OT_speaker_add(wmOperatorType *ot)
1301 {
1302         /* identifiers */
1303         ot->name = "Add Speaker";
1304         ot->description = "Add a speaker object to the scene";
1305         ot->idname = "OBJECT_OT_speaker_add";
1306
1307         /* api callbacks */
1308         ot->exec = object_speaker_add_exec;
1309         ot->poll = ED_operator_objectmode;
1310
1311         /* flags */
1312         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1313
1314         ED_object_add_generic_props(ot, true);
1315 }
1316
1317 /**************************** Delete Object *************************/
1318
1319 /* remove base from a specific scene */
1320 /* note: now unlinks constraints as well */
1321 void ED_object_base_free_and_unlink(Main *bmain, Scene *scene, Object *ob)
1322 {
1323         if (BKE_library_ID_is_indirectly_used(bmain, ob) &&
1324             ID_REAL_USERS(ob) <= 1 && ID_EXTRA_USERS(ob) == 0)
1325         {
1326                 /* We cannot delete indirectly used object... */
1327                 printf("WARNING, undeletable object '%s', should have been catched before reaching this function!",
1328                        ob->id.name + 2);
1329                 return;
1330         }
1331
1332         DEG_id_tag_update_ex(bmain, &ob->id, ID_RECALC_BASE_FLAGS);
1333
1334         BKE_scene_collections_object_remove(bmain, scene, ob, true);
1335 }
1336
1337 static int object_delete_exec(bContext *C, wmOperator *op)
1338 {
1339         Main *bmain = CTX_data_main(C);
1340         Scene *scene = CTX_data_scene(C);
1341         wmWindowManager *wm = CTX_wm_manager(C);
1342         wmWindow *win;
1343         const bool use_global = RNA_boolean_get(op->ptr, "use_global");
1344         uint changed_count = 0;
1345
1346         if (CTX_data_edit_object(C))
1347                 return OPERATOR_CANCELLED;
1348
1349         CTX_DATA_BEGIN (C, Object *, ob, selected_objects)
1350         {
1351                 const bool is_indirectly_used = BKE_library_ID_is_indirectly_used(bmain, ob);
1352                 if (ob->id.tag & LIB_TAG_INDIRECT) {
1353                         /* Can this case ever happen? */
1354                         BKE_reportf(op->reports, RPT_WARNING, "Cannot delete indirectly linked object '%s'", ob->id.name + 2);
1355                         continue;
1356                 }
1357                 else if (is_indirectly_used && ID_REAL_USERS(ob) <= 1 && ID_EXTRA_USERS(ob) == 0) {
1358                         BKE_reportf(op->reports, RPT_WARNING,
1359                                 "Cannot delete object '%s' from scene '%s', indirectly used objects need at least one user",
1360                                 ob->id.name + 2, scene->id.name + 2);
1361                         continue;
1362                 }
1363
1364                 /* if grease pencil object, set cache as dirty */
1365                 if (ob->type == OB_GPENCIL) {
1366                         bGPdata *gpd = (bGPdata *)ob->data;
1367                         DEG_id_tag_update(&gpd->id, ID_RECALC_TRANSFORM | ID_RECALC_GEOMETRY);
1368                 }
1369
1370                 /* This is sort of a quick hack to address T51243 - Proper thing to do here would be to nuke most of all this
1371                  * custom scene/object/base handling, and use generic lib remap/query for that.
1372                  * But this is for later (aka 2.8, once layers & co are settled and working).
1373                  */
1374                 if (use_global && ob->id.lib == NULL) {
1375                         /* We want to nuke the object, let's nuke it the easy way (not for linked data though)... */
1376                         BKE_id_delete(bmain, &ob->id);
1377                         changed_count += 1;
1378                         continue;
1379                 }
1380
1381                 /* remove from Grease Pencil parent */
1382                 /* XXX This is likely not correct? Will also remove parent from grease pencil from other scenes,
1383                  *     even when use_global is false... */
1384                 for (bGPdata *gpd = bmain->gpencil.first; gpd; gpd = gpd->id.next) {
1385                         for (bGPDlayer *gpl = gpd->layers.first; gpl; gpl = gpl->next) {
1386                                 if (gpl->parent != NULL) {
1387                                         if (gpl->parent == ob) {
1388                                                 gpl->parent = NULL;
1389                                         }
1390                                 }
1391                         }
1392                 }
1393
1394                 /* remove from current scene only */
1395                 ED_object_base_free_and_unlink(bmain, scene, ob);
1396                 changed_count += 1;
1397
1398                 if (use_global) {
1399                         Scene *scene_iter;
1400                         for (scene_iter = bmain->scene.first; scene_iter; scene_iter = scene_iter->id.next) {
1401                                 if (scene_iter != scene && !ID_IS_LINKED(scene_iter)) {
1402                                         if (is_indirectly_used && ID_REAL_USERS(ob) <= 1 && ID_EXTRA_USERS(ob) == 0) {
1403                                                 BKE_reportf(op->reports, RPT_WARNING,
1404                                                             "Cannot delete object '%s' from scene '%s', indirectly used objects need at least one user",
1405                                                             ob->id.name + 2, scene_iter->id.name + 2);
1406                                                 break;
1407                                         }
1408                                         ED_object_base_free_and_unlink(bmain, scene_iter, ob);
1409                                 }
1410                         }
1411                 }
1412                 /* end global */
1413         }
1414         CTX_DATA_END;
1415
1416         BKE_reportf(op->reports, RPT_INFO, "Deleted %u object(s)", changed_count);
1417
1418         if (changed_count == 0) {
1419                 return OPERATOR_CANCELLED;
1420         }
1421
1422         /* delete has to handle all open scenes */
1423         BKE_main_id_tag_listbase(&bmain->scene, LIB_TAG_DOIT, true);
1424         for (win = wm->windows.first; win; win = win->next) {
1425                 scene = WM_window_get_active_scene(win);
1426
1427                 if (scene->id.tag & LIB_TAG_DOIT) {
1428                         scene->id.tag &= ~LIB_TAG_DOIT;
1429
1430                         DEG_relations_tag_update(bmain);
1431
1432                         DEG_id_tag_update(&scene->id, ID_RECALC_SELECT);
1433                         WM_event_add_notifier(C, NC_SCENE | ND_OB_ACTIVE, scene);
1434                         WM_event_add_notifier(C, NC_SCENE | ND_LAYER_CONTENT, scene);
1435                 }
1436         }
1437
1438         return OPERATOR_FINISHED;
1439 }
1440
1441 void OBJECT_OT_delete(wmOperatorType *ot)
1442 {
1443         /* identifiers */
1444         ot->name = "Delete";
1445         ot->description = "Delete selected objects";
1446         ot->idname = "OBJECT_OT_delete";
1447
1448         /* api callbacks */
1449         ot->invoke = WM_operator_confirm_or_exec;
1450         ot->exec = object_delete_exec;
1451         ot->poll = ED_operator_objectmode;
1452
1453         /* flags */
1454         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1455
1456         PropertyRNA *prop;
1457         prop = RNA_def_boolean(ot->srna, "use_global", 0, "Delete Globally", "Remove object from all scenes");
1458         RNA_def_property_flag(prop, PROP_HIDDEN | PROP_SKIP_SAVE);
1459         WM_operator_properties_confirm_or_exec(ot);
1460 }
1461
1462 /**************************** Copy Utilities ******************************/
1463
1464 /* after copying objects, copied data should get new pointers */
1465 static void copy_object_set_idnew(bContext *C)
1466 {
1467         Main *bmain = CTX_data_main(C);
1468
1469         CTX_DATA_BEGIN (C, Object *, ob, selected_editable_objects)
1470         {
1471                 BKE_libblock_relink_to_newid(&ob->id);
1472         }
1473         CTX_DATA_END;
1474
1475         BKE_main_id_clear_newpoins(bmain);
1476 }
1477
1478 /********************* Make Duplicates Real ************************/
1479
1480 /**
1481  * \note regarding hashing dupli-objects when using OB_DUPLICOLLECTION, skip the first member of #DupliObject.persistent_id
1482  * since its a unique index and we only want to know if the group objects are from the same dupli-group instance.
1483  */
1484 static unsigned int dupliobject_group_hash(const void *ptr)
1485 {
1486         const DupliObject *dob = ptr;
1487         unsigned int hash = BLI_ghashutil_ptrhash(dob->ob);
1488         unsigned int i;
1489         for (i = 1; (i < MAX_DUPLI_RECUR) && dob->persistent_id[i] != INT_MAX; i++) {
1490                 hash ^= (dob->persistent_id[i] ^ i);
1491         }
1492         return hash;
1493 }
1494
1495 /**
1496  * \note regarding hashing dupli-objects when NOT using OB_DUPLICOLLECTION, include the first member of #DupliObject.persistent_id
1497  * since its the index of the vertex/face the object is instantiated on and we want to identify objects on the same vertex/face.
1498  */
1499 static unsigned int dupliobject_hash(const void *ptr)
1500 {
1501         const DupliObject *dob = ptr;
1502         unsigned int hash = BLI_ghashutil_ptrhash(dob->ob);
1503         hash ^= (dob->persistent_id[0] ^ 0);
1504         return hash;
1505 }
1506
1507 /* Compare function that matches dupliobject_group_hash */
1508 static bool dupliobject_group_cmp(const void *a_, const void *b_)
1509 {
1510         const DupliObject *a = a_;
1511         const DupliObject *b = b_;
1512         unsigned int i;
1513
1514         if (a->ob != b->ob) {
1515                 return true;
1516         }
1517
1518         for (i = 1; (i < MAX_DUPLI_RECUR); i++) {
1519                 if (a->persistent_id[i] != b->persistent_id[i]) {
1520                         return true;
1521                 }
1522                 else if (a->persistent_id[i] == INT_MAX) {
1523                         break;
1524                 }
1525         }
1526
1527         /* matching */
1528         return false;
1529 }
1530
1531 /* Compare function that matches dupliobject_hash */
1532 static bool dupliobject_cmp(const void *a_, const void *b_)
1533 {
1534         const DupliObject *a = a_;
1535         const DupliObject *b = b_;
1536
1537         if (a->ob != b->ob) {
1538                 return true;
1539         }
1540
1541         if (a->persistent_id[0] != b->persistent_id[0]) {
1542                 return true;
1543         }
1544
1545         /* matching */
1546         return false;
1547 }
1548
1549 static void make_object_duplilist_real(bContext *C, Scene *scene, Base *base,
1550                                        const bool use_base_parent,
1551                                        const bool use_hierarchy)
1552 {
1553         Main *bmain = CTX_data_main(C);
1554         ViewLayer *view_layer = CTX_data_view_layer(C);
1555         Depsgraph *depsgraph = CTX_data_depsgraph(C);
1556         ListBase *lb_duplis;
1557         DupliObject *dob;
1558         GHash *dupli_gh, *parent_gh = NULL;
1559
1560         if (!(base->object->transflag & OB_DUPLI)) {
1561                 return;
1562         }
1563
1564         Object *object_eval = DEG_get_evaluated_object(depsgraph, base->object);
1565         lb_duplis = object_duplilist(depsgraph, scene, object_eval);
1566
1567         dupli_gh = BLI_ghash_ptr_new(__func__);
1568         if (use_hierarchy) {
1569                 if (base->object->transflag & OB_DUPLICOLLECTION) {
1570                         parent_gh = BLI_ghash_new(dupliobject_group_hash, dupliobject_group_cmp, __func__);
1571                 }
1572                 else {
1573                         parent_gh = BLI_ghash_new(dupliobject_hash, dupliobject_cmp, __func__);
1574                 }
1575         }
1576
1577         for (dob = lb_duplis->first; dob; dob = dob->next) {
1578                 Object *ob_src = DEG_get_original_object(dob->ob);
1579                 Object *ob_dst = ID_NEW_SET(dob->ob, BKE_object_copy(bmain, ob_src));
1580                 Base *base_dst;
1581
1582                 /* font duplis can have a totcol without material, we get them from parent
1583                  * should be implemented better...
1584                  */
1585                 if (ob_dst->mat == NULL) {
1586                         ob_dst->totcol = 0;
1587                 }
1588
1589                 BKE_collection_object_add_from(bmain, scene, base->object, ob_dst);
1590                 base_dst = BKE_view_layer_base_find(view_layer, ob_dst);
1591                 BLI_assert(base_dst != NULL);
1592
1593                 BKE_scene_object_base_flag_sync_from_base(base_dst);
1594
1595                 /* make sure apply works */
1596                 BKE_animdata_free(&ob_dst->id, true);
1597                 ob_dst->adt = NULL;
1598
1599                 /* Proxies are not to be copied. */
1600                 ob_dst->proxy_from = NULL;
1601                 ob_dst->proxy_group = NULL;
1602                 ob_dst->proxy = NULL;
1603
1604                 ob_dst->parent = NULL;
1605                 BKE_constraints_free(&ob_dst->constraints);
1606                 ob_dst->runtime.curve_cache = NULL;
1607                 ob_dst->transflag &= ~OB_DUPLI;
1608
1609                 copy_m4_m4(ob_dst->obmat, dob->mat);
1610                 BKE_object_apply_mat4(ob_dst, ob_dst->obmat, false, false);
1611
1612                 BLI_ghash_insert(dupli_gh, dob, ob_dst);
1613                 if (parent_gh) {
1614                         void **val;
1615                         /* Due to nature of hash/comparison of this ghash, a lot of duplis may be considered as
1616                          * 'the same', this avoids trying to insert same key several time and
1617                          * raise asserts in debug builds... */
1618                         if (!BLI_ghash_ensure_p(parent_gh, dob, &val)) {
1619                                 *val = ob_dst;
1620                         }
1621                 }
1622         }
1623
1624         for (dob = lb_duplis->first; dob; dob = dob->next) {
1625                 Object *ob_src = dob->ob;
1626                 Object *ob_dst = BLI_ghash_lookup(dupli_gh, dob);
1627
1628                 /* Remap new object to itself, and clear again newid pointer of orig object. */
1629                 BKE_libblock_relink_to_newid(&ob_dst->id);
1630
1631                 DEG_id_tag_update(&ob_dst->id, ID_RECALC_GEOMETRY);
1632
1633                 if (use_hierarchy) {
1634                         /* original parents */
1635                         Object *ob_src_par = ob_src->parent;
1636                         Object *ob_dst_par = NULL;
1637
1638                         /* find parent that was also made real */
1639                         if (ob_src_par) {
1640                                 /* OK to keep most of the members uninitialized,
1641                                  * they won't be read, this is simply for a hash lookup. */
1642                                 DupliObject dob_key;
1643                                 dob_key.ob = ob_src_par;
1644                                 if (base->object->transflag & OB_DUPLICOLLECTION) {
1645                                         memcpy(&dob_key.persistent_id[1],
1646                                                &dob->persistent_id[1],
1647                                                sizeof(dob->persistent_id[1]) * (MAX_DUPLI_RECUR - 1));
1648                                 }
1649                                 else {
1650                                         dob_key.persistent_id[0] = dob->persistent_id[0];
1651                                 }
1652                                 ob_dst_par = BLI_ghash_lookup(parent_gh, &dob_key);
1653                         }
1654
1655                         if (ob_dst_par) {
1656                                 /* allow for all possible parent types */
1657                                 ob_dst->partype = ob_src->partype;
1658                                 BLI_strncpy(ob_dst->parsubstr, ob_src->parsubstr, sizeof(ob_dst->parsubstr));
1659                                 ob_dst->par1 = ob_src->par1;
1660                                 ob_dst->par2 = ob_src->par2;
1661                                 ob_dst->par3 = ob_src->par3;
1662
1663                                 copy_m4_m4(ob_dst->parentinv, ob_src->parentinv);
1664
1665                                 ob_dst->parent = ob_dst_par;
1666                         }
1667                         else if (use_base_parent) {
1668                                 ob_dst->parent = base->object;
1669                                 ob_dst->partype = PAROBJECT;
1670                         }
1671                 }
1672                 else if (use_base_parent) {
1673                         /* since we are ignoring the internal hierarchy - parent all to the
1674                          * base object */
1675                         ob_dst->parent = base->object;
1676                         ob_dst->partype = PAROBJECT;
1677                 }
1678
1679                 if (ob_dst->parent) {
1680                         /* note, this may be the parent of other objects, but it should
1681                          * still work out ok */
1682                         BKE_object_apply_mat4(ob_dst, dob->mat, false, true);
1683
1684                         /* to set ob_dst->orig and in case there's any other discrepancies */
1685                         DEG_id_tag_update(&ob_dst->id, ID_RECALC_TRANSFORM);
1686                 }
1687         }
1688
1689         if (base->object->transflag & OB_DUPLICOLLECTION && base->object->instance_collection) {
1690                 for (Object *ob = bmain->object.first; ob; ob = ob->id.next) {
1691                         if (ob->proxy_group == base->object) {
1692                                 ob->proxy = NULL;
1693                                 ob->proxy_from = NULL;
1694                                 DEG_id_tag_update(&ob->id, ID_RECALC_TRANSFORM);
1695                         }
1696                 }
1697         }
1698
1699         BLI_ghash_free(dupli_gh, NULL, NULL);
1700         if (parent_gh) {
1701                 BLI_ghash_free(parent_gh, NULL, NULL);
1702         }
1703
1704         free_object_duplilist(lb_duplis);
1705
1706         BKE_main_id_clear_newpoins(bmain);
1707
1708         base->object->transflag &= ~OB_DUPLI;
1709         DEG_id_tag_update(&base->object->id, ID_RECALC_COPY_ON_WRITE);
1710 }
1711
1712 static int object_duplicates_make_real_exec(bContext *C, wmOperator *op)
1713 {
1714         Main *bmain = CTX_data_main(C);
1715         Scene *scene = CTX_data_scene(C);
1716
1717         const bool use_base_parent = RNA_boolean_get(op->ptr, "use_base_parent");
1718         const bool use_hierarchy = RNA_boolean_get(op->ptr, "use_hierarchy");
1719
1720         BKE_main_id_clear_newpoins(bmain);
1721
1722         CTX_DATA_BEGIN (C, Base *, base, selected_editable_bases)
1723         {
1724                 make_object_duplilist_real(C, scene, base, use_base_parent, use_hierarchy);
1725
1726                 /* dependencies were changed */
1727                 WM_event_add_notifier(C, NC_OBJECT | ND_PARENT, base->object);
1728         }
1729         CTX_DATA_END;
1730
1731         DEG_relations_tag_update(bmain);
1732         WM_event_add_notifier(C, NC_SCENE, scene);
1733         WM_main_add_notifier(NC_OBJECT | ND_DRAW, NULL);
1734
1735         return OPERATOR_FINISHED;
1736 }
1737
1738 void OBJECT_OT_duplicates_make_real(wmOperatorType *ot)
1739 {
1740         /* identifiers */
1741         ot->name = "Make Duplicates Real";
1742         ot->description = "Make dupli objects attached to this object real";
1743         ot->idname = "OBJECT_OT_duplicates_make_real";
1744
1745         /* api callbacks */
1746         ot->exec = object_duplicates_make_real_exec;
1747
1748         ot->poll = ED_operator_objectmode;
1749
1750         /* flags */
1751         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO | OPTYPE_USE_EVAL_DATA;
1752
1753         RNA_def_boolean(ot->srna, "use_base_parent", 0, "Parent", "Parent newly created objects to the original duplicator");
1754         RNA_def_boolean(ot->srna, "use_hierarchy", 0, "Keep Hierarchy", "Maintain parent child relationships");
1755 }
1756
1757 /**************************** Convert **************************/
1758
1759 static const EnumPropertyItem convert_target_items[] = {
1760         {OB_CURVE, "CURVE", ICON_OUTLINER_OB_CURVE, "Curve from Mesh/Text", ""},
1761         {OB_MESH, "MESH", ICON_OUTLINER_OB_MESH, "Mesh from Curve/Meta/Surf/Text", ""},
1762         {0, NULL, 0, NULL, NULL},
1763 };
1764
1765 static void convert_ensure_curve_cache(Depsgraph *depsgraph, Scene *scene, Object *ob)
1766 {
1767         if (ob->runtime.curve_cache == NULL) {
1768                 /* Force creation. This is normally not needed but on operator
1769                  * redo we might end up with an object which isn't evaluated yet.
1770                  * Also happens in case we are working on a copy of the object (all its caches have been nuked then).
1771                  */
1772                 if (ELEM(ob->type, OB_SURF, OB_CURVE, OB_FONT)) {
1773                         /* We need 'for render' ON here, to enable computing bevel dipslist if needed.
1774                          * Also makes sense anyway, we would not want e.g. to loose hidden parts etc. */
1775                         BKE_displist_make_curveTypes(depsgraph, scene, ob, true, false, NULL);
1776                 }
1777                 else if (ob->type == OB_MBALL) {
1778                         BKE_displist_make_mball(depsgraph, scene, ob);
1779                 }
1780         }
1781 }
1782
1783 static void curvetomesh(Main *bmain, Depsgraph *depsgraph, Scene *scene, Object *ob)
1784 {
1785         convert_ensure_curve_cache(depsgraph, scene, ob);
1786         BKE_mesh_from_nurbs(bmain, ob); /* also does users */
1787
1788         if (ob->type == OB_MESH) {
1789                 BKE_object_free_modifiers(ob, 0);
1790         }
1791 }
1792
1793 static bool convert_poll(bContext *C)
1794 {
1795         Scene *scene = CTX_data_scene(C);
1796         Base *base_act = CTX_data_active_base(C);
1797         Object *obact = base_act ? base_act->object : NULL;
1798
1799         return (!ID_IS_LINKED(scene) && obact && (BKE_object_is_in_editmode(obact) == false) &&
1800                 (base_act->flag & BASE_SELECTED) && !ID_IS_LINKED(obact));
1801 }
1802
1803 /* Helper for convert_exec */
1804 static Base *duplibase_for_convert(Main *bmain, Scene *scene, ViewLayer *view_layer, Base *base, Object *ob)
1805 {
1806         Object *obn;
1807         Base *basen;
1808
1809         if (ob == NULL) {
1810                 ob = base->object;
1811         }
1812
1813         obn = BKE_object_copy(bmain, ob);
1814         DEG_id_tag_update(&ob->id, ID_RECALC_TRANSFORM | ID_RECALC_GEOMETRY | ID_RECALC_ANIMATION);
1815         BKE_collection_object_add_from(bmain, scene, ob, obn);
1816
1817         basen = BKE_view_layer_base_find(view_layer, obn);
1818         ED_object_base_select(basen, BA_SELECT);
1819         ED_object_base_select(basen, BA_DESELECT);
1820         return basen;
1821 }
1822
1823 static int convert_exec(bContext *C, wmOperator *op)
1824 {
1825         Main *bmain = CTX_data_main(C);
1826         Depsgraph *depsgraph = CTX_data_depsgraph(C);
1827         Scene *scene = CTX_data_scene(C);
1828         ViewLayer *view_layer = CTX_data_view_layer(C);
1829         Base *basen = NULL, *basact = NULL;
1830         Object *ob1, *obact = CTX_data_active_object(C);
1831         Curve *cu;
1832         Nurb *nu;
1833         MetaBall *mb;
1834         Mesh *me;
1835         const short target = RNA_enum_get(op->ptr, "target");
1836         bool keep_original = RNA_boolean_get(op->ptr, "keep_original");
1837         int a, mballConverted = 0;
1838
1839         /* don't forget multiple users! */
1840
1841         {
1842                 FOREACH_SCENE_OBJECT_BEGIN(scene, ob)
1843                 {
1844                         ob->flag &= ~OB_DONE;
1845
1846                         /* flag data that's not been edited (only needed for !keep_original) */
1847                         if (ob->data) {
1848                                 ((ID *)ob->data)->tag |= LIB_TAG_DOIT;
1849                         }
1850
1851                         /* possible metaball basis is not in this scene */
1852                         if (ob->type == OB_MBALL && target == OB_MESH) {
1853                                 if (BKE_mball_is_basis(ob) == false) {
1854                                         Object *ob_basis;
1855                                         ob_basis = BKE_mball_basis_find(scene, ob);
1856                                         if (ob_basis) {
1857                                                 ob_basis->flag &= ~OB_DONE;
1858                                         }
1859                                 }
1860                         }
1861                 }
1862                 FOREACH_SCENE_OBJECT_END;
1863         }
1864
1865         ListBase selected_editable_bases = CTX_data_collection_get(C, "selected_editable_bases");
1866
1867         /* Ensure we get all meshes calculated with a sufficient data-mask,
1868          * needed since re-evaluating single modifiers causes bugs if they depend
1869          * on other objects data masks too, see: T50950. */
1870         {
1871                 for (CollectionPointerLink *link = selected_editable_bases.first; link; link = link->next) {
1872                         Base *base = link->ptr.data;
1873                         Object *ob = base->object;
1874
1875                         /* The way object type conversion works currently (enforcing conversion of *all* objects using converted
1876                          * object-data, even some un-selected/hidden/another scene ones, sounds totally bad to me.
1877                          * However, changing this is more design than bug-fix, not to mention convoluted code below,
1878                          * so that will be for later.
1879                          * But at the very least, do not do that with linked IDs! */
1880                         if ((ID_IS_LINKED(ob) || (ob->data && ID_IS_LINKED(ob->data))) && !keep_original) {
1881                                 keep_original = true;
1882                                 BKE_reportf(op->reports, RPT_INFO,
1883                                             "Converting some linked object/object data, enforcing 'Keep Original' option to True");
1884                         }
1885
1886                         DEG_id_tag_update(&base->object->id, ID_RECALC_GEOMETRY);
1887                 }
1888
1889                 uint64_t customdata_mask_prev = scene->customdata_mask;
1890                 scene->customdata_mask |= CD_MASK_MESH;
1891                 BKE_scene_graph_update_tagged(depsgraph, bmain);
1892                 scene->customdata_mask = customdata_mask_prev;
1893         }
1894
1895         for (CollectionPointerLink *link = selected_editable_bases.first; link; link = link->next) {
1896                 Object *newob = NULL;
1897                 Base *base = link->ptr.data;
1898                 Object *ob = base->object;
1899
1900                 if (ob->flag & OB_DONE || !IS_TAGGED(ob->data)) {
1901                         if (ob->type != target) {
1902                                 base->flag &= ~SELECT;
1903                                 ob->flag &= ~SELECT;
1904                         }
1905
1906                         /* obdata already modified */
1907                         if (!IS_TAGGED(ob->data)) {
1908                                 /* When 2 objects with linked data are selected, converting both
1909                                  * would keep modifiers on all but the converted object [#26003] */
1910                                 if (ob->type == OB_MESH) {
1911                                         BKE_object_free_modifiers(ob, 0);  /* after derivedmesh calls! */
1912                                 }
1913                                 if (ob->type == OB_GPENCIL) {
1914                                         BKE_object_free_modifiers(ob, 0);  /* after derivedmesh calls! */
1915                                         BKE_object_free_shaderfx(ob, 0);
1916                                 }
1917                         }
1918                 }
1919                 else if (ob->type == OB_MESH && target == OB_CURVE) {
1920                         ob->flag |= OB_DONE;
1921
1922                         if (keep_original) {
1923                                 basen = duplibase_for_convert(bmain, scene, view_layer, base, NULL);
1924                                 newob = basen->object;
1925
1926                                 /* decrement original mesh's usage count  */
1927                                 me = newob->data;
1928                                 id_us_min(&me->id);
1929
1930                                 /* make a new copy of the mesh */
1931                                 newob->data = BKE_mesh_copy(bmain, me);
1932                         }
1933                         else {
1934                                 newob = ob;
1935                         }
1936
1937                         BKE_mesh_to_curve(bmain, depsgraph, scene, newob);
1938
1939                         if (newob->type == OB_CURVE) {
1940                                 BKE_object_free_modifiers(newob, 0);   /* after derivedmesh calls! */
1941                                 ED_rigidbody_object_remove(bmain, scene, newob);
1942                         }
1943                 }
1944                 else if (ob->type == OB_MESH) {
1945                         ob->flag |= OB_DONE;
1946
1947                         if (keep_original) {
1948                                 basen = duplibase_for_convert(bmain, scene, view_layer, base, NULL);
1949                                 newob = basen->object;
1950
1951                                 /* decrement original mesh's usage count  */
1952                                 me = newob->data;
1953                                 id_us_min(&me->id);
1954
1955                                 /* make a new copy of the mesh */
1956                                 newob->data = BKE_mesh_copy(bmain, me);
1957                         }
1958                         else {
1959                                 newob = ob;
1960                                 DEG_id_tag_update(&ob->id, ID_RECALC_TRANSFORM | ID_RECALC_GEOMETRY | ID_RECALC_ANIMATION);
1961                         }
1962
1963                         /* make new mesh data from the original copy */
1964                         /* note: get the mesh from the original, not from the copy in some
1965                          * cases this doesn't give correct results (when MDEF is used for eg)
1966                          */
1967                         Scene *scene_eval = (Scene *)DEG_get_evaluated_id(depsgraph, &scene->id);
1968                         Object *ob_eval = DEG_get_evaluated_object(depsgraph, ob);
1969                         Mesh *me_eval = mesh_get_eval_final(depsgraph, scene_eval, ob_eval, CD_MASK_MESH);
1970                         me_eval = BKE_mesh_copy_for_eval(me_eval, false);
1971                         BKE_mesh_nomain_to_mesh(me_eval, newob->data, newob, CD_MASK_MESH, true);
1972                         BKE_object_free_modifiers(newob, 0);   /* after derivedmesh calls! */
1973                 }
1974                 else if (ob->type == OB_FONT) {
1975                         ob->flag |= OB_DONE;
1976
1977                         if (keep_original) {
1978                                 basen = duplibase_for_convert(bmain, scene, view_layer, base, NULL);
1979                                 newob = basen->object;
1980
1981                                 /* decrement original curve's usage count  */
1982                                 id_us_min(&((Curve *)newob->data)->id);
1983
1984                                 /* make a new copy of the curve */
1985                                 newob->data = BKE_curve_copy(bmain, ob->data);
1986                         }
1987                         else {
1988                                 newob = ob;
1989                         }
1990
1991                         cu = newob->data;
1992
1993                         /* TODO(sergey): Ideally DAG will create nurbs list for a curve data
1994                          *               datablock, but for until we've got granular update
1995                          *               lets take care by selves.
1996                          */
1997                         /* XXX This may fail/crash, since BKE_vfont_to_curve()
1998                          * accesses evaluated data in some cases (bastien). */
1999                         BKE_vfont_to_curve(newob, FO_EDIT);
2000
2001                         newob->type = OB_CURVE;
2002                         cu->type = OB_CURVE;
2003
2004                         if (cu->vfont) {
2005                                 id_us_min(&cu->vfont->id);
2006                                 cu->vfont = NULL;
2007                         }
2008                         if (cu->vfontb) {
2009                                 id_us_min(&cu->vfontb->id);
2010                                 cu->vfontb = NULL;
2011                         }
2012                         if (cu->vfonti) {
2013                                 id_us_min(&cu->vfonti->id);
2014                                 cu->vfonti = NULL;
2015                         }
2016                         if (cu->vfontbi) {
2017                                 id_us_min(&cu->vfontbi->id);
2018                                 cu->vfontbi = NULL;
2019                         }
2020
2021                         if (!keep_original) {
2022                                 /* other users */
2023                                 if (cu->id.us > 1) {
2024                                         for (ob1 = bmain->object.first; ob1; ob1 = ob1->id.next) {
2025                                                 if (ob1->data == ob->data) {
2026                                                         ob1->type = OB_CURVE;
2027                                                         DEG_id_tag_update(&ob1->id, ID_RECALC_TRANSFORM | ID_RECALC_GEOMETRY | ID_RECALC_ANIMATION);
2028                                                 }
2029                                         }
2030                                 }
2031                         }
2032
2033                         for (nu = cu->nurb.first; nu; nu = nu->next)
2034                                 nu->charidx = 0;
2035
2036                         cu->flag &= ~CU_3D;
2037                         BKE_curve_curve_dimension_update(cu);
2038
2039                         if (target == OB_MESH) {
2040                                 curvetomesh(bmain, depsgraph, scene, newob);
2041
2042                                 /* meshes doesn't use displist */
2043                                 BKE_object_free_curve_cache(newob);
2044                         }
2045                 }
2046                 else if (ELEM(ob->type, OB_CURVE, OB_SURF)) {
2047                         ob->flag |= OB_DONE;
2048
2049                         if (target == OB_MESH) {
2050                                 if (keep_original) {
2051                                         basen = duplibase_for_convert(bmain, scene, view_layer, base, NULL);
2052                                         newob = basen->object;
2053
2054                                         /* decrement original curve's usage count  */
2055                                         id_us_min(&((Curve *)newob->data)->id);
2056
2057                                         /* make a new copy of the curve */
2058                                         newob->data = BKE_curve_copy(bmain, ob->data);
2059                                 }
2060                                 else {
2061                                         newob = ob;
2062                                 }
2063
2064                                 curvetomesh(bmain, depsgraph, scene, newob);
2065
2066                                 /* meshes doesn't use displist */
2067                                 BKE_object_free_curve_cache(newob);
2068                         }
2069                 }
2070                 else if (ob->type == OB_MBALL && target == OB_MESH) {
2071                         Object *baseob;
2072
2073                         base->flag &= ~BASE_SELECTED;
2074                         ob->base_flag &= ~BASE_SELECTED;
2075
2076                         baseob = BKE_mball_basis_find(scene, ob);
2077
2078                         if (ob != baseob) {
2079                                 /* if motherball is converting it would be marked as done later */
2080                                 ob->flag |= OB_DONE;
2081                         }
2082
2083                         if (!(baseob->flag & OB_DONE)) {
2084                                 baseob->flag |= OB_DONE;
2085
2086                                 basen = duplibase_for_convert(bmain, scene, view_layer, base, baseob);
2087                                 newob = basen->object;
2088
2089                                 mb = newob->data;
2090                                 id_us_min(&mb->id);
2091
2092                                 newob->data = BKE_mesh_add(bmain, "Mesh");
2093                                 newob->type = OB_MESH;
2094
2095                                 me = newob->data;
2096                                 me->totcol = mb->totcol;
2097                                 if (newob->totcol) {
2098                                         me->mat = MEM_dupallocN(mb->mat);
2099                                         for (a = 0; a < newob->totcol; a++) id_us_plus((ID *)me->mat[a]);
2100                                 }
2101
2102                                 convert_ensure_curve_cache(depsgraph, scene, baseob);
2103                                 BKE_mesh_from_metaball(&baseob->runtime.curve_cache->disp, newob->data);
2104
2105                                 if (obact->type == OB_MBALL) {
2106                                         basact = basen;
2107                                 }
2108
2109                                 mballConverted = 1;
2110                         }
2111                 }
2112                 else {
2113                         continue;
2114                 }
2115
2116                 /* Ensure new object has consistent material data with its new obdata. */
2117                 if (newob) {
2118                         test_object_materials(bmain, newob, newob->data);
2119                 }
2120
2121                 /* tag obdata if it was been changed */
2122
2123                 /* If the original object is active then make this object active */
2124                 if (basen) {
2125                         if (ob == obact) {
2126                                 /* store new active base to update BASACT */
2127                                 basact = basen;
2128                         }
2129
2130                         basen = NULL;
2131                 }
2132
2133                 if (!keep_original && (ob->flag & OB_DONE)) {
2134                         DEG_id_tag_update(&ob->id, ID_RECALC_GEOMETRY);
2135                         ((ID *)ob->data)->tag &= ~LIB_TAG_DOIT; /* flag not to convert this datablock again */
2136                 }
2137         }
2138         BLI_freelistN(&selected_editable_bases);
2139
2140         if (!keep_original) {
2141                 if (mballConverted) {
2142                         FOREACH_SCENE_OBJECT_BEGIN(scene, ob_mball)
2143                         {
2144                                 if (ob_mball->type == OB_MBALL) {
2145                                         if (ob_mball->flag & OB_DONE) {
2146                                                 Object *ob_basis = NULL;
2147                                                 if (BKE_mball_is_basis(ob_mball) ||
2148                                                     ((ob_basis = BKE_mball_basis_find(scene, ob_mball)) && (ob_basis->flag & OB_DONE)))
2149                                                 {
2150                                                         ED_object_base_free_and_unlink(bmain, scene, ob_mball);
2151                                                 }
2152                                         }
2153                                 }
2154                         }
2155                         FOREACH_SCENE_OBJECT_END;
2156                 }
2157         }
2158
2159 // XXX  ED_object_editmode_enter(C, 0);
2160 // XXX  exit_editmode(C, EM_FREEDATA|); /* freedata, but no undo */
2161
2162         if (basact) {
2163                 /* active base was changed */
2164                 ED_object_base_activate(C, basact);
2165                 BASACT(view_layer) = basact;
2166         }
2167         else if (BASACT(view_layer)->object->flag & OB_DONE) {
2168                 WM_event_add_notifier(C, NC_OBJECT | ND_MODIFIER, BASACT(view_layer)->object);
2169                 WM_event_add_notifier(C, NC_OBJECT | ND_DATA, BASACT(view_layer)->object);
2170         }
2171
2172         DEG_relations_tag_update(bmain);
2173         DEG_id_tag_update(&scene->id, ID_RECALC_SELECT);
2174         WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, scene);
2175         WM_event_add_notifier(C, NC_SCENE | ND_OB_SELECT, scene);
2176
2177         return OPERATOR_FINISHED;
2178 }
2179
2180
2181 void OBJECT_OT_convert(wmOperatorType *ot)
2182 {
2183         /* identifiers */
2184         ot->name = "Convert to";
2185         ot->description = "Convert selected objects to another type";
2186         ot->idname = "OBJECT_OT_convert";
2187
2188         /* api callbacks */
2189         ot->invoke = WM_menu_invoke;
2190         ot->exec = convert_exec;
2191         ot->poll = convert_poll;
2192
2193         /* flags */
2194         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2195
2196         /* properties */
2197         ot->prop = RNA_def_enum(ot->srna, "target", convert_target_items, OB_MESH, "Target", "Type of object to convert to");
2198         RNA_def_boolean(ot->srna, "keep_original", 0, "Keep Original", "Keep original objects instead of replacing them");
2199 }
2200
2201 /**************************** Duplicate ************************/
2202
2203 /*
2204  * dupflag: a flag made from constants declared in DNA_userdef_types.h
2205  * The flag tells adduplicate() whether to copy data linked to the object, or to reference the existing data.
2206  * U.dupflag for default operations or you can construct a flag as python does
2207  * if the dupflag is 0 then no data will be copied (linked duplicate) */
2208
2209 /* used below, assumes id.new is correct */
2210 /* leaves selection of base/object unaltered */
2211 /* Does set ID->newid pointers. */
2212 static Base *object_add_duplicate_internal(Main *bmain, Scene *scene, ViewLayer *view_layer, Object *ob, int dupflag)
2213 {
2214         Base *base, *basen = NULL;
2215         Object *obn;
2216
2217         if (ob->mode & OB_MODE_POSE) {
2218                 ; /* nothing? */
2219         }
2220         else {
2221                 obn = ID_NEW_SET(ob, BKE_object_duplicate(bmain, ob, dupflag));
2222                 DEG_id_tag_update(&obn->id, ID_RECALC_TRANSFORM | ID_RECALC_GEOMETRY);
2223
2224                 base = BKE_view_layer_base_find(view_layer, ob);
2225                 if ((base != NULL) && (base->flag & BASE_VISIBLE)) {
2226                         BKE_collection_object_add_from(bmain, scene, ob, obn);
2227                 }
2228                 else {
2229                         LayerCollection *layer_collection = BKE_layer_collection_get_active(view_layer);
2230                         BKE_collection_object_add(bmain, layer_collection->collection, obn);
2231                 }
2232
2233                 basen = BKE_view_layer_base_find(view_layer, obn);
2234                 if (base != NULL) {
2235                         basen->local_view_bits = base->local_view_bits;
2236                 }
2237
2238                 /* 1) duplis should end up in same collection as the original
2239                  * 2) Rigid Body sim participants MUST always be part of a collection...
2240                  */
2241                 // XXX: is 2) really a good measure here?
2242                 if (ob->rigidbody_object || ob->rigidbody_constraint) {
2243                         Collection *collection;
2244                         for (collection = bmain->collection.first; collection; collection = collection->id.next) {
2245                                 if (BKE_collection_has_object(collection, ob))
2246                                         BKE_collection_object_add(bmain, collection, obn);
2247                         }
2248                 }
2249         }
2250         return basen;
2251 }
2252
2253 /* single object duplicate, if dupflag==0, fully linked, else it uses the flags given */
2254 /* leaves selection of base/object unaltered.
2255  * note: don't call this within a loop since clear_* funcs loop over the entire database.
2256  * note: caller must do DAG_relations_tag_update(bmain);
2257  *       this is not done automatic since we may duplicate many objects in a batch */
2258 Base *ED_object_add_duplicate(Main *bmain, Scene *scene, ViewLayer *view_layer, Base *base, int dupflag)
2259 {
2260         Base *basen;
2261         Object *ob;
2262
2263         basen = object_add_duplicate_internal(bmain, scene, view_layer, base->object, dupflag);
2264         if (basen == NULL) {
2265                 return NULL;
2266         }
2267
2268         ob = basen->object;
2269
2270         /* link own references to the newly duplicated data [#26816] */
2271         BKE_libblock_relink_to_newid(&ob->id);
2272
2273         /* DAG_relations_tag_update(bmain); */ /* caller must do */
2274
2275         if (ob->data != NULL) {
2276                 DEG_id_tag_update_ex(bmain, (ID *)ob->data, ID_RECALC_EDITORS);
2277         }
2278
2279         BKE_main_id_clear_newpoins(bmain);
2280
2281         return basen;
2282 }
2283
2284 /* contextual operator dupli */
2285 static int duplicate_exec(bContext *C, wmOperator *op)
2286 {
2287         Main *bmain = CTX_data_main(C);
2288         Scene *scene = CTX_data_scene(C);
2289         ViewLayer *view_layer = CTX_data_view_layer(C);
2290         const bool linked = RNA_boolean_get(op->ptr, "linked");
2291         int dupflag = (linked) ? 0 : U.dupflag;
2292
2293         CTX_DATA_BEGIN (C, Base *, base, selected_bases)
2294         {
2295                 Base *basen = object_add_duplicate_internal(bmain, scene, view_layer, base->object, dupflag);
2296
2297                 /* note that this is safe to do with this context iterator,
2298                  * the list is made in advance */
2299                 ED_object_base_select(base, BA_DESELECT);
2300                 ED_object_base_select(basen, BA_SELECT);
2301
2302                 if (basen == NULL) {
2303                         continue;
2304                 }
2305
2306                 /* new object becomes active */
2307                 if (BASACT(view_layer) == base)
2308                         ED_object_base_activate(C, basen);
2309
2310                 if (basen->object->data) {
2311                         DEG_id_tag_update(basen->object->data, 0);
2312                 }
2313         }
2314         CTX_DATA_END;
2315
2316         copy_object_set_idnew(C);
2317
2318         DEG_relations_tag_update(bmain);
2319         DEG_id_tag_update(&scene->id, ID_RECALC_COPY_ON_WRITE | ID_RECALC_SELECT);
2320
2321         WM_event_add_notifier(C, NC_SCENE | ND_OB_SELECT, scene);
2322
2323         return OPERATOR_FINISHED;
2324 }
2325
2326 void OBJECT_OT_duplicate(wmOperatorType *ot)
2327 {
2328         PropertyRNA *prop;
2329
2330         /* identifiers */
2331         ot->name = "Duplicate Objects";
2332         ot->description = "Duplicate selected objects";
2333         ot->idname = "OBJECT_OT_duplicate";
2334
2335         /* api callbacks */
2336         ot->exec = duplicate_exec;
2337         ot->poll = ED_operator_objectmode;
2338
2339         /* flags */
2340         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2341
2342         /* to give to transform */
2343         RNA_def_boolean(ot->srna, "linked", 0, "Linked", "Duplicate object but not object data, linking to the original data");
2344         prop = RNA_def_enum(ot->srna, "mode", rna_enum_transform_mode_types, TFM_TRANSLATION, "Mode", "");
2345         RNA_def_property_flag(prop, PROP_HIDDEN);
2346 }
2347
2348 /* **************** add named object, for dragdrop ************* */
2349
2350 static int add_named_exec(bContext *C, wmOperator *op)
2351 {
2352         wmWindow *win = CTX_wm_window(C);
2353         const wmEvent *event = win ? win->eventstate : NULL;
2354         Main *bmain = CTX_data_main(C);
2355         Scene *scene = CTX_data_scene(C);
2356         ViewLayer *view_layer = CTX_data_view_layer(C);
2357         Base *basen;
2358         Object *ob;
2359         const bool linked = RNA_boolean_get(op->ptr, "linked");
2360         int dupflag = (linked) ? 0 : U.dupflag;
2361         char name[MAX_ID_NAME - 2];
2362
2363         /* find object, create fake base */
2364         RNA_string_get(op->ptr, "name", name);
2365         ob = (Object *)BKE_libblock_find_name(bmain, ID_OB, name);
2366
2367         if (ob == NULL) {
2368                 BKE_report(op->reports, RPT_ERROR, "Object not found");
2369                 return OPERATOR_CANCELLED;
2370         }
2371
2372         /* prepare dupli */
2373         basen = object_add_duplicate_internal(bmain, scene, view_layer, ob, dupflag);
2374
2375         if (basen == NULL) {
2376                 BKE_report(op->reports, RPT_ERROR, "Object could not be duplicated");
2377                 return OPERATOR_CANCELLED;
2378         }
2379
2380         BKE_scene_object_base_flag_sync_from_object(basen);
2381         basen->object->restrictflag &= ~OB_RESTRICT_VIEW;
2382
2383         if (event) {
2384                 ARegion *ar = CTX_wm_region(C);
2385                 const int mval[2] = {event->x - ar->winrct.xmin,
2386                                      event->y - ar->winrct.ymin};
2387                 ED_object_location_from_view(C, basen->object->loc);
2388                 ED_view3d_cursor3d_position(C, mval, false, basen->object->loc);
2389         }
2390
2391         ED_object_base_select(basen, BA_SELECT);
2392         ED_object_base_activate(C, basen);
2393
2394         copy_object_set_idnew(C);
2395
2396         /* TODO(sergey): Only update relations for the current scene. */
2397         DEG_relations_tag_update(bmain);
2398
2399         DEG_id_tag_update(&scene->id, ID_RECALC_SELECT);
2400         WM_event_add_notifier(C, NC_SCENE | ND_OB_SELECT, scene);
2401         WM_event_add_notifier(C, NC_SCENE | ND_OB_ACTIVE, scene);
2402
2403         return OPERATOR_FINISHED;
2404 }
2405
2406 void OBJECT_OT_add_named(wmOperatorType *ot)
2407 {
2408         /* identifiers */
2409         ot->name = "Add Named Object";
2410         ot->description = "Add named object";
2411         ot->idname = "OBJECT_OT_add_named";
2412
2413         /* api callbacks */
2414         ot->exec = add_named_exec;
2415         ot->poll = ED_operator_objectmode;
2416
2417         /* flags */
2418         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2419
2420         RNA_def_boolean(ot->srna, "linked", 0, "Linked", "Duplicate object but not object data, linking to the original data");
2421         RNA_def_string(ot->srna, "name", NULL, MAX_ID_NAME - 2, "Name", "Object name to add");
2422 }
2423
2424 /**************************** Join *************************/
2425
2426 static bool join_poll(bContext *C)
2427 {
2428         Object *ob = CTX_data_active_object(C);
2429
2430         if (!ob || ID_IS_LINKED(ob)) return 0;
2431
2432         if (ELEM(ob->type, OB_MESH, OB_CURVE, OB_SURF, OB_ARMATURE, OB_GPENCIL))
2433                 return ED_operator_screenactive(C);
2434         else
2435                 return 0;
2436 }
2437
2438 static int join_exec(bContext *C, wmOperator *op)
2439 {
2440         Object *ob = CTX_data_active_object(C);
2441
2442         if (ob->mode & OB_MODE_EDIT) {
2443                 BKE_report(op->reports, RPT_ERROR, "This data does not support joining in edit mode");
2444                 return OPERATOR_CANCELLED;
2445         }
2446         else if (BKE_object_obdata_is_libdata(ob)) {
2447                 BKE_report(op->reports, RPT_ERROR, "Cannot edit external library data");
2448                 return OPERATOR_CANCELLED;
2449         }
2450         else if (ob->type == OB_GPENCIL) {
2451                 bGPdata *gpd = (bGPdata *)ob->data;
2452                 if ((!gpd) || GPENCIL_ANY_MODE(gpd)) {
2453                         BKE_report(op->reports, RPT_ERROR, "This data does not support joining in this mode");
2454                         return OPERATOR_CANCELLED;
2455                 }
2456         }
2457
2458         if (ob->type == OB_MESH)
2459                 return join_mesh_exec(C, op);
2460         else if (ELEM(ob->type, OB_CURVE, OB_SURF))
2461                 return join_curve_exec(C, op);
2462         else if (ob->type == OB_ARMATURE)
2463                 return join_armature_exec(C, op);
2464         else if (ob->type == OB_GPENCIL)
2465                 return ED_gpencil_join_objects_exec(C, op);
2466
2467         return OPERATOR_CANCELLED;
2468 }
2469
2470 void OBJECT_OT_join(wmOperatorType *ot)
2471 {
2472         /* identifiers */
2473         ot->name = "Join";
2474         ot->description = "Join selected objects into active object";
2475         ot->idname = "OBJECT_OT_join";
2476
2477         /* api callbacks */
2478         ot->exec = join_exec;
2479         ot->poll = join_poll;
2480
2481         /* flags */
2482         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2483 }
2484
2485 /**************************** Join as Shape Key*************************/
2486
2487 static bool join_shapes_poll(bContext *C)
2488 {
2489         Object *ob = CTX_data_active_object(C);
2490
2491         if (!ob || ID_IS_LINKED(ob)) return 0;
2492
2493         /* only meshes supported at the moment */
2494         if (ob->type == OB_MESH)
2495                 return ED_operator_screenactive(C);
2496         else
2497                 return 0;
2498 }
2499
2500 static int join_shapes_exec(bContext *C, wmOperator *op)
2501 {
2502         Object *ob = CTX_data_active_object(C);
2503
2504         if (ob->mode & OB_MODE_EDIT) {
2505                 BKE_report(op->reports, RPT_ERROR, "This data does not support joining in edit mode");
2506                 return OPERATOR_CANCELLED;
2507         }
2508         else if (BKE_object_obdata_is_libdata(ob)) {
2509                 BKE_report(op->reports, RPT_ERROR, "Cannot edit external library data");
2510                 return OPERATOR_CANCELLED;
2511         }
2512
2513         if (ob->type == OB_MESH)
2514                 return join_mesh_shapes_exec(C, op);
2515
2516         return OPERATOR_CANCELLED;
2517 }
2518
2519 void OBJECT_OT_join_shapes(wmOperatorType *ot)
2520 {
2521         /* identifiers */
2522         ot->name = "Join as Shapes";
2523         ot->description = "Merge selected objects to shapes of active object";
2524         ot->idname = "OBJECT_OT_join_shapes";
2525
2526         /* api callbacks */
2527         ot->exec = join_shapes_exec;
2528         ot->poll = join_shapes_poll;
2529
2530         /* flags */
2531         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2532 }