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_depsgraph.h"
71 #include "BKE_DerivedMesh.h"
72 #include "BKE_displist.h"
73 #include "BKE_effect.h"
75 #include "BKE_group.h"
77 #include "BKE_lattice.h"
78 #include "BKE_layer.h"
79 #include "BKE_library.h"
80 #include "BKE_library_query.h"
81 #include "BKE_library_remap.h"
84 #include "BKE_material.h"
85 #include "BKE_mball.h"
88 #include "BKE_object.h"
89 #include "BKE_particle.h"
90 #include "BKE_report.h"
92 #include "BKE_scene.h"
93 #include "BKE_screen.h"
94 #include "BKE_speaker.h"
95 #include "BKE_texture.h"
97 #include "RNA_access.h"
98 #include "RNA_define.h"
99 #include "RNA_enum_types.h"
102 #include "WM_types.h"
104 #include "ED_armature.h"
105 #include "ED_curve.h"
106 #include "ED_mball.h"
109 #include "ED_object.h"
110 #include "ED_physics.h"
111 #include "ED_render.h"
112 #include "ED_screen.h"
113 #include "ED_transform.h"
114 #include "ED_view3d.h"
116 #include "UI_resources.h"
118 #include "GPU_lamp.h"
120 #include "object_intern.h"
122 /* this is an exact copy of the define in rna_lamp.c
123 * kept here because of linking order.
124 * Icons are only defined here */
125 EnumPropertyItem rna_enum_lamp_type_items[] = {
126 {LA_LOCAL, "POINT", ICON_LAMP_POINT, "Point", "Omnidirectional point light source"},
127 {LA_SUN, "SUN", ICON_LAMP_SUN, "Sun", "Constant direction parallel ray light source"},
128 {LA_SPOT, "SPOT", ICON_LAMP_SPOT, "Spot", "Directional cone light source"},
129 {LA_HEMI, "HEMI", ICON_LAMP_HEMI, "Hemi", "180 degree constant light source"},
130 {LA_AREA, "AREA", ICON_LAMP_AREA, "Area", "Directional area light source"},
131 {0, NULL, 0, NULL, NULL}
134 /* copy from rna_object_force.c */
135 static EnumPropertyItem field_type_items[] = {
136 {PFIELD_FORCE, "FORCE", ICON_FORCE_FORCE, "Force", ""},
137 {PFIELD_WIND, "WIND", ICON_FORCE_WIND, "Wind", ""},
138 {PFIELD_VORTEX, "VORTEX", ICON_FORCE_VORTEX, "Vortex", ""},
139 {PFIELD_MAGNET, "MAGNET", ICON_FORCE_MAGNETIC, "Magnetic", ""},
140 {PFIELD_HARMONIC, "HARMONIC", ICON_FORCE_HARMONIC, "Harmonic", ""},
141 {PFIELD_CHARGE, "CHARGE", ICON_FORCE_CHARGE, "Charge", ""},
142 {PFIELD_LENNARDJ, "LENNARDJ", ICON_FORCE_LENNARDJONES, "Lennard-Jones", ""},
143 {PFIELD_TEXTURE, "TEXTURE", ICON_FORCE_TEXTURE, "Texture", ""},
144 {PFIELD_GUIDE, "GUIDE", ICON_FORCE_CURVE, "Curve Guide", ""},
145 {PFIELD_BOID, "BOID", ICON_FORCE_BOID, "Boid", ""},
146 {PFIELD_TURBULENCE, "TURBULENCE", ICON_FORCE_TURBULENCE, "Turbulence", ""},
147 {PFIELD_DRAG, "DRAG", ICON_FORCE_DRAG, "Drag", ""},
148 {PFIELD_SMOKEFLOW, "SMOKE", ICON_FORCE_SMOKEFLOW, "Smoke Flow", ""},
149 {0, NULL, 0, NULL, NULL}
152 /************************** Exported *****************************/
154 void ED_object_location_from_view(bContext *C, float loc[3])
156 View3D *v3d = CTX_wm_view3d(C);
157 Scene *scene = CTX_data_scene(C);
160 cursor = ED_view3d_cursor3d_get(scene, v3d);
162 copy_v3_v3(loc, cursor);
165 void ED_object_rotation_from_view(bContext *C, float rot[3], const char align_axis)
167 RegionView3D *rv3d = CTX_wm_region_view3d(C);
169 BLI_assert(align_axis >= 'X' && align_axis <= 'Z');
174 switch (align_axis) {
178 axis_angle_to_quat(quat_y, rv3d->viewinv[1], -M_PI_2);
179 mul_qt_qtqt(quat, rv3d->viewquat, quat_y);
182 quat_to_eul(rot, quat);
187 copy_qt_qt(quat, rv3d->viewquat);
190 quat_to_eul(rot, quat);
191 rot[0] -= (float)M_PI_2;
196 copy_qt_qt(quat, rv3d->viewquat);
199 quat_to_eul(rot, quat);
210 void ED_object_base_init_transform(bContext *C, Base *base, const float loc[3], const float rot[3])
212 Object *ob = base->object;
213 Scene *scene = CTX_data_scene(C);
218 copy_v3_v3(ob->loc, loc);
221 copy_v3_v3(ob->rot, rot);
223 BKE_object_where_is_calc(scene, ob);
226 /* Uses context to figure out transform for primitive.
227 * Returns standard diameter. */
228 float ED_object_new_primitive_matrix(
229 bContext *C, Object *obedit,
230 const float loc[3], const float rot[3], float primmat[4][4])
232 Scene *scene = CTX_data_scene(C);
233 View3D *v3d = CTX_wm_view3d(C);
234 float mat[3][3], rmat[3][3], cmat[3][3], imat[3][3];
238 eul_to_mat3(rmat, rot);
241 /* inverse transform for initial rotation and object */
242 copy_m3_m4(mat, obedit->obmat);
243 mul_m3_m3m3(cmat, rmat, mat);
244 invert_m3_m3(imat, cmat);
245 copy_m4_m3(primmat, imat);
248 copy_v3_v3(primmat[3], loc);
249 sub_v3_v3v3(primmat[3], primmat[3], obedit->obmat[3]);
250 invert_m3_m3(imat, mat);
251 mul_m3_v3(imat, primmat[3]);
254 const float dia = v3d ? ED_view3d_grid_scale(scene, v3d, NULL) : ED_scene_grid_scale(scene, NULL);
261 /********************* Add Object Operator ********************/
263 static void view_align_update(struct Main *UNUSED(main), struct Scene *UNUSED(scene), struct PointerRNA *ptr)
265 RNA_struct_idprops_unset(ptr, "rotation");
268 void ED_object_add_unit_props(wmOperatorType *ot)
270 RNA_def_float_distance(ot->srna, "radius", 1.0f, 0.0, OBJECT_ADD_SIZE_MAXF, "Radius", "", 0.001, 100.00);
273 void ED_object_add_generic_props(wmOperatorType *ot, bool do_editmode)
277 /* note: this property gets hidden for add-camera operator */
278 prop = RNA_def_boolean(ot->srna, "view_align", 0, "Align to View", "Align the new object to the view");
279 RNA_def_property_update_runtime(prop, view_align_update);
282 prop = RNA_def_boolean(ot->srna, "enter_editmode", 0, "Enter Editmode",
283 "Enter editmode when adding this object");
284 RNA_def_property_flag(prop, PROP_HIDDEN | PROP_SKIP_SAVE);
287 prop = RNA_def_float_vector_xyz(ot->srna, "location", 3, NULL, -OBJECT_ADD_SIZE_MAXF, OBJECT_ADD_SIZE_MAXF,
288 "Location", "Location for the newly added object", -1000.0f, 1000.0f);
289 RNA_def_property_flag(prop, PROP_SKIP_SAVE);
290 prop = RNA_def_float_rotation(ot->srna, "rotation", 3, NULL, -OBJECT_ADD_SIZE_MAXF, OBJECT_ADD_SIZE_MAXF,
291 "Rotation", "Rotation for the newly added object",
292 DEG2RADF(-360.0f), DEG2RADF(360.0f));
293 RNA_def_property_flag(prop, PROP_SKIP_SAVE);
295 prop = RNA_def_boolean_layer_member(ot->srna, "layers", 20, NULL, "Layer", "");
296 RNA_def_property_flag(prop, PROP_HIDDEN | PROP_SKIP_SAVE);
299 void ED_object_add_mesh_props(wmOperatorType *ot)
301 RNA_def_boolean(ot->srna, "calc_uvs", false, "Generate UVs", "Generate a default UV map");
304 bool ED_object_add_generic_get_opts(bContext *C, wmOperator *op, const char view_align_axis,
305 float loc[3], float rot[3],
306 bool *enter_editmode, unsigned int *layer, bool *is_view_aligned)
308 View3D *v3d = CTX_wm_view3d(C);
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);
328 int a, layer_values[20];
332 prop = RNA_struct_find_property(op->ptr, "layers");
333 if (RNA_property_is_set(op->ptr, prop)) {
334 RNA_property_boolean_get_array(op->ptr, prop, layer_values);
336 for (a = 0; a < 20; a++) {
342 Scene *scene = CTX_data_scene(C);
343 *layer = BKE_screen_view3d_layer_active_ex(v3d, scene, false);
344 for (a = 0; a < 20; a++) {
345 layer_values[a] = (*layer & (1 << a)) != 0;
347 RNA_property_boolean_set_array(op->ptr, prop, layer_values);
350 /* in local view we additionally add local view layers,
351 * not part of operator properties */
352 if (v3d && v3d->localvd)
362 if (RNA_struct_property_is_set(op->ptr, "location")) {
363 RNA_float_get_array(op->ptr, "location", loc);
366 ED_object_location_from_view(C, loc);
367 RNA_float_set_array(op->ptr, "location", loc);
373 bool _is_view_aligned;
375 if (!is_view_aligned)
376 is_view_aligned = &_is_view_aligned;
380 if (RNA_struct_property_is_set(op->ptr, "rotation"))
381 *is_view_aligned = false;
382 else if (RNA_struct_property_is_set(op->ptr, "view_align"))
383 *is_view_aligned = RNA_boolean_get(op->ptr, "view_align");
385 *is_view_aligned = (U.flag & USER_ADD_VIEWALIGNED) != 0;
386 RNA_boolean_set(op->ptr, "view_align", *is_view_aligned);
389 if (*is_view_aligned) {
390 ED_object_rotation_from_view(C, rot, view_align_axis);
391 RNA_float_set_array(op->ptr, "rotation", rot);
394 RNA_float_get_array(op->ptr, "rotation", rot);
397 if (layer && *layer == 0) {
398 BKE_report(op->reports, RPT_ERROR, "Property 'layer' has no values set");
405 /* For object add primitive operators.
406 * Do not call undo push in this function (users of this function have to). */
407 Object *ED_object_add_type(
409 int type, const char *name,
410 const float loc[3], const float rot[3],
411 bool enter_editmode, unsigned int UNUSED(layer))
413 Main *bmain = CTX_data_main(C);
414 Scene *scene = CTX_data_scene(C);
415 SceneLayer *sl = CTX_data_scene_layer(C);
418 /* for as long scene has editmode... */
419 if (CTX_data_edit_object(C))
420 ED_object_editmode_exit(C, EM_FREEDATA | EM_FREEUNDO | EM_WAITCURSOR | EM_DO_UNDO); /* freedata, and undo */
422 /* deselects all, sets scene->basact */
423 ob = BKE_object_add(bmain, scene, sl, type, name);
424 /* editor level activate, notifiers */
425 ED_object_base_activate(C, sl->basact);
427 /* more editor stuff */
428 ED_object_base_init_transform(C, sl->basact, loc, rot);
430 /* Ignore collisions by default for non-mesh objects */
431 if (type != OB_MESH) {
432 ob->body_type = OB_BODY_TYPE_NO_COLLISION;
433 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 */
436 DAG_id_type_tag(bmain, ID_OB);
437 DAG_relations_tag_update(bmain);
439 ED_render_id_flush_update(bmain, ob->data);
443 ED_object_editmode_enter(C, EM_IGNORE_LAYER);
445 WM_event_add_notifier(C, NC_SCENE | ND_LAYER_CONTENT, scene);
447 /* TODO(sergey): Use proper flag for tagging here. */
448 DAG_id_tag_update(&scene->id, 0);
453 /* for object add operator */
454 static int object_add_exec(bContext *C, wmOperator *op)
459 float loc[3], rot[3], radius;
461 WM_operator_view3d_unit_defaults(C, op);
462 if (!ED_object_add_generic_get_opts(C, op, 'Z', loc, rot, &enter_editmode, &layer, NULL))
463 return OPERATOR_CANCELLED;
465 radius = RNA_float_get(op->ptr, "radius");
466 ob = ED_object_add_type(C, RNA_enum_get(op->ptr, "type"), NULL, loc, rot, enter_editmode, layer);
468 if (ob->type == OB_LATTICE) {
469 /* lattice is a special case!
470 * we never want to scale the obdata since that is the rest-state */
471 copy_v3_fl(ob->size, radius);
474 BKE_object_obdata_size_init(ob, radius);
477 return OPERATOR_FINISHED;
480 void OBJECT_OT_add(wmOperatorType *ot)
483 ot->name = "Add Object";
484 ot->description = "Add an object to the scene";
485 ot->idname = "OBJECT_OT_add";
488 ot->exec = object_add_exec;
489 ot->poll = ED_operator_objectmode;
492 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
495 ED_object_add_unit_props(ot);
496 RNA_def_enum(ot->srna, "type", rna_enum_object_type_items, 0, "Type", "");
498 ED_object_add_generic_props(ot, true);
501 /********************* Add Effector Operator ********************/
503 /* for object add operator */
504 static int effector_add_exec(bContext *C, wmOperator *op)
510 float loc[3], rot[3];
514 WM_operator_view3d_unit_defaults(C, op);
515 if (!ED_object_add_generic_get_opts(C, op, 'Z', loc, rot, &enter_editmode, &layer, NULL))
516 return OPERATOR_CANCELLED;
518 type = RNA_enum_get(op->ptr, "type");
519 dia = RNA_float_get(op->ptr, "radius");
521 if (type == PFIELD_GUIDE) {
523 const char *name = CTX_DATA_(BLT_I18NCONTEXT_ID_OBJECT, "CurveGuide");
524 ob = ED_object_add_type(C, OB_CURVE, name, loc, rot, false, layer);
527 cu->flag |= CU_PATH | CU_3D;
528 ED_object_editmode_enter(C, 0);
529 ED_object_new_primitive_matrix(C, ob, loc, rot, mat);
530 BLI_addtail(&cu->editnurb->nurbs, ED_curve_add_nurbs_primitive(C, ob, mat, CU_NURBS | CU_PRIM_PATH, dia));
532 ED_object_editmode_exit(C, EM_FREEDATA);
535 const char *name = CTX_DATA_(BLT_I18NCONTEXT_ID_OBJECT, "Field");
536 ob = ED_object_add_type(C, OB_EMPTY, name, loc, rot, false, layer);
537 BKE_object_obdata_size_init(ob, dia);
538 if (ELEM(type, PFIELD_WIND, PFIELD_VORTEX))
539 ob->empty_drawtype = OB_SINGLE_ARROW;
542 ob->pd = object_add_collision_fields(type);
544 DAG_relations_tag_update(CTX_data_main(C));
546 return OPERATOR_FINISHED;
549 void OBJECT_OT_effector_add(wmOperatorType *ot)
552 ot->name = "Add Effector";
553 ot->description = "Add an empty object with a physics effector to the scene";
554 ot->idname = "OBJECT_OT_effector_add";
557 ot->exec = effector_add_exec;
558 ot->poll = ED_operator_objectmode;
561 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
564 ot->prop = RNA_def_enum(ot->srna, "type", field_type_items, 0, "Type", "");
566 ED_object_add_unit_props(ot);
567 ED_object_add_generic_props(ot, true);
570 /********************* Add Camera Operator ********************/
572 static int object_camera_add_exec(bContext *C, wmOperator *op)
574 View3D *v3d = CTX_wm_view3d(C);
575 Scene *scene = CTX_data_scene(C);
580 float loc[3], rot[3];
582 /* force view align for cameras */
583 RNA_boolean_set(op->ptr, "view_align", true);
585 if (!ED_object_add_generic_get_opts(C, op, 'Z', loc, rot, &enter_editmode, &layer, NULL))
586 return OPERATOR_CANCELLED;
588 ob = ED_object_add_type(C, OB_CAMERA, NULL, loc, rot, false, layer);
591 if (v3d->camera == NULL)
593 if (v3d->scenelock && scene->camera == NULL) {
599 cam->drawsize = v3d ? ED_view3d_grid_scale(scene, v3d, NULL) : ED_scene_grid_scale(scene, NULL);
601 return OPERATOR_FINISHED;
604 void OBJECT_OT_camera_add(wmOperatorType *ot)
609 ot->name = "Add Camera";
610 ot->description = "Add a camera object to the scene";
611 ot->idname = "OBJECT_OT_camera_add";
614 ot->exec = object_camera_add_exec;
615 ot->poll = ED_operator_objectmode;
618 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
620 ED_object_add_generic_props(ot, true);
622 /* hide this for cameras, default */
623 prop = RNA_struct_type_find_property(ot->srna, "view_align");
624 RNA_def_property_flag(prop, PROP_HIDDEN);
628 /********************* Add Metaball Operator ********************/
630 static int object_metaball_add_exec(bContext *C, wmOperator *op)
632 Object *obedit = CTX_data_edit_object(C);
636 float loc[3], rot[3];
640 WM_operator_view3d_unit_defaults(C, op);
641 if (!ED_object_add_generic_get_opts(C, op, 'Z', loc, rot, &enter_editmode, &layer, NULL))
642 return OPERATOR_CANCELLED;
644 if (obedit == NULL || obedit->type != OB_MBALL) {
645 obedit = ED_object_add_type(C, OB_MBALL, NULL, loc, rot, true, layer);
649 DAG_id_tag_update(&obedit->id, OB_RECALC_DATA);
652 ED_object_new_primitive_matrix(C, obedit, loc, rot, mat);
653 dia = RNA_float_get(op->ptr, "radius");
655 ED_mball_add_primitive(C, obedit, mat, dia, RNA_enum_get(op->ptr, "type"));
658 if (newob && !enter_editmode) {
659 ED_object_editmode_exit(C, EM_FREEDATA);
662 WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, obedit);
664 return OPERATOR_FINISHED;
667 void OBJECT_OT_metaball_add(wmOperatorType *ot)
670 ot->name = "Add Metaball";
671 ot->description = "Add an metaball object to the scene";
672 ot->idname = "OBJECT_OT_metaball_add";
675 ot->invoke = WM_menu_invoke;
676 ot->exec = object_metaball_add_exec;
677 ot->poll = ED_operator_scene_editable;
680 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
682 ot->prop = RNA_def_enum(ot->srna, "type", rna_enum_metaelem_type_items, 0, "Primitive", "");
684 ED_object_add_unit_props(ot);
685 ED_object_add_generic_props(ot, true);
688 /********************* Add Text Operator ********************/
690 static int object_add_text_exec(bContext *C, wmOperator *op)
692 Object *obedit = CTX_data_edit_object(C);
695 float loc[3], rot[3];
697 WM_operator_view3d_unit_defaults(C, op);
698 if (!ED_object_add_generic_get_opts(C, op, 'Z', loc, rot, &enter_editmode, &layer, NULL))
699 return OPERATOR_CANCELLED;
701 if (obedit && obedit->type == OB_FONT)
702 return OPERATOR_CANCELLED;
704 obedit = ED_object_add_type(C, OB_FONT, NULL, loc, rot, enter_editmode, layer);
705 BKE_object_obdata_size_init(obedit, RNA_float_get(op->ptr, "radius"));
707 WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, obedit);
709 return OPERATOR_FINISHED;
712 void OBJECT_OT_text_add(wmOperatorType *ot)
715 ot->name = "Add Text";
716 ot->description = "Add a text object to the scene";
717 ot->idname = "OBJECT_OT_text_add";
720 ot->exec = object_add_text_exec;
721 ot->poll = ED_operator_objectmode;
724 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
727 ED_object_add_unit_props(ot);
728 ED_object_add_generic_props(ot, true);
731 /********************* Add Armature Operator ********************/
733 static int object_armature_add_exec(bContext *C, wmOperator *op)
735 Object *obedit = CTX_data_edit_object(C);
736 RegionView3D *rv3d = CTX_wm_region_view3d(C);
740 float loc[3], rot[3], dia;
741 bool view_aligned = rv3d && (U.flag & USER_ADD_VIEWALIGNED);
743 WM_operator_view3d_unit_defaults(C, op);
744 if (!ED_object_add_generic_get_opts(C, op, 'Z', loc, rot, &enter_editmode, &layer, NULL))
745 return OPERATOR_CANCELLED;
747 if ((obedit == NULL) || (obedit->type != OB_ARMATURE)) {
748 obedit = ED_object_add_type(C, OB_ARMATURE, NULL, loc, rot, true, layer);
749 ED_object_editmode_enter(C, 0);
753 DAG_id_tag_update(&obedit->id, OB_RECALC_DATA);
756 if (obedit == NULL) {
757 BKE_report(op->reports, RPT_ERROR, "Cannot create editmode armature");
758 return OPERATOR_CANCELLED;
761 dia = RNA_float_get(op->ptr, "radius");
762 ED_armature_edit_bone_add_primitive(obedit, dia, view_aligned);
765 if (newob && !enter_editmode)
766 ED_object_editmode_exit(C, EM_FREEDATA);
768 WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, obedit);
770 return OPERATOR_FINISHED;
773 void OBJECT_OT_armature_add(wmOperatorType *ot)
776 ot->name = "Add Armature";
777 ot->description = "Add an armature object to the scene";
778 ot->idname = "OBJECT_OT_armature_add";
781 ot->exec = object_armature_add_exec;
782 ot->poll = ED_operator_objectmode;
785 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
788 ED_object_add_unit_props(ot);
789 ED_object_add_generic_props(ot, true);
792 /********************* Add Empty Operator ********************/
794 static int object_empty_add_exec(bContext *C, wmOperator *op)
797 int type = RNA_enum_get(op->ptr, "type");
799 float loc[3], rot[3];
801 WM_operator_view3d_unit_defaults(C, op);
802 if (!ED_object_add_generic_get_opts(C, op, 'Z', loc, rot, NULL, &layer, NULL))
803 return OPERATOR_CANCELLED;
805 ob = ED_object_add_type(C, OB_EMPTY, NULL, loc, rot, false, layer);
807 BKE_object_empty_draw_type_set(ob, type);
808 BKE_object_obdata_size_init(ob, RNA_float_get(op->ptr, "radius"));
810 return OPERATOR_FINISHED;
813 void OBJECT_OT_empty_add(wmOperatorType *ot)
816 ot->name = "Add Empty";
817 ot->description = "Add an empty object to the scene";
818 ot->idname = "OBJECT_OT_empty_add";
821 ot->invoke = WM_menu_invoke;
822 ot->exec = object_empty_add_exec;
823 ot->poll = ED_operator_objectmode;
826 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
829 ot->prop = RNA_def_enum(ot->srna, "type", rna_enum_object_empty_drawtype_items, 0, "Type", "");
831 ED_object_add_unit_props(ot);
832 ED_object_add_generic_props(ot, false);
835 static int empty_drop_named_image_invoke(bContext *C, wmOperator *op, const wmEvent *event)
837 Scene *scene = CTX_data_scene(C);
839 BaseLegacy *base = NULL;
843 ima = (Image *)WM_operator_drop_load_path(C, op, ID_IM);
845 return OPERATOR_CANCELLED;
848 id_us_min((ID *)ima);
850 base = ED_view3d_give_base_under_cursor(C, event->mval);
852 /* if empty under cursor, then set object */
853 if (base && base->object->type == OB_EMPTY) {
855 WM_event_add_notifier(C, NC_SCENE | ND_OB_ACTIVE, scene);
862 if (!ED_object_add_generic_get_opts(C, op, 'Z', NULL, rot, NULL, &layer, NULL))
863 return OPERATOR_CANCELLED;
865 ob = ED_object_add_type(C, OB_EMPTY, NULL, NULL, rot, false, layer);
867 /* add under the mouse */
868 ED_object_location_from_view(C, ob->loc);
869 ED_view3d_cursor3d_position(C, ob->loc, event->mval);
872 BKE_object_empty_draw_type_set(ob, OB_EMPTY_IMAGE);
876 id_us_plus(ob->data);
878 return OPERATOR_FINISHED;
881 void OBJECT_OT_drop_named_image(wmOperatorType *ot)
886 ot->name = "Add Empty Image/Drop Image To Empty";
887 ot->description = "Add an empty image type to scene with data";
888 ot->idname = "OBJECT_OT_drop_named_image";
891 ot->invoke = empty_drop_named_image_invoke;
892 ot->poll = ED_operator_objectmode;
895 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
898 prop = RNA_def_string(ot->srna, "filepath", NULL, FILE_MAX, "Filepath", "Path to image file");
899 RNA_def_property_flag(prop, PROP_HIDDEN | PROP_SKIP_SAVE);
900 RNA_def_boolean(ot->srna, "relative_path", true, "Relative Path", "Select the file relative to the blend file");
901 RNA_def_property_flag(prop, PROP_HIDDEN | PROP_SKIP_SAVE);
902 prop = RNA_def_string(ot->srna, "name", NULL, MAX_ID_NAME - 2, "Name", "Image name to assign");
903 RNA_def_property_flag(prop, PROP_HIDDEN | PROP_SKIP_SAVE);
904 ED_object_add_generic_props(ot, false);
907 /********************* Add Lamp Operator ********************/
909 static const char *get_lamp_defname(int type)
912 case LA_LOCAL: return CTX_DATA_(BLT_I18NCONTEXT_ID_LAMP, "Point");
913 case LA_SUN: return CTX_DATA_(BLT_I18NCONTEXT_ID_LAMP, "Sun");
914 case LA_SPOT: return CTX_DATA_(BLT_I18NCONTEXT_ID_LAMP, "Spot");
915 case LA_HEMI: return CTX_DATA_(BLT_I18NCONTEXT_ID_LAMP, "Hemi");
916 case LA_AREA: return CTX_DATA_(BLT_I18NCONTEXT_ID_LAMP, "Area");
918 return CTX_DATA_(BLT_I18NCONTEXT_ID_LAMP, "Lamp");
922 static int object_lamp_add_exec(bContext *C, wmOperator *op)
924 Scene *scene = CTX_data_scene(C);
927 int type = RNA_enum_get(op->ptr, "type");
929 float loc[3], rot[3];
931 WM_operator_view3d_unit_defaults(C, op);
932 if (!ED_object_add_generic_get_opts(C, op, 'Z', loc, rot, NULL, &layer, NULL))
933 return OPERATOR_CANCELLED;
935 ob = ED_object_add_type(C, OB_LAMP, get_lamp_defname(type), loc, rot, false, layer);
936 BKE_object_obdata_size_init(ob, RNA_float_get(op->ptr, "radius"));
938 la = (Lamp *)ob->data;
941 if (BKE_scene_use_new_shading_nodes(scene)) {
942 ED_node_shader_default(C, &la->id);
943 la->use_nodes = true;
946 return OPERATOR_FINISHED;
949 void OBJECT_OT_lamp_add(wmOperatorType *ot)
952 ot->name = "Add Lamp";
953 ot->description = "Add a lamp object to the scene";
954 ot->idname = "OBJECT_OT_lamp_add";
957 ot->invoke = WM_menu_invoke;
958 ot->exec = object_lamp_add_exec;
959 ot->poll = ED_operator_objectmode;
962 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
965 ot->prop = RNA_def_enum(ot->srna, "type", rna_enum_lamp_type_items, 0, "Type", "");
966 RNA_def_property_translation_context(ot->prop, BLT_I18NCONTEXT_ID_LAMP);
968 ED_object_add_unit_props(ot);
969 ED_object_add_generic_props(ot, false);
972 /********************* Add Group Instance Operator ********************/
974 static int group_instance_add_exec(bContext *C, wmOperator *op)
978 float loc[3], rot[3];
980 if (RNA_struct_property_is_set(op->ptr, "name")) {
981 char name[MAX_ID_NAME - 2];
983 RNA_string_get(op->ptr, "name", name);
984 group = (Group *)BKE_libblock_find_name(ID_GR, name);
986 if (0 == RNA_struct_property_is_set(op->ptr, "location")) {
987 wmEvent *event = CTX_wm_window(C)->eventstate;
988 ARegion *ar = CTX_wm_region(C);
989 const int mval[2] = {event->x - ar->winrct.xmin,
990 event->y - ar->winrct.ymin};
991 ED_object_location_from_view(C, loc);
992 ED_view3d_cursor3d_position(C, loc, mval);
993 RNA_float_set_array(op->ptr, "location", loc);
997 group = BLI_findlink(&CTX_data_main(C)->group, RNA_enum_get(op->ptr, "group"));
999 if (!ED_object_add_generic_get_opts(C, op, 'Z', loc, rot, NULL, &layer, NULL))
1000 return OPERATOR_CANCELLED;
1003 Main *bmain = CTX_data_main(C);
1004 Scene *scene = CTX_data_scene(C);
1005 Object *ob = ED_object_add_type(C, OB_EMPTY, group->id.name + 2, loc, rot, false, layer);
1006 ob->dup_group = group;
1007 ob->transflag |= OB_DUPLIGROUP;
1008 id_us_plus(&group->id);
1010 /* works without this except if you try render right after, see: 22027 */
1011 DAG_relations_tag_update(bmain);
1013 WM_event_add_notifier(C, NC_SCENE | ND_OB_ACTIVE, scene);
1015 return OPERATOR_FINISHED;
1018 return OPERATOR_CANCELLED;
1021 /* only used as menu */
1022 void OBJECT_OT_group_instance_add(wmOperatorType *ot)
1027 ot->name = "Add Group Instance";
1028 ot->description = "Add a dupligroup instance";
1029 ot->idname = "OBJECT_OT_group_instance_add";
1032 ot->invoke = WM_enum_search_invoke;
1033 ot->exec = group_instance_add_exec;
1034 ot->poll = ED_operator_objectmode;
1037 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1040 RNA_def_string(ot->srna, "name", "Group", MAX_ID_NAME - 2, "Name", "Group name to add");
1041 prop = RNA_def_enum(ot->srna, "group", DummyRNA_NULL_items, 0, "Group", "");
1042 RNA_def_enum_funcs(prop, RNA_group_itemf);
1043 RNA_def_property_flag(prop, PROP_ENUM_NO_TRANSLATE);
1045 ED_object_add_generic_props(ot, false);
1048 /********************* Add Speaker Operator ********************/
1050 static int object_speaker_add_exec(bContext *C, wmOperator *op)
1054 float loc[3], rot[3];
1055 Scene *scene = CTX_data_scene(C);
1057 if (!ED_object_add_generic_get_opts(C, op, 'Z', loc, rot, NULL, &layer, NULL))
1058 return OPERATOR_CANCELLED;
1060 ob = ED_object_add_type(C, OB_SPEAKER, NULL, loc, rot, false, layer);
1062 /* to make it easier to start using this immediately in NLA, a default sound clip is created
1063 * ready to be moved around to retime the sound and/or make new sound clips
1066 /* create new data for NLA hierarchy */
1067 AnimData *adt = BKE_animdata_add_id(&ob->id);
1068 NlaTrack *nlt = add_nlatrack(adt, NULL);
1069 NlaStrip *strip = add_nla_soundstrip(scene, ob->data);
1070 strip->start = CFRA;
1071 strip->end += strip->start;
1074 BKE_nlatrack_add_strip(nlt, strip);
1076 /* auto-name the strip, and give the track an interesting name */
1077 BLI_strncpy(nlt->name, DATA_("SoundTrack"), sizeof(nlt->name));
1078 BKE_nlastrip_validate_name(adt, strip);
1080 WM_event_add_notifier(C, NC_ANIMATION | ND_NLA | NA_EDITED, NULL);
1083 return OPERATOR_FINISHED;
1086 void OBJECT_OT_speaker_add(wmOperatorType *ot)
1089 ot->name = "Add Speaker";
1090 ot->description = "Add a speaker object to the scene";
1091 ot->idname = "OBJECT_OT_speaker_add";
1094 ot->exec = object_speaker_add_exec;
1095 ot->poll = ED_operator_objectmode;
1098 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1100 ED_object_add_generic_props(ot, true);
1103 /**************************** Delete Object *************************/
1105 static void object_delete_check_glsl_update(Object *ob)
1107 /* some objects could affect on GLSL shading, make sure GLSL settings
1108 * are being tagged to be updated when object is removing from scene
1110 if (ob->type == OB_LAMP) {
1111 if (ob->gpulamp.first)
1116 /* remove base from a specific scene */
1117 /* note: now unlinks constraints as well */
1118 void ED_base_object_free_and_unlink(Main *bmain, Scene *scene, Object *ob)
1120 if (BKE_library_ID_is_indirectly_used(bmain, ob) &&
1121 ID_REAL_USERS(ob) <= 1 && ID_EXTRA_USERS(ob) == 0)
1123 /* We cannot delete indirectly used object... */
1124 printf("WARNING, undeletable object '%s', should have been catched before reaching this function!",
1129 object_delete_check_glsl_update(ob);
1130 BKE_collections_object_remove(bmain, scene, ob, true);
1131 DAG_id_type_tag(bmain, ID_OB);
1134 static int object_delete_exec(bContext *C, wmOperator *op)
1136 Main *bmain = CTX_data_main(C);
1137 Scene *scene = CTX_data_scene(C);
1138 wmWindowManager *wm = CTX_wm_manager(C);
1140 const bool use_global = RNA_boolean_get(op->ptr, "use_global");
1141 bool changed = false;
1143 if (CTX_data_edit_object(C))
1144 return OPERATOR_CANCELLED;
1146 CTX_DATA_BEGIN (C, Object *, ob, selected_objects)
1148 const bool is_indirectly_used = BKE_library_ID_is_indirectly_used(bmain, ob);
1149 if (ob->id.tag & LIB_TAG_INDIRECT) {
1150 /* Can this case ever happen? */
1151 BKE_reportf(op->reports, RPT_WARNING, "Cannot delete indirectly linked object '%s'", ob->id.name + 2);
1154 else if (is_indirectly_used && ID_REAL_USERS(ob) <= 1 && ID_EXTRA_USERS(ob) == 0) {
1155 BKE_reportf(op->reports, RPT_WARNING,
1156 "Cannot delete object '%s' from scene '%s', indirectly used objects need at least one user",
1157 ob->id.name + 2, scene->id.name + 2);
1161 /* This is sort of a quick hack to address T51243 - Proper thing to do here would be to nuke most of all this
1162 * custom scene/object/base handling, and use generic lib remap/query for that.
1163 * But this is for later (aka 2.8, once layers & co are settled and working).
1165 if (use_global && ob->id.lib == NULL) {
1166 /* We want to nuke the object, let's nuke it the easy way (not for linked data though)... */
1167 BKE_libblock_delete(bmain, &ob->id);
1172 /* remove from Grease Pencil parent */
1173 /* XXX This is likely not correct? Will also remove parent from grease pencil from other scenes,
1174 * even when use_global is false... */
1175 for (bGPdata *gpd = bmain->gpencil.first; gpd; gpd = gpd->id.next) {
1176 for (bGPDlayer *gpl = gpd->layers.first; gpl; gpl = gpl->next) {
1177 if (gpl->parent != NULL) {
1178 if (gpl->parent == ob) {
1185 /* remove from current scene only */
1186 ED_base_object_free_and_unlink(bmain, scene, ob);
1191 for (scene_iter = bmain->scene.first; scene_iter; scene_iter = scene_iter->id.next) {
1192 if (scene_iter != scene && !ID_IS_LINKED_DATABLOCK(scene_iter)) {
1193 if (is_indirectly_used && ID_REAL_USERS(ob) <= 1 && ID_EXTRA_USERS(ob) == 0) {
1194 BKE_reportf(op->reports, RPT_WARNING,
1195 "Cannot delete object '%s' from scene '%s', indirectly used objects need at least one user",
1196 ob->id.name + 2, scene_iter->id.name + 2);
1199 ED_base_object_free_and_unlink(bmain, scene_iter, ob);
1208 return OPERATOR_CANCELLED;
1210 /* delete has to handle all open scenes */
1211 BKE_main_id_tag_listbase(&bmain->scene, LIB_TAG_DOIT, true);
1212 for (win = wm->windows.first; win; win = win->next) {
1213 scene = WM_window_get_active_scene(win);
1215 if (scene->id.tag & LIB_TAG_DOIT) {
1216 scene->id.tag &= ~LIB_TAG_DOIT;
1218 DAG_relations_tag_update(bmain);
1220 WM_event_add_notifier(C, NC_SCENE | ND_OB_ACTIVE, scene);
1221 WM_event_add_notifier(C, NC_SCENE | ND_LAYER_CONTENT, scene);
1225 return OPERATOR_FINISHED;
1228 void OBJECT_OT_delete(wmOperatorType *ot)
1231 ot->name = "Delete";
1232 ot->description = "Delete selected objects";
1233 ot->idname = "OBJECT_OT_delete";
1236 ot->invoke = WM_operator_confirm;
1237 ot->exec = object_delete_exec;
1238 ot->poll = ED_operator_objectmode;
1241 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1243 RNA_def_boolean(ot->srna, "use_global", 0, "Delete Globally", "Remove object from all scenes");
1246 /**************************** Copy Utilities ******************************/
1248 /* after copying objects, copied data should get new pointers */
1249 static void copy_object_set_idnew(bContext *C)
1251 Main *bmain = CTX_data_main(C);
1253 CTX_DATA_BEGIN (C, Object *, ob, selected_editable_objects)
1255 BKE_libblock_relink_to_newid(&ob->id);
1259 set_sca_new_poins();
1261 BKE_main_id_clear_newpoins(bmain);
1264 /********************* Make Duplicates Real ************************/
1267 * \note regarding hashing dupli-objects when using OB_DUPLIGROUP, skip the first member of #DupliObject.persistent_id
1268 * since its a unique index and we only want to know if the group objects are from the same dupli-group instance.
1270 static unsigned int dupliobject_group_hash(const void *ptr)
1272 const DupliObject *dob = ptr;
1273 unsigned int hash = BLI_ghashutil_ptrhash(dob->ob);
1275 for (i = 1; (i < MAX_DUPLI_RECUR) && dob->persistent_id[i] != INT_MAX; i++) {
1276 hash ^= (dob->persistent_id[i] ^ i);
1282 * \note regarding hashing dupli-objects when NOT using OB_DUPLIGROUP, include the first member of #DupliObject.persistent_id
1283 * since its the index of the vertex/face the object is instantiated on and we want to identify objects on the same vertex/face.
1285 static unsigned int dupliobject_hash(const void *ptr)
1287 const DupliObject *dob = ptr;
1288 unsigned int hash = BLI_ghashutil_ptrhash(dob->ob);
1289 hash ^= (dob->persistent_id[0] ^ 0);
1293 /* Compare function that matches dupliobject_group_hash */
1294 static bool dupliobject_group_cmp(const void *a_, const void *b_)
1296 const DupliObject *a = a_;
1297 const DupliObject *b = b_;
1300 if (a->ob != b->ob) {
1304 for (i = 1; (i < MAX_DUPLI_RECUR); i++) {
1305 if (a->persistent_id[i] != b->persistent_id[i]) {
1308 else if (a->persistent_id[i] == INT_MAX) {
1317 /* Compare function that matches dupliobject_hash */
1318 static bool dupliobject_cmp(const void *a_, const void *b_)
1320 const DupliObject *a = a_;
1321 const DupliObject *b = b_;
1323 if (a->ob != b->ob) {
1327 if (a->persistent_id[0] != b->persistent_id[0]) {
1335 static void make_object_duplilist_real(bContext *C, Scene *scene, Base *base,
1336 const bool use_base_parent,
1337 const bool use_hierarchy)
1339 Main *bmain = CTX_data_main(C);
1340 SceneLayer *sl = CTX_data_scene_layer(C);
1343 GHash *dupli_gh = NULL, *parent_gh = NULL;
1346 if (!(base->object->transflag & OB_DUPLI))
1349 lb = object_duplilist(bmain->eval_ctx, scene, base->object);
1351 if (use_hierarchy || use_base_parent) {
1352 dupli_gh = BLI_ghash_ptr_new(__func__);
1353 if (use_hierarchy) {
1354 if (base->object->transflag & OB_DUPLIGROUP) {
1355 parent_gh = BLI_ghash_new(dupliobject_group_hash, dupliobject_group_cmp, __func__);
1358 parent_gh = BLI_ghash_new(dupliobject_hash, dupliobject_cmp, __func__);
1363 for (dob = lb->first; dob; dob = dob->next) {
1365 Object *ob = ID_NEW_SET(dob->ob, BKE_object_copy(bmain, dob->ob));
1367 /* font duplis can have a totcol without material, we get them from parent
1368 * should be implemented better...
1370 if (ob->mat == NULL) ob->totcol = 0;
1372 BKE_collection_object_add_from(scene, dob->ob, ob);
1373 basen = BKE_scene_layer_base_find(sl, ob);
1375 BKE_scene_object_base_flag_sync_from_base(basen);
1377 /* make sure apply works */
1378 BKE_animdata_free(&ob->id, true);
1381 /* Proxies are not to be copied. */
1382 ob->proxy_from = NULL;
1383 ob->proxy_group = NULL;
1387 BKE_constraints_free(&ob->constraints);
1388 ob->curve_cache = NULL;
1389 ob->transflag &= ~OB_DUPLI;
1391 copy_m4_m4(ob->obmat, dob->mat);
1392 BKE_object_apply_mat4(ob, ob->obmat, false, false);
1395 BLI_ghash_insert(dupli_gh, dob, ob);
1399 /* Due to nature of hash/comparison of this ghash, a lot of duplis may be considered as 'the same',
1400 * this avoids trying to insert same key several time and raise asserts in debug builds... */
1401 if (!BLI_ghash_ensure_p(parent_gh, dob, &val)) {
1406 /* Remap new object to itself, and clear again newid pointer of orig object. */
1407 BKE_libblock_relink_to_newid(&ob->id);
1408 set_sca_new_poins_ob(ob);
1409 BKE_id_clear_newpoin(&dob->ob->id);
1411 DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
1414 if (use_hierarchy) {
1415 for (dob = lb->first; dob; dob = dob->next) {
1416 /* original parents */
1417 Object *ob_src = dob->ob;
1418 Object *ob_src_par = ob_src->parent;
1420 Object *ob_dst = BLI_ghash_lookup(dupli_gh, dob);
1421 Object *ob_dst_par = NULL;
1423 /* find parent that was also made real */
1425 /* OK to keep most of the members uninitialized,
1426 * they won't be read, this is simply for a hash lookup. */
1427 DupliObject dob_key;
1428 dob_key.ob = ob_src_par;
1429 if (base->object->transflag & OB_DUPLIGROUP) {
1430 memcpy(&dob_key.persistent_id[1],
1431 &dob->persistent_id[1],
1432 sizeof(dob->persistent_id[1]) * (MAX_DUPLI_RECUR - 1));
1435 dob_key.persistent_id[0] = dob->persistent_id[0];
1437 ob_dst_par = BLI_ghash_lookup(parent_gh, &dob_key);
1441 /* allow for all possible parent types */
1442 ob_dst->partype = ob_src->partype;
1443 BLI_strncpy(ob_dst->parsubstr, ob_src->parsubstr, sizeof(ob_dst->parsubstr));
1444 ob_dst->par1 = ob_src->par1;
1445 ob_dst->par2 = ob_src->par2;
1446 ob_dst->par3 = ob_src->par3;
1448 copy_m4_m4(ob_dst->parentinv, ob_src->parentinv);
1450 ob_dst->parent = ob_dst_par;
1452 else if (use_base_parent) {
1453 ob_dst->parent = base->object;
1454 ob_dst->partype = PAROBJECT;
1457 if (ob_dst->parent) {
1458 invert_m4_m4(ob_dst->parentinv, dob->mat);
1460 /* note, this may be the parent of other objects, but it should
1461 * still work out ok */
1462 BKE_object_apply_mat4(ob_dst, dob->mat, false, true);
1464 /* to set ob_dst->orig and in case theres any other discrepicies */
1465 DAG_id_tag_update(&ob_dst->id, OB_RECALC_OB);
1469 else if (use_base_parent) {
1470 /* since we are ignoring the internal hierarchy - parent all to the
1472 for (dob = lb->first; dob; dob = dob->next) {
1473 /* original parents */
1474 Object *ob_dst = BLI_ghash_lookup(dupli_gh, dob);
1476 ob_dst->parent = base->object;
1477 ob_dst->partype = PAROBJECT;
1479 /* similer to the code above, see comments */
1480 invert_m4_m4(ob_dst->parentinv, dob->mat);
1481 BKE_object_apply_mat4(ob_dst, dob->mat, false, true);
1482 DAG_id_tag_update(&ob_dst->id, OB_RECALC_OB);
1486 if (base->object->transflag & OB_DUPLIGROUP && base->object->dup_group) {
1487 for (object = bmain->object.first; object; object = object->id.next) {
1488 if (object->proxy_group == base->object) {
1489 object->proxy = NULL;
1490 object->proxy_from = NULL;
1491 DAG_id_tag_update(&object->id, OB_RECALC_OB);
1497 BLI_ghash_free(dupli_gh, NULL, NULL);
1499 BLI_ghash_free(parent_gh, NULL, NULL);
1501 free_object_duplilist(lb);
1503 base->object->transflag &= ~OB_DUPLI;
1506 static int object_duplicates_make_real_exec(bContext *C, wmOperator *op)
1508 Main *bmain = CTX_data_main(C);
1509 Scene *scene = CTX_data_scene(C);
1511 const bool use_base_parent = RNA_boolean_get(op->ptr, "use_base_parent");
1512 const bool use_hierarchy = RNA_boolean_get(op->ptr, "use_hierarchy");
1514 BKE_main_id_clear_newpoins(bmain);
1516 CTX_DATA_BEGIN (C, Base *, base, selected_editable_bases)
1518 make_object_duplilist_real(C, scene, base, use_base_parent, use_hierarchy);
1520 /* dependencies were changed */
1521 WM_event_add_notifier(C, NC_OBJECT | ND_PARENT, base->object);
1525 DAG_relations_tag_update(bmain);
1526 WM_event_add_notifier(C, NC_SCENE, scene);
1527 WM_main_add_notifier(NC_OBJECT | ND_DRAW, NULL);
1529 return OPERATOR_FINISHED;
1532 void OBJECT_OT_duplicates_make_real(wmOperatorType *ot)
1535 ot->name = "Make Duplicates Real";
1536 ot->description = "Make dupli objects attached to this object real";
1537 ot->idname = "OBJECT_OT_duplicates_make_real";
1540 ot->exec = object_duplicates_make_real_exec;
1542 ot->poll = ED_operator_objectmode;
1545 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1547 RNA_def_boolean(ot->srna, "use_base_parent", 0, "Parent", "Parent newly created objects to the original duplicator");
1548 RNA_def_boolean(ot->srna, "use_hierarchy", 0, "Keep Hierarchy", "Maintain parent child relationships");
1551 /**************************** Convert **************************/
1553 static EnumPropertyItem convert_target_items[] = {
1554 {OB_CURVE, "CURVE", ICON_OUTLINER_OB_CURVE, "Curve from Mesh/Text", ""},
1555 {OB_MESH, "MESH", ICON_OUTLINER_OB_MESH, "Mesh from Curve/Meta/Surf/Text", ""},
1556 {0, NULL, 0, NULL, NULL}
1559 static void convert_ensure_curve_cache(Main *bmain, Scene *scene, Object *ob)
1561 if (ob->curve_cache == NULL) {
1562 /* Force creation. This is normally not needed but on operator
1563 * redo we might end up with an object which isn't evaluated yet.
1565 if (ELEM(ob->type, OB_SURF, OB_CURVE, OB_FONT)) {
1566 BKE_displist_make_curveTypes(scene, ob, false);
1568 else if (ob->type == OB_MBALL) {
1569 BKE_displist_make_mball(bmain->eval_ctx, scene, ob);
1574 static void curvetomesh(Main *bmain, Scene *scene, Object *ob)
1576 convert_ensure_curve_cache(bmain, scene, ob);
1577 BKE_mesh_from_nurbs(ob); /* also does users */
1579 if (ob->type == OB_MESH) {
1580 BKE_object_free_modifiers(ob);
1582 /* Game engine defaults for mesh objects */
1583 ob->body_type = OB_BODY_TYPE_STATIC;
1584 ob->gameflag = OB_PROP | OB_COLLISION;
1588 static int convert_poll(bContext *C)
1590 Object *obact = CTX_data_active_object(C);
1591 Scene *scene = CTX_data_scene(C);
1593 return (!ID_IS_LINKED_DATABLOCK(scene) && obact && scene->obedit != obact &&
1594 (obact->flag & SELECT) && !ID_IS_LINKED_DATABLOCK(obact));
1597 /* Helper for convert_exec */
1598 static Base *duplibase_for_convert(Main *bmain, Scene *scene, SceneLayer *sl, Base *base, Object *ob)
1607 obn = BKE_object_copy(bmain, ob);
1608 DAG_id_tag_update(&ob->id, OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME);
1609 BKE_collection_object_add_from(scene, ob, obn);
1611 basen = BKE_scene_layer_base_find(sl, obn);
1612 ED_object_base_select(basen, BA_SELECT);
1613 ED_object_base_select(basen, BA_DESELECT);
1617 static int convert_exec(bContext *C, wmOperator *op)
1619 Main *bmain = CTX_data_main(C);
1620 Scene *scene = CTX_data_scene(C);
1621 SceneLayer *sl = CTX_data_scene_layer(C);
1622 Base *basen = NULL, *basact = NULL;
1623 Object *ob, *ob1, *newob, *obact = CTX_data_active_object(C);
1629 const short target = RNA_enum_get(op->ptr, "target");
1630 const bool keep_original = RNA_boolean_get(op->ptr, "keep_original");
1631 int a, mballConverted = 0;
1633 /* don't forget multiple users! */
1638 for (base = scene->base.first; base; base = base->next) {
1640 ob->flag &= ~OB_DONE;
1642 /* flag data thats not been edited (only needed for !keep_original) */
1644 ((ID *)ob->data)->tag |= LIB_TAG_DOIT;
1647 /* possible metaball basis is not in this scene */
1648 if (ob->type == OB_MBALL && target == OB_MESH) {
1649 if (BKE_mball_is_basis(ob) == false) {
1651 ob_basis = BKE_mball_basis_find(scene, ob);
1653 ob_basis->flag &= ~OB_DONE;
1660 ListBase selected_editable_bases = CTX_data_collection_get(C, "selected_editable_bases");
1662 /* Ensure we get all meshes calculated with a sufficient data-mask,
1663 * needed since re-evaluating single modifiers causes bugs if they depend
1664 * on other objects data masks too, see: T50950. */
1666 for (CollectionPointerLink *link = selected_editable_bases.first; link; link = link->next) {
1667 Base *base = link->ptr.data;
1668 DAG_id_tag_update(&base->object->id, OB_RECALC_DATA);
1671 uint64_t customdata_mask_prev = scene->customdata_mask;
1672 scene->customdata_mask |= CD_MASK_MESH;
1673 BKE_scene_update_tagged(bmain->eval_ctx, bmain, scene);
1674 scene->customdata_mask = customdata_mask_prev;
1677 for (CollectionPointerLink *link = selected_editable_bases.first; link; link = link->next) {
1678 Base *base = link->ptr.data;
1681 if (ob->flag & OB_DONE || !IS_TAGGED(ob->data)) {
1682 if (ob->type != target) {
1683 base->flag &= ~SELECT;
1684 ob->flag &= ~SELECT;
1687 /* obdata already modified */
1688 if (!IS_TAGGED(ob->data)) {
1689 /* When 2 objects with linked data are selected, converting both
1690 * would keep modifiers on all but the converted object [#26003] */
1691 if (ob->type == OB_MESH) {
1692 BKE_object_free_modifiers(ob); /* after derivedmesh calls! */
1696 else if (ob->type == OB_MESH && target == OB_CURVE) {
1697 ob->flag |= OB_DONE;
1699 if (keep_original) {
1700 basen = duplibase_for_convert(bmain, scene, sl, base, NULL);
1701 newob = basen->object;
1703 /* decrement original mesh's usage count */
1707 /* make a new copy of the mesh */
1708 newob->data = BKE_mesh_copy(bmain, me);
1714 BKE_mesh_to_curve(scene, newob);
1716 if (newob->type == OB_CURVE) {
1717 BKE_object_free_modifiers(newob); /* after derivedmesh calls! */
1718 ED_rigidbody_object_remove(bmain, scene, newob);
1721 else if (ob->type == OB_MESH) {
1722 ob->flag |= OB_DONE;
1724 if (keep_original) {
1725 basen = duplibase_for_convert(bmain, scene, sl, base, NULL);
1726 newob = basen->object;
1728 /* decrement original mesh's usage count */
1732 /* make a new copy of the mesh */
1733 newob->data = BKE_mesh_copy(bmain, me);
1737 DAG_id_tag_update(&ob->id, OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME);
1740 /* make new mesh data from the original copy */
1741 /* note: get the mesh from the original, not from the copy in some
1742 * cases this doesnt give correct results (when MDEF is used for eg)
1744 dm = mesh_get_derived_final(scene, newob, CD_MASK_MESH);
1746 DM_to_mesh(dm, newob->data, newob, CD_MASK_MESH, true);
1748 /* re-tessellation is called by DM_to_mesh */
1750 BKE_object_free_modifiers(newob); /* after derivedmesh calls! */
1752 else if (ob->type == OB_FONT) {
1753 ob->flag |= OB_DONE;
1755 if (keep_original) {
1756 basen = duplibase_for_convert(bmain, scene, sl, base, NULL);
1757 newob = basen->object;
1759 /* decrement original curve's usage count */
1760 id_us_min(&((Curve *)newob->data)->id);
1762 /* make a new copy of the curve */
1763 newob->data = BKE_curve_copy(bmain, ob->data);
1771 /* TODO(sergey): Ideally DAG will create nurbs list for a curve data
1772 * datablock, but for until we've got granular update
1773 * lets take care by selves.
1775 BKE_vfont_to_curve(bmain, newob, FO_EDIT);
1777 newob->type = OB_CURVE;
1778 cu->type = OB_CURVE;
1781 id_us_min(&cu->vfont->id);
1785 id_us_min(&cu->vfontb->id);
1789 id_us_min(&cu->vfonti->id);
1793 id_us_min(&cu->vfontbi->id);
1797 if (!keep_original) {
1799 if (cu->id.us > 1) {
1800 for (ob1 = bmain->object.first; ob1; ob1 = ob1->id.next) {
1801 if (ob1->data == ob->data) {
1802 ob1->type = OB_CURVE;
1803 DAG_id_tag_update(&ob1->id, OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME);
1809 for (nu = cu->nurb.first; nu; nu = nu->next)
1813 BKE_curve_curve_dimension_update(cu);
1815 if (target == OB_MESH) {
1816 curvetomesh(bmain, scene, newob);
1818 /* meshes doesn't use displist */
1819 BKE_object_free_curve_cache(newob);
1822 else if (ELEM(ob->type, OB_CURVE, OB_SURF)) {
1823 ob->flag |= OB_DONE;
1825 if (target == OB_MESH) {
1826 if (keep_original) {
1827 basen = duplibase_for_convert(bmain, scene, sl, base, NULL);
1828 newob = basen->object;
1830 /* decrement original curve's usage count */
1831 id_us_min(&((Curve *)newob->data)->id);
1833 /* make a new copy of the curve */
1834 newob->data = BKE_curve_copy(bmain, ob->data);
1840 curvetomesh(bmain, scene, newob);
1842 /* meshes doesn't use displist */
1843 BKE_object_free_curve_cache(newob);
1846 else if (ob->type == OB_MBALL && target == OB_MESH) {
1849 base->flag &= ~SELECT;
1850 ob->flag &= ~SELECT;
1852 baseob = BKE_mball_basis_find(scene, ob);
1855 /* if motherball is converting it would be marked as done later */
1856 ob->flag |= OB_DONE;
1859 if (!(baseob->flag & OB_DONE)) {
1860 baseob->flag |= OB_DONE;
1862 basen = duplibase_for_convert(bmain, scene, sl, base, baseob);
1863 newob = basen->object;
1868 newob->data = BKE_mesh_add(bmain, "Mesh");
1869 newob->type = OB_MESH;
1872 me->totcol = mb->totcol;
1873 if (newob->totcol) {
1874 me->mat = MEM_dupallocN(mb->mat);
1875 for (a = 0; a < newob->totcol; a++) id_us_plus((ID *)me->mat[a]);
1878 convert_ensure_curve_cache(bmain, scene, baseob);
1879 BKE_mesh_from_metaball(&baseob->curve_cache->disp, newob->data);
1881 if (obact->type == OB_MBALL) {
1892 /* tag obdata if it was been changed */
1894 /* If the original object is active then make this object active */
1897 /* store new active base to update BASACT */
1904 if (!keep_original && (ob->flag & OB_DONE)) {
1905 DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
1906 ((ID *)ob->data)->tag &= ~LIB_TAG_DOIT; /* flag not to convert this datablock again */
1909 BLI_freelistN(&selected_editable_bases);
1911 if (!keep_original) {
1912 if (mballConverted) {
1913 FOREACH_SCENE_OBJECT(scene, ob_mball)
1915 if (ob->type == OB_MBALL) {
1916 if (ob->flag & OB_DONE) {
1917 Object *ob_basis = NULL;
1918 if (BKE_mball_is_basis(ob_mball) ||
1919 ((ob_basis = BKE_mball_basis_find(scene, ob_mball)) && (ob_basis->flag & OB_DONE)))
1921 ED_base_object_free_and_unlink(bmain, scene, ob_mball);
1926 FOREACH_SCENE_OBJECT_END
1929 /* delete object should renew depsgraph */
1930 DAG_relations_tag_update(bmain);
1933 // XXX ED_object_editmode_enter(C, 0);
1934 // XXX exit_editmode(C, EM_FREEDATA|EM_WAITCURSOR); /* freedata, but no undo */
1937 /* active base was changed */
1938 ED_object_base_activate(C, basact);
1939 BASACT_NEW = basact;
1941 else if (BASACT_NEW->object->flag & OB_DONE) {
1942 WM_event_add_notifier(C, NC_OBJECT | ND_MODIFIER, BASACT_NEW->object);
1943 WM_event_add_notifier(C, NC_OBJECT | ND_DATA, BASACT_NEW->object);
1946 DAG_relations_tag_update(bmain);
1947 WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, scene);
1948 WM_event_add_notifier(C, NC_SCENE | ND_OB_SELECT, scene);
1950 return OPERATOR_FINISHED;
1954 void OBJECT_OT_convert(wmOperatorType *ot)
1957 ot->name = "Convert to";
1958 ot->description = "Convert selected objects to another type";
1959 ot->idname = "OBJECT_OT_convert";
1962 ot->invoke = WM_menu_invoke;
1963 ot->exec = convert_exec;
1964 ot->poll = convert_poll;
1967 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1970 ot->prop = RNA_def_enum(ot->srna, "target", convert_target_items, OB_MESH, "Target", "Type of object to convert to");
1971 RNA_def_boolean(ot->srna, "keep_original", 0, "Keep Original", "Keep original objects instead of replacing them");
1974 /**************************** Duplicate ************************/
1977 * dupflag: a flag made from constants declared in DNA_userdef_types.h
1978 * The flag tells adduplicate() whether to copy data linked to the object, or to reference the existing data.
1979 * U.dupflag for default operations or you can construct a flag as python does
1980 * if the dupflag is 0 then no data will be copied (linked duplicate) */
1982 /* used below, assumes id.new is correct */
1983 /* leaves selection of base/object unaltered */
1984 /* Does set ID->newid pointers. */
1985 static Base *object_add_duplicate_internal(Main *bmain, Scene *scene, SceneLayer *sl, Object *ob, int dupflag)
1987 #define ID_NEW_REMAP_US(a) if ( (a)->id.newid) { (a) = (void *)(a)->id.newid; (a)->id.us++; }
1988 #define ID_NEW_REMAP_US2(a) if (((ID *)a)->newid) { (a) = ((ID *)a)->newid; ((ID *)a)->us++; }
1991 Material ***matarar;
1996 if (ob->mode & OB_MODE_POSE) {
2000 obn = ID_NEW_SET(ob, BKE_object_copy(bmain, ob));
2001 DAG_id_tag_update(&obn->id, OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME);
2003 BKE_collection_object_add_from(scene, ob, obn);
2004 basen = BKE_scene_layer_base_find(sl, obn);
2006 /* 1) duplis should end up in same group as the original
2007 * 2) Rigid Body sim participants MUST always be part of a group...
2009 // XXX: is 2) really a good measure here?
2010 if ((ob->flag & OB_FROMGROUP) != 0 || ob->rigidbody_object || ob->rigidbody_constraint) {
2012 for (group = bmain->group.first; group; group = group->id.next) {
2013 if (BKE_group_object_exists(group, ob))
2014 BKE_group_object_add(group, obn);
2018 /* duplicates using userflags */
2019 if (dupflag & USER_DUP_ACT) {
2020 BKE_animdata_copy_id_action(&obn->id, true);
2023 if (dupflag & USER_DUP_MAT) {
2024 for (a = 0; a < obn->totcol; a++) {
2025 id = (ID *)obn->mat[a];
2027 ID_NEW_REMAP_US(obn->mat[a])
2029 obn->mat[a] = ID_NEW_SET(obn->mat[a], BKE_material_copy(bmain, obn->mat[a]));
2033 if (dupflag & USER_DUP_ACT) {
2034 BKE_animdata_copy_id_action(&obn->mat[a]->id, true);
2039 if (dupflag & USER_DUP_PSYS) {
2040 ParticleSystem *psys;
2041 for (psys = obn->particlesystem.first; psys; psys = psys->next) {
2042 id = (ID *) psys->part;
2044 ID_NEW_REMAP_US(psys->part)
2046 psys->part = ID_NEW_SET(psys->part, BKE_particlesettings_copy(bmain, psys->part));
2049 if (dupflag & USER_DUP_ACT) {
2050 BKE_animdata_copy_id_action(&psys->part->id, true);
2061 switch (obn->type) {
2063 if (dupflag & USER_DUP_MESH) {
2064 ID_NEW_REMAP_US2(obn->data)
2066 obn->data = ID_NEW_SET(obn->data, BKE_mesh_copy(bmain, obn->data));
2073 if (dupflag & USER_DUP_CURVE) {
2074 ID_NEW_REMAP_US2(obn->data)
2076 obn->data = ID_NEW_SET(obn->data, BKE_curve_copy(bmain, obn->data));
2083 if (dupflag & USER_DUP_SURF) {
2084 ID_NEW_REMAP_US2(obn->data)
2086 obn->data = ID_NEW_SET(obn->data, BKE_curve_copy(bmain, obn->data));
2093 if (dupflag & USER_DUP_FONT) {
2094 ID_NEW_REMAP_US2(obn->data)
2096 obn->data = ID_NEW_SET(obn->data, BKE_curve_copy(bmain, obn->data));
2103 if (dupflag & USER_DUP_MBALL) {
2104 ID_NEW_REMAP_US2(obn->data)
2106 obn->data = ID_NEW_SET(obn->data, BKE_mball_copy(bmain, obn->data));
2113 if (dupflag & USER_DUP_LAMP) {
2114 ID_NEW_REMAP_US2(obn->data)
2116 obn->data = ID_NEW_SET(obn->data, BKE_lamp_copy(bmain, obn->data));
2123 DAG_id_tag_update(&obn->id, OB_RECALC_DATA);
2125 BKE_pose_tag_recalc(bmain, obn->pose);
2126 if (dupflag & USER_DUP_ARM) {
2127 ID_NEW_REMAP_US2(obn->data)
2129 obn->data = ID_NEW_SET(obn->data, BKE_armature_copy(bmain, obn->data));
2130 BKE_pose_rebuild(obn, obn->data);
2138 ID_NEW_REMAP_US2(obn->data)
2140 obn->data = ID_NEW_SET(obn->data, BKE_lattice_copy(bmain, obn->data));
2148 ID_NEW_REMAP_US2(obn->data)
2150 obn->data = ID_NEW_SET(obn->data, BKE_camera_copy(bmain, obn->data));
2158 ID_NEW_REMAP_US2(obn->data)
2160 obn->data = ID_NEW_SET(obn->data, BKE_speaker_copy(bmain, obn->data));
2168 /* check if obdata is copied */
2170 Key *key = BKE_key_from_object(obn);
2172 if (dupflag & USER_DUP_ACT) {
2175 BKE_animdata_copy_id_action((ID *)obn->data, true);
2177 BKE_animdata_copy_id_action((ID *)key, true);
2180 /* Update the duplicated action in the action actuators */
2181 /* XXX TODO this code is all wrong! actact->act is user-refcounted (see readfile.c),
2182 * and what about other ID pointers of other BGE logic bricks,
2183 * and since this is object-level, why is it only ran if obdata was duplicated??? -mont29 */
2184 for (act = obn->actuators.first; act; act = act->next) {
2185 if (act->type == ACT_ACTION) {
2186 bActionActuator *actact = (bActionActuator *) act->data;
2187 if (ob->adt && actact->act == ob->adt->action) {
2188 actact->act = obn->adt->action;
2194 if (dupflag & USER_DUP_MAT) {
2195 matarar = give_matarar(obn);
2197 for (a = 0; a < obn->totcol; a++) {
2198 id = (ID *)(*matarar)[a];
2200 ID_NEW_REMAP_US((*matarar)[a])
2202 (*matarar)[a] = ID_NEW_SET((*matarar)[a], BKE_material_copy(bmain, (*matarar)[a]));
2213 #undef ID_NEW_REMAP_US
2214 #undef ID_NEW_REMAP_US2
2217 /* single object duplicate, if dupflag==0, fully linked, else it uses the flags given */
2218 /* leaves selection of base/object unaltered.
2219 * note: don't call this within a loop since clear_* funcs loop over the entire database.
2220 * note: caller must do DAG_relations_tag_update(bmain);
2221 * this is not done automatic since we may duplicate many objects in a batch */
2222 Base *ED_object_add_duplicate(Main *bmain, Scene *scene, SceneLayer *sl, Base *base, int dupflag)
2227 clear_sca_new_poins(); /* BGE logic */
2229 basen = object_add_duplicate_internal(bmain, scene, sl, base->object, dupflag);
2230 if (basen == NULL) {
2236 /* link own references to the newly duplicated data [#26816] */
2237 BKE_libblock_relink_to_newid(&ob->id);
2238 set_sca_new_poins_ob(ob);
2240 /* DAG_relations_tag_update(bmain); */ /* caller must do */
2243 ED_render_id_flush_update(bmain, ob->data);
2246 BKE_main_id_clear_newpoins(bmain);
2251 /* contextual operator dupli */
2252 static int duplicate_exec(bContext *C, wmOperator *op)
2254 Main *bmain = CTX_data_main(C);
2255 Scene *scene = CTX_data_scene(C);
2256 SceneLayer *sl = CTX_data_scene_layer(C);
2257 const bool linked = RNA_boolean_get(op->ptr, "linked");
2258 int dupflag = (linked) ? 0 : U.dupflag;
2260 clear_sca_new_poins(); /* BGE logic */
2262 CTX_DATA_BEGIN (C, Base *, base, selected_bases)
2264 Base *basen = object_add_duplicate_internal(bmain, scene, sl, base->object, dupflag);
2266 /* note that this is safe to do with this context iterator,
2267 * the list is made in advance */
2268 ED_object_base_select(base, BA_DESELECT);
2269 ED_object_base_select(basen, BA_SELECT);
2271 if (basen == NULL) {
2275 /* new object becomes active */
2276 if (BASACT_NEW == base)
2277 ED_object_base_activate(C, basen);
2279 if (basen->object->data) {
2280 DAG_id_tag_update(basen->object->data, 0);
2285 copy_object_set_idnew(C);
2287 BKE_main_id_clear_newpoins(bmain);
2289 DAG_relations_tag_update(bmain);
2290 /* TODO(sergey): Use proper flag for tagging here. */
2291 DAG_id_tag_update(&CTX_data_scene(C)->id, 0);
2293 WM_event_add_notifier(C, NC_SCENE | ND_OB_SELECT, scene);
2295 return OPERATOR_FINISHED;
2298 void OBJECT_OT_duplicate(wmOperatorType *ot)
2303 ot->name = "Duplicate Objects";
2304 ot->description = "Duplicate selected objects";
2305 ot->idname = "OBJECT_OT_duplicate";
2308 ot->exec = duplicate_exec;
2309 ot->poll = ED_operator_objectmode;
2312 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2314 /* to give to transform */
2315 RNA_def_boolean(ot->srna, "linked", 0, "Linked", "Duplicate object but not object data, linking to the original data");
2316 prop = RNA_def_enum(ot->srna, "mode", rna_enum_transform_mode_types, TFM_TRANSLATION, "Mode", "");
2317 RNA_def_property_flag(prop, PROP_HIDDEN);
2320 /* **************** add named object, for dragdrop ************* */
2322 static int add_named_exec(bContext *C, wmOperator *op)
2324 wmWindow *win = CTX_wm_window(C);
2325 const wmEvent *event = win ? win->eventstate : NULL;
2326 Main *bmain = CTX_data_main(C);
2327 Scene *scene = CTX_data_scene(C);
2328 SceneLayer *sl = CTX_data_scene_layer(C);
2331 const bool linked = RNA_boolean_get(op->ptr, "linked");
2332 int dupflag = (linked) ? 0 : U.dupflag;
2333 char name[MAX_ID_NAME - 2];
2335 /* find object, create fake base */
2336 RNA_string_get(op->ptr, "name", name);
2337 ob = (Object *)BKE_libblock_find_name(ID_OB, name);
2340 BKE_report(op->reports, RPT_ERROR, "Object not found");
2341 return OPERATOR_CANCELLED;
2345 clear_sca_new_poins(); /* BGE logic */
2347 basen = object_add_duplicate_internal(bmain, scene, sl, ob, dupflag);
2348 BKE_scene_object_base_flag_sync_from_object(basen);
2350 if (basen == NULL) {
2351 BKE_report(op->reports, RPT_ERROR, "Object could not be duplicated");
2352 return OPERATOR_CANCELLED;
2355 basen->object->restrictflag &= ~OB_RESTRICT_VIEW;
2358 ARegion *ar = CTX_wm_region(C);
2359 const int mval[2] = {event->x - ar->winrct.xmin,
2360 event->y - ar->winrct.ymin};
2361 ED_object_location_from_view(C, basen->object->loc);
2362 ED_view3d_cursor3d_position(C, basen->object->loc, mval);
2365 ED_object_base_select(basen, BA_SELECT);
2366 ED_object_base_activate(C, basen);
2368 copy_object_set_idnew(C);
2370 BKE_main_id_clear_newpoins(bmain);
2372 DAG_relations_tag_update(bmain);
2374 WM_event_add_notifier(C, NC_SCENE | ND_OB_SELECT | ND_OB_ACTIVE, scene);
2376 return OPERATOR_FINISHED;
2379 void OBJECT_OT_add_named(wmOperatorType *ot)
2382 ot->name = "Add Named Object";
2383 ot->description = "Add named object";
2384 ot->idname = "OBJECT_OT_add_named";
2387 ot->exec = add_named_exec;
2388 ot->poll = ED_operator_objectmode;
2391 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2393 RNA_def_boolean(ot->srna, "linked", 0, "Linked", "Duplicate object but not object data, linking to the original data");
2394 RNA_def_string(ot->srna, "name", NULL, MAX_ID_NAME - 2, "Name", "Object name to add");
2397 /**************************** Join *************************/
2399 static int join_poll(bContext *C)
2401 Object *ob = CTX_data_active_object(C);
2403 if (!ob || ID_IS_LINKED_DATABLOCK(ob)) return 0;
2405 if (ELEM(ob->type, OB_MESH, OB_CURVE, OB_SURF, OB_ARMATURE))
2406 return ED_operator_screenactive(C);
2411 static int join_exec(bContext *C, wmOperator *op)
2413 Scene *scene = CTX_data_scene(C);
2414 Object *ob = CTX_data_active_object(C);
2416 if (scene->obedit) {
2417 BKE_report(op->reports, RPT_ERROR, "This data does not support joining in edit mode");
2418 return OPERATOR_CANCELLED;
2420 else if (BKE_object_obdata_is_libdata(ob)) {
2421 BKE_report(op->reports, RPT_ERROR, "Cannot edit external libdata");
2422 return OPERATOR_CANCELLED;
2425 if (ob->type == OB_MESH)
2426 return join_mesh_exec(C, op);
2427 else if (ELEM(ob->type, OB_CURVE, OB_SURF))
2428 return join_curve_exec(C, op);
2429 else if (ob->type == OB_ARMATURE)
2430 return join_armature_exec(C, op);
2432 return OPERATOR_CANCELLED;
2435 void OBJECT_OT_join(wmOperatorType *ot)
2439 ot->description = "Join selected objects into active object";
2440 ot->idname = "OBJECT_OT_join";
2443 ot->exec = join_exec;
2444 ot->poll = join_poll;
2447 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2450 /**************************** Join as Shape Key*************************/
2452 static int join_shapes_poll(bContext *C)
2454 Object *ob = CTX_data_active_object(C);
2456 if (!ob || ID_IS_LINKED_DATABLOCK(ob)) return 0;
2458 /* only meshes supported at the moment */
2459 if (ob->type == OB_MESH)
2460 return ED_operator_screenactive(C);
2465 static int join_shapes_exec(bContext *C, wmOperator *op)
2467 Scene *scene = CTX_data_scene(C);
2468 Object *ob = CTX_data_active_object(C);
2470 if (scene->obedit) {
2471 BKE_report(op->reports, RPT_ERROR, "This data does not support joining in edit mode");
2472 return OPERATOR_CANCELLED;
2474 else if (BKE_object_obdata_is_libdata(ob)) {
2475 BKE_report(op->reports, RPT_ERROR, "Cannot edit external libdata");
2476 return OPERATOR_CANCELLED;
2479 if (ob->type == OB_MESH)
2480 return join_mesh_shapes_exec(C, op);
2482 return OPERATOR_CANCELLED;
2485 void OBJECT_OT_join_shapes(wmOperatorType *ot)
2488 ot->name = "Join as Shapes";
2489 ot->description = "Merge selected objects to shapes of active object";
2490 ot->idname = "OBJECT_OT_join_shapes";
2493 ot->exec = join_shapes_exec;
2494 ot->poll = join_shapes_poll;
2497 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;