4 * ***** BEGIN GPL LICENSE BLOCK *****
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21 * All rights reserved.
23 * Contributor(s): Blender Foundation, 2002-2008 full recode
25 * ***** END GPL LICENSE BLOCK *****
28 /** \file blender/editors/object/object_add.c
36 #include "MEM_guardedalloc.h"
38 #include "DNA_anim_types.h"
39 #include "DNA_curve_types.h"
40 #include "DNA_group_types.h"
41 #include "DNA_lamp_types.h"
42 #include "DNA_key_types.h"
43 #include "DNA_material_types.h"
44 #include "DNA_mesh_types.h"
45 #include "DNA_meta_types.h"
46 #include "DNA_object_fluidsim.h"
47 #include "DNA_object_force.h"
48 #include "DNA_scene_types.h"
49 #include "DNA_speaker_types.h"
50 #include "DNA_vfont_types.h"
53 #include "BLI_listbase.h"
54 #include "BLI_utildefines.h"
57 #include "BKE_animsys.h"
58 #include "BKE_armature.h"
59 #include "BKE_constraint.h"
60 #include "BKE_context.h"
61 #include "BKE_curve.h"
62 #include "BKE_depsgraph.h"
63 #include "BKE_DerivedMesh.h"
64 #include "BKE_displist.h"
65 #include "BKE_effect.h"
66 #include "BKE_group.h"
67 #include "BKE_lattice.h"
68 #include "BKE_library.h"
71 #include "BKE_material.h"
72 #include "BKE_mball.h"
74 #include "BKE_modifier.h"
76 #include "BKE_object.h"
77 #include "BKE_particle.h"
78 #include "BKE_report.h"
80 #include "BKE_speaker.h"
81 #include "BKE_texture.h"
83 #include "RNA_access.h"
84 #include "RNA_define.h"
85 #include "RNA_enum_types.h"
90 #include "ED_armature.h"
94 #include "ED_object.h"
95 #include "ED_render.h"
96 #include "ED_screen.h"
97 #include "ED_transform.h"
98 #include "ED_view3d.h"
100 #include "UI_interface.h"
101 #include "UI_resources.h"
103 #include "object_intern.h"
105 /************************** Exported *****************************/
107 void ED_object_location_from_view(bContext *C, float *loc)
109 View3D *v3d= CTX_wm_view3d(C);
110 Scene *scene= CTX_data_scene(C);
113 cursor = give_cursor(scene, v3d);
115 copy_v3_v3(loc, cursor);
118 void ED_object_rotation_from_view(bContext *C, float *rot)
120 RegionView3D *rv3d= CTX_wm_region_view3d(C);
123 copy_qt_qt(quat, rv3d->viewquat);
125 quat_to_eul(rot, quat);
132 void ED_object_base_init_transform(bContext *C, Base *base, float *loc, float *rot)
134 Object *ob= base->object;
135 Scene *scene= CTX_data_scene(C);
140 copy_v3_v3(ob->loc, loc);
143 copy_v3_v3(ob->rot, rot);
145 where_is_object(scene, ob);
148 /* uses context to figure out transform for primitive */
149 /* returns standard diameter */
150 float ED_object_new_primitive_matrix(bContext *C, Object *obedit, float *loc, float *rot, float primmat[][4])
152 View3D *v3d =CTX_wm_view3d(C);
153 float mat[3][3], rmat[3][3], cmat[3][3], imat[3][3];
157 eul_to_mat3(rmat, rot);
160 /* inverse transform for initial rotation and object */
161 copy_m3_m4(mat, obedit->obmat);
162 mul_m3_m3m3(cmat, rmat, mat);
163 invert_m3_m3(imat, cmat);
164 copy_m4_m3(primmat, imat);
167 copy_v3_v3(primmat[3], loc);
168 sub_v3_v3v3(primmat[3], primmat[3], obedit->obmat[3]);
169 invert_m3_m3(imat, mat);
170 mul_m3_v3(imat, primmat[3]);
172 if(v3d) return v3d->grid;
176 /********************* Add Object Operator ********************/
178 void ED_object_add_generic_props(wmOperatorType *ot, int do_editmode)
182 /* note: this property gets hidden for add-camera operator */
183 RNA_def_boolean(ot->srna, "view_align", 0, "Align to View", "Align the new object to the view");
186 prop= RNA_def_boolean(ot->srna, "enter_editmode", 0, "Enter Editmode", "Enter editmode when adding this object");
187 RNA_def_property_flag(prop, PROP_HIDDEN);
190 RNA_def_float_vector_xyz(ot->srna, "location", 3, NULL, -FLT_MAX, FLT_MAX, "Location", "Location for the newly added object", -FLT_MAX, FLT_MAX);
191 RNA_def_float_rotation(ot->srna, "rotation", 3, NULL, -FLT_MAX, FLT_MAX, "Rotation", "Rotation for the newly added object", (float)-M_PI * 2.0f, (float)M_PI * 2.0f);
193 prop = RNA_def_boolean_layer_member(ot->srna, "layers", 20, NULL, "Layer", "");
194 RNA_def_property_flag(prop, PROP_HIDDEN);
197 static void object_add_generic_invoke_options(bContext *C, wmOperator *op)
199 if(RNA_struct_find_property(op->ptr, "enter_editmode")) /* optional */
200 if (!RNA_property_is_set(op->ptr, "enter_editmode"))
201 RNA_boolean_set(op->ptr, "enter_editmode", U.flag & USER_ADD_EDITMODE);
203 if(!RNA_property_is_set(op->ptr, "location")) {
206 ED_object_location_from_view(C, loc);
207 RNA_float_set_array(op->ptr, "location", loc);
210 if(!RNA_property_is_set(op->ptr, "layers")) {
211 View3D *v3d = CTX_wm_view3d(C);
212 Scene *scene = CTX_data_scene(C);
213 int a, values[20], layer;
216 layer = (v3d->scenelock && !v3d->localvd)? scene->layact: v3d->layact;
219 values[a]= (layer & (1<<a));
222 layer = scene->layact;
225 values[a]= (layer & (1<<a));
228 RNA_boolean_set_array(op->ptr, "layers", values);
232 int ED_object_add_generic_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
234 object_add_generic_invoke_options(C, op);
235 return op->type->exec(C, op);
238 int ED_object_add_generic_get_opts(bContext *C, wmOperator *op, float *loc, float *rot, int *enter_editmode, unsigned int *layer)
240 View3D *v3d = CTX_wm_view3d(C);
241 int a, layer_values[20];
244 *enter_editmode = FALSE;
245 if(RNA_struct_find_property(op->ptr, "enter_editmode") && RNA_boolean_get(op->ptr, "enter_editmode")) {
246 *enter_editmode = TRUE;
249 if(RNA_property_is_set(op->ptr, "layers")) {
250 RNA_boolean_get_array(op->ptr, "layers", layer_values);
252 for(a=0; a<20; a++) {
260 /* not set, use the scenes layers */
261 Scene *scene = CTX_data_scene(C);
262 *layer = scene->layact;
265 /* in local view we additionally add local view layers,
266 not part of operator properties */
267 if(v3d && v3d->localvd)
270 if(RNA_property_is_set(op->ptr, "rotation"))
272 else if (RNA_property_is_set(op->ptr, "view_align"))
273 view_align = RNA_boolean_get(op->ptr, "view_align");
275 view_align = U.flag & USER_ADD_VIEWALIGNED;
276 RNA_boolean_set(op->ptr, "view_align", view_align);
280 ED_object_rotation_from_view(C, rot);
281 RNA_float_set_array(op->ptr, "rotation", rot);
284 RNA_float_get_array(op->ptr, "rotation", rot);
287 RNA_float_get_array(op->ptr, "location", loc);
290 BKE_report(op->reports, RPT_ERROR, "Property 'layer' has no values set");
297 /* for object add primitive operators */
298 /* do not call undo push in this function (users of this function have to) */
299 Object *ED_object_add_type(bContext *C, int type, float *loc, float *rot, int enter_editmode, unsigned int layer)
301 Main *bmain= CTX_data_main(C);
302 Scene *scene= CTX_data_scene(C);
305 /* for as long scene has editmode... */
306 if (CTX_data_edit_object(C))
307 ED_object_exit_editmode(C, EM_FREEDATA|EM_FREEUNDO|EM_WAITCURSOR|EM_DO_UNDO); /* freedata, and undo */
309 /* deselects all, sets scene->basact */
310 ob= add_object(scene, type);
311 BASACT->lay = ob->lay = layer;
312 /* editor level activate, notifiers */
313 ED_base_object_activate(C, BASACT);
315 /* more editor stuff */
316 ED_object_base_init_transform(C, BASACT, loc, rot);
318 DAG_scene_sort(bmain, scene);
319 ED_render_id_flush_update(bmain, ob->data);
322 ED_object_enter_editmode(C, EM_IGNORE_LAYER);
324 WM_event_add_notifier(C, NC_SCENE|ND_LAYER_CONTENT, scene);
329 /* for object add operator */
330 static int object_add_exec(bContext *C, wmOperator *op)
334 float loc[3], rot[3];
336 if(!ED_object_add_generic_get_opts(C, op, loc, rot, &enter_editmode, &layer))
337 return OPERATOR_CANCELLED;
339 ED_object_add_type(C, RNA_enum_get(op->ptr, "type"), loc, rot, enter_editmode, layer);
341 return OPERATOR_FINISHED;
344 void OBJECT_OT_add(wmOperatorType *ot)
347 ot->name= "Add Object";
348 ot->description = "Add an object to the scene";
349 ot->idname= "OBJECT_OT_add";
352 ot->invoke= ED_object_add_generic_invoke;
353 ot->exec= object_add_exec;
355 ot->poll= ED_operator_objectmode;
358 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
360 RNA_def_enum(ot->srna, "type", object_type_items, 0, "Type", "");
362 ED_object_add_generic_props(ot, TRUE);
365 /********************* Add Effector Operator ********************/
366 /* copy from rna_object_force.c*/
367 static EnumPropertyItem field_type_items[] = {
368 {PFIELD_FORCE, "FORCE", ICON_FORCE_FORCE, "Force", ""},
369 {PFIELD_WIND, "WIND", ICON_FORCE_WIND, "Wind", ""},
370 {PFIELD_VORTEX, "VORTEX", ICON_FORCE_VORTEX, "Vortex", ""},
371 {PFIELD_MAGNET, "MAGNET", ICON_FORCE_MAGNETIC, "Magnetic", ""},
372 {PFIELD_HARMONIC, "HARMONIC", ICON_FORCE_HARMONIC, "Harmonic", ""},
373 {PFIELD_CHARGE, "CHARGE", ICON_FORCE_CHARGE, "Charge", ""},
374 {PFIELD_LENNARDJ, "LENNARDJ", ICON_FORCE_LENNARDJONES, "Lennard-Jones", ""},
375 {PFIELD_TEXTURE, "TEXTURE", ICON_FORCE_TEXTURE, "Texture", ""},
376 {PFIELD_GUIDE, "GUIDE", ICON_FORCE_CURVE, "Curve Guide", ""},
377 {PFIELD_BOID, "BOID", ICON_FORCE_BOID, "Boid", ""},
378 {PFIELD_TURBULENCE, "TURBULENCE", ICON_FORCE_TURBULENCE, "Turbulence", ""},
379 {PFIELD_DRAG, "DRAG", ICON_FORCE_DRAG, "Drag", ""},
380 {0, NULL, 0, NULL, NULL}};
382 /* for effector add primitive operators */
383 static Object *effector_add_type(bContext *C, wmOperator *op, int type)
388 float loc[3], rot[3];
391 object_add_generic_invoke_options(C, op);
393 if(!ED_object_add_generic_get_opts(C, op, loc, rot, &enter_editmode, &layer))
396 if(type==PFIELD_GUIDE) {
397 ob= ED_object_add_type(C, OB_CURVE, loc, rot, FALSE, layer);
398 rename_id(&ob->id, "CurveGuide");
400 ((Curve*)ob->data)->flag |= CU_PATH|CU_3D;
401 ED_object_enter_editmode(C, 0);
402 ED_object_new_primitive_matrix(C, ob, loc, rot, mat);
403 BLI_addtail(curve_get_editcurve(ob), add_nurbs_primitive(C, mat, CU_NURBS|CU_PRIM_PATH, 1));
406 ED_object_exit_editmode(C, EM_FREEDATA);
409 ob= ED_object_add_type(C, OB_EMPTY, loc, rot, FALSE, layer);
410 rename_id(&ob->id, "Field");
415 ob->empty_drawtype = OB_SINGLE_ARROW;
420 ob->pd= object_add_collision_fields(type);
422 DAG_scene_sort(CTX_data_main(C), CTX_data_scene(C));
427 /* for object add operator */
428 static int effector_add_exec(bContext *C, wmOperator *op)
430 if(effector_add_type(C, op, RNA_enum_get(op->ptr, "type")) == NULL)
431 return OPERATOR_CANCELLED;
433 return OPERATOR_FINISHED;
436 void OBJECT_OT_effector_add(wmOperatorType *ot)
439 ot->name= "Add Effector";
440 ot->description = "Add an empty object with a physics effector to the scene";
441 ot->idname= "OBJECT_OT_effector_add";
444 ot->invoke= WM_menu_invoke;
445 ot->exec= effector_add_exec;
447 ot->poll= ED_operator_objectmode;
450 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
452 ot->prop= RNA_def_enum(ot->srna, "type", field_type_items, 0, "Type", "");
454 ED_object_add_generic_props(ot, TRUE);
457 /* ***************** Add Camera *************** */
459 static int object_camera_add_exec(bContext *C, wmOperator *op)
461 View3D *v3d = CTX_wm_view3d(C);
462 Scene *scene= CTX_data_scene(C);
466 float loc[3], rot[3];
468 /* force view align for cameras */
469 RNA_boolean_set(op->ptr, "view_align", 1);
471 object_add_generic_invoke_options(C, op);
473 if(!ED_object_add_generic_get_opts(C, op, loc, rot, &enter_editmode, &layer))
474 return OPERATOR_CANCELLED;
476 ob= ED_object_add_type(C, OB_CAMERA, loc, rot, FALSE, layer);
479 if (v3d->camera == NULL)
481 if (v3d->scenelock && scene->camera==NULL) {
486 return OPERATOR_FINISHED;
489 void OBJECT_OT_camera_add(wmOperatorType *ot)
494 ot->name= "Add Camera";
495 ot->description = "Add a camera object to the scene";
496 ot->idname= "OBJECT_OT_camera_add";
499 ot->exec= object_camera_add_exec;
500 ot->poll= ED_operator_objectmode;
503 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
505 ED_object_add_generic_props(ot, TRUE);
507 /* hide this for cameras, default */
508 prop= RNA_struct_type_find_property(ot->srna, "view_align");
509 RNA_def_property_flag(prop, PROP_HIDDEN);
514 /* ***************** add primitives *************** */
515 static int object_metaball_add_exec(bContext *C, wmOperator *op)
517 Object *obedit= CTX_data_edit_object(C);
518 /*MetaElem *elem;*/ /*UNUSED*/
522 float loc[3], rot[3];
525 object_add_generic_invoke_options(C, op); // XXX these props don't get set right when only exec() is called
527 if(!ED_object_add_generic_get_opts(C, op, loc, rot, &enter_editmode, &layer))
528 return OPERATOR_CANCELLED;
530 if(obedit==NULL || obedit->type!=OB_MBALL) {
531 obedit= ED_object_add_type(C, OB_MBALL, loc, rot, TRUE, layer);
534 else DAG_id_tag_update(&obedit->id, OB_RECALC_DATA);
536 ED_object_new_primitive_matrix(C, obedit, loc, rot, mat);
538 /* elem= (MetaElem *) */ add_metaball_primitive(C, mat, RNA_enum_get(op->ptr, "type"), newob);
541 if (newob && !enter_editmode) {
542 ED_object_exit_editmode(C, EM_FREEDATA);
545 WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, obedit);
547 return OPERATOR_FINISHED;
550 static int object_metaball_add_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
552 Object *obedit= CTX_data_edit_object(C);
556 object_add_generic_invoke_options(C, op);
558 pup= uiPupMenuBegin(C, op->type->name, ICON_NONE);
559 layout= uiPupMenuLayout(pup);
560 if(!obedit || obedit->type == OB_MBALL)
561 uiItemsEnumO(layout, op->type->idname, "type");
563 uiItemsEnumO(layout, "OBJECT_OT_metaball_add", "type");
564 uiPupMenuEnd(C, pup);
566 return OPERATOR_CANCELLED;
569 void OBJECT_OT_metaball_add(wmOperatorType *ot)
572 ot->name= "Add Metaball";
573 ot->description= "Add an metaball object to the scene";
574 ot->idname= "OBJECT_OT_metaball_add";
577 ot->invoke= object_metaball_add_invoke;
578 ot->exec= object_metaball_add_exec;
579 ot->poll= ED_operator_scene_editable;
582 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
584 RNA_def_enum(ot->srna, "type", metaelem_type_items, 0, "Primitive", "");
585 ED_object_add_generic_props(ot, TRUE);
588 static int object_add_text_exec(bContext *C, wmOperator *op)
590 Object *obedit= CTX_data_edit_object(C);
593 float loc[3], rot[3];
595 object_add_generic_invoke_options(C, op); // XXX these props don't get set right when only exec() is called
596 if(!ED_object_add_generic_get_opts(C, op, loc, rot, &enter_editmode, &layer))
597 return OPERATOR_CANCELLED;
599 if(obedit && obedit->type==OB_FONT)
600 return OPERATOR_CANCELLED;
602 obedit= ED_object_add_type(C, OB_FONT, loc, rot, enter_editmode, layer);
604 WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, obedit);
606 return OPERATOR_FINISHED;
609 void OBJECT_OT_text_add(wmOperatorType *ot)
612 ot->name= "Add Text";
613 ot->description = "Add a text object to the scene";
614 ot->idname= "OBJECT_OT_text_add";
617 ot->invoke= ED_object_add_generic_invoke;
618 ot->exec= object_add_text_exec;
619 ot->poll= ED_operator_objectmode;
622 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
623 ED_object_add_generic_props(ot, TRUE);
626 static int object_armature_add_exec(bContext *C, wmOperator *op)
628 Object *obedit= CTX_data_edit_object(C);
629 View3D *v3d= CTX_wm_view3d(C);
630 RegionView3D *rv3d= CTX_wm_region_view3d(C);
634 float loc[3], rot[3];
636 object_add_generic_invoke_options(C, op); // XXX these props don't get set right when only exec() is called
637 if(!ED_object_add_generic_get_opts(C, op, loc, rot, &enter_editmode, &layer))
638 return OPERATOR_CANCELLED;
640 if ((obedit==NULL) || (obedit->type != OB_ARMATURE)) {
641 obedit= ED_object_add_type(C, OB_ARMATURE, loc, rot, TRUE, layer);
642 ED_object_enter_editmode(C, 0);
645 else DAG_id_tag_update(&obedit->id, OB_RECALC_DATA);
648 BKE_report(op->reports, RPT_ERROR, "Cannot create editmode armature");
649 return OPERATOR_CANCELLED;
652 /* v3d and rv3d are allowed to be NULL */
653 add_primitive_bone(CTX_data_scene(C), v3d, rv3d);
656 if (newob && !enter_editmode)
657 ED_object_exit_editmode(C, EM_FREEDATA);
659 WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, obedit);
661 return OPERATOR_FINISHED;
664 void OBJECT_OT_armature_add(wmOperatorType *ot)
667 ot->name= "Add Armature";
668 ot->description = "Add an armature object to the scene";
669 ot->idname= "OBJECT_OT_armature_add";
672 ot->invoke= ED_object_add_generic_invoke;
673 ot->exec= object_armature_add_exec;
674 ot->poll= ED_operator_objectmode;
677 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
678 ED_object_add_generic_props(ot, TRUE);
681 static const char *get_lamp_defname(int type)
684 case LA_LOCAL: return "Point";
685 case LA_SUN: return "Sun";
686 case LA_SPOT: return "Spot";
687 case LA_HEMI: return "Hemi";
688 case LA_AREA: return "Area";
694 static int object_lamp_add_exec(bContext *C, wmOperator *op)
697 int type= RNA_enum_get(op->ptr, "type");
700 float loc[3], rot[3];
702 object_add_generic_invoke_options(C, op);
703 if(!ED_object_add_generic_get_opts(C, op, loc, rot, &enter_editmode, &layer))
704 return OPERATOR_CANCELLED;
706 ob= ED_object_add_type(C, OB_LAMP, loc, rot, FALSE, layer);
707 ((Lamp*)ob->data)->type= type;
708 rename_id((ID *)ob, get_lamp_defname(type));
709 rename_id((ID *)ob->data, get_lamp_defname(type));
711 return OPERATOR_FINISHED;
714 void OBJECT_OT_lamp_add(wmOperatorType *ot)
716 static EnumPropertyItem lamp_type_items[] = {
717 {LA_LOCAL, "POINT", ICON_LAMP_POINT, "Point", "Omnidirectional point light source"},
718 {LA_SUN, "SUN", ICON_LAMP_SUN, "Sun", "Constant direction parallel ray light source"},
719 {LA_SPOT, "SPOT", ICON_LAMP_SPOT, "Spot", "Directional cone light source"},
720 {LA_HEMI, "HEMI", ICON_LAMP_HEMI, "Hemi", "180 degree constant light source"},
721 {LA_AREA, "AREA", ICON_LAMP_AREA, "Area", "Directional area light source"},
722 {0, NULL, 0, NULL, NULL}};
725 ot->name= "Add Lamp";
726 ot->description = "Add a lamp object to the scene";
727 ot->idname= "OBJECT_OT_lamp_add";
730 ot->invoke= WM_menu_invoke;
731 ot->exec= object_lamp_add_exec;
732 ot->poll= ED_operator_objectmode;
735 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
738 ot->prop= RNA_def_enum(ot->srna, "type", lamp_type_items, 0, "Type", "");
740 ED_object_add_generic_props(ot, FALSE);
743 static int group_instance_add_exec(bContext *C, wmOperator *op)
745 Group *group= BLI_findlink(&CTX_data_main(C)->group, RNA_enum_get(op->ptr, "group"));
749 float loc[3], rot[3];
751 object_add_generic_invoke_options(C, op);
752 if(!ED_object_add_generic_get_opts(C, op, loc, rot, &enter_editmode, &layer))
753 return OPERATOR_CANCELLED;
756 Main *bmain= CTX_data_main(C);
757 Scene *scene= CTX_data_scene(C);
758 Object *ob= ED_object_add_type(C, OB_EMPTY, loc, rot, FALSE, layer);
759 rename_id(&ob->id, group->id.name+2);
760 ob->dup_group= group;
761 ob->transflag |= OB_DUPLIGROUP;
762 id_lib_extern(&group->id);
764 /* works without this except if you try render right after, see: 22027 */
765 DAG_scene_sort(bmain, scene);
767 WM_event_add_notifier(C, NC_SCENE|ND_OB_ACTIVE, CTX_data_scene(C));
769 return OPERATOR_FINISHED;
772 return OPERATOR_CANCELLED;
775 static int object_speaker_add_exec(bContext *C, wmOperator *op)
780 float loc[3], rot[3];
781 Scene *scene = CTX_data_scene(C);
783 object_add_generic_invoke_options(C, op);
784 if(!ED_object_add_generic_get_opts(C, op, loc, rot, &enter_editmode, &layer))
785 return OPERATOR_CANCELLED;
787 ob= ED_object_add_type(C, OB_SPEAKER, loc, rot, FALSE, layer);
789 /* to make it easier to start using this immediately in NLA, a default sound clip is created
790 * ready to be moved around to retime the sound and/or make new sound clips
793 /* create new data for NLA hierarchy */
794 AnimData *adt = BKE_id_add_animdata(&ob->id);
795 NlaTrack *nlt = add_nlatrack(adt, NULL);
796 NlaStrip *strip = add_nla_soundstrip(CTX_data_scene(C), ob->data);
798 strip->end += strip->start;
801 BKE_nlatrack_add_strip(nlt, strip);
803 /* auto-name the strip, and give the track an interesting name */
804 strcpy(nlt->name, "SoundTrack");
805 BKE_nlastrip_validate_name(adt, strip);
807 WM_event_add_notifier(C, NC_ANIMATION|ND_NLA|NA_EDITED, NULL);
810 return OPERATOR_FINISHED;
813 void OBJECT_OT_speaker_add(wmOperatorType *ot)
816 ot->name= "Add Speaker";
817 ot->description = "Add a speaker object to the scene";
818 ot->idname= "OBJECT_OT_speaker_add";
821 ot->exec= object_speaker_add_exec;
822 ot->poll= ED_operator_objectmode;
825 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
827 ED_object_add_generic_props(ot, TRUE);
830 /* only used as menu */
831 void OBJECT_OT_group_instance_add(wmOperatorType *ot)
836 ot->name= "Add Group Instance";
837 ot->description = "Add a dupligroup instance";
838 ot->idname= "OBJECT_OT_group_instance_add";
841 ot->invoke= WM_enum_search_invoke;
842 ot->exec= group_instance_add_exec;
844 ot->poll= ED_operator_objectmode;
847 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
850 prop= RNA_def_enum(ot->srna, "group", DummyRNA_NULL_items, 0, "Group", "");
851 RNA_def_enum_funcs(prop, RNA_group_itemf);
853 ED_object_add_generic_props(ot, FALSE);
856 /**************************** Delete Object *************************/
858 /* remove base from a specific scene */
859 /* note: now unlinks constraints as well */
860 void ED_base_object_free_and_unlink(Main *bmain, Scene *scene, Base *base)
862 BLI_remlink(&scene->base, base);
863 free_libblock_us(&bmain->object, base->object);
864 if(scene->basact==base) scene->basact= NULL;
868 static int object_delete_exec(bContext *C, wmOperator *UNUSED(op))
870 Main *bmain= CTX_data_main(C);
871 Scene *scene= CTX_data_scene(C);
872 /* int islamp= 0; */ /* UNUSED */
874 if(CTX_data_edit_object(C))
875 return OPERATOR_CANCELLED;
877 CTX_DATA_BEGIN(C, Base*, base, selected_bases) {
879 /* if(base->object->type==OB_LAMP) islamp= 1; */
881 /* deselect object -- it could be used in other scenes */
882 base->object->flag &= ~SELECT;
884 /* remove from current scene only */
885 ED_base_object_free_and_unlink(bmain, scene, base);
889 DAG_scene_sort(bmain, scene);
890 DAG_ids_flush_update(bmain, 0);
892 WM_event_add_notifier(C, NC_SCENE|ND_OB_ACTIVE, scene);
893 WM_event_add_notifier(C, NC_SCENE|ND_LAYER_CONTENT, scene);
895 return OPERATOR_FINISHED;
898 void OBJECT_OT_delete(wmOperatorType *ot)
902 ot->description = "Delete selected objects";
903 ot->idname= "OBJECT_OT_delete";
906 ot->invoke= WM_operator_confirm;
907 ot->exec= object_delete_exec;
908 ot->poll= ED_operator_objectmode;
911 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
914 /**************************** Copy Utilities ******************************/
916 /* after copying objects, copied data should get new pointers */
917 static void copy_object_set_idnew(bContext *C, int dupflag)
919 Main *bmain= CTX_data_main(C);
924 /* XXX check object pointers */
925 CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) {
931 if( dupflag & USER_DUP_MAT) {
932 mao= bmain->mat.first;
936 ma= (Material *)mao->id.newid;
938 if(dupflag & USER_DUP_TEX) {
939 for(a=0; a<MAX_MTEX; a++) {
941 id= (ID *)ma->mtex[a]->tex;
943 ID_NEW_US(ma->mtex[a]->tex)
944 else ma->mtex[a]->tex= copy_texture(ma->mtex[a]->tex);
950 #if 0 // XXX old animation system
954 else ma->ipo= copy_ipo(ma->ipo);
957 #endif // XXX old animation system
963 #if 0 // XXX old animation system
965 if( dupflag & USER_DUP_IPO) {
966 Lamp *la= bmain->lamp.first;
969 Lamp *lan= (Lamp *)la->id.newid;
973 else lan->ipo= copy_ipo(lan->ipo);
982 ipo= bmain->ipo.first;
984 if(ipo->id.lib==NULL && ipo->id.newid) {
985 Ipo *ipon= (Ipo *)ipo->id.newid;
987 for(icu= ipon->curve.first; icu; icu= icu->next) {
989 ID_NEW(icu->driver->ob);
995 #endif // XXX old animation system
1002 /********************* Make Duplicates Real ************************/
1004 static void make_object_duplilist_real(bContext *C, Scene *scene, Base *base)
1011 if(!base && !(base = BASACT))
1014 if(!(base->object->transflag & OB_DUPLI))
1017 lb= object_duplilist(scene, base->object);
1019 for(dob= lb->first; dob; dob= dob->next) {
1020 ob= copy_object(dob->ob);
1021 /* font duplis can have a totcol without material, we get them from parent
1022 * should be implemented better...
1024 if(ob->mat==NULL) ob->totcol= 0;
1026 basen= MEM_dupallocN(base);
1027 basen->flag &= ~(OB_FROMDUPLI|OB_FROMGROUP);
1028 ob->flag= basen->flag;
1029 basen->lay= base->lay;
1030 BLI_addhead(&scene->base, basen); /* addhead: othwise eternal loop */
1033 /* make sure apply works */
1034 BKE_free_animdata(&ob->id);
1038 ob->constraints.first= ob->constraints.last= NULL;
1039 ob->disp.first= ob->disp.last= NULL;
1040 ob->transflag &= ~OB_DUPLI;
1043 copy_m4_m4(ob->obmat, dob->mat);
1044 object_apply_mat4(ob, ob->obmat, FALSE, FALSE);
1047 copy_object_set_idnew(C, 0);
1049 free_object_duplilist(lb);
1051 base->object->transflag &= ~OB_DUPLI;
1054 static int object_duplicates_make_real_exec(bContext *C, wmOperator *UNUSED(op))
1056 Main *bmain= CTX_data_main(C);
1057 Scene *scene= CTX_data_scene(C);
1059 clear_id_newpoins();
1061 CTX_DATA_BEGIN(C, Base*, base, selected_editable_bases) {
1062 make_object_duplilist_real(C, scene, base);
1064 /* dependencies were changed */
1065 WM_event_add_notifier(C, NC_OBJECT|ND_PARENT, base->object);
1069 DAG_scene_sort(bmain, scene);
1070 DAG_ids_flush_update(bmain, 0);
1071 WM_event_add_notifier(C, NC_SCENE, scene);
1072 WM_main_add_notifier(NC_OBJECT|ND_DRAW, NULL);
1074 return OPERATOR_FINISHED;
1077 void OBJECT_OT_duplicates_make_real(wmOperatorType *ot)
1081 ot->name= "Make Duplicates Real";
1082 ot->description = "Make dupli objects attached to this object real";
1083 ot->idname= "OBJECT_OT_duplicates_make_real";
1086 ot->exec= object_duplicates_make_real_exec;
1088 ot->poll= ED_operator_objectmode;
1091 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1094 /**************************** Convert **************************/
1096 static EnumPropertyItem convert_target_items[]= {
1097 {OB_CURVE, "CURVE", ICON_OUTLINER_OB_CURVE, "Curve from Mesh/Text", ""},
1098 {OB_MESH, "MESH", ICON_OUTLINER_OB_MESH, "Mesh from Curve/Meta/Surf/Text", ""},
1099 {0, NULL, 0, NULL, NULL}};
1101 static void curvetomesh(Scene *scene, Object *ob)
1103 if(ob->disp.first == NULL)
1104 makeDispListCurveTypes(scene, ob, 0); /* force creation */
1106 nurbs_to_mesh(ob); /* also does users */
1108 if(ob->type == OB_MESH)
1109 object_free_modifiers(ob);
1112 static int convert_poll(bContext *C)
1114 Object *obact= CTX_data_active_object(C);
1115 Scene *scene= CTX_data_scene(C);
1117 return (!scene->id.lib && obact && scene->obedit != obact && (obact->flag & SELECT) && !(obact->id.lib));
1120 /* Helper for convert_exec */
1121 static Base *duplibase_for_convert(Scene *scene, Base *base, Object *ob)
1130 obn= copy_object(ob);
1131 obn->recalc |= OB_RECALC_OB|OB_RECALC_DATA|OB_RECALC_TIME;
1133 basen= MEM_mallocN(sizeof(Base), "duplibase");
1135 BLI_addhead(&scene->base, basen); /* addhead: otherwise eternal loop */
1137 basen->flag |= SELECT;
1138 obn->flag |= SELECT;
1139 base->flag &= ~SELECT;
1140 ob->flag &= ~SELECT;
1145 static int convert_exec(bContext *C, wmOperator *op)
1147 Main *bmain= CTX_data_main(C);
1148 Scene *scene= CTX_data_scene(C);
1149 Base *basen=NULL, *basact=NULL, *basedel=NULL;
1150 Object *ob, *ob1, *newob, *obact= CTX_data_active_object(C);
1156 const short target= RNA_enum_get(op->ptr, "target");
1157 const short keep_original= RNA_boolean_get(op->ptr, "keep_original");
1158 int a, mballConverted= 0;
1160 /* don't forget multiple users! */
1163 CTX_DATA_BEGIN(C, Base*, base, selected_editable_bases) {
1165 ob->flag &= ~OB_DONE;
1167 /* flag data thats not been edited (only needed for !keep_original) */
1169 ((ID *)ob->data)->flag |= LIB_DOIT;
1174 CTX_DATA_BEGIN(C, Base*, base, selected_editable_bases) {
1177 if(ob->flag & OB_DONE || !IS_TAGGED(ob->data)) {
1178 if (ob->type != target) {
1179 base->flag &= ~SELECT;
1180 ob->flag &= ~SELECT;
1183 /* obdata already modified */
1184 if(!IS_TAGGED(ob->data)) {
1185 /* When 2 objects with linked data are selected, converting both
1186 * would keep modifiers on all but the converted object [#26003] */
1187 if(ob->type == OB_MESH) {
1188 object_free_modifiers(ob); /* after derivedmesh calls! */
1192 else if (ob->type==OB_MESH && target == OB_CURVE) {
1193 ob->flag |= OB_DONE;
1195 if (keep_original) {
1196 basen= duplibase_for_convert(scene, base, NULL);
1197 newob= basen->object;
1199 /* decrement original mesh's usage count */
1203 /* make a new copy of the mesh */
1204 newob->data= copy_mesh(me);
1209 mesh_to_curve(scene, newob);
1211 if(newob->type==OB_CURVE)
1212 object_free_modifiers(newob); /* after derivedmesh calls! */
1214 else if(ob->type==OB_MESH && ob->modifiers.first) { /* converting a mesh with no modifiers causes a segfault */
1215 ob->flag |= OB_DONE;
1217 if (keep_original) {
1218 basen= duplibase_for_convert(scene, base, NULL);
1219 newob= basen->object;
1221 /* decrement original mesh's usage count */
1225 /* make a new copy of the mesh */
1226 newob->data= copy_mesh(me);
1229 ob->recalc |= OB_RECALC_OB|OB_RECALC_DATA|OB_RECALC_TIME;
1232 /* make new mesh data from the original copy */
1233 /* note: get the mesh from the original, not from the copy in some
1234 * cases this doesnt give correct results (when MDEF is used for eg)
1236 dm= mesh_get_derived_final(scene, newob, CD_MASK_MESH);
1237 /* dm= mesh_create_derived_no_deform(ob1, NULL); this was called original (instead of get_derived). man o man why! (ton) */
1239 DM_to_mesh(dm, newob->data);
1242 object_free_modifiers(newob); /* after derivedmesh calls! */
1244 else if(ob->type==OB_FONT) {
1245 ob->flag |= OB_DONE;
1247 if (keep_original) {
1248 basen= duplibase_for_convert(scene, base, NULL);
1249 newob= basen->object;
1251 /* decrement original curve's usage count */
1252 ((Curve *)newob->data)->id.us--;
1254 /* make a new copy of the curve */
1255 newob->data= copy_curve(ob->data);
1262 if (!newob->disp.first)
1263 makeDispListCurveTypes(scene, newob, 0);
1265 newob->type= OB_CURVE;
1272 cu->vfontb->id.us--;
1276 cu->vfonti->id.us--;
1280 cu->vfontbi->id.us--;
1284 if (!keep_original) {
1287 for(ob1= bmain->object.first; ob1; ob1=ob1->id.next) {
1288 if(ob1->data==ob->data) {
1289 ob1->type= OB_CURVE;
1290 ob1->recalc |= OB_RECALC_OB|OB_RECALC_DATA|OB_RECALC_TIME;
1296 for(nu=cu->nurb.first; nu; nu=nu->next)
1299 if(target == OB_MESH) {
1300 curvetomesh(scene, newob);
1302 /* meshes doesn't use displist */
1303 freedisplist(&newob->disp);
1306 else if(ELEM(ob->type, OB_CURVE, OB_SURF)) {
1307 ob->flag |= OB_DONE;
1309 if(target == OB_MESH) {
1310 if (keep_original) {
1311 basen= duplibase_for_convert(scene, base, NULL);
1312 newob= basen->object;
1314 /* decrement original curve's usage count */
1315 ((Curve *)newob->data)->id.us--;
1317 /* make a new copy of the curve */
1318 newob->data= copy_curve(ob->data);
1322 /* meshes doesn't use displist */
1323 freedisplist(&newob->disp);
1326 curvetomesh(scene, newob);
1329 else if(ob->type==OB_MBALL && target == OB_MESH) {
1332 base->flag &= ~SELECT;
1333 ob->flag &= ~SELECT;
1335 baseob= find_basis_mball(scene, ob);
1338 /* if motherball is converting it would be marked as done later */
1339 ob->flag |= OB_DONE;
1342 if (!baseob->disp.first) {
1343 makeDispListMBall(scene, baseob);
1346 if(!(baseob->flag & OB_DONE)) {
1347 baseob->flag |= OB_DONE;
1349 basen= duplibase_for_convert(scene, base, baseob);
1350 newob= basen->object;
1355 newob->data= add_mesh("Mesh");
1356 newob->type= OB_MESH;
1359 me->totcol= mb->totcol;
1361 me->mat= MEM_dupallocN(mb->mat);
1362 for(a=0; a<newob->totcol; a++) id_us_plus((ID *)me->mat[a]);
1365 mball_to_mesh(&baseob->disp, newob->data);
1367 if (obact->type == OB_MBALL) {
1378 /* tag obdata if it was been changed */
1380 /* If the original object is active then make this object active */
1383 /* store new active base to update BASACT */
1390 if (!keep_original && (ob->flag & OB_DONE)) {
1391 DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
1392 ((ID *)ob->data)->flag &= ~LIB_DOIT; /* flag not to convert this datablock again */
1395 /* delete original if needed */
1398 ED_base_object_free_and_unlink(bmain, scene, basedel);
1405 if(!keep_original) {
1406 if (mballConverted) {
1407 Base *base= scene->base.first, *tmpbase;
1413 if (ob->type == OB_MBALL) {
1414 ED_base_object_free_and_unlink(bmain, scene, tmpbase);
1419 /* delete object should renew depsgraph */
1420 DAG_scene_sort(bmain, scene);
1423 // XXX ED_object_enter_editmode(C, 0);
1424 // XXX exit_editmode(C, EM_FREEDATA|EM_WAITCURSOR); /* freedata, but no undo */
1427 /* active base was changed */
1428 ED_base_object_activate(C, basact);
1430 } else if (BASACT->object->flag & OB_DONE) {
1431 WM_event_add_notifier(C, NC_OBJECT|ND_MODIFIER, BASACT->object);
1432 WM_event_add_notifier(C, NC_OBJECT|ND_DATA, BASACT->object);
1435 DAG_scene_sort(bmain, scene);
1436 WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, scene);
1437 WM_event_add_notifier(C, NC_SCENE|ND_OB_SELECT, scene);
1439 return OPERATOR_FINISHED;
1443 void OBJECT_OT_convert(wmOperatorType *ot)
1446 ot->name= "Convert to";
1447 ot->description = "Convert selected objects to another type";
1448 ot->idname= "OBJECT_OT_convert";
1451 ot->invoke= WM_menu_invoke;
1452 ot->exec= convert_exec;
1453 ot->poll= convert_poll;
1456 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1459 ot->prop= RNA_def_enum(ot->srna, "target", convert_target_items, OB_MESH, "Target", "Type of object to convert to");
1460 RNA_def_boolean(ot->srna, "keep_original", 0, "Keep Original", "Keep original objects instead of replacing them");
1463 /**************************** Duplicate ************************/
1466 dupflag: a flag made from constants declared in DNA_userdef_types.h
1467 The flag tells adduplicate() weather to copy data linked to the object, or to reference the existing data.
1468 U.dupflag for default operations or you can construct a flag as python does
1469 if the dupflag is 0 then no data will be copied (linked duplicate) */
1471 /* used below, assumes id.new is correct */
1472 /* leaves selection of base/object unaltered */
1473 static Base *object_add_duplicate_internal(Main *bmain, Scene *scene, Base *base, int dupflag)
1476 Material ***matarar;
1482 if(ob->mode & OB_MODE_POSE) {
1486 obn= copy_object(ob);
1487 obn->recalc |= OB_RECALC_OB|OB_RECALC_DATA|OB_RECALC_TIME;
1489 basen= MEM_mallocN(sizeof(Base), "duplibase");
1491 BLI_addhead(&scene->base, basen); /* addhead: prevent eternal loop */
1494 if(basen->flag & OB_FROMGROUP) {
1496 for(group= bmain->group.first; group; group= group->id.next) {
1497 if(object_in_group(ob, group))
1498 add_to_group(group, obn, scene, basen);
1502 /* duplicates using userflags */
1503 if(dupflag & USER_DUP_ACT) {
1504 BKE_copy_animdata_id_action(&obn->id);
1507 if(dupflag & USER_DUP_MAT) {
1508 for(a=0; a<obn->totcol; a++) {
1509 id= (ID *)obn->mat[a];
1511 ID_NEW_US(obn->mat[a])
1512 else obn->mat[a]= copy_material(obn->mat[a]);
1515 if(dupflag & USER_DUP_ACT) {
1516 BKE_copy_animdata_id_action(&obn->mat[a]->id);
1521 if(dupflag & USER_DUP_PSYS) {
1522 ParticleSystem *psys;
1523 for(psys=obn->particlesystem.first; psys; psys=psys->next) {
1524 id= (ID*) psys->part;
1526 ID_NEW_US(psys->part)
1527 else psys->part= psys_copy_settings(psys->part);
1529 if(dupflag & USER_DUP_ACT) {
1530 BKE_copy_animdata_id_action(&psys->part->id);
1543 if(dupflag & USER_DUP_MESH) {
1544 ID_NEW_US2( obn->data )
1546 obn->data= copy_mesh(obn->data);
1548 if(obn->fluidsimSettings) {
1549 obn->fluidsimSettings->orgMesh = (Mesh *)obn->data;
1558 if(dupflag & USER_DUP_CURVE) {
1559 ID_NEW_US2(obn->data )
1561 obn->data= copy_curve(obn->data);
1568 if(dupflag & USER_DUP_SURF) {
1569 ID_NEW_US2( obn->data )
1571 obn->data= copy_curve(obn->data);
1578 if(dupflag & USER_DUP_FONT) {
1579 ID_NEW_US2( obn->data )
1581 obn->data= copy_curve(obn->data);
1588 if(dupflag & USER_DUP_MBALL) {
1589 ID_NEW_US2(obn->data )
1591 obn->data= copy_mball(obn->data);
1598 if(dupflag & USER_DUP_LAMP) {
1599 ID_NEW_US2(obn->data )
1601 obn->data= copy_lamp(obn->data);
1609 obn->recalc |= OB_RECALC_DATA;
1610 if(obn->pose) obn->pose->flag |= POSE_RECALC;
1612 if(dupflag & USER_DUP_ARM) {
1613 ID_NEW_US2(obn->data )
1615 obn->data= copy_armature(obn->data);
1616 armature_rebuild_pose(obn, obn->data);
1626 ID_NEW_US2(obn->data )
1628 obn->data= copy_lattice(obn->data);
1636 ID_NEW_US2(obn->data )
1638 obn->data= copy_camera(obn->data);
1646 ID_NEW_US2(obn->data )
1648 obn->data= copy_speaker(obn->data);
1657 /* check if obdata is copied */
1659 Key *key = ob_get_key(obn);
1661 if(dupflag & USER_DUP_ACT) {
1662 BKE_copy_animdata_id_action((ID *)obn->data);
1663 if(key) BKE_copy_animdata_id_action((ID*)key);
1666 if(dupflag & USER_DUP_MAT) {
1667 matarar= give_matarar(obn);
1669 for(a=0; a<obn->totcol; a++) {
1670 id= (ID *)(*matarar)[a];
1672 ID_NEW_US( (*matarar)[a] )
1673 else (*matarar)[a]= copy_material((*matarar)[a]);
1685 /* single object duplicate, if dupflag==0, fully linked, else it uses the flags given */
1686 /* leaves selection of base/object unaltered.
1687 * note: don't call this within a loop since clear_* funcs loop over the entire database. */
1688 Base *ED_object_add_duplicate(Main *bmain, Scene *scene, Base *base, int dupflag)
1693 clear_id_newpoins();
1694 clear_sca_new_poins(); /* sensor/contr/act */
1696 basen= object_add_duplicate_internal(bmain, scene, base, dupflag);
1697 if (basen == NULL) {
1703 /* link own references to the newly duplicated data [#26816] */
1705 set_sca_new_poins_ob(ob);
1707 DAG_scene_sort(bmain, scene);
1708 ED_render_id_flush_update(bmain, ob->data);
1713 /* contextual operator dupli */
1714 static int duplicate_exec(bContext *C, wmOperator *op)
1716 Main *bmain= CTX_data_main(C);
1717 Scene *scene= CTX_data_scene(C);
1718 int linked= RNA_boolean_get(op->ptr, "linked");
1719 int dupflag= (linked)? 0: U.dupflag;
1721 clear_id_newpoins();
1722 clear_sca_new_poins(); /* sensor/contr/act */
1724 CTX_DATA_BEGIN(C, Base*, base, selected_editable_bases) {
1725 Base *basen= object_add_duplicate_internal(bmain, scene, base, dupflag);
1727 /* note that this is safe to do with this context iterator,
1728 the list is made in advance */
1729 ED_base_object_select(base, BA_DESELECT);
1731 if (basen == NULL) {
1735 /* new object becomes active */
1737 ED_base_object_activate(C, basen);
1739 if(basen->object->data) {
1740 DAG_id_tag_update(basen->object->data, 0);
1745 copy_object_set_idnew(C, dupflag);
1747 DAG_scene_sort(bmain, scene);
1748 DAG_ids_flush_update(bmain, 0);
1750 WM_event_add_notifier(C, NC_SCENE|ND_OB_SELECT, scene);
1752 return OPERATOR_FINISHED;
1755 void OBJECT_OT_duplicate(wmOperatorType *ot)
1760 ot->name= "Duplicate Objects";
1761 ot->description = "Duplicate selected objects";
1762 ot->idname= "OBJECT_OT_duplicate";
1765 ot->exec= duplicate_exec;
1766 ot->poll= ED_operator_objectmode;
1769 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1771 /* to give to transform */
1772 RNA_def_boolean(ot->srna, "linked", 0, "Linked", "Duplicate object but not object data, linking to the original data");
1773 prop= RNA_def_enum(ot->srna, "mode", transform_mode_types, TFM_TRANSLATION, "Mode", "");
1774 RNA_def_property_flag(prop, PROP_HIDDEN);
1777 /* **************** add named object, for dragdrop ************* */
1780 static int add_named_exec(bContext *C, wmOperator *op)
1782 Main *bmain= CTX_data_main(C);
1783 Scene *scene= CTX_data_scene(C);
1786 int linked= RNA_boolean_get(op->ptr, "linked");
1787 int dupflag= (linked)? 0: U.dupflag;
1790 /* find object, create fake base */
1791 RNA_string_get(op->ptr, "name", name);
1792 ob= (Object *)find_id("OB", name);
1794 return OPERATOR_CANCELLED;
1796 base= MEM_callocN(sizeof(Base), "duplibase");
1798 base->flag= ob->flag;
1801 clear_id_newpoins();
1802 clear_sca_new_poins(); /* sensor/contr/act */
1804 basen= object_add_duplicate_internal(bmain, scene, base, dupflag);
1806 if (basen == NULL) {
1808 return OPERATOR_CANCELLED;
1811 basen->lay= basen->object->lay= scene->lay;
1813 ED_object_location_from_view(C, basen->object->loc);
1814 ED_base_object_activate(C, basen);
1816 copy_object_set_idnew(C, dupflag);
1818 DAG_scene_sort(bmain, scene);
1819 DAG_ids_flush_update(bmain, 0);
1823 WM_event_add_notifier(C, NC_SCENE|ND_OB_SELECT, scene);
1825 return OPERATOR_FINISHED;
1828 void OBJECT_OT_add_named(wmOperatorType *ot)
1831 ot->name= "Add Named Object";
1832 ot->description = "Add named object";
1833 ot->idname= "OBJECT_OT_add_named";
1836 ot->exec= add_named_exec;
1837 ot->poll= ED_operator_objectmode;
1840 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1842 RNA_def_boolean(ot->srna, "linked", 0, "Linked", "Duplicate object but not object data, linking to the original data");
1843 RNA_def_string(ot->srna, "name", "Cube", 24, "Name", "Object name to add");
1848 /**************************** Join *************************/
1849 static int join_poll(bContext *C)
1851 Object *ob= CTX_data_active_object(C);
1853 if (!ob || ob->id.lib) return 0;
1855 if (ELEM4(ob->type, OB_MESH, OB_CURVE, OB_SURF, OB_ARMATURE))
1856 return ED_operator_screenactive(C);
1862 static int join_exec(bContext *C, wmOperator *op)
1864 Scene *scene= CTX_data_scene(C);
1865 Object *ob= CTX_data_active_object(C);
1868 BKE_report(op->reports, RPT_ERROR, "This data does not support joining in editmode");
1869 return OPERATOR_CANCELLED;
1871 else if(object_data_is_libdata(ob)) {
1872 BKE_report(op->reports, RPT_ERROR, "Can't edit external libdata");
1873 return OPERATOR_CANCELLED;
1876 if(ob->type == OB_MESH)
1877 return join_mesh_exec(C, op);
1878 else if(ELEM(ob->type, OB_CURVE, OB_SURF))
1879 return join_curve_exec(C, op);
1880 else if(ob->type == OB_ARMATURE)
1881 return join_armature_exec(C, op);
1883 return OPERATOR_CANCELLED;
1886 void OBJECT_OT_join(wmOperatorType *ot)
1890 ot->description = "Join selected objects into active object";
1891 ot->idname= "OBJECT_OT_join";
1894 ot->exec= join_exec;
1895 ot->poll= join_poll;
1898 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1901 /**************************** Join as Shape Key*************************/
1902 static int join_shapes_poll(bContext *C)
1904 Object *ob= CTX_data_active_object(C);
1906 if (!ob || ob->id.lib) return 0;
1908 /* only meshes supported at the moment */
1909 if (ob->type == OB_MESH)
1910 return ED_operator_screenactive(C);
1915 static int join_shapes_exec(bContext *C, wmOperator *op)
1917 Scene *scene= CTX_data_scene(C);
1918 Object *ob= CTX_data_active_object(C);
1921 BKE_report(op->reports, RPT_ERROR, "This data does not support joining in editmode");
1922 return OPERATOR_CANCELLED;
1924 else if(object_data_is_libdata(ob)) {
1925 BKE_report(op->reports, RPT_ERROR, "Can't edit external libdata");
1926 return OPERATOR_CANCELLED;
1929 if(ob->type == OB_MESH)
1930 return join_mesh_shapes_exec(C, op);
1932 return OPERATOR_CANCELLED;
1935 void OBJECT_OT_join_shapes(wmOperatorType *ot)
1938 ot->name= "Join as Shapes";
1939 ot->description = "Merge selected objects to shapes of active object";
1940 ot->idname= "OBJECT_OT_join_shapes";
1943 ot->exec= join_shapes_exec;
1944 ot->poll= join_shapes_poll;
1947 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;