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_curve_types.h"
38 #include "DNA_group_types.h"
39 #include "DNA_lamp_types.h"
40 #include "DNA_key_types.h"
41 #include "DNA_material_types.h"
42 #include "DNA_mesh_types.h"
43 #include "DNA_meta_types.h"
44 #include "DNA_object_fluidsim.h"
45 #include "DNA_object_force.h"
46 #include "DNA_scene_types.h"
47 #include "DNA_speaker_types.h"
48 #include "DNA_vfont_types.h"
49 #include "DNA_actuator_types.h"
51 #include "BLI_ghash.h"
52 #include "BLI_listbase.h"
54 #include "BLI_string.h"
55 #include "BLI_utildefines.h"
58 #include "BKE_animsys.h"
59 #include "BKE_armature.h"
60 #include "BKE_camera.h"
61 #include "BKE_constraint.h"
62 #include "BKE_context.h"
63 #include "BKE_curve.h"
64 #include "BKE_depsgraph.h"
65 #include "BKE_DerivedMesh.h"
66 #include "BKE_displist.h"
67 #include "BKE_effect.h"
68 #include "BKE_group.h"
70 #include "BKE_lattice.h"
71 #include "BKE_library.h"
74 #include "BKE_material.h"
75 #include "BKE_mball.h"
77 #include "BKE_modifier.h"
79 #include "BKE_object.h"
80 #include "BKE_particle.h"
81 #include "BKE_report.h"
83 #include "BKE_scene.h"
84 #include "BKE_speaker.h"
85 #include "BKE_texture.h"
87 #include "RNA_access.h"
88 #include "RNA_define.h"
89 #include "RNA_enum_types.h"
94 #include "ED_armature.h"
99 #include "ED_object.h"
100 #include "ED_render.h"
101 #include "ED_screen.h"
102 #include "ED_transform.h"
103 #include "ED_view3d.h"
105 #include "UI_interface.h"
106 #include "UI_resources.h"
108 #include "object_intern.h"
110 /* this is an exact copy of the define in rna_lamp.c
111 * kept here because of linking order */
112 EnumPropertyItem lamp_type_items[] = {
113 {LA_LOCAL, "POINT", 0, "Point", "Omnidirectional point light source"},
114 {LA_SUN, "SUN", 0, "Sun", "Constant direction parallel ray light source"},
115 {LA_SPOT, "SPOT", 0, "Spot", "Directional cone light source"},
116 {LA_HEMI, "HEMI", 0, "Hemi", "180 degree constant light source"},
117 {LA_AREA, "AREA", 0, "Area", "Directional area light source"},
118 {0, NULL, 0, NULL, NULL}
121 /************************** Exported *****************************/
123 void ED_object_location_from_view(bContext *C, float *loc)
125 View3D *v3d = CTX_wm_view3d(C);
126 Scene *scene = CTX_data_scene(C);
129 cursor = give_cursor(scene, v3d);
131 copy_v3_v3(loc, cursor);
134 void ED_object_rotation_from_view(bContext *C, float *rot)
136 RegionView3D *rv3d = CTX_wm_region_view3d(C);
139 copy_qt_qt(quat, rv3d->viewquat);
141 quat_to_eul(rot, quat);
148 void ED_object_base_init_transform(bContext *C, Base *base, float *loc, float *rot)
150 Object *ob = base->object;
151 Scene *scene = CTX_data_scene(C);
156 copy_v3_v3(ob->loc, loc);
159 copy_v3_v3(ob->rot, rot);
161 BKE_object_where_is_calc(scene, ob);
164 /* uses context to figure out transform for primitive */
165 /* returns standard diameter */
166 float ED_object_new_primitive_matrix(bContext *C, Object *obedit, float *loc, float *rot, float primmat[][4])
168 View3D *v3d = CTX_wm_view3d(C);
169 float mat[3][3], rmat[3][3], cmat[3][3], imat[3][3];
173 eul_to_mat3(rmat, rot);
176 /* inverse transform for initial rotation and object */
177 copy_m3_m4(mat, obedit->obmat);
178 mul_m3_m3m3(cmat, rmat, mat);
179 invert_m3_m3(imat, cmat);
180 copy_m4_m3(primmat, imat);
183 copy_v3_v3(primmat[3], loc);
184 sub_v3_v3v3(primmat[3], primmat[3], obedit->obmat[3]);
185 invert_m3_m3(imat, mat);
186 mul_m3_v3(imat, primmat[3]);
188 if (v3d) return v3d->grid;
192 /********************* Add Object Operator ********************/
194 void view_align_update(struct Main *UNUSED(main), struct Scene *UNUSED(scene), struct PointerRNA *ptr)
196 RNA_struct_idprops_unset(ptr, "rotation");
199 void ED_object_add_generic_props(wmOperatorType *ot, int do_editmode)
203 /* note: this property gets hidden for add-camera operator */
204 prop = RNA_def_boolean(ot->srna, "view_align", 0, "Align to View", "Align the new object to the view");
205 RNA_def_property_update_runtime(prop, view_align_update);
208 prop = RNA_def_boolean(ot->srna, "enter_editmode", 0, "Enter Editmode",
209 "Enter editmode when adding this object");
210 RNA_def_property_flag(prop, PROP_HIDDEN | PROP_SKIP_SAVE);
213 prop = RNA_def_float_vector_xyz(ot->srna, "location", 3, NULL, -FLT_MAX, FLT_MAX, "Location",
214 "Location for the newly added object", -FLT_MAX, FLT_MAX);
215 RNA_def_property_flag(prop, PROP_SKIP_SAVE);
216 prop = RNA_def_float_rotation(ot->srna, "rotation", 3, NULL, -FLT_MAX, FLT_MAX, "Rotation",
217 "Rotation for the newly added object", (float)-M_PI * 2.0f, (float)M_PI * 2.0f);
218 RNA_def_property_flag(prop, PROP_SKIP_SAVE);
220 prop = RNA_def_boolean_layer_member(ot->srna, "layers", 20, NULL, "Layer", "");
221 RNA_def_property_flag(prop, PROP_HIDDEN | PROP_SKIP_SAVE);
224 static void object_add_generic_invoke_options(bContext *C, wmOperator *op)
226 if (RNA_struct_find_property(op->ptr, "enter_editmode")) /* optional */
227 if (!RNA_struct_property_is_set(op->ptr, "enter_editmode"))
228 RNA_boolean_set(op->ptr, "enter_editmode", U.flag & USER_ADD_EDITMODE);
230 if (!RNA_struct_property_is_set(op->ptr, "location")) {
233 ED_object_location_from_view(C, loc);
234 RNA_float_set_array(op->ptr, "location", loc);
237 if (!RNA_struct_property_is_set(op->ptr, "layers")) {
238 View3D *v3d = CTX_wm_view3d(C);
239 Scene *scene = CTX_data_scene(C);
240 int a, values[20], layer;
243 layer = (v3d->scenelock && !v3d->localvd) ? scene->layact : v3d->layact;
246 layer = scene->layact;
249 for (a = 0; a < 20; a++) {
250 values[a] = (layer & (1 << a));
253 RNA_boolean_set_array(op->ptr, "layers", values);
257 int ED_object_add_generic_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
259 object_add_generic_invoke_options(C, op);
260 return op->type->exec(C, op);
263 int ED_object_add_generic_get_opts(bContext *C, wmOperator *op, float *loc,
264 float *rot, int *enter_editmode, unsigned int *layer, int *is_view_aligned)
266 View3D *v3d = CTX_wm_view3d(C);
267 int a, layer_values[20];
270 *enter_editmode = FALSE;
271 if (RNA_struct_find_property(op->ptr, "enter_editmode") && RNA_boolean_get(op->ptr, "enter_editmode")) {
272 *enter_editmode = TRUE;
275 if (RNA_struct_property_is_set(op->ptr, "layers")) {
276 RNA_boolean_get_array(op->ptr, "layers", layer_values);
278 for (a = 0; a < 20; a++) {
286 /* not set, use the scenes layers */
287 Scene *scene = CTX_data_scene(C);
288 *layer = scene->layact;
291 /* in local view we additionally add local view layers,
292 * not part of operator properties */
293 if (v3d && v3d->localvd)
296 if (RNA_struct_property_is_set(op->ptr, "rotation"))
298 else if (RNA_struct_property_is_set(op->ptr, "view_align"))
299 view_align = RNA_boolean_get(op->ptr, "view_align");
301 view_align = U.flag & USER_ADD_VIEWALIGNED;
302 RNA_boolean_set(op->ptr, "view_align", view_align);
306 ED_object_rotation_from_view(C, rot);
307 RNA_float_set_array(op->ptr, "rotation", rot);
310 RNA_float_get_array(op->ptr, "rotation", rot);
313 *is_view_aligned = view_align;
315 RNA_float_get_array(op->ptr, "location", loc);
318 BKE_report(op->reports, RPT_ERROR, "Property 'layer' has no values set");
325 /* for object add primitive operators */
326 /* do not call undo push in this function (users of this function have to) */
327 Object *ED_object_add_type(bContext *C, int type, float *loc, float *rot,
328 int enter_editmode, unsigned int layer)
330 Main *bmain = CTX_data_main(C);
331 Scene *scene = CTX_data_scene(C);
334 /* for as long scene has editmode... */
335 if (CTX_data_edit_object(C))
336 ED_object_exit_editmode(C, EM_FREEDATA | EM_FREEUNDO | EM_WAITCURSOR | EM_DO_UNDO); /* freedata, and undo */
338 /* deselects all, sets scene->basact */
339 ob = BKE_object_add(scene, type);
340 BASACT->lay = ob->lay = layer;
341 /* editor level activate, notifiers */
342 ED_base_object_activate(C, BASACT);
344 /* more editor stuff */
345 ED_object_base_init_transform(C, BASACT, loc, rot);
347 DAG_id_type_tag(bmain, ID_OB);
348 DAG_scene_sort(bmain, scene);
350 ED_render_id_flush_update(bmain, ob->data);
354 ED_object_enter_editmode(C, EM_IGNORE_LAYER);
356 WM_event_add_notifier(C, NC_SCENE | ND_LAYER_CONTENT, scene);
361 /* for object add operator */
362 static int object_add_exec(bContext *C, wmOperator *op)
366 float loc[3], rot[3];
368 if (!ED_object_add_generic_get_opts(C, op, loc, rot, &enter_editmode, &layer, NULL))
369 return OPERATOR_CANCELLED;
371 ED_object_add_type(C, RNA_enum_get(op->ptr, "type"), loc, rot, enter_editmode, layer);
373 return OPERATOR_FINISHED;
376 void OBJECT_OT_add(wmOperatorType *ot)
379 ot->name = "Add Object";
380 ot->description = "Add an object to the scene";
381 ot->idname = "OBJECT_OT_add";
384 ot->invoke = ED_object_add_generic_invoke;
385 ot->exec = object_add_exec;
387 ot->poll = ED_operator_objectmode;
390 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
392 RNA_def_enum(ot->srna, "type", object_type_items, 0, "Type", "");
394 ED_object_add_generic_props(ot, TRUE);
397 /********************* Add Effector Operator ********************/
398 /* copy from rna_object_force.c*/
399 static EnumPropertyItem field_type_items[] = {
400 {PFIELD_FORCE, "FORCE", ICON_FORCE_FORCE, "Force", ""},
401 {PFIELD_WIND, "WIND", ICON_FORCE_WIND, "Wind", ""},
402 {PFIELD_VORTEX, "VORTEX", ICON_FORCE_VORTEX, "Vortex", ""},
403 {PFIELD_MAGNET, "MAGNET", ICON_FORCE_MAGNETIC, "Magnetic", ""},
404 {PFIELD_HARMONIC, "HARMONIC", ICON_FORCE_HARMONIC, "Harmonic", ""},
405 {PFIELD_CHARGE, "CHARGE", ICON_FORCE_CHARGE, "Charge", ""},
406 {PFIELD_LENNARDJ, "LENNARDJ", ICON_FORCE_LENNARDJONES, "Lennard-Jones", ""},
407 {PFIELD_TEXTURE, "TEXTURE", ICON_FORCE_TEXTURE, "Texture", ""},
408 {PFIELD_GUIDE, "GUIDE", ICON_FORCE_CURVE, "Curve Guide", ""},
409 {PFIELD_BOID, "BOID", ICON_FORCE_BOID, "Boid", ""},
410 {PFIELD_TURBULENCE, "TURBULENCE", ICON_FORCE_TURBULENCE, "Turbulence", ""},
411 {PFIELD_DRAG, "DRAG", ICON_FORCE_DRAG, "Drag", ""},
412 {0, NULL, 0, NULL, NULL}};
414 /* for effector add primitive operators */
415 static Object *effector_add_type(bContext *C, wmOperator *op, int type)
420 float loc[3], rot[3];
423 object_add_generic_invoke_options(C, op);
425 if (!ED_object_add_generic_get_opts(C, op, loc, rot, &enter_editmode, &layer, NULL))
428 if (type == PFIELD_GUIDE) {
429 ob = ED_object_add_type(C, OB_CURVE, loc, rot, FALSE, layer);
430 rename_id(&ob->id, "CurveGuide");
432 ((Curve *)ob->data)->flag |= CU_PATH | CU_3D;
433 ED_object_enter_editmode(C, 0);
434 ED_object_new_primitive_matrix(C, ob, loc, rot, mat);
435 BLI_addtail(object_editcurve_get(ob), add_nurbs_primitive(C, mat, CU_NURBS | CU_PRIM_PATH, 1));
438 ED_object_exit_editmode(C, EM_FREEDATA);
441 ob = ED_object_add_type(C, OB_EMPTY, loc, rot, FALSE, layer);
442 rename_id(&ob->id, "Field");
447 ob->empty_drawtype = OB_SINGLE_ARROW;
452 ob->pd = object_add_collision_fields(type);
454 DAG_scene_sort(CTX_data_main(C), CTX_data_scene(C));
459 /* for object add operator */
460 static int effector_add_exec(bContext *C, wmOperator *op)
462 if (effector_add_type(C, op, RNA_enum_get(op->ptr, "type")) == NULL)
463 return OPERATOR_CANCELLED;
465 return OPERATOR_FINISHED;
468 void OBJECT_OT_effector_add(wmOperatorType *ot)
471 ot->name = "Add Effector";
472 ot->description = "Add an empty object with a physics effector to the scene";
473 ot->idname = "OBJECT_OT_effector_add";
476 ot->invoke = WM_menu_invoke;
477 ot->exec = effector_add_exec;
479 ot->poll = ED_operator_objectmode;
482 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
484 ot->prop = RNA_def_enum(ot->srna, "type", field_type_items, 0, "Type", "");
486 ED_object_add_generic_props(ot, TRUE);
489 /* ***************** Add Camera *************** */
491 static int object_camera_add_exec(bContext *C, wmOperator *op)
493 View3D *v3d = CTX_wm_view3d(C);
494 Scene *scene = CTX_data_scene(C);
498 float loc[3], rot[3];
500 /* force view align for cameras */
501 RNA_boolean_set(op->ptr, "view_align", TRUE);
503 object_add_generic_invoke_options(C, op);
505 if (!ED_object_add_generic_get_opts(C, op, loc, rot, &enter_editmode, &layer, NULL))
506 return OPERATOR_CANCELLED;
508 ob = ED_object_add_type(C, OB_CAMERA, loc, rot, FALSE, layer);
511 if (v3d->camera == NULL)
513 if (v3d->scenelock && scene->camera == NULL) {
518 return OPERATOR_FINISHED;
521 void OBJECT_OT_camera_add(wmOperatorType *ot)
526 ot->name = "Add Camera";
527 ot->description = "Add a camera object to the scene";
528 ot->idname = "OBJECT_OT_camera_add";
531 ot->exec = object_camera_add_exec;
532 ot->poll = ED_operator_objectmode;
535 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
537 ED_object_add_generic_props(ot, TRUE);
539 /* hide this for cameras, default */
540 prop = RNA_struct_type_find_property(ot->srna, "view_align");
541 RNA_def_property_flag(prop, PROP_HIDDEN);
546 /* ***************** add primitives *************** */
547 static int object_metaball_add_exec(bContext *C, wmOperator *op)
549 Object *obedit = CTX_data_edit_object(C);
550 /*MetaElem *elem;*/ /*UNUSED*/
554 float loc[3], rot[3];
557 object_add_generic_invoke_options(C, op); // XXX these props don't get set right when only exec() is called
559 if (!ED_object_add_generic_get_opts(C, op, loc, rot, &enter_editmode, &layer, NULL))
560 return OPERATOR_CANCELLED;
562 if (obedit == NULL || obedit->type != OB_MBALL) {
563 obedit = ED_object_add_type(C, OB_MBALL, loc, rot, TRUE, layer);
566 else DAG_id_tag_update(&obedit->id, OB_RECALC_DATA);
568 ED_object_new_primitive_matrix(C, obedit, loc, rot, mat);
570 /* elem= (MetaElem *) */ add_metaball_primitive(C, mat, RNA_enum_get(op->ptr, "type"), newob);
573 if (newob && !enter_editmode) {
574 ED_object_exit_editmode(C, EM_FREEDATA);
577 WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, obedit);
579 return OPERATOR_FINISHED;
582 static int object_metaball_add_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
584 Object *obedit = CTX_data_edit_object(C);
588 object_add_generic_invoke_options(C, op);
590 pup = uiPupMenuBegin(C, op->type->name, ICON_NONE);
591 layout = uiPupMenuLayout(pup);
592 if (!obedit || obedit->type == OB_MBALL)
593 uiItemsEnumO(layout, op->type->idname, "type");
595 uiItemsEnumO(layout, "OBJECT_OT_metaball_add", "type");
596 uiPupMenuEnd(C, pup);
598 return OPERATOR_CANCELLED;
601 void OBJECT_OT_metaball_add(wmOperatorType *ot)
604 ot->name = "Add Metaball";
605 ot->description = "Add an metaball object to the scene";
606 ot->idname = "OBJECT_OT_metaball_add";
609 ot->invoke = object_metaball_add_invoke;
610 ot->exec = object_metaball_add_exec;
611 ot->poll = ED_operator_scene_editable;
614 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
616 RNA_def_enum(ot->srna, "type", metaelem_type_items, 0, "Primitive", "");
617 ED_object_add_generic_props(ot, TRUE);
620 static int object_add_text_exec(bContext *C, wmOperator *op)
622 Object *obedit = CTX_data_edit_object(C);
625 float loc[3], rot[3];
627 object_add_generic_invoke_options(C, op); // XXX these props don't get set right when only exec() is called
628 if (!ED_object_add_generic_get_opts(C, op, loc, rot, &enter_editmode, &layer, NULL))
629 return OPERATOR_CANCELLED;
631 if (obedit && obedit->type == OB_FONT)
632 return OPERATOR_CANCELLED;
634 obedit = ED_object_add_type(C, OB_FONT, loc, rot, enter_editmode, layer);
636 WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, obedit);
638 return OPERATOR_FINISHED;
641 void OBJECT_OT_text_add(wmOperatorType *ot)
644 ot->name = "Add Text";
645 ot->description = "Add a text object to the scene";
646 ot->idname = "OBJECT_OT_text_add";
649 ot->invoke = ED_object_add_generic_invoke;
650 ot->exec = object_add_text_exec;
651 ot->poll = ED_operator_objectmode;
654 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
655 ED_object_add_generic_props(ot, TRUE);
658 static int object_armature_add_exec(bContext *C, wmOperator *op)
660 Object *obedit = CTX_data_edit_object(C);
661 View3D *v3d = CTX_wm_view3d(C);
662 RegionView3D *rv3d = CTX_wm_region_view3d(C);
666 float loc[3], rot[3];
668 object_add_generic_invoke_options(C, op); // XXX these props don't get set right when only exec() is called
669 if (!ED_object_add_generic_get_opts(C, op, loc, rot, &enter_editmode, &layer, NULL))
670 return OPERATOR_CANCELLED;
672 if ((obedit == NULL) || (obedit->type != OB_ARMATURE)) {
673 obedit = ED_object_add_type(C, OB_ARMATURE, loc, rot, TRUE, layer);
674 ED_object_enter_editmode(C, 0);
677 else DAG_id_tag_update(&obedit->id, OB_RECALC_DATA);
679 if (obedit == NULL) {
680 BKE_report(op->reports, RPT_ERROR, "Cannot create editmode armature");
681 return OPERATOR_CANCELLED;
684 /* v3d and rv3d are allowed to be NULL */
685 add_primitive_bone(CTX_data_scene(C), v3d, rv3d);
688 if (newob && !enter_editmode)
689 ED_object_exit_editmode(C, EM_FREEDATA);
691 WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, obedit);
693 return OPERATOR_FINISHED;
696 void OBJECT_OT_armature_add(wmOperatorType *ot)
699 ot->name = "Add Armature";
700 ot->description = "Add an armature object to the scene";
701 ot->idname = "OBJECT_OT_armature_add";
704 ot->invoke = ED_object_add_generic_invoke;
705 ot->exec = object_armature_add_exec;
706 ot->poll = ED_operator_objectmode;
709 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
710 ED_object_add_generic_props(ot, TRUE);
713 static const char *get_lamp_defname(int type)
716 case LA_LOCAL: return "Point";
717 case LA_SUN: return "Sun";
718 case LA_SPOT: return "Spot";
719 case LA_HEMI: return "Hemi";
720 case LA_AREA: return "Area";
726 static int object_lamp_add_exec(bContext *C, wmOperator *op)
728 Scene *scene = CTX_data_scene(C);
731 int type = RNA_enum_get(op->ptr, "type");
734 float loc[3], rot[3];
736 object_add_generic_invoke_options(C, op);
737 if (!ED_object_add_generic_get_opts(C, op, loc, rot, &enter_editmode, &layer, NULL))
738 return OPERATOR_CANCELLED;
740 ob = ED_object_add_type(C, OB_LAMP, loc, rot, FALSE, layer);
741 la = (Lamp *)ob->data;
744 rename_id(&ob->id, get_lamp_defname(type));
745 rename_id(&la->id, get_lamp_defname(type));
747 if (BKE_scene_use_new_shading_nodes(scene)) {
748 ED_node_shader_default(scene, &la->id);
749 la->use_nodes = TRUE;
752 return OPERATOR_FINISHED;
755 void OBJECT_OT_lamp_add(wmOperatorType *ot)
758 ot->name = "Add Lamp";
759 ot->description = "Add a lamp object to the scene";
760 ot->idname = "OBJECT_OT_lamp_add";
763 ot->invoke = WM_menu_invoke;
764 ot->exec = object_lamp_add_exec;
765 ot->poll = ED_operator_objectmode;
768 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
771 ot->prop = RNA_def_enum(ot->srna, "type", lamp_type_items, 0, "Type", "");
773 ED_object_add_generic_props(ot, FALSE);
776 static int group_instance_add_exec(bContext *C, wmOperator *op)
778 Group *group = BLI_findlink(&CTX_data_main(C)->group, RNA_enum_get(op->ptr, "group"));
782 float loc[3], rot[3];
784 object_add_generic_invoke_options(C, op);
785 if (!ED_object_add_generic_get_opts(C, op, loc, rot, &enter_editmode, &layer, NULL))
786 return OPERATOR_CANCELLED;
789 Main *bmain = CTX_data_main(C);
790 Scene *scene = CTX_data_scene(C);
791 Object *ob = ED_object_add_type(C, OB_EMPTY, loc, rot, FALSE, layer);
792 rename_id(&ob->id, group->id.name + 2);
793 ob->dup_group = group;
794 ob->transflag |= OB_DUPLIGROUP;
795 id_lib_extern(&group->id);
797 /* works without this except if you try render right after, see: 22027 */
798 DAG_scene_sort(bmain, scene);
800 WM_event_add_notifier(C, NC_SCENE | ND_OB_ACTIVE, CTX_data_scene(C));
802 return OPERATOR_FINISHED;
805 return OPERATOR_CANCELLED;
808 static int object_speaker_add_exec(bContext *C, wmOperator *op)
813 float loc[3], rot[3];
814 Scene *scene = CTX_data_scene(C);
816 object_add_generic_invoke_options(C, op);
817 if (!ED_object_add_generic_get_opts(C, op, loc, rot, &enter_editmode, &layer, NULL))
818 return OPERATOR_CANCELLED;
820 ob = ED_object_add_type(C, OB_SPEAKER, loc, rot, FALSE, layer);
822 /* to make it easier to start using this immediately in NLA, a default sound clip is created
823 * ready to be moved around to retime the sound and/or make new sound clips
826 /* create new data for NLA hierarchy */
827 AnimData *adt = BKE_id_add_animdata(&ob->id);
828 NlaTrack *nlt = add_nlatrack(adt, NULL);
829 NlaStrip *strip = add_nla_soundstrip(CTX_data_scene(C), ob->data);
831 strip->end += strip->start;
834 BKE_nlatrack_add_strip(nlt, strip);
836 /* auto-name the strip, and give the track an interesting name */
837 strcpy(nlt->name, "SoundTrack");
838 BKE_nlastrip_validate_name(adt, strip);
840 WM_event_add_notifier(C, NC_ANIMATION | ND_NLA | NA_EDITED, NULL);
843 return OPERATOR_FINISHED;
846 void OBJECT_OT_speaker_add(wmOperatorType *ot)
849 ot->name = "Add Speaker";
850 ot->description = "Add a speaker object to the scene";
851 ot->idname = "OBJECT_OT_speaker_add";
854 ot->exec = object_speaker_add_exec;
855 ot->poll = ED_operator_objectmode;
858 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
860 ED_object_add_generic_props(ot, TRUE);
863 /* only used as menu */
864 void OBJECT_OT_group_instance_add(wmOperatorType *ot)
869 ot->name = "Add Group Instance";
870 ot->description = "Add a dupligroup instance";
871 ot->idname = "OBJECT_OT_group_instance_add";
874 ot->invoke = WM_enum_search_invoke;
875 ot->exec = group_instance_add_exec;
877 ot->poll = ED_operator_objectmode;
880 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
883 prop = RNA_def_enum(ot->srna, "group", DummyRNA_NULL_items, 0, "Group", "");
884 RNA_def_enum_funcs(prop, RNA_group_itemf);
886 ED_object_add_generic_props(ot, FALSE);
889 /**************************** Delete Object *************************/
891 /* remove base from a specific scene */
892 /* note: now unlinks constraints as well */
893 void ED_base_object_free_and_unlink(Main *bmain, Scene *scene, Base *base)
895 DAG_id_type_tag(bmain, ID_OB);
896 BLI_remlink(&scene->base, base);
897 BKE_libblock_free_us(&bmain->object, base->object);
898 if (scene->basact == base) scene->basact = NULL;
902 static int object_delete_exec(bContext *C, wmOperator *op)
904 Main *bmain = CTX_data_main(C);
905 Scene *scene = CTX_data_scene(C);
906 const short use_global = RNA_boolean_get(op->ptr, "use_global");
907 /* int is_lamp = FALSE; */ /* UNUSED */
909 if (CTX_data_edit_object(C))
910 return OPERATOR_CANCELLED;
912 CTX_DATA_BEGIN (C, Base *, base, selected_bases)
915 /* if (base->object->type==OB_LAMP) is_lamp = TRUE; */
917 /* deselect object -- it could be used in other scenes */
918 base->object->flag &= ~SELECT;
920 /* remove from current scene only */
921 ED_base_object_free_and_unlink(bmain, scene, base);
927 for (scene_iter = bmain->scene.first; scene_iter; scene_iter = scene_iter->id.next) {
928 if (scene_iter != scene && !(scene_iter->id.lib)) {
929 base_other = BKE_scene_base_find(scene_iter, base->object);
931 ED_base_object_free_and_unlink(bmain, scene_iter, base_other);
941 DAG_scene_sort(bmain, scene);
942 DAG_ids_flush_update(bmain, 0);
944 WM_event_add_notifier(C, NC_SCENE | ND_OB_ACTIVE, scene);
945 WM_event_add_notifier(C, NC_SCENE | ND_LAYER_CONTENT, scene);
947 return OPERATOR_FINISHED;
950 void OBJECT_OT_delete(wmOperatorType *ot)
954 ot->description = "Delete selected objects";
955 ot->idname = "OBJECT_OT_delete";
958 ot->invoke = WM_operator_confirm;
959 ot->exec = object_delete_exec;
960 ot->poll = ED_operator_objectmode;
963 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
965 RNA_def_boolean(ot->srna, "use_global", 0, "Delete Globally", "Remove object from all scenes");
968 /**************************** Copy Utilities ******************************/
970 /* after copying objects, copied data should get new pointers */
971 static void copy_object_set_idnew(bContext *C, int dupflag)
973 Main *bmain = CTX_data_main(C);
978 /* XXX check object pointers */
979 CTX_DATA_BEGIN (C, Object *, ob, selected_editable_objects)
981 BKE_object_relink(ob);
986 if (dupflag & USER_DUP_MAT) {
987 mao = bmain->mat.first;
991 ma = (Material *)mao->id.newid;
993 if (dupflag & USER_DUP_TEX) {
994 for (a = 0; a < MAX_MTEX; a++) {
996 id = (ID *)ma->mtex[a]->tex;
998 ID_NEW_US(ma->mtex[a]->tex)
999 else ma->mtex[a]->tex = BKE_texture_copy(ma->mtex[a]->tex);
1005 #if 0 // XXX old animation system
1009 else ma->ipo = copy_ipo(ma->ipo);
1012 #endif // XXX old animation system
1018 #if 0 // XXX old animation system
1020 if (dupflag & USER_DUP_IPO) {
1021 Lamp *la = bmain->lamp.first;
1024 Lamp *lan = (Lamp *)la->id.newid;
1025 id = (ID *)lan->ipo;
1028 else lan->ipo = copy_ipo(lan->ipo);
1037 ipo = bmain->ipo.first;
1039 if (ipo->id.lib == NULL && ipo->id.newid) {
1040 Ipo *ipon = (Ipo *)ipo->id.newid;
1042 for (icu = ipon->curve.first; icu; icu = icu->next) {
1044 ID_NEW(icu->driver->ob);
1050 #endif // XXX old animation system
1052 set_sca_new_poins();
1054 clear_id_newpoins();
1057 /********************* Make Duplicates Real ************************/
1059 static void make_object_duplilist_real(bContext *C, Scene *scene, Base *base,
1060 const short use_base_parent,
1061 const short use_hierarchy)
1065 GHash *dupli_gh = NULL, *parent_gh = NULL;
1067 if (!(base->object->transflag & OB_DUPLI))
1070 lb = object_duplilist(scene, base->object);
1072 if (use_hierarchy || use_base_parent) {
1073 dupli_gh = BLI_ghash_ptr_new("make_object_duplilist_real dupli_gh");
1074 parent_gh = BLI_ghash_pair_new("make_object_duplilist_real parent_gh");
1077 for (dob = lb->first; dob; dob = dob->next) {
1079 Object *ob = BKE_object_copy(dob->ob);
1080 /* font duplis can have a totcol without material, we get them from parent
1081 * should be implemented better...
1083 if (ob->mat == NULL) ob->totcol = 0;
1085 basen = MEM_dupallocN(base);
1086 basen->flag &= ~(OB_FROMDUPLI | OB_FROMGROUP);
1087 ob->flag = basen->flag;
1088 basen->lay = base->lay;
1089 BLI_addhead(&scene->base, basen); /* addhead: othwise eternal loop */
1092 /* make sure apply works */
1093 BKE_free_animdata(&ob->id);
1097 ob->constraints.first = ob->constraints.last = NULL;
1098 ob->disp.first = ob->disp.last = NULL;
1099 ob->transflag &= ~OB_DUPLI;
1100 ob->lay = base->lay;
1102 copy_m4_m4(ob->obmat, dob->mat);
1103 BKE_object_apply_mat4(ob, ob->obmat, FALSE, FALSE);
1106 BLI_ghash_insert(dupli_gh, dob, ob);
1108 BLI_ghash_insert(parent_gh, BLI_ghashutil_pairalloc(dob->ob, SET_INT_IN_POINTER(dob->index)), ob);
1111 if (use_hierarchy) {
1112 for (dob = lb->first; dob; dob = dob->next) {
1113 /* original parents */
1114 Object *ob_src = dob->ob;
1115 Object *ob_src_par = ob_src->parent;
1117 Object *ob_dst = BLI_ghash_lookup(dupli_gh, dob);
1118 Object *ob_dst_par = NULL;
1120 /* find parent that was also made real */
1122 GHashPair *pair = BLI_ghashutil_pairalloc(ob_src_par, SET_INT_IN_POINTER(dob->index));
1123 ob_dst_par = BLI_ghash_lookup(parent_gh, pair);
1124 BLI_ghashutil_pairfree(pair);
1128 /* allow for all possible parent types */
1129 ob_dst->partype = ob_src->partype;
1130 BLI_strncpy(ob_dst->parsubstr, ob_src->parsubstr, sizeof(ob_dst->parsubstr));
1131 ob_dst->par1 = ob_src->par1;
1132 ob_dst->par2 = ob_src->par2;
1133 ob_dst->par3 = ob_src->par3;
1135 copy_m4_m4(ob_dst->parentinv, ob_src->parentinv);
1137 ob_dst->parent = ob_dst_par;
1139 else if (use_base_parent) {
1140 ob_dst->parent = base->object;
1141 ob_dst->partype = PAROBJECT;
1144 if (ob_dst->parent) {
1145 invert_m4_m4(ob_dst->parentinv, dob->mat);
1147 /* note, this may be the parent of other objects, but it should
1148 * still work out ok */
1149 BKE_object_apply_mat4(ob_dst, dob->mat, FALSE, TRUE);
1151 /* to set ob_dst->orig and in case theres any other discrepicies */
1152 DAG_id_tag_update(&ob_dst->id, OB_RECALC_OB);
1156 else if (use_base_parent) {
1157 /* since we are ignoring the internal hierarchy - parent all to the
1159 for (dob = lb->first; dob; dob = dob->next) {
1160 /* original parents */
1161 Object *ob_dst = BLI_ghash_lookup(dupli_gh, dob);
1163 ob_dst->parent = base->object;
1164 ob_dst->partype = PAROBJECT;
1166 /* similer to the code above, see comments */
1167 invert_m4_m4(ob_dst->parentinv, dob->mat);
1168 BKE_object_apply_mat4(ob_dst, dob->mat, FALSE, TRUE);
1169 DAG_id_tag_update(&ob_dst->id, OB_RECALC_OB);
1176 BLI_ghash_free(dupli_gh, NULL, NULL);
1178 BLI_ghash_free(parent_gh, BLI_ghashutil_pairfree, NULL);
1180 copy_object_set_idnew(C, 0);
1182 free_object_duplilist(lb);
1184 base->object->transflag &= ~OB_DUPLI;
1187 static int object_duplicates_make_real_exec(bContext *C, wmOperator *op)
1189 Main *bmain = CTX_data_main(C);
1190 Scene *scene = CTX_data_scene(C);
1192 const short use_base_parent = RNA_boolean_get(op->ptr, "use_base_parent");
1193 const short use_hierarchy = RNA_boolean_get(op->ptr, "use_hierarchy");
1195 clear_id_newpoins();
1197 CTX_DATA_BEGIN (C, Base *, base, selected_editable_bases)
1199 make_object_duplilist_real(C, scene, base, use_base_parent, use_hierarchy);
1201 /* dependencies were changed */
1202 WM_event_add_notifier(C, NC_OBJECT | ND_PARENT, base->object);
1206 DAG_scene_sort(bmain, scene);
1207 DAG_ids_flush_update(bmain, 0);
1208 WM_event_add_notifier(C, NC_SCENE, scene);
1209 WM_main_add_notifier(NC_OBJECT | ND_DRAW, NULL);
1211 return OPERATOR_FINISHED;
1214 void OBJECT_OT_duplicates_make_real(wmOperatorType *ot)
1218 ot->name = "Make Duplicates Real";
1219 ot->description = "Make dupli objects attached to this object real";
1220 ot->idname = "OBJECT_OT_duplicates_make_real";
1223 ot->exec = object_duplicates_make_real_exec;
1225 ot->poll = ED_operator_objectmode;
1228 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1230 RNA_def_boolean(ot->srna, "use_base_parent", 0, "Parent", "Parent newly created objects to the original duplicator");
1231 RNA_def_boolean(ot->srna, "use_hierarchy", 0, "Keep Hierarchy", "Maintain parent child relationships");
1234 /**************************** Convert **************************/
1236 static EnumPropertyItem convert_target_items[] = {
1237 {OB_CURVE, "CURVE", ICON_OUTLINER_OB_CURVE, "Curve from Mesh/Text", ""},
1238 {OB_MESH, "MESH", ICON_OUTLINER_OB_MESH, "Mesh from Curve/Meta/Surf/Text", ""},
1239 {0, NULL, 0, NULL, NULL}
1242 static void curvetomesh(Scene *scene, Object *ob)
1244 if (ob->disp.first == NULL)
1245 BKE_displist_make_curveTypes(scene, ob, 0); /* force creation */
1247 BKE_mesh_from_nurbs(ob); /* also does users */
1249 if (ob->type == OB_MESH)
1250 BKE_object_free_modifiers(ob);
1253 static int convert_poll(bContext *C)
1255 Object *obact = CTX_data_active_object(C);
1256 Scene *scene = CTX_data_scene(C);
1258 return (!scene->id.lib && obact && scene->obedit != obact && (obact->flag & SELECT) && !(obact->id.lib));
1261 /* Helper for convert_exec */
1262 static Base *duplibase_for_convert(Scene *scene, Base *base, Object *ob)
1271 obn = BKE_object_copy(ob);
1272 obn->recalc |= OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME;
1274 basen = MEM_mallocN(sizeof(Base), "duplibase");
1276 BLI_addhead(&scene->base, basen); /* addhead: otherwise eternal loop */
1277 basen->object = obn;
1278 basen->flag |= SELECT;
1279 obn->flag |= SELECT;
1280 base->flag &= ~SELECT;
1281 ob->flag &= ~SELECT;
1286 static int convert_exec(bContext *C, wmOperator *op)
1288 Main *bmain = CTX_data_main(C);
1289 Scene *scene = CTX_data_scene(C);
1290 Base *basen = NULL, *basact = NULL, *basedel = NULL;
1291 Object *ob, *ob1, *newob, *obact = CTX_data_active_object(C);
1297 const short target = RNA_enum_get(op->ptr, "target");
1298 const short keep_original = RNA_boolean_get(op->ptr, "keep_original");
1299 int a, mballConverted = 0;
1301 /* don't forget multiple users! */
1304 CTX_DATA_BEGIN (C, Base *, base, selected_editable_bases)
1307 ob->flag &= ~OB_DONE;
1309 /* flag data thats not been edited (only needed for !keep_original) */
1311 ((ID *)ob->data)->flag |= LIB_DOIT;
1316 CTX_DATA_BEGIN (C, Base *, base, selected_editable_bases)
1320 if (ob->flag & OB_DONE || !IS_TAGGED(ob->data)) {
1321 if (ob->type != target) {
1322 base->flag &= ~SELECT;
1323 ob->flag &= ~SELECT;
1326 /* obdata already modified */
1327 if (!IS_TAGGED(ob->data)) {
1328 /* When 2 objects with linked data are selected, converting both
1329 * would keep modifiers on all but the converted object [#26003] */
1330 if (ob->type == OB_MESH) {
1331 BKE_object_free_modifiers(ob); /* after derivedmesh calls! */
1335 else if (ob->type == OB_MESH && target == OB_CURVE) {
1336 ob->flag |= OB_DONE;
1338 if (keep_original) {
1339 basen = duplibase_for_convert(scene, base, NULL);
1340 newob = basen->object;
1342 /* decrement original mesh's usage count */
1346 /* make a new copy of the mesh */
1347 newob->data = BKE_mesh_copy(me);
1353 BKE_mesh_from_curve(scene, newob);
1355 if (newob->type == OB_CURVE)
1356 BKE_object_free_modifiers(newob); /* after derivedmesh calls! */
1358 else if (ob->type == OB_MESH && ob->modifiers.first) { /* converting a mesh with no modifiers causes a segfault */
1359 ob->flag |= OB_DONE;
1361 if (keep_original) {
1362 basen = duplibase_for_convert(scene, base, NULL);
1363 newob = basen->object;
1365 /* decrement original mesh's usage count */
1369 /* make a new copy of the mesh */
1370 newob->data = BKE_mesh_copy(me);
1374 ob->recalc |= OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME;
1377 /* make new mesh data from the original copy */
1378 /* note: get the mesh from the original, not from the copy in some
1379 * cases this doesnt give correct results (when MDEF is used for eg)
1381 dm = mesh_get_derived_final(scene, newob, CD_MASK_MESH);
1382 /* dm= mesh_create_derived_no_deform(ob1, NULL); this was called original (instead of get_derived). man o man why! (ton) */
1384 DM_to_mesh(dm, newob->data, newob);
1386 /* re-tessellation is called by DM_to_mesh */
1389 BKE_object_free_modifiers(newob); /* after derivedmesh calls! */
1391 else if (ob->type == OB_FONT) {
1392 ob->flag |= OB_DONE;
1394 if (keep_original) {
1395 basen = duplibase_for_convert(scene, base, NULL);
1396 newob = basen->object;
1398 /* decrement original curve's usage count */
1399 ((Curve *)newob->data)->id.us--;
1401 /* make a new copy of the curve */
1402 newob->data = BKE_curve_copy(ob->data);
1410 if (!newob->disp.first)
1411 BKE_displist_make_curveTypes(scene, newob, 0);
1413 newob->type = OB_CURVE;
1414 cu->type = OB_CURVE;
1421 cu->vfontb->id.us--;
1425 cu->vfonti->id.us--;
1429 cu->vfontbi->id.us--;
1433 if (!keep_original) {
1435 if (cu->id.us > 1) {
1436 for (ob1 = bmain->object.first; ob1; ob1 = ob1->id.next) {
1437 if (ob1->data == ob->data) {
1438 ob1->type = OB_CURVE;
1439 ob1->recalc |= OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME;
1445 for (nu = cu->nurb.first; nu; nu = nu->next)
1448 if (target == OB_MESH) {
1449 curvetomesh(scene, newob);
1451 /* meshes doesn't use displist */
1452 BKE_displist_free(&newob->disp);
1455 else if (ELEM(ob->type, OB_CURVE, OB_SURF)) {
1456 ob->flag |= OB_DONE;
1458 if (target == OB_MESH) {
1459 if (keep_original) {
1460 basen = duplibase_for_convert(scene, base, NULL);
1461 newob = basen->object;
1463 /* decrement original curve's usage count */
1464 ((Curve *)newob->data)->id.us--;
1466 /* make a new copy of the curve */
1467 newob->data = BKE_curve_copy(ob->data);
1472 /* meshes doesn't use displist */
1473 BKE_displist_free(&newob->disp);
1476 curvetomesh(scene, newob);
1479 else if (ob->type == OB_MBALL && target == OB_MESH) {
1482 base->flag &= ~SELECT;
1483 ob->flag &= ~SELECT;
1485 baseob = BKE_mball_basis_find(scene, ob);
1488 /* if motherball is converting it would be marked as done later */
1489 ob->flag |= OB_DONE;
1492 if (!baseob->disp.first) {
1493 BKE_displist_make_mball(scene, baseob);
1496 if (!(baseob->flag & OB_DONE)) {
1497 baseob->flag |= OB_DONE;
1499 basen = duplibase_for_convert(scene, base, baseob);
1500 newob = basen->object;
1505 newob->data = BKE_mesh_add("Mesh");
1506 newob->type = OB_MESH;
1509 me->totcol = mb->totcol;
1510 if (newob->totcol) {
1511 me->mat = MEM_dupallocN(mb->mat);
1512 for (a = 0; a < newob->totcol; a++) id_us_plus((ID *)me->mat[a]);
1515 BKE_mesh_from_metaball(&baseob->disp, newob->data);
1517 if (obact->type == OB_MBALL) {
1528 /* tag obdata if it was been changed */
1530 /* If the original object is active then make this object active */
1533 /* store new active base to update BASACT */
1540 if (!keep_original && (ob->flag & OB_DONE)) {
1541 DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
1542 ((ID *)ob->data)->flag &= ~LIB_DOIT; /* flag not to convert this datablock again */
1545 /* delete original if needed */
1548 ED_base_object_free_and_unlink(bmain, scene, basedel);
1555 if (!keep_original) {
1556 if (mballConverted) {
1557 Base *base = scene->base.first, *tmpbase;
1563 if (ob->type == OB_MBALL) {
1564 ED_base_object_free_and_unlink(bmain, scene, tmpbase);
1569 /* delete object should renew depsgraph */
1570 DAG_scene_sort(bmain, scene);
1573 // XXX ED_object_enter_editmode(C, 0);
1574 // XXX exit_editmode(C, EM_FREEDATA|EM_WAITCURSOR); /* freedata, but no undo */
1577 /* active base was changed */
1578 ED_base_object_activate(C, basact);
1581 else if (BASACT->object->flag & OB_DONE) {
1582 WM_event_add_notifier(C, NC_OBJECT | ND_MODIFIER, BASACT->object);
1583 WM_event_add_notifier(C, NC_OBJECT | ND_DATA, BASACT->object);
1586 DAG_scene_sort(bmain, scene);
1587 WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, scene);
1588 WM_event_add_notifier(C, NC_SCENE | ND_OB_SELECT, scene);
1590 return OPERATOR_FINISHED;
1594 void OBJECT_OT_convert(wmOperatorType *ot)
1597 ot->name = "Convert to";
1598 ot->description = "Convert selected objects to another type";
1599 ot->idname = "OBJECT_OT_convert";
1602 ot->invoke = WM_menu_invoke;
1603 ot->exec = convert_exec;
1604 ot->poll = convert_poll;
1607 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1610 ot->prop = RNA_def_enum(ot->srna, "target", convert_target_items, OB_MESH, "Target", "Type of object to convert to");
1611 RNA_def_boolean(ot->srna, "keep_original", 0, "Keep Original", "Keep original objects instead of replacing them");
1614 /**************************** Duplicate ************************/
1617 * dupflag: a flag made from constants declared in DNA_userdef_types.h
1618 * The flag tells adduplicate() weather to copy data linked to the object, or to reference the existing data.
1619 * U.dupflag for default operations or you can construct a flag as python does
1620 * if the dupflag is 0 then no data will be copied (linked duplicate) */
1622 /* used below, assumes id.new is correct */
1623 /* leaves selection of base/object unaltered */
1624 static Base *object_add_duplicate_internal(Main *bmain, Scene *scene, Base *base, int dupflag)
1627 Material ***matarar;
1633 if (ob->mode & OB_MODE_POSE) {
1637 obn = BKE_object_copy(ob);
1638 obn->recalc |= OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME;
1640 basen = MEM_mallocN(sizeof(Base), "duplibase");
1642 BLI_addhead(&scene->base, basen); /* addhead: prevent eternal loop */
1643 basen->object = obn;
1645 if (basen->flag & OB_FROMGROUP) {
1647 for (group = bmain->group.first; group; group = group->id.next) {
1648 if (object_in_group(ob, group))
1649 add_to_group(group, obn, scene, basen);
1653 /* duplicates using userflags */
1654 if (dupflag & USER_DUP_ACT) {
1655 BKE_copy_animdata_id_action(&obn->id);
1658 if (dupflag & USER_DUP_MAT) {
1659 for (a = 0; a < obn->totcol; a++) {
1660 id = (ID *)obn->mat[a];
1662 ID_NEW_US(obn->mat[a])
1663 else obn->mat[a] = BKE_material_copy(obn->mat[a]);
1666 if (dupflag & USER_DUP_ACT) {
1667 BKE_copy_animdata_id_action(&obn->mat[a]->id);
1672 if (dupflag & USER_DUP_PSYS) {
1673 ParticleSystem *psys;
1674 for (psys = obn->particlesystem.first; psys; psys = psys->next) {
1675 id = (ID *) psys->part;
1677 ID_NEW_US(psys->part)
1678 else psys->part = BKE_particlesettings_copy(psys->part);
1680 if (dupflag & USER_DUP_ACT) {
1681 BKE_copy_animdata_id_action(&psys->part->id);
1692 switch (obn->type) {
1694 if (dupflag & USER_DUP_MESH) {
1695 ID_NEW_US2(obn->data)
1697 obn->data = BKE_mesh_copy(obn->data);
1699 if (obn->fluidsimSettings) {
1700 obn->fluidsimSettings->orgMesh = (Mesh *)obn->data;
1709 if (dupflag & USER_DUP_CURVE) {
1710 ID_NEW_US2(obn->data)
1712 obn->data = BKE_curve_copy(obn->data);
1719 if (dupflag & USER_DUP_SURF) {
1720 ID_NEW_US2(obn->data)
1722 obn->data = BKE_curve_copy(obn->data);
1729 if (dupflag & USER_DUP_FONT) {
1730 ID_NEW_US2(obn->data)
1732 obn->data = BKE_curve_copy(obn->data);
1739 if (dupflag & USER_DUP_MBALL) {
1740 ID_NEW_US2(obn->data)
1742 obn->data = BKE_mball_copy(obn->data);
1749 if (dupflag & USER_DUP_LAMP) {
1750 ID_NEW_US2(obn->data)
1752 obn->data = BKE_lamp_copy(obn->data);
1760 obn->recalc |= OB_RECALC_DATA;
1761 if (obn->pose) obn->pose->flag |= POSE_RECALC;
1763 if (dupflag & USER_DUP_ARM) {
1764 ID_NEW_US2(obn->data)
1766 obn->data = BKE_armature_copy(obn->data);
1767 BKE_pose_rebuild(obn, obn->data);
1777 ID_NEW_US2(obn->data)
1779 obn->data = BKE_lattice_copy(obn->data);
1787 ID_NEW_US2(obn->data)
1789 obn->data = BKE_camera_copy(obn->data);
1797 ID_NEW_US2(obn->data)
1799 obn->data = BKE_speaker_copy(obn->data);
1808 /* check if obdata is copied */
1810 Key *key = ob_get_key(obn);
1812 if (dupflag & USER_DUP_ACT) {
1815 BKE_copy_animdata_id_action((ID *)obn->data);
1817 BKE_copy_animdata_id_action((ID *)key);
1820 /* Update the duplicated action in the action actuators */
1821 for (act = obn->actuators.first; act; act = act->next) {
1822 if (act->type == ACT_ACTION) {
1823 bActionActuator *actact = (bActionActuator *) act->data;
1824 if (ob->adt && actact->act == ob->adt->action) {
1825 actact->act = obn->adt->action;
1831 if (dupflag & USER_DUP_MAT) {
1832 matarar = give_matarar(obn);
1834 for (a = 0; a < obn->totcol; a++) {
1835 id = (ID *)(*matarar)[a];
1837 ID_NEW_US((*matarar)[a])
1838 else (*matarar)[a] = BKE_material_copy((*matarar)[a]);
1850 /* single object duplicate, if dupflag==0, fully linked, else it uses the flags given */
1851 /* leaves selection of base/object unaltered.
1852 * note: don't call this within a loop since clear_* funcs loop over the entire database. */
1853 Base *ED_object_add_duplicate(Main *bmain, Scene *scene, Base *base, int dupflag)
1858 clear_id_newpoins();
1859 clear_sca_new_poins(); /* sensor/contr/act */
1861 basen = object_add_duplicate_internal(bmain, scene, base, dupflag);
1862 if (basen == NULL) {
1868 /* link own references to the newly duplicated data [#26816] */
1869 BKE_object_relink(ob);
1870 set_sca_new_poins_ob(ob);
1872 DAG_scene_sort(bmain, scene);
1874 ED_render_id_flush_update(bmain, ob->data);
1880 /* contextual operator dupli */
1881 static int duplicate_exec(bContext *C, wmOperator *op)
1883 Main *bmain = CTX_data_main(C);
1884 Scene *scene = CTX_data_scene(C);
1885 int linked = RNA_boolean_get(op->ptr, "linked");
1886 int dupflag = (linked) ? 0 : U.dupflag;
1888 clear_id_newpoins();
1889 clear_sca_new_poins(); /* sensor/contr/act */
1891 CTX_DATA_BEGIN (C, Base *, base, selected_editable_bases)
1893 Base *basen = object_add_duplicate_internal(bmain, scene, base, dupflag);
1895 /* note that this is safe to do with this context iterator,
1896 * the list is made in advance */
1897 ED_base_object_select(base, BA_DESELECT);
1899 if (basen == NULL) {
1903 /* new object becomes active */
1905 ED_base_object_activate(C, basen);
1907 if (basen->object->data) {
1908 DAG_id_tag_update(basen->object->data, 0);
1913 copy_object_set_idnew(C, dupflag);
1915 DAG_scene_sort(bmain, scene);
1916 DAG_ids_flush_update(bmain, 0);
1918 WM_event_add_notifier(C, NC_SCENE | ND_OB_SELECT, scene);
1920 return OPERATOR_FINISHED;
1923 void OBJECT_OT_duplicate(wmOperatorType *ot)
1928 ot->name = "Duplicate Objects";
1929 ot->description = "Duplicate selected objects";
1930 ot->idname = "OBJECT_OT_duplicate";
1933 ot->exec = duplicate_exec;
1934 ot->poll = ED_operator_objectmode;
1937 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1939 /* to give to transform */
1940 RNA_def_boolean(ot->srna, "linked", 0, "Linked", "Duplicate object but not object data, linking to the original data");
1941 prop = RNA_def_enum(ot->srna, "mode", transform_mode_types, TFM_TRANSLATION, "Mode", "");
1942 RNA_def_property_flag(prop, PROP_HIDDEN);
1945 /* **************** add named object, for dragdrop ************* */
1948 static int add_named_exec(bContext *C, wmOperator *op)
1950 Main *bmain = CTX_data_main(C);
1951 Scene *scene = CTX_data_scene(C);
1954 int linked = RNA_boolean_get(op->ptr, "linked");
1955 int dupflag = (linked) ? 0 : U.dupflag;
1956 char name[MAX_ID_NAME - 2];
1958 /* find object, create fake base */
1959 RNA_string_get(op->ptr, "name", name);
1960 ob = (Object *)BKE_libblock_find_name(ID_OB, name);
1962 return OPERATOR_CANCELLED;
1964 base = MEM_callocN(sizeof(Base), "duplibase");
1966 base->flag = ob->flag;
1969 clear_id_newpoins();
1970 clear_sca_new_poins(); /* sensor/contr/act */
1972 basen = object_add_duplicate_internal(bmain, scene, base, dupflag);
1974 if (basen == NULL) {
1976 return OPERATOR_CANCELLED;
1979 basen->lay = basen->object->lay = scene->lay;
1981 ED_object_location_from_view(C, basen->object->loc);
1982 ED_base_object_activate(C, basen);
1984 copy_object_set_idnew(C, dupflag);
1986 DAG_scene_sort(bmain, scene);
1987 DAG_ids_flush_update(bmain, 0);
1991 WM_event_add_notifier(C, NC_SCENE | ND_OB_SELECT, scene);
1993 return OPERATOR_FINISHED;
1996 void OBJECT_OT_add_named(wmOperatorType *ot)
1999 ot->name = "Add Named Object";
2000 ot->description = "Add named object";
2001 ot->idname = "OBJECT_OT_add_named";
2004 ot->exec = add_named_exec;
2005 ot->poll = ED_operator_objectmode;
2008 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2010 RNA_def_boolean(ot->srna, "linked", 0, "Linked", "Duplicate object but not object data, linking to the original data");
2011 RNA_def_string(ot->srna, "name", "Cube", MAX_ID_NAME - 2, "Name", "Object name to add");
2016 /**************************** Join *************************/
2017 static int join_poll(bContext *C)
2019 Object *ob = CTX_data_active_object(C);
2021 if (!ob || ob->id.lib) return 0;
2023 if (ELEM4(ob->type, OB_MESH, OB_CURVE, OB_SURF, OB_ARMATURE))
2024 return ED_operator_screenactive(C);
2030 static int join_exec(bContext *C, wmOperator *op)
2032 Scene *scene = CTX_data_scene(C);
2033 Object *ob = CTX_data_active_object(C);
2035 if (scene->obedit) {
2036 BKE_report(op->reports, RPT_ERROR, "This data does not support joining in editmode");
2037 return OPERATOR_CANCELLED;
2039 else if (BKE_object_obdata_is_libdata(ob)) {
2040 BKE_report(op->reports, RPT_ERROR, "Can't edit external libdata");
2041 return OPERATOR_CANCELLED;
2044 if (ob->type == OB_MESH)
2045 return join_mesh_exec(C, op);
2046 else if (ELEM(ob->type, OB_CURVE, OB_SURF))
2047 return join_curve_exec(C, op);
2048 else if (ob->type == OB_ARMATURE)
2049 return join_armature_exec(C, op);
2051 return OPERATOR_CANCELLED;
2054 void OBJECT_OT_join(wmOperatorType *ot)
2058 ot->description = "Join selected objects into active object";
2059 ot->idname = "OBJECT_OT_join";
2062 ot->exec = join_exec;
2063 ot->poll = join_poll;
2066 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2069 /**************************** Join as Shape Key*************************/
2070 static int join_shapes_poll(bContext *C)
2072 Object *ob = CTX_data_active_object(C);
2074 if (!ob || ob->id.lib) return 0;
2076 /* only meshes supported at the moment */
2077 if (ob->type == OB_MESH)
2078 return ED_operator_screenactive(C);
2083 static int join_shapes_exec(bContext *C, wmOperator *op)
2085 Scene *scene = CTX_data_scene(C);
2086 Object *ob = CTX_data_active_object(C);
2088 if (scene->obedit) {
2089 BKE_report(op->reports, RPT_ERROR, "This data does not support joining in editmode");
2090 return OPERATOR_CANCELLED;
2092 else if (BKE_object_obdata_is_libdata(ob)) {
2093 BKE_report(op->reports, RPT_ERROR, "Can't edit external libdata");
2094 return OPERATOR_CANCELLED;
2097 if (ob->type == OB_MESH)
2098 return join_mesh_shapes_exec(C, op);
2100 return OPERATOR_CANCELLED;
2103 void OBJECT_OT_join_shapes(wmOperatorType *ot)
2106 ot->name = "Join as Shapes";
2107 ot->description = "Merge selected objects to shapes of active object";
2108 ot->idname = "OBJECT_OT_join_shapes";
2111 ot->exec = join_shapes_exec;
2112 ot->poll = join_shapes_poll;
2115 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;