2 * ***** BEGIN GPL LICENSE BLOCK *****
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19 * All rights reserved.
21 * Contributor(s): Blender Foundation, 2002-2008 full recode
23 * ***** END GPL LICENSE BLOCK *****
26 /** \file blender/editors/object/object_add.c
34 #include "MEM_guardedalloc.h"
36 #include "DNA_anim_types.h"
37 #include "DNA_camera_types.h"
38 #include "DNA_curve_types.h"
39 #include "DNA_group_types.h"
40 #include "DNA_lamp_types.h"
41 #include "DNA_key_types.h"
42 #include "DNA_material_types.h"
43 #include "DNA_mesh_types.h"
44 #include "DNA_meta_types.h"
45 #include "DNA_object_fluidsim.h"
46 #include "DNA_object_force.h"
47 #include "DNA_object_types.h"
48 #include "DNA_scene_types.h"
49 #include "DNA_vfont_types.h"
50 #include "DNA_actuator_types.h"
51 #include "DNA_gpencil_types.h"
53 #include "BLI_utildefines.h"
54 #include "BLI_ghash.h"
55 #include "BLI_listbase.h"
57 #include "BLI_string.h"
59 #include "BLT_translation.h"
61 #include "BKE_action.h"
63 #include "BKE_animsys.h"
64 #include "BKE_armature.h"
65 #include "BKE_camera.h"
66 #include "BKE_collection.h"
67 #include "BKE_context.h"
68 #include "BKE_constraint.h"
69 #include "BKE_curve.h"
70 #include "BKE_DerivedMesh.h"
71 #include "BKE_displist.h"
72 #include "BKE_effect.h"
74 #include "BKE_group.h"
76 #include "BKE_lattice.h"
77 #include "BKE_layer.h"
78 #include "BKE_library.h"
79 #include "BKE_library_query.h"
80 #include "BKE_library_remap.h"
83 #include "BKE_material.h"
84 #include "BKE_mball.h"
87 #include "BKE_object.h"
88 #include "BKE_particle.h"
89 #include "BKE_report.h"
91 #include "BKE_scene.h"
92 #include "BKE_screen.h"
93 #include "BKE_speaker.h"
94 #include "BKE_texture.h"
96 #include "DEG_depsgraph.h"
97 #include "DEG_depsgraph_build.h"
99 #include "RNA_access.h"
100 #include "RNA_define.h"
101 #include "RNA_enum_types.h"
104 #include "WM_types.h"
106 #include "ED_armature.h"
107 #include "ED_curve.h"
108 #include "ED_mball.h"
111 #include "ED_object.h"
112 #include "ED_physics.h"
113 #include "ED_render.h"
114 #include "ED_screen.h"
115 #include "ED_transform.h"
116 #include "ED_view3d.h"
118 #include "UI_resources.h"
120 #include "GPU_lamp.h"
122 #include "object_intern.h"
124 /* this is an exact copy of the define in rna_lamp.c
125 * kept here because of linking order.
126 * Icons are only defined here */
127 EnumPropertyItem rna_enum_lamp_type_items[] = {
128 {LA_LOCAL, "POINT", ICON_LAMP_POINT, "Point", "Omnidirectional point light source"},
129 {LA_SUN, "SUN", ICON_LAMP_SUN, "Sun", "Constant direction parallel ray light source"},
130 {LA_SPOT, "SPOT", ICON_LAMP_SPOT, "Spot", "Directional cone light source"},
131 {LA_HEMI, "HEMI", ICON_LAMP_HEMI, "Hemi", "180 degree constant light source"},
132 {LA_AREA, "AREA", ICON_LAMP_AREA, "Area", "Directional area light source"},
133 {0, NULL, 0, NULL, NULL}
136 /* copy from rna_object_force.c */
137 static EnumPropertyItem field_type_items[] = {
138 {PFIELD_FORCE, "FORCE", ICON_FORCE_FORCE, "Force", ""},
139 {PFIELD_WIND, "WIND", ICON_FORCE_WIND, "Wind", ""},
140 {PFIELD_VORTEX, "VORTEX", ICON_FORCE_VORTEX, "Vortex", ""},
141 {PFIELD_MAGNET, "MAGNET", ICON_FORCE_MAGNETIC, "Magnetic", ""},
142 {PFIELD_HARMONIC, "HARMONIC", ICON_FORCE_HARMONIC, "Harmonic", ""},
143 {PFIELD_CHARGE, "CHARGE", ICON_FORCE_CHARGE, "Charge", ""},
144 {PFIELD_LENNARDJ, "LENNARDJ", ICON_FORCE_LENNARDJONES, "Lennard-Jones", ""},
145 {PFIELD_TEXTURE, "TEXTURE", ICON_FORCE_TEXTURE, "Texture", ""},
146 {PFIELD_GUIDE, "GUIDE", ICON_FORCE_CURVE, "Curve Guide", ""},
147 {PFIELD_BOID, "BOID", ICON_FORCE_BOID, "Boid", ""},
148 {PFIELD_TURBULENCE, "TURBULENCE", ICON_FORCE_TURBULENCE, "Turbulence", ""},
149 {PFIELD_DRAG, "DRAG", ICON_FORCE_DRAG, "Drag", ""},
150 {PFIELD_SMOKEFLOW, "SMOKE", ICON_FORCE_SMOKEFLOW, "Smoke Flow", ""},
151 {0, NULL, 0, NULL, NULL}
154 /************************** Exported *****************************/
156 void ED_object_location_from_view(bContext *C, float loc[3])
158 View3D *v3d = CTX_wm_view3d(C);
159 Scene *scene = CTX_data_scene(C);
162 cursor = ED_view3d_cursor3d_get(scene, v3d);
164 copy_v3_v3(loc, cursor);
167 void ED_object_rotation_from_view(bContext *C, float rot[3], const char align_axis)
169 RegionView3D *rv3d = CTX_wm_region_view3d(C);
171 BLI_assert(align_axis >= 'X' && align_axis <= 'Z');
176 switch (align_axis) {
180 axis_angle_to_quat(quat_y, rv3d->viewinv[1], -M_PI_2);
181 mul_qt_qtqt(quat, rv3d->viewquat, quat_y);
184 quat_to_eul(rot, quat);
189 copy_qt_qt(quat, rv3d->viewquat);
192 quat_to_eul(rot, quat);
193 rot[0] -= (float)M_PI_2;
198 copy_qt_qt(quat, rv3d->viewquat);
201 quat_to_eul(rot, quat);
212 void ED_object_base_init_transform(bContext *C, Base *base, const float loc[3], const float rot[3])
214 Object *ob = base->object;
215 Scene *scene = CTX_data_scene(C);
220 copy_v3_v3(ob->loc, loc);
223 copy_v3_v3(ob->rot, rot);
225 BKE_object_where_is_calc(scene, ob);
228 /* Uses context to figure out transform for primitive.
229 * Returns standard diameter. */
230 float ED_object_new_primitive_matrix(
231 bContext *C, Object *obedit,
232 const float loc[3], const float rot[3], float primmat[4][4])
234 Scene *scene = CTX_data_scene(C);
235 View3D *v3d = CTX_wm_view3d(C);
236 float mat[3][3], rmat[3][3], cmat[3][3], imat[3][3];
240 eul_to_mat3(rmat, rot);
243 /* inverse transform for initial rotation and object */
244 copy_m3_m4(mat, obedit->obmat);
245 mul_m3_m3m3(cmat, rmat, mat);
246 invert_m3_m3(imat, cmat);
247 copy_m4_m3(primmat, imat);
250 copy_v3_v3(primmat[3], loc);
251 sub_v3_v3v3(primmat[3], primmat[3], obedit->obmat[3]);
252 invert_m3_m3(imat, mat);
253 mul_m3_v3(imat, primmat[3]);
256 const float dia = v3d ? ED_view3d_grid_scale(scene, v3d, NULL) : ED_scene_grid_scale(scene, NULL);
263 /********************* Add Object Operator ********************/
265 static void view_align_update(struct Main *UNUSED(main), struct Scene *UNUSED(scene), struct PointerRNA *ptr)
267 RNA_struct_idprops_unset(ptr, "rotation");
270 void ED_object_add_unit_props(wmOperatorType *ot)
272 RNA_def_float_distance(ot->srna, "radius", 1.0f, 0.0, OBJECT_ADD_SIZE_MAXF, "Radius", "", 0.001, 100.00);
275 void ED_object_add_generic_props(wmOperatorType *ot, bool do_editmode)
279 /* note: this property gets hidden for add-camera operator */
280 prop = RNA_def_boolean(ot->srna, "view_align", 0, "Align to View", "Align the new object to the view");
281 RNA_def_property_update_runtime(prop, view_align_update);
284 prop = RNA_def_boolean(ot->srna, "enter_editmode", 0, "Enter Editmode",
285 "Enter editmode when adding this object");
286 RNA_def_property_flag(prop, PROP_HIDDEN | PROP_SKIP_SAVE);
289 prop = RNA_def_float_vector_xyz(ot->srna, "location", 3, NULL, -OBJECT_ADD_SIZE_MAXF, OBJECT_ADD_SIZE_MAXF,
290 "Location", "Location for the newly added object", -1000.0f, 1000.0f);
291 RNA_def_property_flag(prop, PROP_SKIP_SAVE);
292 prop = RNA_def_float_rotation(ot->srna, "rotation", 3, NULL, -OBJECT_ADD_SIZE_MAXF, OBJECT_ADD_SIZE_MAXF,
293 "Rotation", "Rotation for the newly added object",
294 DEG2RADF(-360.0f), DEG2RADF(360.0f));
295 RNA_def_property_flag(prop, PROP_SKIP_SAVE);
297 prop = RNA_def_boolean_layer_member(ot->srna, "layers", 20, NULL, "Layer", "");
298 RNA_def_property_flag(prop, PROP_HIDDEN | PROP_SKIP_SAVE);
301 void ED_object_add_mesh_props(wmOperatorType *ot)
303 RNA_def_boolean(ot->srna, "calc_uvs", false, "Generate UVs", "Generate a default UV map");
306 bool ED_object_add_generic_get_opts(bContext *C, wmOperator *op, const char view_align_axis,
307 float loc[3], float rot[3],
308 bool *enter_editmode, unsigned int *layer, bool *is_view_aligned)
310 View3D *v3d = CTX_wm_view3d(C);
314 /* Switch to Edit mode? optional prop */
315 if ((prop = RNA_struct_find_property(op->ptr, "enter_editmode"))) {
316 bool _enter_editmode;
318 enter_editmode = &_enter_editmode;
320 if (RNA_property_is_set(op->ptr, prop) && enter_editmode)
321 *enter_editmode = RNA_property_boolean_get(op->ptr, prop);
323 *enter_editmode = (U.flag & USER_ADD_EDITMODE) != 0;
324 RNA_property_boolean_set(op->ptr, prop, *enter_editmode);
330 int a, layer_values[20];
334 prop = RNA_struct_find_property(op->ptr, "layers");
335 if (RNA_property_is_set(op->ptr, prop)) {
336 RNA_property_boolean_get_array(op->ptr, prop, layer_values);
338 for (a = 0; a < 20; a++) {
344 Scene *scene = CTX_data_scene(C);
345 *layer = BKE_screen_view3d_layer_active_ex(v3d, scene, false);
346 for (a = 0; a < 20; a++) {
347 layer_values[a] = (*layer & (1 << a)) != 0;
349 RNA_property_boolean_set_array(op->ptr, prop, layer_values);
352 /* in local view we additionally add local view layers,
353 * not part of operator properties */
354 if (v3d && v3d->localvd)
364 if (RNA_struct_property_is_set(op->ptr, "location")) {
365 RNA_float_get_array(op->ptr, "location", loc);
368 ED_object_location_from_view(C, loc);
369 RNA_float_set_array(op->ptr, "location", loc);
375 bool _is_view_aligned;
377 if (!is_view_aligned)
378 is_view_aligned = &_is_view_aligned;
382 if (RNA_struct_property_is_set(op->ptr, "rotation"))
383 *is_view_aligned = false;
384 else if (RNA_struct_property_is_set(op->ptr, "view_align"))
385 *is_view_aligned = RNA_boolean_get(op->ptr, "view_align");
387 *is_view_aligned = (U.flag & USER_ADD_VIEWALIGNED) != 0;
388 RNA_boolean_set(op->ptr, "view_align", *is_view_aligned);
391 if (*is_view_aligned) {
392 ED_object_rotation_from_view(C, rot, view_align_axis);
393 RNA_float_set_array(op->ptr, "rotation", rot);
396 RNA_float_get_array(op->ptr, "rotation", rot);
399 if (layer && *layer == 0) {
400 BKE_report(op->reports, RPT_ERROR, "Property 'layer' has no values set");
407 /* For object add primitive operators.
408 * Do not call undo push in this function (users of this function have to). */
409 Object *ED_object_add_type(
411 int type, const char *name,
412 const float loc[3], const float rot[3],
413 bool enter_editmode, unsigned int UNUSED(layer))
415 Main *bmain = CTX_data_main(C);
416 Scene *scene = CTX_data_scene(C);
417 SceneLayer *sl = CTX_data_scene_layer(C);
420 /* for as long scene has editmode... */
421 if (CTX_data_edit_object(C))
422 ED_object_editmode_exit(C, EM_FREEDATA | EM_FREEUNDO | EM_WAITCURSOR | EM_DO_UNDO); /* freedata, and undo */
424 /* deselects all, sets scene->basact */
425 ob = BKE_object_add(bmain, scene, sl, type, name);
426 /* editor level activate, notifiers */
427 ED_object_base_activate(C, sl->basact);
429 /* more editor stuff */
430 ED_object_base_init_transform(C, sl->basact, loc, rot);
432 /* Ignore collisions by default for non-mesh objects */
433 if (type != OB_MESH) {
434 ob->body_type = OB_BODY_TYPE_NO_COLLISION;
435 ob->gameflag &= ~(OB_SENSOR | OB_RIGID_BODY | OB_SOFT_BODY | OB_COLLISION | OB_CHARACTER | OB_OCCLUDER | OB_DYNAMIC | OB_NAVMESH); /* copied from rna_object.c */
438 DEG_id_type_tag(bmain, ID_OB);
439 DEG_relations_tag_update(bmain);
441 ED_render_id_flush_update(bmain, ob->data);
445 ED_object_editmode_enter(C, EM_IGNORE_LAYER);
447 WM_event_add_notifier(C, NC_SCENE | ND_LAYER_CONTENT, scene);
449 /* TODO(sergey): Use proper flag for tagging here. */
450 DEG_id_tag_update(&scene->id, 0);
455 /* for object add operator */
456 static int object_add_exec(bContext *C, wmOperator *op)
461 float loc[3], rot[3], radius;
463 WM_operator_view3d_unit_defaults(C, op);
464 if (!ED_object_add_generic_get_opts(C, op, 'Z', loc, rot, &enter_editmode, &layer, NULL))
465 return OPERATOR_CANCELLED;
467 radius = RNA_float_get(op->ptr, "radius");
468 ob = ED_object_add_type(C, RNA_enum_get(op->ptr, "type"), NULL, loc, rot, enter_editmode, layer);
470 if (ob->type == OB_LATTICE) {
471 /* lattice is a special case!
472 * we never want to scale the obdata since that is the rest-state */
473 copy_v3_fl(ob->size, radius);
476 BKE_object_obdata_size_init(ob, radius);
479 return OPERATOR_FINISHED;
482 void OBJECT_OT_add(wmOperatorType *ot)
485 ot->name = "Add Object";
486 ot->description = "Add an object to the scene";
487 ot->idname = "OBJECT_OT_add";
490 ot->exec = object_add_exec;
491 ot->poll = ED_operator_objectmode;
494 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
497 ED_object_add_unit_props(ot);
498 RNA_def_enum(ot->srna, "type", rna_enum_object_type_items, 0, "Type", "");
500 ED_object_add_generic_props(ot, true);
503 /********************* Add Effector Operator ********************/
505 /* for object add operator */
506 static int effector_add_exec(bContext *C, wmOperator *op)
512 float loc[3], rot[3];
516 WM_operator_view3d_unit_defaults(C, op);
517 if (!ED_object_add_generic_get_opts(C, op, 'Z', loc, rot, &enter_editmode, &layer, NULL))
518 return OPERATOR_CANCELLED;
520 type = RNA_enum_get(op->ptr, "type");
521 dia = RNA_float_get(op->ptr, "radius");
523 if (type == PFIELD_GUIDE) {
525 const char *name = CTX_DATA_(BLT_I18NCONTEXT_ID_OBJECT, "CurveGuide");
526 ob = ED_object_add_type(C, OB_CURVE, name, loc, rot, false, layer);
529 cu->flag |= CU_PATH | CU_3D;
530 ED_object_editmode_enter(C, 0);
531 ED_object_new_primitive_matrix(C, ob, loc, rot, mat);
532 BLI_addtail(&cu->editnurb->nurbs, ED_curve_add_nurbs_primitive(C, ob, mat, CU_NURBS | CU_PRIM_PATH, dia));
534 ED_object_editmode_exit(C, EM_FREEDATA);
537 const char *name = CTX_DATA_(BLT_I18NCONTEXT_ID_OBJECT, "Field");
538 ob = ED_object_add_type(C, OB_EMPTY, name, loc, rot, false, layer);
539 BKE_object_obdata_size_init(ob, dia);
540 if (ELEM(type, PFIELD_WIND, PFIELD_VORTEX))
541 ob->empty_drawtype = OB_SINGLE_ARROW;
544 ob->pd = object_add_collision_fields(type);
546 DEG_relations_tag_update(CTX_data_main(C));
548 return OPERATOR_FINISHED;
551 void OBJECT_OT_effector_add(wmOperatorType *ot)
554 ot->name = "Add Effector";
555 ot->description = "Add an empty object with a physics effector to the scene";
556 ot->idname = "OBJECT_OT_effector_add";
559 ot->exec = effector_add_exec;
560 ot->poll = ED_operator_objectmode;
563 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
566 ot->prop = RNA_def_enum(ot->srna, "type", field_type_items, 0, "Type", "");
568 ED_object_add_unit_props(ot);
569 ED_object_add_generic_props(ot, true);
572 /********************* Add Camera Operator ********************/
574 static int object_camera_add_exec(bContext *C, wmOperator *op)
576 View3D *v3d = CTX_wm_view3d(C);
577 Scene *scene = CTX_data_scene(C);
582 float loc[3], rot[3];
584 /* force view align for cameras */
585 RNA_boolean_set(op->ptr, "view_align", true);
587 if (!ED_object_add_generic_get_opts(C, op, 'Z', loc, rot, &enter_editmode, &layer, NULL))
588 return OPERATOR_CANCELLED;
590 ob = ED_object_add_type(C, OB_CAMERA, NULL, loc, rot, false, layer);
593 if (v3d->camera == NULL)
595 if (v3d->scenelock && scene->camera == NULL) {
601 cam->drawsize = v3d ? ED_view3d_grid_scale(scene, v3d, NULL) : ED_scene_grid_scale(scene, NULL);
603 return OPERATOR_FINISHED;
606 void OBJECT_OT_camera_add(wmOperatorType *ot)
611 ot->name = "Add Camera";
612 ot->description = "Add a camera object to the scene";
613 ot->idname = "OBJECT_OT_camera_add";
616 ot->exec = object_camera_add_exec;
617 ot->poll = ED_operator_objectmode;
620 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
622 ED_object_add_generic_props(ot, true);
624 /* hide this for cameras, default */
625 prop = RNA_struct_type_find_property(ot->srna, "view_align");
626 RNA_def_property_flag(prop, PROP_HIDDEN);
630 /********************* Add Metaball Operator ********************/
632 static int object_metaball_add_exec(bContext *C, wmOperator *op)
634 Object *obedit = CTX_data_edit_object(C);
638 float loc[3], rot[3];
642 WM_operator_view3d_unit_defaults(C, op);
643 if (!ED_object_add_generic_get_opts(C, op, 'Z', loc, rot, &enter_editmode, &layer, NULL))
644 return OPERATOR_CANCELLED;
646 if (obedit == NULL || obedit->type != OB_MBALL) {
647 obedit = ED_object_add_type(C, OB_MBALL, NULL, loc, rot, true, layer);
651 DEG_id_tag_update(&obedit->id, OB_RECALC_DATA);
654 ED_object_new_primitive_matrix(C, obedit, loc, rot, mat);
655 dia = RNA_float_get(op->ptr, "radius");
657 ED_mball_add_primitive(C, obedit, mat, dia, RNA_enum_get(op->ptr, "type"));
660 if (newob && !enter_editmode) {
661 ED_object_editmode_exit(C, EM_FREEDATA);
664 WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, obedit);
666 return OPERATOR_FINISHED;
669 void OBJECT_OT_metaball_add(wmOperatorType *ot)
672 ot->name = "Add Metaball";
673 ot->description = "Add an metaball object to the scene";
674 ot->idname = "OBJECT_OT_metaball_add";
677 ot->invoke = WM_menu_invoke;
678 ot->exec = object_metaball_add_exec;
679 ot->poll = ED_operator_scene_editable;
682 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
684 ot->prop = RNA_def_enum(ot->srna, "type", rna_enum_metaelem_type_items, 0, "Primitive", "");
686 ED_object_add_unit_props(ot);
687 ED_object_add_generic_props(ot, true);
690 /********************* Add Text Operator ********************/
692 static int object_add_text_exec(bContext *C, wmOperator *op)
694 Object *obedit = CTX_data_edit_object(C);
697 float loc[3], rot[3];
699 WM_operator_view3d_unit_defaults(C, op);
700 if (!ED_object_add_generic_get_opts(C, op, 'Z', loc, rot, &enter_editmode, &layer, NULL))
701 return OPERATOR_CANCELLED;
703 if (obedit && obedit->type == OB_FONT)
704 return OPERATOR_CANCELLED;
706 obedit = ED_object_add_type(C, OB_FONT, NULL, loc, rot, enter_editmode, layer);
707 BKE_object_obdata_size_init(obedit, RNA_float_get(op->ptr, "radius"));
709 WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, obedit);
711 return OPERATOR_FINISHED;
714 void OBJECT_OT_text_add(wmOperatorType *ot)
717 ot->name = "Add Text";
718 ot->description = "Add a text object to the scene";
719 ot->idname = "OBJECT_OT_text_add";
722 ot->exec = object_add_text_exec;
723 ot->poll = ED_operator_objectmode;
726 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
729 ED_object_add_unit_props(ot);
730 ED_object_add_generic_props(ot, true);
733 /********************* Add Armature Operator ********************/
735 static int object_armature_add_exec(bContext *C, wmOperator *op)
737 Object *obedit = CTX_data_edit_object(C);
738 RegionView3D *rv3d = CTX_wm_region_view3d(C);
742 float loc[3], rot[3], dia;
743 bool view_aligned = rv3d && (U.flag & USER_ADD_VIEWALIGNED);
745 WM_operator_view3d_unit_defaults(C, op);
746 if (!ED_object_add_generic_get_opts(C, op, 'Z', loc, rot, &enter_editmode, &layer, NULL))
747 return OPERATOR_CANCELLED;
749 if ((obedit == NULL) || (obedit->type != OB_ARMATURE)) {
750 obedit = ED_object_add_type(C, OB_ARMATURE, NULL, loc, rot, true, layer);
751 ED_object_editmode_enter(C, 0);
755 DEG_id_tag_update(&obedit->id, OB_RECALC_DATA);
758 if (obedit == NULL) {
759 BKE_report(op->reports, RPT_ERROR, "Cannot create editmode armature");
760 return OPERATOR_CANCELLED;
763 dia = RNA_float_get(op->ptr, "radius");
764 ED_armature_edit_bone_add_primitive(obedit, dia, view_aligned);
767 if (newob && !enter_editmode)
768 ED_object_editmode_exit(C, EM_FREEDATA);
770 WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, obedit);
772 return OPERATOR_FINISHED;
775 void OBJECT_OT_armature_add(wmOperatorType *ot)
778 ot->name = "Add Armature";
779 ot->description = "Add an armature object to the scene";
780 ot->idname = "OBJECT_OT_armature_add";
783 ot->exec = object_armature_add_exec;
784 ot->poll = ED_operator_objectmode;
787 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
790 ED_object_add_unit_props(ot);
791 ED_object_add_generic_props(ot, true);
794 /********************* Add Empty Operator ********************/
796 static int object_empty_add_exec(bContext *C, wmOperator *op)
799 int type = RNA_enum_get(op->ptr, "type");
801 float loc[3], rot[3];
803 WM_operator_view3d_unit_defaults(C, op);
804 if (!ED_object_add_generic_get_opts(C, op, 'Z', loc, rot, NULL, &layer, NULL))
805 return OPERATOR_CANCELLED;
807 ob = ED_object_add_type(C, OB_EMPTY, NULL, loc, rot, false, layer);
809 BKE_object_empty_draw_type_set(ob, type);
810 BKE_object_obdata_size_init(ob, RNA_float_get(op->ptr, "radius"));
812 return OPERATOR_FINISHED;
815 void OBJECT_OT_empty_add(wmOperatorType *ot)
818 ot->name = "Add Empty";
819 ot->description = "Add an empty object to the scene";
820 ot->idname = "OBJECT_OT_empty_add";
823 ot->invoke = WM_menu_invoke;
824 ot->exec = object_empty_add_exec;
825 ot->poll = ED_operator_objectmode;
828 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
831 ot->prop = RNA_def_enum(ot->srna, "type", rna_enum_object_empty_drawtype_items, 0, "Type", "");
833 ED_object_add_unit_props(ot);
834 ED_object_add_generic_props(ot, false);
837 static int empty_drop_named_image_invoke(bContext *C, wmOperator *op, const wmEvent *event)
839 Scene *scene = CTX_data_scene(C);
841 BaseLegacy *base = NULL;
845 ima = (Image *)WM_operator_drop_load_path(C, op, ID_IM);
847 return OPERATOR_CANCELLED;
850 id_us_min((ID *)ima);
852 base = ED_view3d_give_base_under_cursor(C, event->mval);
854 /* if empty under cursor, then set object */
855 if (base && base->object->type == OB_EMPTY) {
857 WM_event_add_notifier(C, NC_SCENE | ND_OB_ACTIVE, scene);
864 if (!ED_object_add_generic_get_opts(C, op, 'Z', NULL, rot, NULL, &layer, NULL))
865 return OPERATOR_CANCELLED;
867 ob = ED_object_add_type(C, OB_EMPTY, NULL, NULL, rot, false, layer);
869 /* add under the mouse */
870 ED_object_location_from_view(C, ob->loc);
871 ED_view3d_cursor3d_position(C, ob->loc, event->mval);
874 BKE_object_empty_draw_type_set(ob, OB_EMPTY_IMAGE);
878 id_us_plus(ob->data);
880 return OPERATOR_FINISHED;
883 void OBJECT_OT_drop_named_image(wmOperatorType *ot)
888 ot->name = "Add Empty Image/Drop Image To Empty";
889 ot->description = "Add an empty image type to scene with data";
890 ot->idname = "OBJECT_OT_drop_named_image";
893 ot->invoke = empty_drop_named_image_invoke;
894 ot->poll = ED_operator_objectmode;
897 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
900 prop = RNA_def_string(ot->srna, "filepath", NULL, FILE_MAX, "Filepath", "Path to image file");
901 RNA_def_property_flag(prop, PROP_HIDDEN | PROP_SKIP_SAVE);
902 RNA_def_boolean(ot->srna, "relative_path", true, "Relative Path", "Select the file relative to the blend file");
903 RNA_def_property_flag(prop, PROP_HIDDEN | PROP_SKIP_SAVE);
904 prop = RNA_def_string(ot->srna, "name", NULL, MAX_ID_NAME - 2, "Name", "Image name to assign");
905 RNA_def_property_flag(prop, PROP_HIDDEN | PROP_SKIP_SAVE);
906 ED_object_add_generic_props(ot, false);
909 /********************* Add Lamp Operator ********************/
911 static const char *get_lamp_defname(int type)
914 case LA_LOCAL: return CTX_DATA_(BLT_I18NCONTEXT_ID_LAMP, "Point");
915 case LA_SUN: return CTX_DATA_(BLT_I18NCONTEXT_ID_LAMP, "Sun");
916 case LA_SPOT: return CTX_DATA_(BLT_I18NCONTEXT_ID_LAMP, "Spot");
917 case LA_HEMI: return CTX_DATA_(BLT_I18NCONTEXT_ID_LAMP, "Hemi");
918 case LA_AREA: return CTX_DATA_(BLT_I18NCONTEXT_ID_LAMP, "Area");
920 return CTX_DATA_(BLT_I18NCONTEXT_ID_LAMP, "Lamp");
924 static int object_lamp_add_exec(bContext *C, wmOperator *op)
926 Scene *scene = CTX_data_scene(C);
929 int type = RNA_enum_get(op->ptr, "type");
931 float loc[3], rot[3];
933 WM_operator_view3d_unit_defaults(C, op);
934 if (!ED_object_add_generic_get_opts(C, op, 'Z', loc, rot, NULL, &layer, NULL))
935 return OPERATOR_CANCELLED;
937 ob = ED_object_add_type(C, OB_LAMP, get_lamp_defname(type), loc, rot, false, layer);
938 BKE_object_obdata_size_init(ob, RNA_float_get(op->ptr, "radius"));
940 la = (Lamp *)ob->data;
943 if (BKE_scene_use_new_shading_nodes(scene)) {
944 ED_node_shader_default(C, &la->id);
945 la->use_nodes = true;
948 return OPERATOR_FINISHED;
951 void OBJECT_OT_lamp_add(wmOperatorType *ot)
954 ot->name = "Add Lamp";
955 ot->description = "Add a lamp object to the scene";
956 ot->idname = "OBJECT_OT_lamp_add";
959 ot->invoke = WM_menu_invoke;
960 ot->exec = object_lamp_add_exec;
961 ot->poll = ED_operator_objectmode;
964 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
967 ot->prop = RNA_def_enum(ot->srna, "type", rna_enum_lamp_type_items, 0, "Type", "");
968 RNA_def_property_translation_context(ot->prop, BLT_I18NCONTEXT_ID_LAMP);
970 ED_object_add_unit_props(ot);
971 ED_object_add_generic_props(ot, false);
974 /********************* Add Group Instance Operator ********************/
976 static int group_instance_add_exec(bContext *C, wmOperator *op)
980 float loc[3], rot[3];
982 if (RNA_struct_property_is_set(op->ptr, "name")) {
983 char name[MAX_ID_NAME - 2];
985 RNA_string_get(op->ptr, "name", name);
986 group = (Group *)BKE_libblock_find_name(ID_GR, name);
988 if (0 == RNA_struct_property_is_set(op->ptr, "location")) {
989 wmEvent *event = CTX_wm_window(C)->eventstate;
990 ARegion *ar = CTX_wm_region(C);
991 const int mval[2] = {event->x - ar->winrct.xmin,
992 event->y - ar->winrct.ymin};
993 ED_object_location_from_view(C, loc);
994 ED_view3d_cursor3d_position(C, loc, mval);
995 RNA_float_set_array(op->ptr, "location", loc);
999 group = BLI_findlink(&CTX_data_main(C)->group, RNA_enum_get(op->ptr, "group"));
1001 if (!ED_object_add_generic_get_opts(C, op, 'Z', loc, rot, NULL, &layer, NULL))
1002 return OPERATOR_CANCELLED;
1005 Main *bmain = CTX_data_main(C);
1006 Scene *scene = CTX_data_scene(C);
1007 Object *ob = ED_object_add_type(C, OB_EMPTY, group->id.name + 2, loc, rot, false, layer);
1008 ob->dup_group = group;
1009 ob->transflag |= OB_DUPLIGROUP;
1010 id_us_plus(&group->id);
1012 /* works without this except if you try render right after, see: 22027 */
1013 DEG_relations_tag_update(bmain);
1015 WM_event_add_notifier(C, NC_SCENE | ND_OB_ACTIVE, scene);
1017 return OPERATOR_FINISHED;
1020 return OPERATOR_CANCELLED;
1023 /* only used as menu */
1024 void OBJECT_OT_group_instance_add(wmOperatorType *ot)
1029 ot->name = "Add Group Instance";
1030 ot->description = "Add a dupligroup instance";
1031 ot->idname = "OBJECT_OT_group_instance_add";
1034 ot->invoke = WM_enum_search_invoke;
1035 ot->exec = group_instance_add_exec;
1036 ot->poll = ED_operator_objectmode;
1039 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1042 RNA_def_string(ot->srna, "name", "Group", MAX_ID_NAME - 2, "Name", "Group name to add");
1043 prop = RNA_def_enum(ot->srna, "group", DummyRNA_NULL_items, 0, "Group", "");
1044 RNA_def_enum_funcs(prop, RNA_group_itemf);
1045 RNA_def_property_flag(prop, PROP_ENUM_NO_TRANSLATE);
1047 ED_object_add_generic_props(ot, false);
1050 /********************* Add Speaker Operator ********************/
1052 static int object_speaker_add_exec(bContext *C, wmOperator *op)
1056 float loc[3], rot[3];
1057 Scene *scene = CTX_data_scene(C);
1059 if (!ED_object_add_generic_get_opts(C, op, 'Z', loc, rot, NULL, &layer, NULL))
1060 return OPERATOR_CANCELLED;
1062 ob = ED_object_add_type(C, OB_SPEAKER, NULL, loc, rot, false, layer);
1064 /* to make it easier to start using this immediately in NLA, a default sound clip is created
1065 * ready to be moved around to retime the sound and/or make new sound clips
1068 /* create new data for NLA hierarchy */
1069 AnimData *adt = BKE_animdata_add_id(&ob->id);
1070 NlaTrack *nlt = add_nlatrack(adt, NULL);
1071 NlaStrip *strip = add_nla_soundstrip(scene, ob->data);
1072 strip->start = CFRA;
1073 strip->end += strip->start;
1076 BKE_nlatrack_add_strip(nlt, strip);
1078 /* auto-name the strip, and give the track an interesting name */
1079 BLI_strncpy(nlt->name, DATA_("SoundTrack"), sizeof(nlt->name));
1080 BKE_nlastrip_validate_name(adt, strip);
1082 WM_event_add_notifier(C, NC_ANIMATION | ND_NLA | NA_EDITED, NULL);
1085 return OPERATOR_FINISHED;
1088 void OBJECT_OT_speaker_add(wmOperatorType *ot)
1091 ot->name = "Add Speaker";
1092 ot->description = "Add a speaker object to the scene";
1093 ot->idname = "OBJECT_OT_speaker_add";
1096 ot->exec = object_speaker_add_exec;
1097 ot->poll = ED_operator_objectmode;
1100 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1102 ED_object_add_generic_props(ot, true);
1105 /**************************** Delete Object *************************/
1107 static void object_delete_check_glsl_update(Object *ob)
1109 /* some objects could affect on GLSL shading, make sure GLSL settings
1110 * are being tagged to be updated when object is removing from scene
1112 if (ob->type == OB_LAMP) {
1113 if (ob->gpulamp.first)
1118 /* remove base from a specific scene */
1119 /* note: now unlinks constraints as well */
1120 void ED_base_object_free_and_unlink(Main *bmain, Scene *scene, Object *ob)
1122 if (BKE_library_ID_is_indirectly_used(bmain, ob) &&
1123 ID_REAL_USERS(ob) <= 1 && ID_EXTRA_USERS(ob) == 0)
1125 /* We cannot delete indirectly used object... */
1126 printf("WARNING, undeletable object '%s', should have been catched before reaching this function!",
1131 object_delete_check_glsl_update(ob);
1132 BKE_collections_object_remove(bmain, scene, ob, true);
1133 DEG_id_type_tag(bmain, ID_OB);
1136 static int object_delete_exec(bContext *C, wmOperator *op)
1138 Main *bmain = CTX_data_main(C);
1139 Scene *scene = CTX_data_scene(C);
1140 wmWindowManager *wm = CTX_wm_manager(C);
1142 const bool use_global = RNA_boolean_get(op->ptr, "use_global");
1143 bool changed = false;
1145 if (CTX_data_edit_object(C))
1146 return OPERATOR_CANCELLED;
1148 CTX_DATA_BEGIN (C, Object *, ob, selected_objects)
1150 const bool is_indirectly_used = BKE_library_ID_is_indirectly_used(bmain, ob);
1151 if (ob->id.tag & LIB_TAG_INDIRECT) {
1152 /* Can this case ever happen? */
1153 BKE_reportf(op->reports, RPT_WARNING, "Cannot delete indirectly linked object '%s'", ob->id.name + 2);
1156 else if (is_indirectly_used && ID_REAL_USERS(ob) <= 1 && ID_EXTRA_USERS(ob) == 0) {
1157 BKE_reportf(op->reports, RPT_WARNING,
1158 "Cannot delete object '%s' from scene '%s', indirectly used objects need at least one user",
1159 ob->id.name + 2, scene->id.name + 2);
1163 /* This is sort of a quick hack to address T51243 - Proper thing to do here would be to nuke most of all this
1164 * custom scene/object/base handling, and use generic lib remap/query for that.
1165 * But this is for later (aka 2.8, once layers & co are settled and working).
1167 if (use_global && ob->id.lib == NULL) {
1168 /* We want to nuke the object, let's nuke it the easy way (not for linked data though)... */
1169 BKE_libblock_delete(bmain, &ob->id);
1174 /* remove from Grease Pencil parent */
1175 /* XXX This is likely not correct? Will also remove parent from grease pencil from other scenes,
1176 * even when use_global is false... */
1177 for (bGPdata *gpd = bmain->gpencil.first; gpd; gpd = gpd->id.next) {
1178 for (bGPDlayer *gpl = gpd->layers.first; gpl; gpl = gpl->next) {
1179 if (gpl->parent != NULL) {
1180 if (gpl->parent == ob) {
1187 /* remove from current scene only */
1188 ED_base_object_free_and_unlink(bmain, scene, ob);
1193 for (scene_iter = bmain->scene.first; scene_iter; scene_iter = scene_iter->id.next) {
1194 if (scene_iter != scene && !ID_IS_LINKED_DATABLOCK(scene_iter)) {
1195 if (is_indirectly_used && ID_REAL_USERS(ob) <= 1 && ID_EXTRA_USERS(ob) == 0) {
1196 BKE_reportf(op->reports, RPT_WARNING,
1197 "Cannot delete object '%s' from scene '%s', indirectly used objects need at least one user",
1198 ob->id.name + 2, scene_iter->id.name + 2);
1201 ED_base_object_free_and_unlink(bmain, scene_iter, ob);
1210 return OPERATOR_CANCELLED;
1212 /* delete has to handle all open scenes */
1213 BKE_main_id_tag_listbase(&bmain->scene, LIB_TAG_DOIT, true);
1214 for (win = wm->windows.first; win; win = win->next) {
1215 scene = WM_window_get_active_scene(win);
1217 if (scene->id.tag & LIB_TAG_DOIT) {
1218 scene->id.tag &= ~LIB_TAG_DOIT;
1220 DEG_relations_tag_update(bmain);
1222 WM_event_add_notifier(C, NC_SCENE | ND_OB_ACTIVE, scene);
1223 WM_event_add_notifier(C, NC_SCENE | ND_LAYER_CONTENT, scene);
1227 return OPERATOR_FINISHED;
1230 void OBJECT_OT_delete(wmOperatorType *ot)
1233 ot->name = "Delete";
1234 ot->description = "Delete selected objects";
1235 ot->idname = "OBJECT_OT_delete";
1238 ot->invoke = WM_operator_confirm;
1239 ot->exec = object_delete_exec;
1240 ot->poll = ED_operator_objectmode;
1243 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1245 RNA_def_boolean(ot->srna, "use_global", 0, "Delete Globally", "Remove object from all scenes");
1248 /**************************** Copy Utilities ******************************/
1250 /* after copying objects, copied data should get new pointers */
1251 static void copy_object_set_idnew(bContext *C)
1253 Main *bmain = CTX_data_main(C);
1255 CTX_DATA_BEGIN (C, Object *, ob, selected_editable_objects)
1257 BKE_libblock_relink_to_newid(&ob->id);
1261 set_sca_new_poins();
1263 BKE_main_id_clear_newpoins(bmain);
1266 /********************* Make Duplicates Real ************************/
1269 * \note regarding hashing dupli-objects when using OB_DUPLIGROUP, skip the first member of #DupliObject.persistent_id
1270 * since its a unique index and we only want to know if the group objects are from the same dupli-group instance.
1272 static unsigned int dupliobject_group_hash(const void *ptr)
1274 const DupliObject *dob = ptr;
1275 unsigned int hash = BLI_ghashutil_ptrhash(dob->ob);
1277 for (i = 1; (i < MAX_DUPLI_RECUR) && dob->persistent_id[i] != INT_MAX; i++) {
1278 hash ^= (dob->persistent_id[i] ^ i);
1284 * \note regarding hashing dupli-objects when NOT using OB_DUPLIGROUP, include the first member of #DupliObject.persistent_id
1285 * since its the index of the vertex/face the object is instantiated on and we want to identify objects on the same vertex/face.
1287 static unsigned int dupliobject_hash(const void *ptr)
1289 const DupliObject *dob = ptr;
1290 unsigned int hash = BLI_ghashutil_ptrhash(dob->ob);
1291 hash ^= (dob->persistent_id[0] ^ 0);
1295 /* Compare function that matches dupliobject_group_hash */
1296 static bool dupliobject_group_cmp(const void *a_, const void *b_)
1298 const DupliObject *a = a_;
1299 const DupliObject *b = b_;
1302 if (a->ob != b->ob) {
1306 for (i = 1; (i < MAX_DUPLI_RECUR); i++) {
1307 if (a->persistent_id[i] != b->persistent_id[i]) {
1310 else if (a->persistent_id[i] == INT_MAX) {
1319 /* Compare function that matches dupliobject_hash */
1320 static bool dupliobject_cmp(const void *a_, const void *b_)
1322 const DupliObject *a = a_;
1323 const DupliObject *b = b_;
1325 if (a->ob != b->ob) {
1329 if (a->persistent_id[0] != b->persistent_id[0]) {
1337 static void make_object_duplilist_real(bContext *C, Scene *scene, Base *base,
1338 const bool use_base_parent,
1339 const bool use_hierarchy)
1341 Main *bmain = CTX_data_main(C);
1342 SceneLayer *sl = CTX_data_scene_layer(C);
1345 GHash *dupli_gh = NULL, *parent_gh = NULL;
1348 if (!(base->object->transflag & OB_DUPLI))
1351 lb = object_duplilist(bmain->eval_ctx, scene, base->object);
1353 if (use_hierarchy || use_base_parent) {
1354 dupli_gh = BLI_ghash_ptr_new(__func__);
1355 if (use_hierarchy) {
1356 if (base->object->transflag & OB_DUPLIGROUP) {
1357 parent_gh = BLI_ghash_new(dupliobject_group_hash, dupliobject_group_cmp, __func__);
1360 parent_gh = BLI_ghash_new(dupliobject_hash, dupliobject_cmp, __func__);
1365 for (dob = lb->first; dob; dob = dob->next) {
1367 Object *ob = ID_NEW_SET(dob->ob, BKE_object_copy(bmain, dob->ob));
1369 /* font duplis can have a totcol without material, we get them from parent
1370 * should be implemented better...
1372 if (ob->mat == NULL) ob->totcol = 0;
1374 BKE_collection_object_add_from(scene, dob->ob, ob);
1375 basen = BKE_scene_layer_base_find(sl, ob);
1377 BKE_scene_object_base_flag_sync_from_base(basen);
1379 /* make sure apply works */
1380 BKE_animdata_free(&ob->id, true);
1383 /* Proxies are not to be copied. */
1384 ob->proxy_from = NULL;
1385 ob->proxy_group = NULL;
1389 BKE_constraints_free(&ob->constraints);
1390 ob->curve_cache = NULL;
1391 ob->transflag &= ~OB_DUPLI;
1393 copy_m4_m4(ob->obmat, dob->mat);
1394 BKE_object_apply_mat4(ob, ob->obmat, false, false);
1397 BLI_ghash_insert(dupli_gh, dob, ob);
1401 /* Due to nature of hash/comparison of this ghash, a lot of duplis may be considered as 'the same',
1402 * this avoids trying to insert same key several time and raise asserts in debug builds... */
1403 if (!BLI_ghash_ensure_p(parent_gh, dob, &val)) {
1408 /* Remap new object to itself, and clear again newid pointer of orig object. */
1409 BKE_libblock_relink_to_newid(&ob->id);
1410 set_sca_new_poins_ob(ob);
1411 BKE_id_clear_newpoin(&dob->ob->id);
1413 DEG_id_tag_update(&ob->id, OB_RECALC_DATA);
1416 if (use_hierarchy) {
1417 for (dob = lb->first; dob; dob = dob->next) {
1418 /* original parents */
1419 Object *ob_src = dob->ob;
1420 Object *ob_src_par = ob_src->parent;
1422 Object *ob_dst = BLI_ghash_lookup(dupli_gh, dob);
1423 Object *ob_dst_par = NULL;
1425 /* find parent that was also made real */
1427 /* OK to keep most of the members uninitialized,
1428 * they won't be read, this is simply for a hash lookup. */
1429 DupliObject dob_key;
1430 dob_key.ob = ob_src_par;
1431 if (base->object->transflag & OB_DUPLIGROUP) {
1432 memcpy(&dob_key.persistent_id[1],
1433 &dob->persistent_id[1],
1434 sizeof(dob->persistent_id[1]) * (MAX_DUPLI_RECUR - 1));
1437 dob_key.persistent_id[0] = dob->persistent_id[0];
1439 ob_dst_par = BLI_ghash_lookup(parent_gh, &dob_key);
1443 /* allow for all possible parent types */
1444 ob_dst->partype = ob_src->partype;
1445 BLI_strncpy(ob_dst->parsubstr, ob_src->parsubstr, sizeof(ob_dst->parsubstr));
1446 ob_dst->par1 = ob_src->par1;
1447 ob_dst->par2 = ob_src->par2;
1448 ob_dst->par3 = ob_src->par3;
1450 copy_m4_m4(ob_dst->parentinv, ob_src->parentinv);
1452 ob_dst->parent = ob_dst_par;
1454 else if (use_base_parent) {
1455 ob_dst->parent = base->object;
1456 ob_dst->partype = PAROBJECT;
1459 if (ob_dst->parent) {
1460 invert_m4_m4(ob_dst->parentinv, dob->mat);
1462 /* note, this may be the parent of other objects, but it should
1463 * still work out ok */
1464 BKE_object_apply_mat4(ob_dst, dob->mat, false, true);
1466 /* to set ob_dst->orig and in case theres any other discrepicies */
1467 DEG_id_tag_update(&ob_dst->id, OB_RECALC_OB);
1471 else if (use_base_parent) {
1472 /* since we are ignoring the internal hierarchy - parent all to the
1474 for (dob = lb->first; dob; dob = dob->next) {
1475 /* original parents */
1476 Object *ob_dst = BLI_ghash_lookup(dupli_gh, dob);
1478 ob_dst->parent = base->object;
1479 ob_dst->partype = PAROBJECT;
1481 /* similer to the code above, see comments */
1482 invert_m4_m4(ob_dst->parentinv, dob->mat);
1483 BKE_object_apply_mat4(ob_dst, dob->mat, false, true);
1484 DEG_id_tag_update(&ob_dst->id, OB_RECALC_OB);
1488 if (base->object->transflag & OB_DUPLIGROUP && base->object->dup_group) {
1489 for (object = bmain->object.first; object; object = object->id.next) {
1490 if (object->proxy_group == base->object) {
1491 object->proxy = NULL;
1492 object->proxy_from = NULL;
1493 DEG_id_tag_update(&object->id, OB_RECALC_OB);
1499 BLI_ghash_free(dupli_gh, NULL, NULL);
1501 BLI_ghash_free(parent_gh, NULL, NULL);
1503 free_object_duplilist(lb);
1505 base->object->transflag &= ~OB_DUPLI;
1508 static int object_duplicates_make_real_exec(bContext *C, wmOperator *op)
1510 Main *bmain = CTX_data_main(C);
1511 Scene *scene = CTX_data_scene(C);
1513 const bool use_base_parent = RNA_boolean_get(op->ptr, "use_base_parent");
1514 const bool use_hierarchy = RNA_boolean_get(op->ptr, "use_hierarchy");
1516 BKE_main_id_clear_newpoins(bmain);
1518 CTX_DATA_BEGIN (C, Base *, base, selected_editable_bases)
1520 make_object_duplilist_real(C, scene, base, use_base_parent, use_hierarchy);
1522 /* dependencies were changed */
1523 WM_event_add_notifier(C, NC_OBJECT | ND_PARENT, base->object);
1527 DEG_relations_tag_update(bmain);
1528 WM_event_add_notifier(C, NC_SCENE, scene);
1529 WM_main_add_notifier(NC_OBJECT | ND_DRAW, NULL);
1531 return OPERATOR_FINISHED;
1534 void OBJECT_OT_duplicates_make_real(wmOperatorType *ot)
1537 ot->name = "Make Duplicates Real";
1538 ot->description = "Make dupli objects attached to this object real";
1539 ot->idname = "OBJECT_OT_duplicates_make_real";
1542 ot->exec = object_duplicates_make_real_exec;
1544 ot->poll = ED_operator_objectmode;
1547 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1549 RNA_def_boolean(ot->srna, "use_base_parent", 0, "Parent", "Parent newly created objects to the original duplicator");
1550 RNA_def_boolean(ot->srna, "use_hierarchy", 0, "Keep Hierarchy", "Maintain parent child relationships");
1553 /**************************** Convert **************************/
1555 static EnumPropertyItem convert_target_items[] = {
1556 {OB_CURVE, "CURVE", ICON_OUTLINER_OB_CURVE, "Curve from Mesh/Text", ""},
1557 {OB_MESH, "MESH", ICON_OUTLINER_OB_MESH, "Mesh from Curve/Meta/Surf/Text", ""},
1558 {0, NULL, 0, NULL, NULL}
1561 static void convert_ensure_curve_cache(Main *bmain, Scene *scene, Object *ob)
1563 if (ob->curve_cache == NULL) {
1564 /* Force creation. This is normally not needed but on operator
1565 * redo we might end up with an object which isn't evaluated yet.
1567 if (ELEM(ob->type, OB_SURF, OB_CURVE, OB_FONT)) {
1568 BKE_displist_make_curveTypes(scene, ob, false);
1570 else if (ob->type == OB_MBALL) {
1571 BKE_displist_make_mball(bmain->eval_ctx, scene, ob);
1576 static void curvetomesh(Main *bmain, Scene *scene, Object *ob)
1578 convert_ensure_curve_cache(bmain, scene, ob);
1579 BKE_mesh_from_nurbs(ob); /* also does users */
1581 if (ob->type == OB_MESH) {
1582 BKE_object_free_modifiers(ob);
1584 /* Game engine defaults for mesh objects */
1585 ob->body_type = OB_BODY_TYPE_STATIC;
1586 ob->gameflag = OB_PROP | OB_COLLISION;
1590 static int convert_poll(bContext *C)
1592 Object *obact = CTX_data_active_object(C);
1593 Scene *scene = CTX_data_scene(C);
1595 return (!ID_IS_LINKED_DATABLOCK(scene) && obact && scene->obedit != obact &&
1596 (obact->flag & SELECT) && !ID_IS_LINKED_DATABLOCK(obact));
1599 /* Helper for convert_exec */
1600 static Base *duplibase_for_convert(Main *bmain, Scene *scene, SceneLayer *sl, Base *base, Object *ob)
1609 obn = BKE_object_copy(bmain, ob);
1610 DEG_id_tag_update(&ob->id, OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME);
1611 BKE_collection_object_add_from(scene, ob, obn);
1613 basen = BKE_scene_layer_base_find(sl, obn);
1614 ED_object_base_select(basen, BA_SELECT);
1615 ED_object_base_select(basen, BA_DESELECT);
1619 static int convert_exec(bContext *C, wmOperator *op)
1621 Main *bmain = CTX_data_main(C);
1622 Scene *scene = CTX_data_scene(C);
1623 SceneLayer *sl = CTX_data_scene_layer(C);
1624 Base *basen = NULL, *basact = NULL;
1625 Object *ob, *ob1, *newob, *obact = CTX_data_active_object(C);
1631 const short target = RNA_enum_get(op->ptr, "target");
1632 const bool keep_original = RNA_boolean_get(op->ptr, "keep_original");
1633 int a, mballConverted = 0;
1635 /* don't forget multiple users! */
1640 for (base = scene->base.first; base; base = base->next) {
1642 ob->flag &= ~OB_DONE;
1644 /* flag data thats not been edited (only needed for !keep_original) */
1646 ((ID *)ob->data)->tag |= LIB_TAG_DOIT;
1649 /* possible metaball basis is not in this scene */
1650 if (ob->type == OB_MBALL && target == OB_MESH) {
1651 if (BKE_mball_is_basis(ob) == false) {
1653 ob_basis = BKE_mball_basis_find(scene, ob);
1655 ob_basis->flag &= ~OB_DONE;
1662 ListBase selected_editable_bases = CTX_data_collection_get(C, "selected_editable_bases");
1664 /* Ensure we get all meshes calculated with a sufficient data-mask,
1665 * needed since re-evaluating single modifiers causes bugs if they depend
1666 * on other objects data masks too, see: T50950. */
1668 for (CollectionPointerLink *link = selected_editable_bases.first; link; link = link->next) {
1669 Base *base = link->ptr.data;
1670 DEG_id_tag_update(&base->object->id, OB_RECALC_DATA);
1673 uint64_t customdata_mask_prev = scene->customdata_mask;
1674 scene->customdata_mask |= CD_MASK_MESH;
1675 BKE_scene_update_tagged(bmain->eval_ctx, bmain, scene);
1676 scene->customdata_mask = customdata_mask_prev;
1679 for (CollectionPointerLink *link = selected_editable_bases.first; link; link = link->next) {
1680 Base *base = link->ptr.data;
1683 if (ob->flag & OB_DONE || !IS_TAGGED(ob->data)) {
1684 if (ob->type != target) {
1685 base->flag &= ~SELECT;
1686 ob->flag &= ~SELECT;
1689 /* obdata already modified */
1690 if (!IS_TAGGED(ob->data)) {
1691 /* When 2 objects with linked data are selected, converting both
1692 * would keep modifiers on all but the converted object [#26003] */
1693 if (ob->type == OB_MESH) {
1694 BKE_object_free_modifiers(ob); /* after derivedmesh calls! */
1698 else if (ob->type == OB_MESH && target == OB_CURVE) {
1699 ob->flag |= OB_DONE;
1701 if (keep_original) {
1702 basen = duplibase_for_convert(bmain, scene, sl, base, NULL);
1703 newob = basen->object;
1705 /* decrement original mesh's usage count */
1709 /* make a new copy of the mesh */
1710 newob->data = BKE_mesh_copy(bmain, me);
1716 BKE_mesh_to_curve(scene, newob);
1718 if (newob->type == OB_CURVE) {
1719 BKE_object_free_modifiers(newob); /* after derivedmesh calls! */
1720 ED_rigidbody_object_remove(bmain, scene, newob);
1723 else if (ob->type == OB_MESH) {
1724 ob->flag |= OB_DONE;
1726 if (keep_original) {
1727 basen = duplibase_for_convert(bmain, scene, sl, base, NULL);
1728 newob = basen->object;
1730 /* decrement original mesh's usage count */
1734 /* make a new copy of the mesh */
1735 newob->data = BKE_mesh_copy(bmain, me);
1739 DEG_id_tag_update(&ob->id, OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME);
1742 /* make new mesh data from the original copy */
1743 /* note: get the mesh from the original, not from the copy in some
1744 * cases this doesnt give correct results (when MDEF is used for eg)
1746 dm = mesh_get_derived_final(scene, newob, CD_MASK_MESH);
1748 DM_to_mesh(dm, newob->data, newob, CD_MASK_MESH, true);
1750 /* re-tessellation is called by DM_to_mesh */
1752 BKE_object_free_modifiers(newob); /* after derivedmesh calls! */
1754 else if (ob->type == OB_FONT) {
1755 ob->flag |= OB_DONE;
1757 if (keep_original) {
1758 basen = duplibase_for_convert(bmain, scene, sl, base, NULL);
1759 newob = basen->object;
1761 /* decrement original curve's usage count */
1762 id_us_min(&((Curve *)newob->data)->id);
1764 /* make a new copy of the curve */
1765 newob->data = BKE_curve_copy(bmain, ob->data);
1773 /* TODO(sergey): Ideally DAG will create nurbs list for a curve data
1774 * datablock, but for until we've got granular update
1775 * lets take care by selves.
1777 BKE_vfont_to_curve(bmain, newob, FO_EDIT);
1779 newob->type = OB_CURVE;
1780 cu->type = OB_CURVE;
1783 id_us_min(&cu->vfont->id);
1787 id_us_min(&cu->vfontb->id);
1791 id_us_min(&cu->vfonti->id);
1795 id_us_min(&cu->vfontbi->id);
1799 if (!keep_original) {
1801 if (cu->id.us > 1) {
1802 for (ob1 = bmain->object.first; ob1; ob1 = ob1->id.next) {
1803 if (ob1->data == ob->data) {
1804 ob1->type = OB_CURVE;
1805 DEG_id_tag_update(&ob1->id, OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME);
1811 for (nu = cu->nurb.first; nu; nu = nu->next)
1815 BKE_curve_curve_dimension_update(cu);
1817 if (target == OB_MESH) {
1818 curvetomesh(bmain, scene, newob);
1820 /* meshes doesn't use displist */
1821 BKE_object_free_curve_cache(newob);
1824 else if (ELEM(ob->type, OB_CURVE, OB_SURF)) {
1825 ob->flag |= OB_DONE;
1827 if (target == OB_MESH) {
1828 if (keep_original) {
1829 basen = duplibase_for_convert(bmain, scene, sl, base, NULL);
1830 newob = basen->object;
1832 /* decrement original curve's usage count */
1833 id_us_min(&((Curve *)newob->data)->id);
1835 /* make a new copy of the curve */
1836 newob->data = BKE_curve_copy(bmain, ob->data);
1842 curvetomesh(bmain, scene, newob);
1844 /* meshes doesn't use displist */
1845 BKE_object_free_curve_cache(newob);
1848 else if (ob->type == OB_MBALL && target == OB_MESH) {
1851 base->flag &= ~SELECT;
1852 ob->flag &= ~SELECT;
1854 baseob = BKE_mball_basis_find(scene, ob);
1857 /* if motherball is converting it would be marked as done later */
1858 ob->flag |= OB_DONE;
1861 if (!(baseob->flag & OB_DONE)) {
1862 baseob->flag |= OB_DONE;
1864 basen = duplibase_for_convert(bmain, scene, sl, base, baseob);
1865 newob = basen->object;
1870 newob->data = BKE_mesh_add(bmain, "Mesh");
1871 newob->type = OB_MESH;
1874 me->totcol = mb->totcol;
1875 if (newob->totcol) {
1876 me->mat = MEM_dupallocN(mb->mat);
1877 for (a = 0; a < newob->totcol; a++) id_us_plus((ID *)me->mat[a]);
1880 convert_ensure_curve_cache(bmain, scene, baseob);
1881 BKE_mesh_from_metaball(&baseob->curve_cache->disp, newob->data);
1883 if (obact->type == OB_MBALL) {
1894 /* tag obdata if it was been changed */
1896 /* If the original object is active then make this object active */
1899 /* store new active base to update BASACT */
1906 if (!keep_original && (ob->flag & OB_DONE)) {
1907 DEG_id_tag_update(&ob->id, OB_RECALC_DATA);
1908 ((ID *)ob->data)->tag &= ~LIB_TAG_DOIT; /* flag not to convert this datablock again */
1911 BLI_freelistN(&selected_editable_bases);
1913 if (!keep_original) {
1914 if (mballConverted) {
1915 FOREACH_SCENE_OBJECT(scene, ob_mball)
1917 if (ob->type == OB_MBALL) {
1918 if (ob->flag & OB_DONE) {
1919 Object *ob_basis = NULL;
1920 if (BKE_mball_is_basis(ob_mball) ||
1921 ((ob_basis = BKE_mball_basis_find(scene, ob_mball)) && (ob_basis->flag & OB_DONE)))
1923 ED_base_object_free_and_unlink(bmain, scene, ob_mball);
1928 FOREACH_SCENE_OBJECT_END
1931 /* delete object should renew depsgraph */
1932 DEG_relations_tag_update(bmain);
1935 // XXX ED_object_editmode_enter(C, 0);
1936 // XXX exit_editmode(C, EM_FREEDATA|EM_WAITCURSOR); /* freedata, but no undo */
1939 /* active base was changed */
1940 ED_object_base_activate(C, basact);
1941 BASACT_NEW = basact;
1943 else if (BASACT_NEW->object->flag & OB_DONE) {
1944 WM_event_add_notifier(C, NC_OBJECT | ND_MODIFIER, BASACT_NEW->object);
1945 WM_event_add_notifier(C, NC_OBJECT | ND_DATA, BASACT_NEW->object);
1948 DEG_relations_tag_update(bmain);
1949 WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, scene);
1950 WM_event_add_notifier(C, NC_SCENE | ND_OB_SELECT, scene);
1952 return OPERATOR_FINISHED;
1956 void OBJECT_OT_convert(wmOperatorType *ot)
1959 ot->name = "Convert to";
1960 ot->description = "Convert selected objects to another type";
1961 ot->idname = "OBJECT_OT_convert";
1964 ot->invoke = WM_menu_invoke;
1965 ot->exec = convert_exec;
1966 ot->poll = convert_poll;
1969 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1972 ot->prop = RNA_def_enum(ot->srna, "target", convert_target_items, OB_MESH, "Target", "Type of object to convert to");
1973 RNA_def_boolean(ot->srna, "keep_original", 0, "Keep Original", "Keep original objects instead of replacing them");
1976 /**************************** Duplicate ************************/
1979 * dupflag: a flag made from constants declared in DNA_userdef_types.h
1980 * The flag tells adduplicate() whether to copy data linked to the object, or to reference the existing data.
1981 * U.dupflag for default operations or you can construct a flag as python does
1982 * if the dupflag is 0 then no data will be copied (linked duplicate) */
1984 /* used below, assumes id.new is correct */
1985 /* leaves selection of base/object unaltered */
1986 /* Does set ID->newid pointers. */
1987 static Base *object_add_duplicate_internal(Main *bmain, Scene *scene, SceneLayer *sl, Object *ob, int dupflag)
1989 #define ID_NEW_REMAP_US(a) if ( (a)->id.newid) { (a) = (void *)(a)->id.newid; (a)->id.us++; }
1990 #define ID_NEW_REMAP_US2(a) if (((ID *)a)->newid) { (a) = ((ID *)a)->newid; ((ID *)a)->us++; }
1993 Material ***matarar;
1998 if (ob->mode & OB_MODE_POSE) {
2002 obn = ID_NEW_SET(ob, BKE_object_copy(bmain, ob));
2003 DEG_id_tag_update(&obn->id, OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME);
2005 BKE_collection_object_add_from(scene, ob, obn);
2006 basen = BKE_scene_layer_base_find(sl, obn);
2008 /* 1) duplis should end up in same group as the original
2009 * 2) Rigid Body sim participants MUST always be part of a group...
2011 // XXX: is 2) really a good measure here?
2012 if ((ob->flag & OB_FROMGROUP) != 0 || ob->rigidbody_object || ob->rigidbody_constraint) {
2014 for (group = bmain->group.first; group; group = group->id.next) {
2015 if (BKE_group_object_exists(group, ob))
2016 BKE_group_object_add(group, obn);
2020 /* duplicates using userflags */
2021 if (dupflag & USER_DUP_ACT) {
2022 BKE_animdata_copy_id_action(&obn->id, true);
2025 if (dupflag & USER_DUP_MAT) {
2026 for (a = 0; a < obn->totcol; a++) {
2027 id = (ID *)obn->mat[a];
2029 ID_NEW_REMAP_US(obn->mat[a])
2031 obn->mat[a] = ID_NEW_SET(obn->mat[a], BKE_material_copy(bmain, obn->mat[a]));
2035 if (dupflag & USER_DUP_ACT) {
2036 BKE_animdata_copy_id_action(&obn->mat[a]->id, true);
2041 if (dupflag & USER_DUP_PSYS) {
2042 ParticleSystem *psys;
2043 for (psys = obn->particlesystem.first; psys; psys = psys->next) {
2044 id = (ID *) psys->part;
2046 ID_NEW_REMAP_US(psys->part)
2048 psys->part = ID_NEW_SET(psys->part, BKE_particlesettings_copy(bmain, psys->part));
2051 if (dupflag & USER_DUP_ACT) {
2052 BKE_animdata_copy_id_action(&psys->part->id, true);
2063 switch (obn->type) {
2065 if (dupflag & USER_DUP_MESH) {
2066 ID_NEW_REMAP_US2(obn->data)
2068 obn->data = ID_NEW_SET(obn->data, BKE_mesh_copy(bmain, obn->data));
2075 if (dupflag & USER_DUP_CURVE) {
2076 ID_NEW_REMAP_US2(obn->data)
2078 obn->data = ID_NEW_SET(obn->data, BKE_curve_copy(bmain, obn->data));
2085 if (dupflag & USER_DUP_SURF) {
2086 ID_NEW_REMAP_US2(obn->data)
2088 obn->data = ID_NEW_SET(obn->data, BKE_curve_copy(bmain, obn->data));
2095 if (dupflag & USER_DUP_FONT) {
2096 ID_NEW_REMAP_US2(obn->data)
2098 obn->data = ID_NEW_SET(obn->data, BKE_curve_copy(bmain, obn->data));
2105 if (dupflag & USER_DUP_MBALL) {
2106 ID_NEW_REMAP_US2(obn->data)
2108 obn->data = ID_NEW_SET(obn->data, BKE_mball_copy(bmain, obn->data));
2115 if (dupflag & USER_DUP_LAMP) {
2116 ID_NEW_REMAP_US2(obn->data)
2118 obn->data = ID_NEW_SET(obn->data, BKE_lamp_copy(bmain, obn->data));
2125 DEG_id_tag_update(&obn->id, OB_RECALC_DATA);
2127 BKE_pose_tag_recalc(bmain, obn->pose);
2128 if (dupflag & USER_DUP_ARM) {
2129 ID_NEW_REMAP_US2(obn->data)
2131 obn->data = ID_NEW_SET(obn->data, BKE_armature_copy(bmain, obn->data));
2132 BKE_pose_rebuild(obn, obn->data);
2140 ID_NEW_REMAP_US2(obn->data)
2142 obn->data = ID_NEW_SET(obn->data, BKE_lattice_copy(bmain, obn->data));
2150 ID_NEW_REMAP_US2(obn->data)
2152 obn->data = ID_NEW_SET(obn->data, BKE_camera_copy(bmain, obn->data));
2160 ID_NEW_REMAP_US2(obn->data)
2162 obn->data = ID_NEW_SET(obn->data, BKE_speaker_copy(bmain, obn->data));
2170 /* check if obdata is copied */
2172 Key *key = BKE_key_from_object(obn);
2174 if (dupflag & USER_DUP_ACT) {
2177 BKE_animdata_copy_id_action((ID *)obn->data, true);
2179 BKE_animdata_copy_id_action((ID *)key, true);
2182 /* Update the duplicated action in the action actuators */
2183 /* XXX TODO this code is all wrong! actact->act is user-refcounted (see readfile.c),
2184 * and what about other ID pointers of other BGE logic bricks,
2185 * and since this is object-level, why is it only ran if obdata was duplicated??? -mont29 */
2186 for (act = obn->actuators.first; act; act = act->next) {
2187 if (act->type == ACT_ACTION) {
2188 bActionActuator *actact = (bActionActuator *) act->data;
2189 if (ob->adt && actact->act == ob->adt->action) {
2190 actact->act = obn->adt->action;
2196 if (dupflag & USER_DUP_MAT) {
2197 matarar = give_matarar(obn);
2199 for (a = 0; a < obn->totcol; a++) {
2200 id = (ID *)(*matarar)[a];
2202 ID_NEW_REMAP_US((*matarar)[a])
2204 (*matarar)[a] = ID_NEW_SET((*matarar)[a], BKE_material_copy(bmain, (*matarar)[a]));
2215 #undef ID_NEW_REMAP_US
2216 #undef ID_NEW_REMAP_US2
2219 /* single object duplicate, if dupflag==0, fully linked, else it uses the flags given */
2220 /* leaves selection of base/object unaltered.
2221 * note: don't call this within a loop since clear_* funcs loop over the entire database.
2222 * note: caller must do DAG_relations_tag_update(bmain);
2223 * this is not done automatic since we may duplicate many objects in a batch */
2224 Base *ED_object_add_duplicate(Main *bmain, Scene *scene, SceneLayer *sl, Base *base, int dupflag)
2229 clear_sca_new_poins(); /* BGE logic */
2231 basen = object_add_duplicate_internal(bmain, scene, sl, base->object, dupflag);
2232 if (basen == NULL) {
2238 /* link own references to the newly duplicated data [#26816] */
2239 BKE_libblock_relink_to_newid(&ob->id);
2240 set_sca_new_poins_ob(ob);
2242 /* DAG_relations_tag_update(bmain); */ /* caller must do */
2245 ED_render_id_flush_update(bmain, ob->data);
2248 BKE_main_id_clear_newpoins(bmain);
2253 /* contextual operator dupli */
2254 static int duplicate_exec(bContext *C, wmOperator *op)
2256 Main *bmain = CTX_data_main(C);
2257 Scene *scene = CTX_data_scene(C);
2258 SceneLayer *sl = CTX_data_scene_layer(C);
2259 const bool linked = RNA_boolean_get(op->ptr, "linked");
2260 int dupflag = (linked) ? 0 : U.dupflag;
2262 clear_sca_new_poins(); /* BGE logic */
2264 CTX_DATA_BEGIN (C, Base *, base, selected_bases)
2266 Base *basen = object_add_duplicate_internal(bmain, scene, sl, base->object, dupflag);
2268 /* note that this is safe to do with this context iterator,
2269 * the list is made in advance */
2270 ED_object_base_select(base, BA_DESELECT);
2271 ED_object_base_select(basen, BA_SELECT);
2273 if (basen == NULL) {
2277 /* new object becomes active */
2278 if (BASACT_NEW == base)
2279 ED_object_base_activate(C, basen);
2281 if (basen->object->data) {
2282 DEG_id_tag_update(basen->object->data, 0);
2287 copy_object_set_idnew(C);
2289 BKE_main_id_clear_newpoins(bmain);
2291 DEG_relations_tag_update(bmain);
2292 /* TODO(sergey): Use proper flag for tagging here. */
2293 DEG_id_tag_update(&CTX_data_scene(C)->id, 0);
2295 WM_event_add_notifier(C, NC_SCENE | ND_OB_SELECT, scene);
2297 return OPERATOR_FINISHED;
2300 void OBJECT_OT_duplicate(wmOperatorType *ot)
2305 ot->name = "Duplicate Objects";
2306 ot->description = "Duplicate selected objects";
2307 ot->idname = "OBJECT_OT_duplicate";
2310 ot->exec = duplicate_exec;
2311 ot->poll = ED_operator_objectmode;
2314 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2316 /* to give to transform */
2317 RNA_def_boolean(ot->srna, "linked", 0, "Linked", "Duplicate object but not object data, linking to the original data");
2318 prop = RNA_def_enum(ot->srna, "mode", rna_enum_transform_mode_types, TFM_TRANSLATION, "Mode", "");
2319 RNA_def_property_flag(prop, PROP_HIDDEN);
2322 /* **************** add named object, for dragdrop ************* */
2324 static int add_named_exec(bContext *C, wmOperator *op)
2326 wmWindow *win = CTX_wm_window(C);
2327 const wmEvent *event = win ? win->eventstate : NULL;
2328 Main *bmain = CTX_data_main(C);
2329 Scene *scene = CTX_data_scene(C);
2330 SceneLayer *sl = CTX_data_scene_layer(C);
2333 const bool linked = RNA_boolean_get(op->ptr, "linked");
2334 int dupflag = (linked) ? 0 : U.dupflag;
2335 char name[MAX_ID_NAME - 2];
2337 /* find object, create fake base */
2338 RNA_string_get(op->ptr, "name", name);
2339 ob = (Object *)BKE_libblock_find_name(ID_OB, name);
2342 BKE_report(op->reports, RPT_ERROR, "Object not found");
2343 return OPERATOR_CANCELLED;
2347 clear_sca_new_poins(); /* BGE logic */
2349 basen = object_add_duplicate_internal(bmain, scene, sl, ob, dupflag);
2350 BKE_scene_object_base_flag_sync_from_object(basen);
2352 if (basen == NULL) {
2353 BKE_report(op->reports, RPT_ERROR, "Object could not be duplicated");
2354 return OPERATOR_CANCELLED;
2357 basen->object->restrictflag &= ~OB_RESTRICT_VIEW;
2360 ARegion *ar = CTX_wm_region(C);
2361 const int mval[2] = {event->x - ar->winrct.xmin,
2362 event->y - ar->winrct.ymin};
2363 ED_object_location_from_view(C, basen->object->loc);
2364 ED_view3d_cursor3d_position(C, basen->object->loc, mval);
2367 ED_object_base_select(basen, BA_SELECT);
2368 ED_object_base_activate(C, basen);
2370 copy_object_set_idnew(C);
2372 BKE_main_id_clear_newpoins(bmain);
2374 DEG_relations_tag_update(bmain);
2376 WM_event_add_notifier(C, NC_SCENE | ND_OB_SELECT | ND_OB_ACTIVE, scene);
2378 return OPERATOR_FINISHED;
2381 void OBJECT_OT_add_named(wmOperatorType *ot)
2384 ot->name = "Add Named Object";
2385 ot->description = "Add named object";
2386 ot->idname = "OBJECT_OT_add_named";
2389 ot->exec = add_named_exec;
2390 ot->poll = ED_operator_objectmode;
2393 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2395 RNA_def_boolean(ot->srna, "linked", 0, "Linked", "Duplicate object but not object data, linking to the original data");
2396 RNA_def_string(ot->srna, "name", NULL, MAX_ID_NAME - 2, "Name", "Object name to add");
2399 /**************************** Join *************************/
2401 static int join_poll(bContext *C)
2403 Object *ob = CTX_data_active_object(C);
2405 if (!ob || ID_IS_LINKED_DATABLOCK(ob)) return 0;
2407 if (ELEM(ob->type, OB_MESH, OB_CURVE, OB_SURF, OB_ARMATURE))
2408 return ED_operator_screenactive(C);
2413 static int join_exec(bContext *C, wmOperator *op)
2415 Scene *scene = CTX_data_scene(C);
2416 Object *ob = CTX_data_active_object(C);
2418 if (scene->obedit) {
2419 BKE_report(op->reports, RPT_ERROR, "This data does not support joining in edit mode");
2420 return OPERATOR_CANCELLED;
2422 else if (BKE_object_obdata_is_libdata(ob)) {
2423 BKE_report(op->reports, RPT_ERROR, "Cannot edit external libdata");
2424 return OPERATOR_CANCELLED;
2427 if (ob->type == OB_MESH)
2428 return join_mesh_exec(C, op);
2429 else if (ELEM(ob->type, OB_CURVE, OB_SURF))
2430 return join_curve_exec(C, op);
2431 else if (ob->type == OB_ARMATURE)
2432 return join_armature_exec(C, op);
2434 return OPERATOR_CANCELLED;
2437 void OBJECT_OT_join(wmOperatorType *ot)
2441 ot->description = "Join selected objects into active object";
2442 ot->idname = "OBJECT_OT_join";
2445 ot->exec = join_exec;
2446 ot->poll = join_poll;
2449 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2452 /**************************** Join as Shape Key*************************/
2454 static int join_shapes_poll(bContext *C)
2456 Object *ob = CTX_data_active_object(C);
2458 if (!ob || ID_IS_LINKED_DATABLOCK(ob)) return 0;
2460 /* only meshes supported at the moment */
2461 if (ob->type == OB_MESH)
2462 return ED_operator_screenactive(C);
2467 static int join_shapes_exec(bContext *C, wmOperator *op)
2469 Scene *scene = CTX_data_scene(C);
2470 Object *ob = CTX_data_active_object(C);
2472 if (scene->obedit) {
2473 BKE_report(op->reports, RPT_ERROR, "This data does not support joining in edit mode");
2474 return OPERATOR_CANCELLED;
2476 else if (BKE_object_obdata_is_libdata(ob)) {
2477 BKE_report(op->reports, RPT_ERROR, "Cannot edit external libdata");
2478 return OPERATOR_CANCELLED;
2481 if (ob->type == OB_MESH)
2482 return join_mesh_shapes_exec(C, op);
2484 return OPERATOR_CANCELLED;
2487 void OBJECT_OT_join_shapes(wmOperatorType *ot)
2490 ot->name = "Join as Shapes";
2491 ot->description = "Merge selected objects to shapes of active object";
2492 ot->idname = "OBJECT_OT_join_shapes";
2495 ot->exec = join_shapes_exec;
2496 ot->poll = join_shapes_poll;
2499 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;