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.
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.
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.
16 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
17 * All rights reserved.
29 #include "MEM_guardedalloc.h"
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"
48 #include "BLI_utildefines.h"
49 #include "BLI_ghash.h"
50 #include "BLI_listbase.h"
52 #include "BLI_string.h"
54 #include "BLT_translation.h"
56 #include "BKE_action.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"
68 #include "BKE_gpencil.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"
78 #include "BKE_material.h"
79 #include "BKE_mball.h"
81 #include "BKE_mesh_runtime.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"
89 #include "DEG_depsgraph.h"
90 #include "DEG_depsgraph_build.h"
91 #include "DEG_depsgraph_query.h"
93 #include "RNA_access.h"
94 #include "RNA_define.h"
95 #include "RNA_enum_types.h"
100 #include "ED_armature.h"
101 #include "ED_curve.h"
102 #include "ED_gpencil.h"
103 #include "ED_mball.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"
113 #include "UI_resources.h"
115 #include "object_intern.h"
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},
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},
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},
156 /************************** Exported *****************************/
158 void ED_object_location_from_view(bContext *C, float loc[3])
160 const Scene *scene = CTX_data_scene(C);
161 copy_v3_v3(loc, scene->cursor.location);
164 void ED_object_rotation_from_quat(float rot[3], const float viewquat[4], const char align_axis)
166 BLI_assert(align_axis >= 'X' && align_axis <= 'Z');
168 switch (align_axis) {
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);
181 quat_to_eul(rot, viewquat);
182 rot[0] -= (float)M_PI_2;
187 quat_to_eul(rot, viewquat);
193 void ED_object_rotation_from_view(bContext *C, float rot[3], const char align_axis)
195 RegionView3D *rv3d = CTX_wm_region_view3d(C);
196 BLI_assert(align_axis >= 'X' && align_axis <= 'Z');
199 copy_qt_qt(viewquat, rv3d->viewquat);
200 viewquat[0] *= -1.0f;
201 ED_object_rotation_from_quat(rot, viewquat, align_axis);
208 void ED_object_base_init_transform(bContext *C, Base *base, const float loc[3], const float rot[3])
210 Object *ob = base->object;
211 Scene *scene = CTX_data_scene(C);
212 Depsgraph *depsgraph = CTX_data_depsgraph(C);
217 copy_v3_v3(ob->loc, loc);
220 copy_v3_v3(ob->rot, rot);
222 BKE_object_where_is_calc(depsgraph, scene, ob);
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])
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];
237 eul_to_mat3(rmat, rot);
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);
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]);
253 const float dia = v3d ? ED_view3d_grid_scale(scene, v3d, NULL) : ED_scene_grid_scale(scene, NULL);
260 /********************* Add Object Operator ********************/
262 static void view_align_update(struct Main *UNUSED(main), struct Scene *UNUSED(scene), struct PointerRNA *ptr)
264 RNA_struct_idprops_unset(ptr, "rotation");
267 void ED_object_add_unit_props_size(wmOperatorType *ot)
269 RNA_def_float_distance(ot->srna, "size", 2.0f, 0.0, OBJECT_ADD_SIZE_MAXF, "Size", "", 0.001, 100.00);
272 void ED_object_add_unit_props_radius(wmOperatorType *ot)
274 RNA_def_float_distance(ot->srna, "radius", 1.0f, 0.0, OBJECT_ADD_SIZE_MAXF, "Radius", "", 0.001, 100.00);
277 void ED_object_add_generic_props(wmOperatorType *ot, bool do_editmode)
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);
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);
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);
300 void ED_object_add_mesh_props(wmOperatorType *ot)
302 RNA_def_boolean(ot->srna, "calc_uvs", true, "Generate UVs", "Generate a default UV map");
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)
312 /* Switch to Edit mode? optional prop */
313 if ((prop = RNA_struct_find_property(op->ptr, "enter_editmode"))) {
314 bool _enter_editmode;
316 enter_editmode = &_enter_editmode;
318 if (RNA_property_is_set(op->ptr, prop) && enter_editmode)
319 *enter_editmode = RNA_property_boolean_get(op->ptr, prop);
321 *enter_editmode = (U.flag & USER_ADD_EDITMODE) != 0;
322 RNA_property_boolean_set(op->ptr, prop, *enter_editmode);
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;
339 if (RNA_struct_property_is_set(op->ptr, "location")) {
340 RNA_float_get_array(op->ptr, "location", loc);
343 ED_object_location_from_view(C, loc);
344 RNA_float_set_array(op->ptr, "location", loc);
350 bool _is_view_aligned;
352 if (!is_view_aligned)
353 is_view_aligned = &_is_view_aligned;
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");
362 *is_view_aligned = (U.flag & USER_ADD_VIEWALIGNED) != 0;
363 RNA_boolean_set(op->ptr, "view_align", *is_view_aligned);
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);
371 RNA_float_get_array(op->ptr, "rotation", rot);
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(
381 int type, const char *name,
382 const float loc[3], const float rot[3],
383 bool enter_editmode, ushort local_view_bits)
385 Main *bmain = CTX_data_main(C);
386 Scene *scene = CTX_data_scene(C);
387 ViewLayer *view_layer = CTX_data_view_layer(C);
390 /* for as long scene has editmode... */
391 if (CTX_data_edit_object(C)) {
392 ED_object_editmode_exit(C, EM_FREEDATA);
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);
401 /* more editor stuff */
402 ED_object_base_init_transform(C, view_layer->basact, loc, rot);
404 /* TODO(sergey): This is weird to manually tag objects for update, better to
405 * use DEG_id_tag_update here perhaps.
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);
413 if (enter_editmode) {
414 ED_object_editmode_enter_ex(bmain, scene, ob, 0);
417 WM_event_add_notifier(C, NC_SCENE | ND_LAYER_CONTENT, scene);
419 /* TODO(sergey): Use proper flag for tagging here. */
420 DEG_id_tag_update(&scene->id, 0);
425 /* for object add operator */
426 static int object_add_exec(bContext *C, wmOperator *op)
430 ushort local_view_bits;
431 float loc[3], rot[3], radius;
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;
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);
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);
446 BKE_object_obdata_size_init(ob, radius);
449 return OPERATOR_FINISHED;
452 void OBJECT_OT_add(wmOperatorType *ot)
455 ot->name = "Add Object";
456 ot->description = "Add an object to the scene";
457 ot->idname = "OBJECT_OT_add";
460 ot->exec = object_add_exec;
461 ot->poll = ED_operator_objectmode;
464 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
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);
471 ED_object_add_generic_props(ot, true);
474 /********************** Add Probe Operator **********************/
476 /* for object add operator */
477 static const char *get_lightprobe_defname(int 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");
484 return CTX_DATA_(BLT_I18NCONTEXT_ID_LIGHT, "LightProbe");
488 static int lightprobe_add_exec(bContext *C, wmOperator *op)
494 ushort local_view_bits;
495 float loc[3], rot[3];
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;
502 type = RNA_enum_get(op->ptr, "type");
503 radius = RNA_float_get(op->ptr, "radius");
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);
508 probe = (LightProbe *)ob->data;
512 case LIGHTPROBE_TYPE_GRID:
513 probe->distinf = 0.3f;
514 probe->falloff = 1.0f;
515 probe->clipsta = 0.01f;
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;
523 case LIGHTPROBE_TYPE_CUBE:
524 probe->attenuation_type = LIGHTPROBE_SHAPE_ELIPSOID;
527 BLI_assert(!"LightProbe type not configured.");
531 DEG_relations_tag_update(CTX_data_main(C));
533 return OPERATOR_FINISHED;
536 void OBJECT_OT_lightprobe_add(wmOperatorType *ot)
539 ot->name = "Add Light Probe";
540 ot->description = "Add a light probe object";
541 ot->idname = "OBJECT_OT_lightprobe_add";
544 ot->exec = lightprobe_add_exec;
545 ot->poll = ED_operator_objectmode;
548 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
551 ot->prop = RNA_def_enum(ot->srna, "type", lightprobe_type_items, 0, "Type", "");
553 ED_object_add_unit_props_radius(ot);
554 ED_object_add_generic_props(ot, true);
557 /********************* Add Effector Operator ********************/
559 /* for object add operator */
560 static int effector_add_exec(bContext *C, wmOperator *op)
565 ushort local_view_bits;
566 float loc[3], rot[3];
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;
574 type = RNA_enum_get(op->ptr, "type");
575 dia = RNA_float_get(op->ptr, "radius");
577 if (type == PFIELD_GUIDE) {
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);
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));
588 ED_object_editmode_exit(C, EM_FREEDATA);
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;
598 ob->pd = BKE_partdeflect_new(type);
600 DEG_relations_tag_update(CTX_data_main(C));
602 return OPERATOR_FINISHED;
605 void OBJECT_OT_effector_add(wmOperatorType *ot)
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";
613 ot->exec = effector_add_exec;
614 ot->poll = ED_operator_objectmode;
617 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
620 ot->prop = RNA_def_enum(ot->srna, "type", field_type_items, 0, "Type", "");
622 ED_object_add_unit_props_radius(ot);
623 ED_object_add_generic_props(ot, true);
626 /********************* Add Camera Operator ********************/
628 static int object_camera_add_exec(bContext *C, wmOperator *op)
630 View3D *v3d = CTX_wm_view3d(C);
631 Scene *scene = CTX_data_scene(C);
635 ushort local_view_bits;
636 float loc[3], rot[3];
638 /* force view align for cameras */
639 RNA_boolean_set(op->ptr, "view_align", true);
641 if (!ED_object_add_generic_get_opts(C, op, 'Z', loc, rot, &enter_editmode, &local_view_bits, NULL)) {
642 return OPERATOR_CANCELLED;
644 ob = ED_object_add_type(C, OB_CAMERA, NULL, loc, rot, false, local_view_bits);
647 if (v3d->camera == NULL)
649 if (v3d->scenelock && scene->camera == NULL) {
655 cam->drawsize = v3d ? ED_view3d_grid_scale(scene, v3d, NULL) : ED_scene_grid_scale(scene, NULL);
657 return OPERATOR_FINISHED;
660 void OBJECT_OT_camera_add(wmOperatorType *ot)
665 ot->name = "Add Camera";
666 ot->description = "Add a camera object to the scene";
667 ot->idname = "OBJECT_OT_camera_add";
670 ot->exec = object_camera_add_exec;
671 ot->poll = ED_operator_objectmode;
674 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
676 ED_object_add_generic_props(ot, true);
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);
684 /********************* Add Metaball Operator ********************/
686 static int object_metaball_add_exec(bContext *C, wmOperator *op)
688 Object *obedit = CTX_data_edit_object(C);
691 ushort local_view_bits;
692 float loc[3], rot[3];
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;
700 if (obedit == NULL || obedit->type != OB_MBALL) {
701 obedit = ED_object_add_type(C, OB_MBALL, NULL, loc, rot, true, local_view_bits);
705 DEG_id_tag_update(&obedit->id, ID_RECALC_GEOMETRY);
708 ED_object_new_primitive_matrix(C, obedit, loc, rot, mat);
709 dia = RNA_float_get(op->ptr, "radius");
711 ED_mball_add_primitive(C, obedit, mat, dia, RNA_enum_get(op->ptr, "type"));
714 if (newob && !enter_editmode) {
715 ED_object_editmode_exit(C, EM_FREEDATA);
718 WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, obedit);
720 return OPERATOR_FINISHED;
723 void OBJECT_OT_metaball_add(wmOperatorType *ot)
726 ot->name = "Add Metaball";
727 ot->description = "Add an metaball object to the scene";
728 ot->idname = "OBJECT_OT_metaball_add";
731 ot->invoke = WM_menu_invoke;
732 ot->exec = object_metaball_add_exec;
733 ot->poll = ED_operator_scene_editable;
736 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
738 ot->prop = RNA_def_enum(ot->srna, "type", rna_enum_metaelem_type_items, 0, "Primitive", "");
740 ED_object_add_unit_props_radius(ot);
741 ED_object_add_generic_props(ot, true);
744 /********************* Add Text Operator ********************/
746 static int object_add_text_exec(bContext *C, wmOperator *op)
748 Object *obedit = CTX_data_edit_object(C);
750 ushort local_view_bits;
751 float loc[3], rot[3];
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;
757 if (obedit && obedit->type == OB_FONT)
758 return OPERATOR_CANCELLED;
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"));
763 WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, obedit);
765 return OPERATOR_FINISHED;
768 void OBJECT_OT_text_add(wmOperatorType *ot)
771 ot->name = "Add Text";
772 ot->description = "Add a text object to the scene";
773 ot->idname = "OBJECT_OT_text_add";
776 ot->exec = object_add_text_exec;
777 ot->poll = ED_operator_objectmode;
780 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
783 ED_object_add_unit_props_radius(ot);
784 ED_object_add_generic_props(ot, true);
787 /********************* Add Armature Operator ********************/
789 static int object_armature_add_exec(bContext *C, wmOperator *op)
791 Object *obedit = CTX_data_edit_object(C);
792 RegionView3D *rv3d = CTX_wm_region_view3d(C);
795 ushort local_view_bits;
796 float loc[3], rot[3], dia;
797 bool view_aligned = rv3d && (U.flag & USER_ADD_VIEWALIGNED);
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;
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);
809 DEG_id_tag_update(&obedit->id, ID_RECALC_GEOMETRY);
812 if (obedit == NULL) {
813 BKE_report(op->reports, RPT_ERROR, "Cannot create editmode armature");
814 return OPERATOR_CANCELLED;
817 dia = RNA_float_get(op->ptr, "radius");
818 ED_armature_ebone_add_primitive(obedit, dia, view_aligned);
821 if (newob && !enter_editmode)
822 ED_object_editmode_exit(C, EM_FREEDATA);
824 WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, obedit);
826 return OPERATOR_FINISHED;
829 void OBJECT_OT_armature_add(wmOperatorType *ot)
832 ot->name = "Add Armature";
833 ot->description = "Add an armature object to the scene";
834 ot->idname = "OBJECT_OT_armature_add";
837 ot->exec = object_armature_add_exec;
838 ot->poll = ED_operator_objectmode;
841 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
844 ED_object_add_unit_props_radius(ot);
845 ED_object_add_generic_props(ot, true);
848 /********************* Add Empty Operator ********************/
850 static int object_empty_add_exec(bContext *C, wmOperator *op)
853 int type = RNA_enum_get(op->ptr, "type");
854 ushort local_view_bits;
855 float loc[3], rot[3];
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;
861 ob = ED_object_add_type(C, OB_EMPTY, NULL, loc, rot, false, local_view_bits);
863 BKE_object_empty_draw_type_set(ob, type);
864 BKE_object_obdata_size_init(ob, RNA_float_get(op->ptr, "radius"));
866 return OPERATOR_FINISHED;
869 void OBJECT_OT_empty_add(wmOperatorType *ot)
872 ot->name = "Add Empty";
873 ot->description = "Add an empty object to the scene";
874 ot->idname = "OBJECT_OT_empty_add";
877 ot->invoke = WM_menu_invoke;
878 ot->exec = object_empty_add_exec;
879 ot->poll = ED_operator_objectmode;
882 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
885 ot->prop = RNA_def_enum(ot->srna, "type", rna_enum_object_empty_drawtype_items, 0, "Type", "");
887 ED_object_add_unit_props_radius(ot);
888 ED_object_add_generic_props(ot, false);
891 static int empty_drop_named_image_invoke(bContext *C, wmOperator *op, const wmEvent *event)
893 Scene *scene = CTX_data_scene(C);
897 ima = (Image *)WM_operator_drop_load_path(C, op, ID_IM);
899 return OPERATOR_CANCELLED;
902 id_us_min((ID *)ima);
905 Object *ob_cursor = ED_view3d_give_object_under_cursor(C, event->mval);
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);
915 ushort local_view_bits;
918 if (!ED_object_add_generic_get_opts(C, op, 'Z', NULL, rot, NULL, &local_view_bits, NULL)) {
919 return OPERATOR_CANCELLED;
921 ob = ED_object_add_type(C, OB_EMPTY, NULL, NULL, rot, false, local_view_bits);
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;
929 BKE_object_empty_draw_type_set(ob, OB_EMPTY_IMAGE);
933 id_us_plus(ob->data);
935 return OPERATOR_FINISHED;
938 void OBJECT_OT_drop_named_image(wmOperatorType *ot)
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";
948 ot->invoke = empty_drop_named_image_invoke;
949 ot->poll = ED_operator_objectmode;
952 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
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);
964 /********************* Add Gpencil Operator ********************/
965 static bool object_gpencil_add_poll(bContext *C)
967 Scene *scene = CTX_data_scene(C);
968 Object *obact = CTX_data_active_object(C);
970 if ((scene == NULL) || (ID_IS_LINKED(scene))) {
974 if (obact && obact->type == OB_GPENCIL) {
975 if (obact->mode != OB_MODE_OBJECT) {
983 static int object_gpencil_add_exec(bContext *C, wmOperator *op)
985 Object *ob = CTX_data_active_object(C);
986 bGPdata *gpd = (ob && (ob->type == OB_GPENCIL)) ? ob->data : NULL;
988 const int type = RNA_enum_get(op->ptr, "type");
990 ushort local_view_bits;
991 float loc[3], rot[3];
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;
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)
1002 if ((gpd == NULL) || (GPENCIL_ANY_MODE(gpd) == false) || (type == GP_EMPTY)) {
1003 const char *ob_name = NULL;
1007 ob_name = "Suzanne";
1021 ob = ED_object_add_type(C, OB_GPENCIL, ob_name, loc, rot, true, local_view_bits);
1026 DEG_id_tag_update(&ob->id, ID_RECALC_GEOMETRY);
1027 WM_event_add_notifier(C, NC_GPENCIL | ND_DATA | NA_ADDED, NULL);
1030 /* create relevant geometry */
1034 float radius = RNA_float_get(op->ptr, "radius");
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);
1042 ED_gpencil_create_stroke(C, ob, mat);
1047 float radius = RNA_float_get(op->ptr, "radius");
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);
1055 ED_gpencil_create_monkey(C, ob, mat);
1063 BKE_report(op->reports, RPT_WARNING, "Not implemented");
1067 /* if this is a new object, initialise default stuff (colors, etc.) */
1069 ED_gpencil_add_defaults(C, ob);
1072 return OPERATOR_FINISHED;
1075 void OBJECT_OT_gpencil_add(wmOperatorType *ot)
1078 ot->name = "Add Grease Pencil";
1079 ot->description = "Add a Grease Pencil object to the scene";
1080 ot->idname = "OBJECT_OT_gpencil_add";
1083 ot->invoke = WM_menu_invoke;
1084 ot->exec = object_gpencil_add_exec;
1085 ot->poll = object_gpencil_add_poll;
1088 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1091 ED_object_add_unit_props_radius(ot);
1092 ED_object_add_generic_props(ot, false);
1094 ot->prop = RNA_def_enum(ot->srna, "type", rna_enum_object_gpencil_type_items, 0, "Type", "");
1097 /********************* Add Light Operator ********************/
1099 static const char *get_light_defname(int type)
1102 case LA_LOCAL: return CTX_DATA_(BLT_I18NCONTEXT_ID_LIGHT, "Point");
1103 case LA_SUN: return CTX_DATA_(BLT_I18NCONTEXT_ID_LIGHT, "Sun");
1104 case LA_SPOT: return CTX_DATA_(BLT_I18NCONTEXT_ID_LIGHT, "Spot");
1105 case LA_AREA: return CTX_DATA_(BLT_I18NCONTEXT_ID_LIGHT, "Area");
1107 return CTX_DATA_(BLT_I18NCONTEXT_ID_LIGHT, "Light");
1111 static int object_light_add_exec(bContext *C, wmOperator *op)
1113 Scene *scene = CTX_data_scene(C);
1116 int type = RNA_enum_get(op->ptr, "type");
1117 ushort local_view_bits;
1118 float loc[3], rot[3];
1120 WM_operator_view3d_unit_defaults(C, op);
1121 if (!ED_object_add_generic_get_opts(C, op, 'Z', loc, rot, NULL, &local_view_bits, NULL)) {
1122 return OPERATOR_CANCELLED;
1124 ob = ED_object_add_type(C, OB_LAMP, get_light_defname(type), loc, rot, false, local_view_bits);
1126 float size = RNA_float_get(op->ptr, "radius");
1127 /* Better defaults for light size. */
1139 BKE_object_obdata_size_init(ob, size);
1141 la = (Light *)ob->data;
1144 if (BKE_scene_uses_cycles(scene)) {
1145 ED_node_shader_default(C, &la->id);
1146 la->use_nodes = true;
1149 return OPERATOR_FINISHED;
1152 void OBJECT_OT_light_add(wmOperatorType *ot)
1155 ot->name = "Add Light";
1156 ot->description = "Add a light object to the scene";
1157 ot->idname = "OBJECT_OT_light_add";
1160 ot->invoke = WM_menu_invoke;
1161 ot->exec = object_light_add_exec;
1162 ot->poll = ED_operator_objectmode;
1165 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1168 ot->prop = RNA_def_enum(ot->srna, "type", rna_enum_light_type_items, 0, "Type", "");
1169 RNA_def_property_translation_context(ot->prop, BLT_I18NCONTEXT_ID_LIGHT);
1171 ED_object_add_unit_props_radius(ot);
1172 ED_object_add_generic_props(ot, false);
1175 /********************* Add Collection Instance Operator ********************/
1177 static int collection_instance_add_exec(bContext *C, wmOperator *op)
1179 Main *bmain = CTX_data_main(C);
1180 Collection *collection;
1181 ushort local_view_bits;
1182 float loc[3], rot[3];
1184 if (RNA_struct_property_is_set(op->ptr, "name")) {
1185 char name[MAX_ID_NAME - 2];
1187 RNA_string_get(op->ptr, "name", name);
1188 collection = (Collection *)BKE_libblock_find_name(bmain, ID_GR, name);
1190 if (0 == RNA_struct_property_is_set(op->ptr, "location")) {
1191 const wmEvent *event = CTX_wm_window(C)->eventstate;
1192 ARegion *ar = CTX_wm_region(C);
1193 const int mval[2] = {event->x - ar->winrct.xmin,
1194 event->y - ar->winrct.ymin};
1195 ED_object_location_from_view(C, loc);
1196 ED_view3d_cursor3d_position(C, mval, false, loc);
1197 RNA_float_set_array(op->ptr, "location", loc);
1201 collection = BLI_findlink(&CTX_data_main(C)->collection, RNA_enum_get(op->ptr, "collection"));
1203 if (!ED_object_add_generic_get_opts(C, op, 'Z', loc, rot, NULL, &local_view_bits, NULL)) {
1204 return OPERATOR_CANCELLED;
1207 Scene *scene = CTX_data_scene(C);
1208 ViewLayer *view_layer = CTX_data_view_layer(C);
1210 /* Avoid dependency cycles. */
1211 LayerCollection *active_lc = BKE_layer_collection_get_active(view_layer);
1212 while (BKE_collection_find_cycle(active_lc->collection, collection)) {
1213 active_lc = BKE_layer_collection_activate_parent(view_layer, active_lc);
1216 Object *ob = ED_object_add_type(C, OB_EMPTY, collection->id.name + 2, loc, rot, false, local_view_bits);
1217 ob->instance_collection = collection;
1218 ob->transflag |= OB_DUPLICOLLECTION;
1219 id_us_plus(&collection->id);
1221 /* works without this except if you try render right after, see: 22027 */
1222 DEG_relations_tag_update(bmain);
1223 DEG_id_tag_update(&scene->id, ID_RECALC_SELECT);
1224 WM_event_add_notifier(C, NC_SCENE | ND_OB_ACTIVE, scene);
1226 return OPERATOR_FINISHED;
1229 return OPERATOR_CANCELLED;
1232 /* only used as menu */
1233 void OBJECT_OT_collection_instance_add(wmOperatorType *ot)
1238 ot->name = "Add Collection Instance";
1239 ot->description = "Add a collection instance";
1240 ot->idname = "OBJECT_OT_collection_instance_add";
1243 ot->invoke = WM_enum_search_invoke;
1244 ot->exec = collection_instance_add_exec;
1245 ot->poll = ED_operator_objectmode;
1248 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1251 RNA_def_string(ot->srna, "name", "Collection", MAX_ID_NAME - 2, "Name", "Collection name to add");
1252 prop = RNA_def_enum(ot->srna, "collection", DummyRNA_NULL_items, 0, "Collection", "");
1253 RNA_def_enum_funcs(prop, RNA_collection_itemf);
1254 RNA_def_property_flag(prop, PROP_ENUM_NO_TRANSLATE);
1256 ED_object_add_generic_props(ot, false);
1259 /********************* Add Speaker Operator ********************/
1261 static int object_speaker_add_exec(bContext *C, wmOperator *op)
1264 ushort local_view_bits;
1265 float loc[3], rot[3];
1266 Scene *scene = CTX_data_scene(C);
1268 if (!ED_object_add_generic_get_opts(C, op, 'Z', loc, rot, NULL, &local_view_bits, NULL)) {
1269 return OPERATOR_CANCELLED;
1271 ob = ED_object_add_type(C, OB_SPEAKER, NULL, loc, rot, false, local_view_bits);
1273 /* to make it easier to start using this immediately in NLA, a default sound clip is created
1274 * ready to be moved around to retime the sound and/or make new sound clips
1277 /* create new data for NLA hierarchy */
1278 AnimData *adt = BKE_animdata_add_id(&ob->id);
1279 NlaTrack *nlt = BKE_nlatrack_add(adt, NULL);
1280 NlaStrip *strip = BKE_nla_add_soundstrip(scene, ob->data);
1281 strip->start = CFRA;
1282 strip->end += strip->start;
1285 BKE_nlatrack_add_strip(nlt, strip);
1287 /* auto-name the strip, and give the track an interesting name */
1288 BLI_strncpy(nlt->name, DATA_("SoundTrack"), sizeof(nlt->name));
1289 BKE_nlastrip_validate_name(adt, strip);
1291 WM_event_add_notifier(C, NC_ANIMATION | ND_NLA | NA_EDITED, NULL);
1294 return OPERATOR_FINISHED;
1297 void OBJECT_OT_speaker_add(wmOperatorType *ot)
1300 ot->name = "Add Speaker";
1301 ot->description = "Add a speaker object to the scene";
1302 ot->idname = "OBJECT_OT_speaker_add";
1305 ot->exec = object_speaker_add_exec;
1306 ot->poll = ED_operator_objectmode;
1309 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1311 ED_object_add_generic_props(ot, true);
1314 /**************************** Delete Object *************************/
1316 /* remove base from a specific scene */
1317 /* note: now unlinks constraints as well */
1318 void ED_object_base_free_and_unlink(Main *bmain, Scene *scene, Object *ob)
1320 if (BKE_library_ID_is_indirectly_used(bmain, ob) &&
1321 ID_REAL_USERS(ob) <= 1 && ID_EXTRA_USERS(ob) == 0)
1323 /* We cannot delete indirectly used object... */
1324 printf("WARNING, undeletable object '%s', should have been catched before reaching this function!",
1329 DEG_id_tag_update_ex(bmain, &ob->id, ID_RECALC_BASE_FLAGS);
1331 BKE_scene_collections_object_remove(bmain, scene, ob, true);
1334 static int object_delete_exec(bContext *C, wmOperator *op)
1336 Main *bmain = CTX_data_main(C);
1337 Scene *scene = CTX_data_scene(C);
1338 wmWindowManager *wm = CTX_wm_manager(C);
1340 const bool use_global = RNA_boolean_get(op->ptr, "use_global");
1341 uint changed_count = 0;
1343 if (CTX_data_edit_object(C))
1344 return OPERATOR_CANCELLED;
1346 CTX_DATA_BEGIN (C, Object *, ob, selected_objects)
1348 const bool is_indirectly_used = BKE_library_ID_is_indirectly_used(bmain, ob);
1349 if (ob->id.tag & LIB_TAG_INDIRECT) {
1350 /* Can this case ever happen? */
1351 BKE_reportf(op->reports, RPT_WARNING, "Cannot delete indirectly linked object '%s'", ob->id.name + 2);
1354 else if (is_indirectly_used && ID_REAL_USERS(ob) <= 1 && ID_EXTRA_USERS(ob) == 0) {
1355 BKE_reportf(op->reports, RPT_WARNING,
1356 "Cannot delete object '%s' from scene '%s', indirectly used objects need at least one user",
1357 ob->id.name + 2, scene->id.name + 2);
1361 /* if grease pencil object, set cache as dirty */
1362 if (ob->type == OB_GPENCIL) {
1363 bGPdata *gpd = (bGPdata *)ob->data;
1364 DEG_id_tag_update(&gpd->id, ID_RECALC_TRANSFORM | ID_RECALC_GEOMETRY);
1367 /* This is sort of a quick hack to address T51243 - Proper thing to do here would be to nuke most of all this
1368 * custom scene/object/base handling, and use generic lib remap/query for that.
1369 * But this is for later (aka 2.8, once layers & co are settled and working).
1371 if (use_global && ob->id.lib == NULL) {
1372 /* We want to nuke the object, let's nuke it the easy way (not for linked data though)... */
1373 BKE_id_delete(bmain, &ob->id);
1378 /* remove from Grease Pencil parent */
1379 /* XXX This is likely not correct? Will also remove parent from grease pencil from other scenes,
1380 * even when use_global is false... */
1381 for (bGPdata *gpd = bmain->gpencil.first; gpd; gpd = gpd->id.next) {
1382 for (bGPDlayer *gpl = gpd->layers.first; gpl; gpl = gpl->next) {
1383 if (gpl->parent != NULL) {
1384 if (gpl->parent == ob) {
1391 /* remove from current scene only */
1392 ED_object_base_free_and_unlink(bmain, scene, ob);
1397 for (scene_iter = bmain->scene.first; scene_iter; scene_iter = scene_iter->id.next) {
1398 if (scene_iter != scene && !ID_IS_LINKED(scene_iter)) {
1399 if (is_indirectly_used && ID_REAL_USERS(ob) <= 1 && ID_EXTRA_USERS(ob) == 0) {
1400 BKE_reportf(op->reports, RPT_WARNING,
1401 "Cannot delete object '%s' from scene '%s', indirectly used objects need at least one user",
1402 ob->id.name + 2, scene_iter->id.name + 2);
1405 ED_object_base_free_and_unlink(bmain, scene_iter, ob);
1413 BKE_reportf(op->reports, RPT_INFO, "Deleted %u object(s)", changed_count);
1415 if (changed_count == 0) {
1416 return OPERATOR_CANCELLED;
1419 /* delete has to handle all open scenes */
1420 BKE_main_id_tag_listbase(&bmain->scene, LIB_TAG_DOIT, true);
1421 for (win = wm->windows.first; win; win = win->next) {
1422 scene = WM_window_get_active_scene(win);
1424 if (scene->id.tag & LIB_TAG_DOIT) {
1425 scene->id.tag &= ~LIB_TAG_DOIT;
1427 DEG_relations_tag_update(bmain);
1429 DEG_id_tag_update(&scene->id, ID_RECALC_SELECT);
1430 WM_event_add_notifier(C, NC_SCENE | ND_OB_ACTIVE, scene);
1431 WM_event_add_notifier(C, NC_SCENE | ND_LAYER_CONTENT, scene);
1435 return OPERATOR_FINISHED;
1438 void OBJECT_OT_delete(wmOperatorType *ot)
1441 ot->name = "Delete";
1442 ot->description = "Delete selected objects";
1443 ot->idname = "OBJECT_OT_delete";
1446 ot->invoke = WM_operator_confirm_or_exec;
1447 ot->exec = object_delete_exec;
1448 ot->poll = ED_operator_objectmode;
1451 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1454 prop = RNA_def_boolean(ot->srna, "use_global", 0, "Delete Globally", "Remove object from all scenes");
1455 RNA_def_property_flag(prop, PROP_HIDDEN | PROP_SKIP_SAVE);
1456 WM_operator_properties_confirm_or_exec(ot);
1459 /**************************** Copy Utilities ******************************/
1461 /* after copying objects, copied data should get new pointers */
1462 static void copy_object_set_idnew(bContext *C)
1464 Main *bmain = CTX_data_main(C);
1466 CTX_DATA_BEGIN (C, Object *, ob, selected_editable_objects)
1468 BKE_libblock_relink_to_newid(&ob->id);
1472 BKE_main_id_clear_newpoins(bmain);
1475 /********************* Make Duplicates Real ************************/
1478 * \note regarding hashing dupli-objects when using OB_DUPLICOLLECTION, skip the first member of #DupliObject.persistent_id
1479 * since its a unique index and we only want to know if the group objects are from the same dupli-group instance.
1481 static unsigned int dupliobject_group_hash(const void *ptr)
1483 const DupliObject *dob = ptr;
1484 unsigned int hash = BLI_ghashutil_ptrhash(dob->ob);
1486 for (i = 1; (i < MAX_DUPLI_RECUR) && dob->persistent_id[i] != INT_MAX; i++) {
1487 hash ^= (dob->persistent_id[i] ^ i);
1493 * \note regarding hashing dupli-objects when NOT using OB_DUPLICOLLECTION, include the first member of #DupliObject.persistent_id
1494 * since its the index of the vertex/face the object is instantiated on and we want to identify objects on the same vertex/face.
1496 static unsigned int dupliobject_hash(const void *ptr)
1498 const DupliObject *dob = ptr;
1499 unsigned int hash = BLI_ghashutil_ptrhash(dob->ob);
1500 hash ^= (dob->persistent_id[0] ^ 0);
1504 /* Compare function that matches dupliobject_group_hash */
1505 static bool dupliobject_group_cmp(const void *a_, const void *b_)
1507 const DupliObject *a = a_;
1508 const DupliObject *b = b_;
1511 if (a->ob != b->ob) {
1515 for (i = 1; (i < MAX_DUPLI_RECUR); i++) {
1516 if (a->persistent_id[i] != b->persistent_id[i]) {
1519 else if (a->persistent_id[i] == INT_MAX) {
1528 /* Compare function that matches dupliobject_hash */
1529 static bool dupliobject_cmp(const void *a_, const void *b_)
1531 const DupliObject *a = a_;
1532 const DupliObject *b = b_;
1534 if (a->ob != b->ob) {
1538 if (a->persistent_id[0] != b->persistent_id[0]) {
1546 static void make_object_duplilist_real(bContext *C, Scene *scene, Base *base,
1547 const bool use_base_parent,
1548 const bool use_hierarchy)
1550 Main *bmain = CTX_data_main(C);
1551 ViewLayer *view_layer = CTX_data_view_layer(C);
1552 Depsgraph *depsgraph = CTX_data_depsgraph(C);
1553 ListBase *lb_duplis;
1555 GHash *dupli_gh, *parent_gh = NULL;
1557 if (!(base->object->transflag & OB_DUPLI)) {
1561 Object *object_eval = DEG_get_evaluated_object(depsgraph, base->object);
1562 lb_duplis = object_duplilist(depsgraph, scene, object_eval);
1564 dupli_gh = BLI_ghash_ptr_new(__func__);
1565 if (use_hierarchy) {
1566 if (base->object->transflag & OB_DUPLICOLLECTION) {
1567 parent_gh = BLI_ghash_new(dupliobject_group_hash, dupliobject_group_cmp, __func__);
1570 parent_gh = BLI_ghash_new(dupliobject_hash, dupliobject_cmp, __func__);
1574 for (dob = lb_duplis->first; dob; dob = dob->next) {
1575 Object *ob_src = DEG_get_original_object(dob->ob);
1576 Object *ob_dst = ID_NEW_SET(dob->ob, BKE_object_copy(bmain, ob_src));
1579 /* font duplis can have a totcol without material, we get them from parent
1580 * should be implemented better...
1582 if (ob_dst->mat == NULL) {
1586 BKE_collection_object_add_from(bmain, scene, base->object, ob_dst);
1587 base_dst = BKE_view_layer_base_find(view_layer, ob_dst);
1588 BLI_assert(base_dst != NULL);
1590 BKE_scene_object_base_flag_sync_from_base(base_dst);
1592 /* make sure apply works */
1593 BKE_animdata_free(&ob_dst->id, true);
1596 /* Proxies are not to be copied. */
1597 ob_dst->proxy_from = NULL;
1598 ob_dst->proxy_group = NULL;
1599 ob_dst->proxy = NULL;
1601 ob_dst->parent = NULL;
1602 BKE_constraints_free(&ob_dst->constraints);
1603 ob_dst->runtime.curve_cache = NULL;
1604 ob_dst->transflag &= ~OB_DUPLI;
1606 copy_m4_m4(ob_dst->obmat, dob->mat);
1607 BKE_object_apply_mat4(ob_dst, ob_dst->obmat, false, false);
1609 BLI_ghash_insert(dupli_gh, dob, ob_dst);
1612 /* Due to nature of hash/comparison of this ghash, a lot of duplis may be considered as
1613 * 'the same', this avoids trying to insert same key several time and
1614 * raise asserts in debug builds... */
1615 if (!BLI_ghash_ensure_p(parent_gh, dob, &val)) {
1621 for (dob = lb_duplis->first; dob; dob = dob->next) {
1622 Object *ob_src = dob->ob;
1623 Object *ob_dst = BLI_ghash_lookup(dupli_gh, dob);
1625 /* Remap new object to itself, and clear again newid pointer of orig object. */
1626 BKE_libblock_relink_to_newid(&ob_dst->id);
1628 DEG_id_tag_update(&ob_dst->id, ID_RECALC_GEOMETRY);
1630 if (use_hierarchy) {
1631 /* original parents */
1632 Object *ob_src_par = ob_src->parent;
1633 Object *ob_dst_par = NULL;
1635 /* find parent that was also made real */
1637 /* OK to keep most of the members uninitialized,
1638 * they won't be read, this is simply for a hash lookup. */
1639 DupliObject dob_key;
1640 dob_key.ob = ob_src_par;
1641 if (base->object->transflag & OB_DUPLICOLLECTION) {
1642 memcpy(&dob_key.persistent_id[1],
1643 &dob->persistent_id[1],
1644 sizeof(dob->persistent_id[1]) * (MAX_DUPLI_RECUR - 1));
1647 dob_key.persistent_id[0] = dob->persistent_id[0];
1649 ob_dst_par = BLI_ghash_lookup(parent_gh, &dob_key);
1653 /* allow for all possible parent types */
1654 ob_dst->partype = ob_src->partype;
1655 BLI_strncpy(ob_dst->parsubstr, ob_src->parsubstr, sizeof(ob_dst->parsubstr));
1656 ob_dst->par1 = ob_src->par1;
1657 ob_dst->par2 = ob_src->par2;
1658 ob_dst->par3 = ob_src->par3;
1660 copy_m4_m4(ob_dst->parentinv, ob_src->parentinv);
1662 ob_dst->parent = ob_dst_par;
1664 else if (use_base_parent) {
1665 ob_dst->parent = base->object;
1666 ob_dst->partype = PAROBJECT;
1669 else if (use_base_parent) {
1670 /* since we are ignoring the internal hierarchy - parent all to the
1672 ob_dst->parent = base->object;
1673 ob_dst->partype = PAROBJECT;
1676 if (ob_dst->parent) {
1677 /* note, this may be the parent of other objects, but it should
1678 * still work out ok */
1679 BKE_object_apply_mat4(ob_dst, dob->mat, false, true);
1681 /* to set ob_dst->orig and in case there's any other discrepancies */
1682 DEG_id_tag_update(&ob_dst->id, ID_RECALC_TRANSFORM);
1686 if (base->object->transflag & OB_DUPLICOLLECTION && base->object->instance_collection) {
1687 for (Object *ob = bmain->object.first; ob; ob = ob->id.next) {
1688 if (ob->proxy_group == base->object) {
1690 ob->proxy_from = NULL;
1691 DEG_id_tag_update(&ob->id, ID_RECALC_TRANSFORM);
1696 BLI_ghash_free(dupli_gh, NULL, NULL);
1698 BLI_ghash_free(parent_gh, NULL, NULL);
1701 free_object_duplilist(lb_duplis);
1703 BKE_main_id_clear_newpoins(bmain);
1705 base->object->transflag &= ~OB_DUPLI;
1706 DEG_id_tag_update(&base->object->id, ID_RECALC_COPY_ON_WRITE);
1709 static int object_duplicates_make_real_exec(bContext *C, wmOperator *op)
1711 Main *bmain = CTX_data_main(C);
1712 Scene *scene = CTX_data_scene(C);
1714 const bool use_base_parent = RNA_boolean_get(op->ptr, "use_base_parent");
1715 const bool use_hierarchy = RNA_boolean_get(op->ptr, "use_hierarchy");
1717 BKE_main_id_clear_newpoins(bmain);
1719 CTX_DATA_BEGIN (C, Base *, base, selected_editable_bases)
1721 make_object_duplilist_real(C, scene, base, use_base_parent, use_hierarchy);
1723 /* dependencies were changed */
1724 WM_event_add_notifier(C, NC_OBJECT | ND_PARENT, base->object);
1728 DEG_relations_tag_update(bmain);
1729 WM_event_add_notifier(C, NC_SCENE, scene);
1730 WM_main_add_notifier(NC_OBJECT | ND_DRAW, NULL);
1732 return OPERATOR_FINISHED;
1735 void OBJECT_OT_duplicates_make_real(wmOperatorType *ot)
1738 ot->name = "Make Duplicates Real";
1739 ot->description = "Make dupli objects attached to this object real";
1740 ot->idname = "OBJECT_OT_duplicates_make_real";
1743 ot->exec = object_duplicates_make_real_exec;
1745 ot->poll = ED_operator_objectmode;
1748 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO | OPTYPE_USE_EVAL_DATA;
1750 RNA_def_boolean(ot->srna, "use_base_parent", 0, "Parent", "Parent newly created objects to the original duplicator");
1751 RNA_def_boolean(ot->srna, "use_hierarchy", 0, "Keep Hierarchy", "Maintain parent child relationships");
1754 /**************************** Convert **************************/
1756 static const EnumPropertyItem convert_target_items[] = {
1757 {OB_CURVE, "CURVE", ICON_OUTLINER_OB_CURVE, "Curve from Mesh/Text", ""},
1758 {OB_MESH, "MESH", ICON_OUTLINER_OB_MESH, "Mesh from Curve/Meta/Surf/Text", ""},
1759 {0, NULL, 0, NULL, NULL},
1762 static void convert_ensure_curve_cache(Depsgraph *depsgraph, Scene *scene, Object *ob)
1764 if (ob->runtime.curve_cache == NULL) {
1765 /* Force creation. This is normally not needed but on operator
1766 * redo we might end up with an object which isn't evaluated yet.
1767 * Also happens in case we are working on a copy of the object (all its caches have been nuked then).
1769 if (ELEM(ob->type, OB_SURF, OB_CURVE, OB_FONT)) {
1770 /* We need 'for render' ON here, to enable computing bevel dipslist if needed.
1771 * Also makes sense anyway, we would not want e.g. to loose hidden parts etc. */
1772 BKE_displist_make_curveTypes(depsgraph, scene, ob, true, false, NULL);
1774 else if (ob->type == OB_MBALL) {
1775 BKE_displist_make_mball(depsgraph, scene, ob);
1780 static void curvetomesh(Main *bmain, Depsgraph *depsgraph, Scene *scene, Object *ob)
1782 convert_ensure_curve_cache(depsgraph, scene, ob);
1783 BKE_mesh_from_nurbs(bmain, ob); /* also does users */
1785 if (ob->type == OB_MESH) {
1786 BKE_object_free_modifiers(ob, 0);
1790 static bool convert_poll(bContext *C)
1792 Scene *scene = CTX_data_scene(C);
1793 Base *base_act = CTX_data_active_base(C);
1794 Object *obact = base_act ? base_act->object : NULL;
1796 return (!ID_IS_LINKED(scene) && obact && (BKE_object_is_in_editmode(obact) == false) &&
1797 (base_act->flag & BASE_SELECTED) && !ID_IS_LINKED(obact));
1800 /* Helper for convert_exec */
1801 static Base *duplibase_for_convert(Main *bmain, Scene *scene, ViewLayer *view_layer, Base *base, Object *ob)
1810 obn = BKE_object_copy(bmain, ob);
1811 DEG_id_tag_update(&ob->id, ID_RECALC_TRANSFORM | ID_RECALC_GEOMETRY | ID_RECALC_ANIMATION);
1812 BKE_collection_object_add_from(bmain, scene, ob, obn);
1814 basen = BKE_view_layer_base_find(view_layer, obn);
1815 ED_object_base_select(basen, BA_SELECT);
1816 ED_object_base_select(basen, BA_DESELECT);
1820 static int convert_exec(bContext *C, wmOperator *op)
1822 Main *bmain = CTX_data_main(C);
1823 Depsgraph *depsgraph = CTX_data_depsgraph(C);
1824 Scene *scene = CTX_data_scene(C);
1825 ViewLayer *view_layer = CTX_data_view_layer(C);
1826 Base *basen = NULL, *basact = NULL;
1827 Object *ob1, *obact = CTX_data_active_object(C);
1832 const short target = RNA_enum_get(op->ptr, "target");
1833 bool keep_original = RNA_boolean_get(op->ptr, "keep_original");
1834 int a, mballConverted = 0;
1836 /* don't forget multiple users! */
1839 FOREACH_SCENE_OBJECT_BEGIN(scene, ob)
1841 ob->flag &= ~OB_DONE;
1843 /* flag data that's not been edited (only needed for !keep_original) */
1845 ((ID *)ob->data)->tag |= LIB_TAG_DOIT;
1848 /* possible metaball basis is not in this scene */
1849 if (ob->type == OB_MBALL && target == OB_MESH) {
1850 if (BKE_mball_is_basis(ob) == false) {
1852 ob_basis = BKE_mball_basis_find(scene, ob);
1854 ob_basis->flag &= ~OB_DONE;
1859 FOREACH_SCENE_OBJECT_END;
1862 ListBase selected_editable_bases = CTX_data_collection_get(C, "selected_editable_bases");
1864 /* Ensure we get all meshes calculated with a sufficient data-mask,
1865 * needed since re-evaluating single modifiers causes bugs if they depend
1866 * on other objects data masks too, see: T50950. */
1868 for (CollectionPointerLink *link = selected_editable_bases.first; link; link = link->next) {
1869 Base *base = link->ptr.data;
1870 Object *ob = base->object;
1872 /* The way object type conversion works currently (enforcing conversion of *all* objects using converted
1873 * object-data, even some un-selected/hidden/another scene ones, sounds totally bad to me.
1874 * However, changing this is more design than bug-fix, not to mention convoluted code below,
1875 * so that will be for later.
1876 * But at the very least, do not do that with linked IDs! */
1877 if ((ID_IS_LINKED(ob) || (ob->data && ID_IS_LINKED(ob->data))) && !keep_original) {
1878 keep_original = true;
1879 BKE_reportf(op->reports, RPT_INFO,
1880 "Converting some linked object/object data, enforcing 'Keep Original' option to True");
1883 DEG_id_tag_update(&base->object->id, ID_RECALC_GEOMETRY);
1886 uint64_t customdata_mask_prev = scene->customdata_mask;
1887 scene->customdata_mask |= CD_MASK_MESH;
1888 BKE_scene_graph_update_tagged(depsgraph, bmain);
1889 scene->customdata_mask = customdata_mask_prev;
1892 for (CollectionPointerLink *link = selected_editable_bases.first; link; link = link->next) {
1893 Object *newob = NULL;
1894 Base *base = link->ptr.data;
1895 Object *ob = base->object;
1897 if (ob->flag & OB_DONE || !IS_TAGGED(ob->data)) {
1898 if (ob->type != target) {
1899 base->flag &= ~SELECT;
1900 ob->flag &= ~SELECT;
1903 /* obdata already modified */
1904 if (!IS_TAGGED(ob->data)) {
1905 /* When 2 objects with linked data are selected, converting both
1906 * would keep modifiers on all but the converted object [#26003] */
1907 if (ob->type == OB_MESH) {
1908 BKE_object_free_modifiers(ob, 0); /* after derivedmesh calls! */
1910 if (ob->type == OB_GPENCIL) {
1911 BKE_object_free_modifiers(ob, 0); /* after derivedmesh calls! */
1912 BKE_object_free_shaderfx(ob, 0);
1916 else if (ob->type == OB_MESH && target == OB_CURVE) {
1917 ob->flag |= OB_DONE;
1919 if (keep_original) {
1920 basen = duplibase_for_convert(bmain, scene, view_layer, base, NULL);
1921 newob = basen->object;
1923 /* decrement original mesh's usage count */
1927 /* make a new copy of the mesh */
1928 newob->data = BKE_mesh_copy(bmain, me);
1934 BKE_mesh_to_curve(bmain, depsgraph, scene, newob);
1936 if (newob->type == OB_CURVE) {
1937 BKE_object_free_modifiers(newob, 0); /* after derivedmesh calls! */
1938 ED_rigidbody_object_remove(bmain, scene, newob);
1941 else if (ob->type == OB_MESH) {
1942 ob->flag |= OB_DONE;
1944 if (keep_original) {
1945 basen = duplibase_for_convert(bmain, scene, view_layer, base, NULL);
1946 newob = basen->object;
1948 /* decrement original mesh's usage count */
1952 /* make a new copy of the mesh */
1953 newob->data = BKE_mesh_copy(bmain, me);
1957 DEG_id_tag_update(&ob->id, ID_RECALC_TRANSFORM | ID_RECALC_GEOMETRY | ID_RECALC_ANIMATION);
1960 /* make new mesh data from the original copy */
1961 /* note: get the mesh from the original, not from the copy in some
1962 * cases this doesn't give correct results (when MDEF is used for eg)
1964 Scene *scene_eval = (Scene *)DEG_get_evaluated_id(depsgraph, &scene->id);
1965 Object *ob_eval = DEG_get_evaluated_object(depsgraph, ob);
1966 Mesh *me_eval = mesh_get_eval_final(depsgraph, scene_eval, ob_eval, CD_MASK_MESH);
1967 me_eval = BKE_mesh_copy_for_eval(me_eval, false);
1968 BKE_mesh_nomain_to_mesh(me_eval, newob->data, newob, CD_MASK_MESH, true);
1969 BKE_object_free_modifiers(newob, 0); /* after derivedmesh calls! */
1971 else if (ob->type == OB_FONT) {
1972 ob->flag |= OB_DONE;
1974 if (keep_original) {
1975 basen = duplibase_for_convert(bmain, scene, view_layer, base, NULL);
1976 newob = basen->object;
1978 /* decrement original curve's usage count */
1979 id_us_min(&((Curve *)newob->data)->id);
1981 /* make a new copy of the curve */
1982 newob->data = BKE_curve_copy(bmain, ob->data);
1990 /* TODO(sergey): Ideally DAG will create nurbs list for a curve data
1991 * datablock, but for until we've got granular update
1992 * lets take care by selves.
1994 /* XXX This may fail/crash, since BKE_vfont_to_curve()
1995 * accesses evaluated data in some cases (bastien). */
1996 BKE_vfont_to_curve(newob, FO_EDIT);
1998 newob->type = OB_CURVE;
1999 cu->type = OB_CURVE;
2002 id_us_min(&cu->vfont->id);
2006 id_us_min(&cu->vfontb->id);
2010 id_us_min(&cu->vfonti->id);
2014 id_us_min(&cu->vfontbi->id);
2018 if (!keep_original) {
2020 if (cu->id.us > 1) {
2021 for (ob1 = bmain->object.first; ob1; ob1 = ob1->id.next) {
2022 if (ob1->data == ob->data) {
2023 ob1->type = OB_CURVE;
2024 DEG_id_tag_update(&ob1->id, ID_RECALC_TRANSFORM | ID_RECALC_GEOMETRY | ID_RECALC_ANIMATION);
2030 for (nu = cu->nurb.first; nu; nu = nu->next)
2034 BKE_curve_curve_dimension_update(cu);
2036 if (target == OB_MESH) {
2037 curvetomesh(bmain, depsgraph, scene, newob);
2039 /* meshes doesn't use displist */
2040 BKE_object_free_curve_cache(newob);
2043 else if (ELEM(ob->type, OB_CURVE, OB_SURF)) {
2044 ob->flag |= OB_DONE;
2046 if (target == OB_MESH) {
2047 if (keep_original) {
2048 basen = duplibase_for_convert(bmain, scene, view_layer, base, NULL);
2049 newob = basen->object;
2051 /* decrement original curve's usage count */
2052 id_us_min(&((Curve *)newob->data)->id);
2054 /* make a new copy of the curve */
2055 newob->data = BKE_curve_copy(bmain, ob->data);
2061 curvetomesh(bmain, depsgraph, scene, newob);
2063 /* meshes doesn't use displist */
2064 BKE_object_free_curve_cache(newob);
2067 else if (ob->type == OB_MBALL && target == OB_MESH) {
2070 base->flag &= ~BASE_SELECTED;
2071 ob->base_flag &= ~BASE_SELECTED;
2073 baseob = BKE_mball_basis_find(scene, ob);
2076 /* if motherball is converting it would be marked as done later */
2077 ob->flag |= OB_DONE;
2080 if (!(baseob->flag & OB_DONE)) {
2081 baseob->flag |= OB_DONE;
2083 basen = duplibase_for_convert(bmain, scene, view_layer, base, baseob);
2084 newob = basen->object;
2089 newob->data = BKE_mesh_add(bmain, "Mesh");
2090 newob->type = OB_MESH;
2093 me->totcol = mb->totcol;
2094 if (newob->totcol) {
2095 me->mat = MEM_dupallocN(mb->mat);
2096 for (a = 0; a < newob->totcol; a++) id_us_plus((ID *)me->mat[a]);
2099 convert_ensure_curve_cache(depsgraph, scene, baseob);
2100 BKE_mesh_from_metaball(&baseob->runtime.curve_cache->disp, newob->data);
2102 if (obact->type == OB_MBALL) {
2113 /* Ensure new object has consistent material data with its new obdata. */
2115 test_object_materials(bmain, newob, newob->data);
2118 /* tag obdata if it was been changed */
2120 /* If the original object is active then make this object active */
2123 /* store new active base to update BASACT */
2130 if (!keep_original && (ob->flag & OB_DONE)) {
2131 DEG_id_tag_update(&ob->id, ID_RECALC_GEOMETRY);
2132 ((ID *)ob->data)->tag &= ~LIB_TAG_DOIT; /* flag not to convert this datablock again */
2135 BLI_freelistN(&selected_editable_bases);
2137 if (!keep_original) {
2138 if (mballConverted) {
2139 FOREACH_SCENE_OBJECT_BEGIN(scene, ob_mball)
2141 if (ob_mball->type == OB_MBALL) {
2142 if (ob_mball->flag & OB_DONE) {
2143 Object *ob_basis = NULL;
2144 if (BKE_mball_is_basis(ob_mball) ||
2145 ((ob_basis = BKE_mball_basis_find(scene, ob_mball)) && (ob_basis->flag & OB_DONE)))
2147 ED_object_base_free_and_unlink(bmain, scene, ob_mball);
2152 FOREACH_SCENE_OBJECT_END;
2156 // XXX ED_object_editmode_enter(C, 0);
2157 // XXX exit_editmode(C, EM_FREEDATA|); /* freedata, but no undo */
2160 /* active base was changed */
2161 ED_object_base_activate(C, basact);
2162 BASACT(view_layer) = basact;
2164 else if (BASACT(view_layer)->object->flag & OB_DONE) {
2165 WM_event_add_notifier(C, NC_OBJECT | ND_MODIFIER, BASACT(view_layer)->object);
2166 WM_event_add_notifier(C, NC_OBJECT | ND_DATA, BASACT(view_layer)->object);
2169 DEG_relations_tag_update(bmain);
2170 DEG_id_tag_update(&scene->id, ID_RECALC_SELECT);
2171 WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, scene);
2172 WM_event_add_notifier(C, NC_SCENE | ND_OB_SELECT, scene);
2174 return OPERATOR_FINISHED;
2178 void OBJECT_OT_convert(wmOperatorType *ot)
2181 ot->name = "Convert to";
2182 ot->description = "Convert selected objects to another type";
2183 ot->idname = "OBJECT_OT_convert";
2186 ot->invoke = WM_menu_invoke;
2187 ot->exec = convert_exec;
2188 ot->poll = convert_poll;
2191 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2194 ot->prop = RNA_def_enum(ot->srna, "target", convert_target_items, OB_MESH, "Target", "Type of object to convert to");
2195 RNA_def_boolean(ot->srna, "keep_original", 0, "Keep Original", "Keep original objects instead of replacing them");
2198 /**************************** Duplicate ************************/
2201 * dupflag: a flag made from constants declared in DNA_userdef_types.h
2202 * The flag tells adduplicate() whether to copy data linked to the object, or to reference the existing data.
2203 * U.dupflag for default operations or you can construct a flag as python does
2204 * if the dupflag is 0 then no data will be copied (linked duplicate) */
2206 /* used below, assumes id.new is correct */
2207 /* leaves selection of base/object unaltered */
2208 /* Does set ID->newid pointers. */
2209 static Base *object_add_duplicate_internal(Main *bmain, Scene *scene, ViewLayer *view_layer, Object *ob, int dupflag)
2211 #define ID_NEW_REMAP_US(a) if ( (a)->id.newid) { (a) = (void *)(a)->id.newid; (a)->id.us++; }
2212 #define ID_NEW_REMAP_US2(a) if (((ID *)a)->newid) { (a) = ((ID *)a)->newid; ((ID *)a)->us++; }
2214 Base *base, *basen = NULL;
2215 Material ***matarar;
2220 if (ob->mode & OB_MODE_POSE) {
2224 obn = ID_NEW_SET(ob, BKE_object_copy(bmain, ob));
2225 DEG_id_tag_update(&obn->id, ID_RECALC_TRANSFORM | ID_RECALC_GEOMETRY);
2227 base = BKE_view_layer_base_find(view_layer, ob);
2228 if ((base != NULL) && (base->flag & BASE_VISIBLE)) {
2229 BKE_collection_object_add_from(bmain, scene, ob, obn);
2232 LayerCollection *layer_collection = BKE_layer_collection_get_active(view_layer);
2233 BKE_collection_object_add(bmain, layer_collection->collection, obn);
2236 basen = BKE_view_layer_base_find(view_layer, obn);
2238 basen->local_view_bits = base->local_view_bits;
2241 /* 1) duplis should end up in same collection as the original
2242 * 2) Rigid Body sim participants MUST always be part of a collection...
2244 // XXX: is 2) really a good measure here?
2245 if (ob->rigidbody_object || ob->rigidbody_constraint) {
2246 Collection *collection;
2247 for (collection = bmain->collection.first; collection; collection = collection->id.next) {
2248 if (BKE_collection_has_object(collection, ob))
2249 BKE_collection_object_add(bmain, collection, obn);
2253 /* duplicates using userflags */
2254 if (dupflag & USER_DUP_ACT) {
2255 BKE_animdata_copy_id_action(bmain, &obn->id, true);
2258 if (dupflag & USER_DUP_MAT) {
2259 for (a = 0; a < obn->totcol; a++) {
2260 id = (ID *)obn->mat[a];
2262 ID_NEW_REMAP_US(obn->mat[a])
2264 obn->mat[a] = ID_NEW_SET(obn->mat[a], BKE_material_copy(bmain, obn->mat[a]));
2268 if (dupflag & USER_DUP_ACT) {
2269 BKE_animdata_copy_id_action(bmain, &obn->mat[a]->id, true);
2274 if (dupflag & USER_DUP_PSYS) {
2275 ParticleSystem *psys;
2276 for (psys = obn->particlesystem.first; psys; psys = psys->next) {
2277 id = (ID *) psys->part;
2279 ID_NEW_REMAP_US(psys->part)
2281 psys->part = ID_NEW_SET(psys->part, BKE_particlesettings_copy(bmain, psys->part));
2284 if (dupflag & USER_DUP_ACT) {
2285 BKE_animdata_copy_id_action(bmain, &psys->part->id, true);
2296 switch (obn->type) {
2298 if (dupflag & USER_DUP_MESH) {
2299 ID_NEW_REMAP_US2(obn->data)
2301 obn->data = ID_NEW_SET(obn->data, BKE_mesh_copy(bmain, obn->data));
2308 if (dupflag & USER_DUP_CURVE) {
2309 ID_NEW_REMAP_US2(obn->data)
2311 obn->data = ID_NEW_SET(obn->data, BKE_curve_copy(bmain, obn->data));
2318 if (dupflag & USER_DUP_SURF) {
2319 ID_NEW_REMAP_US2(obn->data)
2321 obn->data = ID_NEW_SET(obn->data, BKE_curve_copy(bmain, obn->data));
2328 if (dupflag & USER_DUP_FONT) {
2329 ID_NEW_REMAP_US2(obn->data)
2331 obn->data = ID_NEW_SET(obn->data, BKE_curve_copy(bmain, obn->data));
2338 if (dupflag & USER_DUP_MBALL) {
2339 ID_NEW_REMAP_US2(obn->data)
2341 obn->data = ID_NEW_SET(obn->data, BKE_mball_copy(bmain, obn->data));
2348 if (dupflag & USER_DUP_LAMP) {
2349 ID_NEW_REMAP_US2(obn->data)
2351 obn->data = ID_NEW_SET(obn->data, BKE_light_copy(bmain, obn->data));
2358 DEG_id_tag_update(&obn->id, ID_RECALC_GEOMETRY);
2360 BKE_pose_tag_recalc(bmain, obn->pose);
2361 if (dupflag & USER_DUP_ARM) {
2362 ID_NEW_REMAP_US2(obn->data)
2364 obn->data = ID_NEW_SET(obn->data, BKE_armature_copy(bmain, obn->data));
2365 BKE_pose_rebuild(bmain, obn, obn->data, true);
2373 ID_NEW_REMAP_US2(obn->data)
2375 obn->data = ID_NEW_SET(obn->data, BKE_lattice_copy(bmain, obn->data));
2383 ID_NEW_REMAP_US2(obn->data)
2385 obn->data = ID_NEW_SET(obn->data, BKE_camera_copy(bmain, obn->data));
2393 ID_NEW_REMAP_US2(obn->data)
2395 obn->data = ID_NEW_SET(obn->data, BKE_lightprobe_copy(bmain, obn->data));
2403 ID_NEW_REMAP_US2(obn->data)
2405 obn->data = ID_NEW_SET(obn->data, BKE_speaker_copy(bmain, obn->data));
2413 ID_NEW_REMAP_US2(obn->data)
2415 obn->data = ID_NEW_SET(obn->data, BKE_gpencil_copy(bmain, obn->data));
2423 /* check if obdata is copied */
2425 Key *key = BKE_key_from_object(obn);
2427 Key *oldkey = BKE_key_from_object(ob);
2428 if (oldkey != NULL) {
2429 ID_NEW_SET(oldkey, key);
2432 if (dupflag & USER_DUP_ACT) {
2433 BKE_animdata_copy_id_action(bmain, (ID *)obn->data, true);
2435 BKE_animdata_copy_id_action(bmain, (ID *)key, true);
2439 if (dupflag & USER_DUP_MAT) {
2440 matarar = give_matarar(obn);
2442 for (a = 0; a < obn->totcol; a++) {
2443 id = (ID *)(*matarar)[a];
2445 ID_NEW_REMAP_US((*matarar)[a])
2447 (*matarar)[a] = ID_NEW_SET((*matarar)[a], BKE_material_copy(bmain, (*matarar)[a]));
2458 #undef ID_NEW_REMAP_US
2459 #undef ID_NEW_REMAP_US2
2462 /* single object duplicate, if dupflag==0, fully linked, else it uses the flags given */
2463 /* leaves selection of base/object unaltered.
2464 * note: don't call this within a loop since clear_* funcs loop over the entire database.
2465 * note: caller must do DAG_relations_tag_update(bmain);
2466 * this is not done automatic since we may duplicate many objects in a batch */
2467 Base *ED_object_add_duplicate(Main *bmain, Scene *scene, ViewLayer *view_layer, Base *base, int dupflag)
2472 basen = object_add_duplicate_internal(bmain, scene, view_layer, base->object, dupflag);
2473 if (basen == NULL) {
2479 /* link own references to the newly duplicated data [#26816] */
2480 BKE_libblock_relink_to_newid(&ob->id);
2482 /* DAG_relations_tag_update(bmain); */ /* caller must do */
2484 if (ob->data != NULL) {
2485 DEG_id_tag_update_ex(bmain, (ID *)ob->data, ID_RECALC_EDITORS);
2488 BKE_main_id_clear_newpoins(bmain);
2493 /* contextual operator dupli */
2494 static int duplicate_exec(bContext *C, wmOperator *op)
2496 Main *bmain = CTX_data_main(C);
2497 Scene *scene = CTX_data_scene(C);
2498 ViewLayer *view_layer = CTX_data_view_layer(C);
2499 const bool linked = RNA_boolean_get(op->ptr, "linked");
2500 int dupflag = (linked) ? 0 : U.dupflag;
2502 CTX_DATA_BEGIN (C, Base *, base, selected_bases)
2504 Base *basen = object_add_duplicate_internal(bmain, scene, view_layer, base->object, dupflag);
2506 /* note that this is safe to do with this context iterator,
2507 * the list is made in advance */
2508 ED_object_base_select(base, BA_DESELECT);
2509 ED_object_base_select(basen, BA_SELECT);
2511 if (basen == NULL) {
2515 /* new object becomes active */
2516 if (BASACT(view_layer) == base)
2517 ED_object_base_activate(C, basen);
2519 if (basen->object->data) {
2520 DEG_id_tag_update(basen->object->data, 0);
2525 copy_object_set_idnew(C);
2527 DEG_relations_tag_update(bmain);
2528 DEG_id_tag_update(&scene->id, ID_RECALC_COPY_ON_WRITE | ID_RECALC_SELECT);
2530 WM_event_add_notifier(C, NC_SCENE | ND_OB_SELECT, scene);
2532 return OPERATOR_FINISHED;
2535 void OBJECT_OT_duplicate(wmOperatorType *ot)
2540 ot->name = "Duplicate Objects";
2541 ot->description = "Duplicate selected objects";
2542 ot->idname = "OBJECT_OT_duplicate";
2545 ot->exec = duplicate_exec;
2546 ot->poll = ED_operator_objectmode;
2549 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2551 /* to give to transform */
2552 RNA_def_boolean(ot->srna, "linked", 0, "Linked", "Duplicate object but not object data, linking to the original data");
2553 prop = RNA_def_enum(ot->srna, "mode", rna_enum_transform_mode_types, TFM_TRANSLATION, "Mode", "");
2554 RNA_def_property_flag(prop, PROP_HIDDEN);
2557 /* **************** add named object, for dragdrop ************* */
2559 static int add_named_exec(bContext *C, wmOperator *op)
2561 wmWindow *win = CTX_wm_window(C);
2562 const wmEvent *event = win ? win->eventstate : NULL;
2563 Main *bmain = CTX_data_main(C);
2564 Scene *scene = CTX_data_scene(C);
2565 ViewLayer *view_layer = CTX_data_view_layer(C);
2568 const bool linked = RNA_boolean_get(op->ptr, "linked");
2569 int dupflag = (linked) ? 0 : U.dupflag;
2570 char name[MAX_ID_NAME - 2];
2572 /* find object, create fake base */
2573 RNA_string_get(op->ptr, "name", name);
2574 ob = (Object *)BKE_libblock_find_name(bmain, ID_OB, name);
2577 BKE_report(op->reports, RPT_ERROR, "Object not found");
2578 return OPERATOR_CANCELLED;
2582 basen = object_add_duplicate_internal(bmain, scene, view_layer, ob, dupflag);
2584 if (basen == NULL) {
2585 BKE_report(op->reports, RPT_ERROR, "Object could not be duplicated");
2586 return OPERATOR_CANCELLED;
2589 BKE_scene_object_base_flag_sync_from_object(basen);
2590 basen->object->restrictflag &= ~OB_RESTRICT_VIEW;
2593 ARegion *ar = CTX_wm_region(C);
2594 const int mval[2] = {event->x - ar->winrct.xmin,
2595 event->y - ar->winrct.ymin};
2596 ED_object_location_from_view(C, basen->object->loc);
2597 ED_view3d_cursor3d_position(C, mval, false, basen->object->loc);
2600 ED_object_base_select(basen, BA_SELECT);
2601 ED_object_base_activate(C, basen);
2603 copy_object_set_idnew(C);
2605 /* TODO(sergey): Only update relations for the current scene. */
2606 DEG_relations_tag_update(bmain);
2608 DEG_id_tag_update(&scene->id, ID_RECALC_SELECT);
2609 WM_event_add_notifier(C, NC_SCENE | ND_OB_SELECT, scene);
2610 WM_event_add_notifier(C, NC_SCENE | ND_OB_ACTIVE, scene);
2612 return OPERATOR_FINISHED;
2615 void OBJECT_OT_add_named(wmOperatorType *ot)
2618 ot->name = "Add Named Object";
2619 ot->description = "Add named object";
2620 ot->idname = "OBJECT_OT_add_named";
2623 ot->exec = add_named_exec;
2624 ot->poll = ED_operator_objectmode;
2627 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2629 RNA_def_boolean(ot->srna, "linked", 0, "Linked", "Duplicate object but not object data, linking to the original data");
2630 RNA_def_string(ot->srna, "name", NULL, MAX_ID_NAME - 2, "Name", "Object name to add");
2633 /**************************** Join *************************/
2635 static bool join_poll(bContext *C)
2637 Object *ob = CTX_data_active_object(C);
2639 if (!ob || ID_IS_LINKED(ob)) return 0;
2641 if (ELEM(ob->type, OB_MESH, OB_CURVE, OB_SURF, OB_ARMATURE, OB_GPENCIL))
2642 return ED_operator_screenactive(C);
2647 static int join_exec(bContext *C, wmOperator *op)
2649 Object *ob = CTX_data_active_object(C);
2651 if (ob->mode & OB_MODE_EDIT) {
2652 BKE_report(op->reports, RPT_ERROR, "This data does not support joining in edit mode");
2653 return OPERATOR_CANCELLED;
2655 else if (BKE_object_obdata_is_libdata(ob)) {
2656 BKE_report(op->reports, RPT_ERROR, "Cannot edit external libdata");
2657 return OPERATOR_CANCELLED;
2659 else if (ob->type == OB_GPENCIL) {
2660 bGPdata *gpd = (bGPdata *)ob->data;
2661 if ((!gpd) || GPENCIL_ANY_MODE(gpd)) {
2662 BKE_report(op->reports, RPT_ERROR, "This data does not support joining in this mode");
2663 return OPERATOR_CANCELLED;
2667 if (ob->type == OB_MESH)
2668 return join_mesh_exec(C, op);
2669 else if (ELEM(ob->type, OB_CURVE, OB_SURF))
2670 return join_curve_exec(C, op);
2671 else if (ob->type == OB_ARMATURE)
2672 return join_armature_exec(C, op);
2673 else if (ob->type == OB_GPENCIL)
2674 return ED_gpencil_join_objects_exec(C, op);
2676 return OPERATOR_CANCELLED;
2679 void OBJECT_OT_join(wmOperatorType *ot)
2683 ot->description = "Join selected objects into active object";
2684 ot->idname = "OBJECT_OT_join";
2687 ot->exec = join_exec;
2688 ot->poll = join_poll;
2691 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2694 /**************************** Join as Shape Key*************************/
2696 static bool join_shapes_poll(bContext *C)
2698 Object *ob = CTX_data_active_object(C);
2700 if (!ob || ID_IS_LINKED(ob)) return 0;
2702 /* only meshes supported at the moment */
2703 if (ob->type == OB_MESH)
2704 return ED_operator_screenactive(C);
2709 static int join_shapes_exec(bContext *C, wmOperator *op)
2711 Object *ob = CTX_data_active_object(C);
2713 if (ob->mode & OB_MODE_EDIT) {
2714 BKE_report(op->reports, RPT_ERROR, "This data does not support joining in edit mode");
2715 return OPERATOR_CANCELLED;
2717 else if (BKE_object_obdata_is_libdata(ob)) {
2718 BKE_report(op->reports, RPT_ERROR, "Cannot edit external libdata");
2719 return OPERATOR_CANCELLED;
2722 if (ob->type == OB_MESH)
2723 return join_mesh_shapes_exec(C, op);
2725 return OPERATOR_CANCELLED;
2728 void OBJECT_OT_join_shapes(wmOperatorType *ot)
2731 ot->name = "Join as Shapes";
2732 ot->description = "Merge selected objects to shapes of active object";
2733 ot->idname = "OBJECT_OT_join_shapes";
2736 ot->exec = join_shapes_exec;
2737 ot->poll = join_shapes_poll;
2740 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;