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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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 *****
31 #include "MEM_guardedalloc.h"
33 #include "DNA_action_types.h"
34 #include "DNA_curve_types.h"
35 #include "DNA_group_types.h"
36 #include "DNA_lamp_types.h"
37 #include "DNA_material_types.h"
38 #include "DNA_mesh_types.h"
39 #include "DNA_meta_types.h"
40 #include "DNA_object_fluidsim.h"
41 #include "DNA_object_types.h"
42 #include "DNA_object_force.h"
43 #include "DNA_scene_types.h"
44 #include "DNA_screen_types.h"
45 #include "DNA_userdef_types.h"
46 #include "DNA_view3d_types.h"
47 #include "DNA_vfont_types.h"
50 #include "BLI_listbase.h"
53 #include "BKE_armature.h"
54 #include "BKE_constraint.h"
55 #include "BKE_context.h"
56 #include "BKE_curve.h"
57 #include "BKE_customdata.h"
58 #include "BKE_depsgraph.h"
59 #include "BKE_DerivedMesh.h"
60 #include "BKE_displist.h"
61 #include "BKE_effect.h"
62 #include "BKE_global.h"
63 #include "BKE_group.h"
64 #include "BKE_lattice.h"
65 #include "BKE_library.h"
67 #include "BKE_material.h"
68 #include "BKE_mball.h"
70 #include "BKE_modifier.h"
71 #include "BKE_object.h"
72 #include "BKE_particle.h"
73 #include "BKE_report.h"
75 #include "BKE_scene.h"
76 #include "BKE_texture.h"
77 #include "BKE_utildefines.h"
79 #include "RNA_access.h"
80 #include "RNA_define.h"
81 #include "RNA_enum_types.h"
86 #include "ED_anim_api.h"
87 #include "ED_armature.h"
91 #include "ED_object.h"
92 #include "ED_screen.h"
93 #include "ED_transform.h"
95 #include "UI_interface.h"
96 #include "UI_resources.h"
98 #include "object_intern.h"
100 /************************** Exported *****************************/
102 void ED_object_base_init_from_view(bContext *C, Base *base, int view_align)
104 View3D *v3d= CTX_wm_view3d(C);
105 Scene *scene= CTX_data_scene(C);
106 Object *ob= base->object;
112 base->lay = scene->lay;
113 VECCOPY(ob->loc, scene->cursor);
117 base->lay= ob->lay= v3d->layact | v3d->lay;
118 VECCOPY(ob->loc, v3d->cursor);
121 base->lay= ob->lay= v3d->layact;
122 VECCOPY(ob->loc, scene->cursor);
126 RegionView3D *rv3d = CTX_wm_region_view3d(C);
128 rv3d->viewquat[0]= -rv3d->viewquat[0];
129 quat_to_eul( ob->rot,rv3d->viewquat);
130 rv3d->viewquat[0]= -rv3d->viewquat[0];
134 where_is_object(scene, ob);
137 /********************* Add Object Operator ********************/
139 void add_object_draw(Scene *scene, View3D *v3d, int type) /* for toolbox or menus, only non-editmode stuff */
141 /* keep here to get things compile, remove later */
144 void ED_object_add_generic_props(wmOperatorType *ot, int do_editmode)
146 RNA_def_boolean(ot->srna, "view_align", 0, "View Align", "Align the new object to the view.");
149 RNA_def_boolean(ot->srna, "enter_editmode", 0, "Enter Editmode", "Enter editmode when adding this object.");
152 static void object_add_generic_invoke_options(bContext *C, wmOperator *op)
154 if (!RNA_property_is_set(op->ptr, "view_align"))
155 RNA_boolean_set(op->ptr, "view_align", U.flag & USER_ADD_VIEWALIGNED);
157 if(RNA_struct_find_property(op->ptr, "enter_editmode")) /* optional */
158 if (!RNA_property_is_set(op->ptr, "enter_editmode"))
159 RNA_boolean_set(op->ptr, "enter_editmode", U.flag & USER_ADD_EDITMODE);
162 int ED_object_add_generic_invoke(bContext *C, wmOperator *op, wmEvent *event)
164 object_add_generic_invoke_options(C, op);
165 return op->type->exec(C, op);
168 void ED_object_add_generic_get_opts(wmOperator *op, int *view_align, int *enter_editmode)
170 *view_align= RNA_boolean_get(op->ptr, "view_align");
171 *enter_editmode = FALSE;
173 if(RNA_struct_find_property(op->ptr, "enter_editmode") && RNA_boolean_get(op->ptr, "enter_editmode")) {
174 *enter_editmode = TRUE;
178 /* for object add primitive operators */
179 Object *ED_object_add_type(bContext *C, int type, int view_align, int enter_editmode)
181 Scene *scene= CTX_data_scene(C);
184 /* for as long scene has editmode... */
185 if (CTX_data_edit_object(C))
186 ED_object_exit_editmode(C, EM_FREEDATA|EM_FREEUNDO|EM_WAITCURSOR|EM_DO_UNDO); /* freedata, and undo */
188 /* deselects all, sets scene->basact */
189 ob= add_object(scene, type);
190 /* editor level activate, notifiers */
191 ED_base_object_activate(C, BASACT);
193 /* more editor stuff */
194 ED_object_base_init_from_view(C, BASACT, view_align);
196 DAG_scene_sort(scene);
199 ED_object_enter_editmode(C, EM_IGNORE_LAYER);
204 /* for object add operator */
205 static int object_add_exec(bContext *C, wmOperator *op)
207 int view_align, enter_editmode;
208 ED_object_add_generic_get_opts(op, &view_align, &enter_editmode);
209 ED_object_add_type(C, RNA_enum_get(op->ptr, "type"), view_align, enter_editmode);
211 return OPERATOR_FINISHED;
214 void OBJECT_OT_add(wmOperatorType *ot)
217 ot->name= "Add Object";
218 ot->description = "Add an object to the scene.";
219 ot->idname= "OBJECT_OT_add";
222 ot->invoke= ED_object_add_generic_invoke;
223 ot->exec= object_add_exec;
225 ot->poll= ED_operator_scene_editable;
228 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
230 RNA_def_enum(ot->srna, "type", object_type_items, 0, "Type", "");
232 ED_object_add_generic_props(ot, TRUE);
235 /********************* Add Effector Operator ********************/
236 /* copy from rna_object_force.c*/
237 static EnumPropertyItem field_type_items[] = {
238 {0, "NONE", 0, "None", ""},
239 {PFIELD_FORCE, "FORCE", 0, "Force", ""},
240 {PFIELD_WIND, "WIND", 0, "Wind", ""},
241 {PFIELD_VORTEX, "VORTEX", 0, "Vortex", ""},
242 {PFIELD_MAGNET, "MAGNET", 0, "Magnetic", ""},
243 {PFIELD_HARMONIC, "HARMONIC", 0, "Harmonic", ""},
244 {PFIELD_CHARGE, "CHARGE", 0, "Charge", ""},
245 {PFIELD_LENNARDJ, "LENNARDJ", 0, "Lennard-Jones", ""},
246 {PFIELD_TEXTURE, "TEXTURE", 0, "Texture", ""},
247 {PFIELD_GUIDE, "GUIDE", 0, "Curve Guide", ""},
248 {PFIELD_BOID, "BOID", 0, "Boid", ""},
249 {PFIELD_TURBULENCE, "TURBULENCE", 0, "Turbulence", ""},
250 {PFIELD_DRAG, "DRAG", 0, "Drag", ""},
251 {0, NULL, 0, NULL, NULL}};
253 void add_effector_draw(Scene *scene, View3D *v3d, int type) /* for toolbox or menus, only non-editmode stuff */
255 /* keep here to get things compile, remove later */
258 /* for effector add primitive operators */
259 static Object *effector_add_type(bContext *C, wmOperator *op, int type)
262 int view_align, enter_editmode;
263 ED_object_add_generic_get_opts(op, &view_align, &enter_editmode);
265 if(type==PFIELD_GUIDE) {
266 ob= ED_object_add_type(C, OB_CURVE, view_align, FALSE);
267 rename_id(&ob->id, "CurveGuide");
269 ((Curve*)ob->data)->flag |= CU_PATH|CU_3D;
270 ED_object_enter_editmode(C, 0);
271 BLI_addtail(curve_get_editcurve(ob), add_nurbs_primitive(C, CU_NURBS|CU_PRIM_PATH, 1));
274 ED_object_exit_editmode(C, EM_FREEDATA|EM_DO_UNDO);
277 ob= ED_object_add_type(C, OB_EMPTY, view_align, FALSE);
278 rename_id(&ob->id, "Field");
283 ob->empty_drawtype = OB_SINGLE_ARROW;
288 ob->pd= object_add_collision_fields(type);
293 /* for object add operator */
294 static int effector_add_exec(bContext *C, wmOperator *op)
296 effector_add_type(C, op, RNA_int_get(op->ptr, "type"));
298 return OPERATOR_FINISHED;
301 void OBJECT_OT_effector_add(wmOperatorType *ot)
304 ot->name= "Add Effector";
305 ot->description = "Add an empty object with a physics effector to the scene.";
306 ot->idname= "OBJECT_OT_effector_add";
309 ot->invoke= WM_menu_invoke;
310 ot->exec= effector_add_exec;
312 ot->poll= ED_operator_scene_editable;
315 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
317 RNA_def_enum(ot->srna, "type", field_type_items, 0, "Type", "");
319 ED_object_add_generic_props(ot, TRUE);
322 /* ***************** add primitives *************** */
324 static EnumPropertyItem prop_curve_types[] = {
325 {CU_BEZIER|CU_PRIM_CURVE, "BEZIER_CURVE", ICON_CURVE_BEZCURVE, "Bezier Curve", ""},
326 {CU_BEZIER|CU_PRIM_CIRCLE, "BEZIER_CIRCLE", ICON_CURVE_BEZCIRCLE, "Bezier Circle", ""},
327 {CU_NURBS|CU_PRIM_CURVE, "NURBS_CURVE", ICON_CURVE_NCURVE, "NURBS Curve", ""},
328 {CU_NURBS|CU_PRIM_CIRCLE, "NURBS_CIRCLE", ICON_CURVE_NCIRCLE, "NURBS Circle", ""},
329 {CU_NURBS|CU_PRIM_PATH, "PATH", ICON_CURVE_PATH, "Path", ""},
330 {0, NULL, 0, NULL, NULL}
333 static int object_add_curve_exec(bContext *C, wmOperator *op)
335 Object *obedit= CTX_data_edit_object(C);
338 int newob= 0, type= RNA_enum_get(op->ptr, "type");
339 int view_align, enter_editmode;
341 object_add_generic_invoke_options(C, op); // XXX these props don't get set right when only exec() is called
342 ED_object_add_generic_get_opts(op, &view_align, &enter_editmode);
344 if(obedit==NULL || obedit->type!=OB_CURVE) {
345 obedit= ED_object_add_type(C, OB_CURVE, view_align, TRUE);
348 if(type & CU_PRIM_PATH)
349 ((Curve*)obedit->data)->flag |= CU_PATH|CU_3D;
351 else DAG_id_flush_update(&obedit->id, OB_RECALC_DATA);
353 nu= add_nurbs_primitive(C, type, newob);
354 editnurb= curve_get_editcurve(obedit);
355 BLI_addtail(editnurb, nu);
358 if (newob && !enter_editmode) {
359 ED_object_exit_editmode(C, EM_FREEDATA|EM_DO_UNDO);
362 WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, obedit);
364 return OPERATOR_FINISHED;
367 static int object_add_curve_invoke(bContext *C, wmOperator *op, wmEvent *event)
369 Object *obedit= CTX_data_edit_object(C);
373 object_add_generic_invoke_options(C, op);
375 pup= uiPupMenuBegin(C, op->type->name, 0);
376 layout= uiPupMenuLayout(pup);
377 if(!obedit || obedit->type == OB_CURVE)
378 uiItemsEnumO(layout, op->type->idname, "type");
380 uiItemsEnumO(layout, "OBJECT_OT_surface_add", "type");
381 uiPupMenuEnd(C, pup);
383 return OPERATOR_CANCELLED;
386 void OBJECT_OT_curve_add(wmOperatorType *ot)
389 ot->name= "Add Curve";
390 ot->description = "Add a curve object to the scene.";
391 ot->idname= "OBJECT_OT_curve_add";
394 ot->invoke= object_add_curve_invoke;
395 ot->exec= object_add_curve_exec;
397 ot->poll= ED_operator_scene_editable;
400 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
402 RNA_def_enum(ot->srna, "type", prop_curve_types, 0, "Primitive", "");
404 ED_object_add_generic_props(ot, TRUE);
407 static EnumPropertyItem prop_surface_types[]= {
408 {CU_PRIM_CURVE|CU_NURBS, "NURBS_CURVE", ICON_SURFACE_NCURVE, "NURBS Curve", ""},
409 {CU_PRIM_CIRCLE|CU_NURBS, "NURBS_CIRCLE", ICON_SURFACE_NCIRCLE, "NURBS Circle", ""},
410 {CU_PRIM_PATCH|CU_NURBS, "NURBS_SURFACE", ICON_SURFACE_NSURFACE, "NURBS Surface", ""},
411 {CU_PRIM_TUBE|CU_NURBS, "NURBS_TUBE", ICON_SURFACE_NTUBE, "NURBS Tube", ""},
412 {CU_PRIM_SPHERE|CU_NURBS, "NURBS_SPHERE", ICON_SURFACE_NSPHERE, "NURBS Sphere", ""},
413 {CU_PRIM_DONUT|CU_NURBS, "NURBS_DONUT", ICON_SURFACE_NDONUT, "NURBS Donut", ""},
414 {0, NULL, 0, NULL, NULL}
417 static int object_add_surface_exec(bContext *C, wmOperator *op)
419 Object *obedit= CTX_data_edit_object(C);
423 int view_align, enter_editmode;
425 object_add_generic_invoke_options(C, op); // XXX these props don't get set right when only exec() is called
426 ED_object_add_generic_get_opts(op, &view_align, &enter_editmode);
428 if(obedit==NULL || obedit->type!=OB_SURF) {
429 obedit= ED_object_add_type(C, OB_SURF, view_align, TRUE);
432 else DAG_id_flush_update(&obedit->id, OB_RECALC_DATA);
434 nu= add_nurbs_primitive(C, RNA_enum_get(op->ptr, "type"), newob);
435 editnurb= curve_get_editcurve(obedit);
436 BLI_addtail(editnurb, nu);
439 if (newob && !enter_editmode) {
440 ED_object_exit_editmode(C, EM_FREEDATA|EM_DO_UNDO);
443 WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, obedit);
445 return OPERATOR_FINISHED;
448 void OBJECT_OT_surface_add(wmOperatorType *ot)
451 ot->name= "Add Surface";
452 ot->description = "Add a surface object to the scene.";
453 ot->idname= "OBJECT_OT_surface_add";
456 ot->invoke= ED_object_add_generic_invoke; // WM_menu_invoke
457 ot->exec= object_add_surface_exec;
459 ot->poll= ED_operator_scene_editable;
462 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
464 RNA_def_enum(ot->srna, "type", prop_surface_types, 0, "Primitive", "");
465 ED_object_add_generic_props(ot, TRUE);
468 static EnumPropertyItem prop_metaball_types[]= {
469 {MB_BALL, "MBALL_BALL", ICON_META_BALL, "Meta Ball", ""},
470 {MB_TUBE, "MBALL_TUBE", ICON_META_TUBE, "Meta Tube", ""},
471 {MB_PLANE, "MBALL_PLANE", ICON_META_PLANE, "Meta Plane", ""},
472 {MB_CUBE, "MBALL_CUBE", ICON_META_CUBE, "Meta Cube", ""},
473 {MB_ELIPSOID, "MBALL_ELLIPSOID", ICON_META_ELLIPSOID, "Meta Ellipsoid", ""},
474 {0, NULL, 0, NULL, NULL}
477 static int object_metaball_add_exec(bContext *C, wmOperator *op)
479 Object *obedit= CTX_data_edit_object(C);
483 int view_align, enter_editmode;
485 object_add_generic_invoke_options(C, op); // XXX these props don't get set right when only exec() is called
486 ED_object_add_generic_get_opts(op, &view_align, &enter_editmode);
488 if(obedit==NULL || obedit->type!=OB_MBALL) {
489 obedit= ED_object_add_type(C, OB_MBALL, view_align, TRUE);
492 else DAG_id_flush_update(&obedit->id, OB_RECALC_DATA);
494 elem= (MetaElem*)add_metaball_primitive(C, RNA_enum_get(op->ptr, "type"), newob);
495 mball= (MetaBall*)obedit->data;
496 BLI_addtail(mball->editelems, elem);
499 if (newob && !enter_editmode) {
500 ED_object_exit_editmode(C, EM_FREEDATA|EM_DO_UNDO);
503 WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, obedit);
505 return OPERATOR_FINISHED;
508 static int object_metaball_add_invoke(bContext *C, wmOperator *op, wmEvent *event)
510 Object *obedit= CTX_data_edit_object(C);
514 object_add_generic_invoke_options(C, op);
516 pup= uiPupMenuBegin(C, op->type->name, 0);
517 layout= uiPupMenuLayout(pup);
518 if(!obedit || obedit->type == OB_MBALL)
519 uiItemsEnumO(layout, op->type->idname, "type");
521 uiItemsEnumO(layout, "OBJECT_OT_metaball_add", "type");
522 uiPupMenuEnd(C, pup);
524 return OPERATOR_CANCELLED;
527 void OBJECT_OT_metaball_add(wmOperatorType *ot)
530 ot->name= "Add Metaball";
531 ot->description= "Add an metaball object to the scene.";
532 ot->idname= "OBJECT_OT_metaball_add";
535 ot->invoke= object_metaball_add_invoke;
536 ot->exec= object_metaball_add_exec;
537 ot->poll= ED_operator_scene_editable;
540 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
542 RNA_def_enum(ot->srna, "type", prop_metaball_types, 0, "Primitive", "");
543 ED_object_add_generic_props(ot, TRUE);
545 static int object_add_text_exec(bContext *C, wmOperator *op)
547 Object *obedit= CTX_data_edit_object(C);
548 int view_align, enter_editmode;
550 object_add_generic_invoke_options(C, op); // XXX these props don't get set right when only exec() is called
551 ED_object_add_generic_get_opts(op, &view_align, &enter_editmode);
553 if(obedit && obedit->type==OB_FONT)
554 return OPERATOR_CANCELLED;
556 obedit= ED_object_add_type(C, OB_FONT, view_align, enter_editmode);
558 WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, obedit);
560 return OPERATOR_FINISHED;
563 void OBJECT_OT_text_add(wmOperatorType *ot)
566 ot->name= "Add Text";
567 ot->description = "Add a text object to the scene";
568 ot->idname= "OBJECT_OT_text_add";
571 ot->invoke= ED_object_add_generic_invoke;
572 ot->exec= object_add_text_exec;
573 ot->poll= ED_operator_scene_editable;
576 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
577 ED_object_add_generic_props(ot, TRUE);
580 static int object_armature_add_exec(bContext *C, wmOperator *op)
582 Object *obedit= CTX_data_edit_object(C);
583 View3D *v3d= CTX_wm_view3d(C);
584 RegionView3D *rv3d= NULL;
586 int view_align, enter_editmode;
588 object_add_generic_invoke_options(C, op); // XXX these props don't get set right when only exec() is called
589 ED_object_add_generic_get_opts(op, &view_align, &enter_editmode);
591 if ((obedit==NULL) || (obedit->type != OB_ARMATURE)) {
592 obedit= ED_object_add_type(C, OB_ARMATURE, view_align, TRUE);
593 ED_object_enter_editmode(C, 0);
596 else DAG_id_flush_update(&obedit->id, OB_RECALC_DATA);
599 BKE_report(op->reports, RPT_ERROR, "Cannot create editmode armature.");
600 return OPERATOR_CANCELLED;
604 rv3d= CTX_wm_region(C)->regiondata;
606 /* v3d and rv3d are allowed to be NULL */
607 add_primitive_bone(CTX_data_scene(C), v3d, rv3d);
610 if (newob && !enter_editmode) {
611 ED_object_exit_editmode(C, EM_FREEDATA|EM_DO_UNDO);
614 WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, obedit);
616 return OPERATOR_FINISHED;
619 void OBJECT_OT_armature_add(wmOperatorType *ot)
622 ot->name= "Add Armature";
623 ot->description = "Add an armature object to the scene.";
624 ot->idname= "OBJECT_OT_armature_add";
627 ot->invoke= ED_object_add_generic_invoke;
628 ot->exec= object_armature_add_exec;
629 ot->poll= ED_operator_scene_editable;
632 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
633 ED_object_add_generic_props(ot, TRUE);
636 static int object_lamp_add_exec(bContext *C, wmOperator *op)
639 int type= RNA_enum_get(op->ptr, "type");
640 int view_align, enter_editmode;
641 ED_object_add_generic_get_opts(op, &view_align, &enter_editmode);
643 ob= ED_object_add_type(C, OB_LAMP, view_align, FALSE);
645 ((Lamp*)ob->data)->type= type;
647 return OPERATOR_FINISHED;
650 void OBJECT_OT_lamp_add(wmOperatorType *ot)
652 static EnumPropertyItem lamp_type_items[] = {
653 {LA_LOCAL, "POINT", ICON_LAMP_POINT, "Point", "Omnidirectional point light source."},
654 {LA_SUN, "SUN", ICON_LAMP_SUN, "Sun", "Constant direction parallel ray light source."},
655 {LA_SPOT, "SPOT", ICON_LAMP_SPOT, "Spot", "Directional cone light source."},
656 {LA_HEMI, "HEMI", ICON_LAMP_HEMI, "Hemi", "180 degree constant light source."},
657 {LA_AREA, "AREA", ICON_LAMP_AREA, "Area", "Directional area light source."},
658 {0, NULL, 0, NULL, NULL}};
661 ot->name= "Add Lamp";
662 ot->description = "Add a lamp object to the scene.";
663 ot->idname= "OBJECT_OT_lamp_add";
666 ot->invoke= WM_menu_invoke;
667 ot->exec= object_lamp_add_exec;
668 ot->poll= ED_operator_scene_editable;
671 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
674 RNA_def_enum(ot->srna, "type", lamp_type_items, 0, "Type", "");
676 ED_object_add_generic_props(ot, FALSE);
679 static int group_instance_add_exec(bContext *C, wmOperator *op)
681 Group *group= BLI_findlink(&CTX_data_main(C)->group, RNA_enum_get(op->ptr, "type"));
683 int view_align, enter_editmode;
684 ED_object_add_generic_get_opts(op, &view_align, &enter_editmode);
687 Object *ob= ED_object_add_type(C, OB_EMPTY, view_align, FALSE);
688 rename_id(&ob->id, group->id.name+2);
689 ob->dup_group= group;
690 ob->transflag |= OB_DUPLIGROUP;
691 id_us_plus(&group->id);
694 WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, ob);
696 return OPERATOR_FINISHED;
699 return OPERATOR_CANCELLED;
702 /* only used as menu */
703 void OBJECT_OT_group_instance_add(wmOperatorType *ot)
708 ot->name= "Add Group Instance";
709 ot->description = "Add a dupligroup instance.";
710 ot->idname= "OBJECT_OT_group_instance_add";
713 ot->exec= group_instance_add_exec;
715 ot->poll= ED_operator_scene_editable;
718 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
721 prop= RNA_def_enum(ot->srna, "type", DummyRNA_NULL_items, 0, "Type", "");
722 RNA_def_enum_funcs(prop, RNA_group_itemf);
723 ED_object_add_generic_props(ot, FALSE);
726 /**************************** Delete Object *************************/
728 /* remove base from a specific scene */
729 /* note: now unlinks constraints as well */
730 void ED_base_object_free_and_unlink(Scene *scene, Base *base)
732 BLI_remlink(&scene->base, base);
733 free_libblock_us(&G.main->object, base->object);
734 if(scene->basact==base) scene->basact= NULL;
738 static int object_delete_exec(bContext *C, wmOperator *op)
740 Scene *scene= CTX_data_scene(C);
743 if(CTX_data_edit_object(C))
744 return OPERATOR_CANCELLED;
746 CTX_DATA_BEGIN(C, Base*, base, selected_editable_bases) {
748 if(base->object->type==OB_LAMP) islamp= 1;
750 /* remove from current scene only */
751 ED_base_object_free_and_unlink(scene, base);
755 if(islamp) reshadeall_displist(scene); /* only frees displist */
757 DAG_scene_sort(scene);
758 ED_anim_dag_flush_update(C);
760 WM_event_add_notifier(C, NC_SCENE|ND_OB_ACTIVE, CTX_data_scene(C));
762 return OPERATOR_FINISHED;
765 void OBJECT_OT_delete(wmOperatorType *ot)
769 ot->description = "Delete selected objects.";
770 ot->idname= "OBJECT_OT_delete";
773 ot->invoke= WM_operator_confirm;
774 ot->exec= object_delete_exec;
775 ot->poll= ED_operator_scene_editable;
778 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
781 /**************************** Copy Utilities ******************************/
783 static void copy_object__forwardModifierLinks(void *userData, Object *ob,
786 /* this is copied from ID_NEW; it might be better to have a macro */
787 if(*idpoin && (*idpoin)->newid) *idpoin = (*idpoin)->newid;
790 /* after copying objects, copied data should get new pointers */
791 static void copy_object_set_idnew(bContext *C, int dupflag)
795 #if 0 // XXX old animation system
798 #endif // XXX old animation system
801 /* XXX check object pointers */
802 CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) {
803 relink_constraints(&ob->constraints);
806 for (chan = ob->pose->chanbase.first; chan; chan=chan->next){
807 relink_constraints(&chan->constraints);
810 modifiers_foreachIDLink(ob, copy_object__forwardModifierLinks, NULL);
814 ID_NEW(ob->proxy_group);
816 #if 0 // XXX old animation system
817 for(strip= ob->nlastrips.first; strip; strip= strip->next) {
818 bActionModifier *amod;
819 for(amod= strip->modifiers.first; amod; amod= amod->next)
822 #endif // XXX old animation system
827 if( dupflag & USER_DUP_MAT) {
828 mao= G.main->mat.first;
832 ma= (Material *)mao->id.newid;
834 if(dupflag & USER_DUP_TEX) {
835 for(a=0; a<MAX_MTEX; a++) {
837 id= (ID *)ma->mtex[a]->tex;
839 ID_NEW_US(ma->mtex[a]->tex)
840 else ma->mtex[a]->tex= copy_texture(ma->mtex[a]->tex);
846 #if 0 // XXX old animation system
850 else ma->ipo= copy_ipo(ma->ipo);
853 #endif // XXX old animation system
859 #if 0 // XXX old animation system
861 if( dupflag & USER_DUP_IPO) {
862 Lamp *la= G.main->lamp.first;
865 Lamp *lan= (Lamp *)la->id.newid;
869 else lan->ipo= copy_ipo(lan->ipo);
878 ipo= G.main->ipo.first;
880 if(ipo->id.lib==NULL && ipo->id.newid) {
881 Ipo *ipon= (Ipo *)ipo->id.newid;
883 for(icu= ipon->curve.first; icu; icu= icu->next) {
885 ID_NEW(icu->driver->ob);
891 #endif // XXX old animation system
898 /********************* Make Duplicates Real ************************/
900 static void make_object_duplilist_real(bContext *C, Scene *scene, Base *base)
907 if(!base && !(base = BASACT))
910 if(!(base->object->transflag & OB_DUPLI))
913 lb= object_duplilist(scene, base->object);
915 for(dob= lb->first; dob; dob= dob->next) {
916 ob= copy_object(dob->ob);
917 /* font duplis can have a totcol without material, we get them from parent
918 * should be implemented better...
920 if(ob->mat==NULL) ob->totcol= 0;
922 basen= MEM_dupallocN(base);
923 basen->flag &= ~OB_FROMDUPLI;
924 BLI_addhead(&scene->base, basen); /* addhead: othwise eternal loop */
926 ob->ipo= NULL; /* make sure apply works */
927 ob->parent= ob->track= NULL;
928 ob->disp.first= ob->disp.last= NULL;
929 ob->transflag &= ~OB_DUPLI;
931 copy_m4_m4(ob->obmat, dob->mat);
932 ED_object_apply_obmat(ob);
935 copy_object_set_idnew(C, 0);
937 free_object_duplilist(lb);
939 base->object->transflag &= ~OB_DUPLI;
942 static int object_duplicates_make_real_exec(bContext *C, wmOperator *op)
944 Scene *scene= CTX_data_scene(C);
948 CTX_DATA_BEGIN(C, Base*, base, selected_editable_bases) {
949 make_object_duplilist_real(C, scene, base);
953 DAG_scene_sort(scene);
954 ED_anim_dag_flush_update(C);
955 WM_event_add_notifier(C, NC_SCENE, scene);
957 return OPERATOR_FINISHED;
960 void OBJECT_OT_duplicates_make_real(wmOperatorType *ot)
964 ot->name= "Make Duplicates Real";
965 ot->description = "Make dupli objects attached to this object real.";
966 ot->idname= "OBJECT_OT_duplicates_make_real";
969 ot->invoke= WM_operator_confirm;
970 ot->exec= object_duplicates_make_real_exec;
972 ot->poll= ED_operator_scene_editable;
975 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
978 /**************************** Convert **************************/
980 static EnumPropertyItem convert_target_items[]= {
981 {OB_CURVE, "CURVE", ICON_OUTLINER_OB_CURVE, "Curve from Mesh/Text", ""},
982 {OB_MESH, "MESH", ICON_OUTLINER_OB_MESH, "Mesh from Curve/Meta/Surf/Mesh", ""},
983 {0, NULL, 0, NULL, NULL}};
985 static void curvetomesh(Scene *scene, Object *ob)
989 if(cu->disp.first==0)
990 makeDispListCurveTypes(scene, ob, 0); /* force creation */
992 nurbs_to_mesh(ob); /* also does users */
994 if(ob->type == OB_MESH)
995 object_free_modifiers(ob);
998 static int convert_poll(bContext *C)
1000 Object *obact= CTX_data_active_object(C);
1001 Scene *scene= CTX_data_scene(C);
1003 return (!scene->id.lib && obact && scene->obedit != obact && (obact->flag & SELECT));
1006 static int convert_exec(bContext *C, wmOperator *op)
1008 Scene *scene= CTX_data_scene(C);
1009 Base *basen=NULL, *basact=NULL, *basedel=NULL;
1010 Object *ob, *ob1, *obact= CTX_data_active_object(C);
1016 int target= RNA_enum_get(op->ptr, "target");
1017 int keep_original= RNA_boolean_get(op->ptr, "keep_original");
1020 /* don't forget multiple users! */
1023 CTX_DATA_BEGIN(C, Base*, base, selected_editable_bases) {
1025 ob->flag &= ~OB_DONE;
1029 CTX_DATA_BEGIN(C, Base*, base, selected_editable_bases) {
1032 if(ob->flag & OB_DONE)
1034 else if (ob->type==OB_MESH && target == OB_CURVE) {
1035 ob->flag |= OB_DONE;
1037 ob1= copy_object(ob);
1038 ob1->recalc |= OB_RECALC;
1040 basen= MEM_mallocN(sizeof(Base), "duplibase");
1042 BLI_addhead(&scene->base, basen); /* addhead: otherwise eternal loop */
1044 basen->flag |= SELECT;
1045 base->flag &= ~SELECT;
1046 ob->flag &= ~SELECT;
1048 mesh_to_curve(scene, ob1);
1050 if(ob1->type==OB_CURVE)
1051 object_free_modifiers(ob1); /* after derivedmesh calls! */
1053 else if(ob->type==OB_MESH && ob->modifiers.first) { /* converting a mesh with no modifiers causes a segfault */
1054 ob->flag |= OB_DONE;
1057 ob1= copy_object(ob);
1058 ob1->recalc |= OB_RECALC;
1060 basen= MEM_mallocN(sizeof(Base), "duplibase");
1062 BLI_addhead(&scene->base, basen); /* addhead: otherwise eternal loop */
1064 basen->flag |= SELECT;
1065 base->flag &= ~SELECT;
1066 ob->flag &= ~SELECT;
1068 /* decrement original mesh's usage count */
1072 /* make a new copy of the mesh */
1073 ob1->data= copy_mesh(me);
1075 /* make new mesh data from the original copy */
1076 dm= mesh_get_derived_final(scene, ob1, CD_MASK_MESH);
1077 /* dm= mesh_create_derived_no_deform(ob1, NULL); this was called original (instead of get_derived). man o man why! (ton) */
1079 DM_to_mesh(dm, ob1->data);
1082 object_free_modifiers(ob1); /* after derivedmesh calls! */
1084 else if(ob->type==OB_FONT) {
1085 ob->flag |= OB_DONE;
1095 cu->vfontb->id.us--;
1099 cu->vfonti->id.us--;
1103 cu->vfontbi->id.us--;
1108 for(ob1= G.main->object.first; ob1; ob1=ob1->id.next) {
1110 ob1->type= OB_CURVE;
1111 ob1->recalc |= OB_RECALC;
1116 for(nu=cu->nurb.first; nu; nu=nu->next)
1119 if(target == OB_MESH)
1120 curvetomesh(scene, ob);
1122 else if(ELEM(ob->type, OB_CURVE, OB_SURF)) {
1123 ob->flag |= OB_DONE;
1125 if(target == OB_MESH)
1126 curvetomesh(scene, ob);
1128 else if(ob->type==OB_MBALL) {
1129 ob= find_basis_mball(scene, ob);
1131 if(ob->disp.first && !(ob->flag & OB_DONE)) {
1132 ob->flag |= OB_DONE;
1135 ob1= copy_object(ob);
1136 ob1->recalc |= OB_RECALC;
1138 basen= MEM_mallocN(sizeof(Base), "duplibase");
1140 BLI_addhead(&scene->base, basen); /* addhead: otherwise eternal loop */
1142 basen->flag |= SELECT;
1143 basedel->flag &= ~SELECT;
1144 ob->flag &= ~SELECT;
1149 ob1->data= add_mesh("Mesh");
1153 me->totcol= mb->totcol;
1155 me->mat= MEM_dupallocN(mb->mat);
1156 for(a=0; a<ob1->totcol; a++) id_us_plus((ID *)me->mat[a]);
1159 mball_to_mesh(&ob->disp, ob1->data);
1161 /* So we can see the wireframe */
1162 BASACT= basen; // XXX hm
1170 /* If the original object is active then make this object active */
1173 ED_base_object_activate(C, basen);
1180 /* delete original if needed */
1183 ED_base_object_free_and_unlink(scene, basedel);
1190 /* delete object should renew depsgraph */
1192 DAG_scene_sort(scene);
1194 /* texspace and normals */
1195 if(!basen) BASACT= NULL; // XXX base;
1197 // XXX ED_object_enter_editmode(C, 0);
1198 // XXX exit_editmode(C, EM_FREEDATA|EM_WAITCURSOR); /* freedata, but no undo */
1201 DAG_scene_sort(scene);
1202 WM_event_add_notifier(C, NC_SCENE|NC_OBJECT|ND_DRAW, scene); /* is NC_SCENE needed ? */
1206 return OPERATOR_FINISHED;
1210 void OBJECT_OT_convert(wmOperatorType *ot)
1213 ot->name= "Convert";
1214 ot->description = "Convert selected objects to another type.";
1215 ot->idname= "OBJECT_OT_convert";
1218 ot->invoke= WM_menu_invoke;
1219 ot->exec= convert_exec;
1220 ot->poll= convert_poll;
1223 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1226 RNA_def_enum(ot->srna, "target", convert_target_items, OB_MESH, "Target", "Type of object to convert to.");
1227 RNA_def_boolean(ot->srna, "keep_original", 0, "Keep Original", "Keep original objects instead of replacing them.");
1230 /**************************** Duplicate ************************/
1233 dupflag: a flag made from constants declared in DNA_userdef_types.h
1234 The flag tells adduplicate() weather to copy data linked to the object, or to reference the existing data.
1235 U.dupflag for default operations or you can construct a flag as python does
1236 if the dupflag is 0 then no data will be copied (linked duplicate) */
1238 /* used below, assumes id.new is correct */
1239 /* leaves selection of base/object unaltered */
1240 static Base *object_add_duplicate_internal(Scene *scene, Base *base, int dupflag)
1243 Material ***matarar;
1249 if(ob->mode & OB_MODE_POSE) {
1253 obn= copy_object(ob);
1254 obn->recalc |= OB_RECALC;
1256 basen= MEM_mallocN(sizeof(Base), "duplibase");
1258 BLI_addhead(&scene->base, basen); /* addhead: prevent eternal loop */
1261 if(basen->flag & OB_FROMGROUP) {
1263 for(group= G.main->group.first; group; group= group->id.next) {
1264 if(object_in_group(ob, group))
1265 add_to_group(group, obn);
1267 obn->flag |= OB_FROMGROUP; /* this flag is unset with copy_object() */
1270 /* duplicates using userflags */
1271 #if 0 // XXX old animation system
1272 if(dupflag & USER_DUP_IPO) {
1273 bConstraintChannel *chan;
1277 ID_NEW_US( obn->ipo)
1278 else obn->ipo= copy_ipo(obn->ipo);
1281 /* Handle constraint ipos */
1282 for (chan=obn->constraintChannels.first; chan; chan=chan->next){
1283 id= (ID *)chan->ipo;
1285 ID_NEW_US( chan->ipo)
1286 else chan->ipo= copy_ipo(chan->ipo);
1291 if(dupflag & USER_DUP_ACT){ /* Not buttons in the UI to modify this, add later? */
1292 id= (ID *)obn->action;
1294 ID_NEW_US(obn->action)
1296 obn->action= copy_action(obn->action);
1301 #endif // XXX old animation system
1302 if(dupflag & USER_DUP_MAT) {
1303 for(a=0; a<obn->totcol; a++) {
1304 id= (ID *)obn->mat[a];
1306 ID_NEW_US(obn->mat[a])
1307 else obn->mat[a]= copy_material(obn->mat[a]);
1312 if(dupflag & USER_DUP_PSYS) {
1313 ParticleSystem *psys;
1314 for(psys=obn->particlesystem.first; psys; psys=psys->next) {
1315 id= (ID*) psys->part;
1317 ID_NEW_US(psys->part)
1318 else psys->part= psys_copy_settings(psys->part);
1329 if(dupflag & USER_DUP_MESH) {
1330 ID_NEW_US2( obn->data )
1332 obn->data= copy_mesh(obn->data);
1334 if(obn->fluidsimSettings) {
1335 obn->fluidsimSettings->orgMesh = (Mesh *)obn->data;
1344 if(dupflag & USER_DUP_CURVE) {
1345 ID_NEW_US2(obn->data )
1347 obn->data= copy_curve(obn->data);
1354 if(dupflag & USER_DUP_SURF) {
1355 ID_NEW_US2( obn->data )
1357 obn->data= copy_curve(obn->data);
1364 if(dupflag & USER_DUP_FONT) {
1365 ID_NEW_US2( obn->data )
1367 obn->data= copy_curve(obn->data);
1374 if(dupflag & USER_DUP_MBALL) {
1375 ID_NEW_US2(obn->data )
1377 obn->data= copy_mball(obn->data);
1384 if(dupflag & USER_DUP_LAMP) {
1385 ID_NEW_US2(obn->data )
1386 else obn->data= copy_lamp(obn->data);
1392 obn->recalc |= OB_RECALC_DATA;
1393 if(obn->pose) obn->pose->flag |= POSE_RECALC;
1395 if(dupflag & USER_DUP_ARM) {
1396 ID_NEW_US2(obn->data )
1398 obn->data= copy_armature(obn->data);
1399 armature_rebuild_pose(obn, obn->data);
1409 ID_NEW_US2(obn->data )
1410 else obn->data= copy_lattice(obn->data);
1416 ID_NEW_US2(obn->data )
1417 else obn->data= copy_camera(obn->data);
1423 if(dupflag & USER_DUP_MAT) {
1424 matarar= give_matarar(obn);
1425 if(didit && matarar) {
1426 for(a=0; a<obn->totcol; a++) {
1427 id= (ID *)(*matarar)[a];
1429 ID_NEW_US( (*matarar)[a] )
1430 else (*matarar)[a]= copy_material((*matarar)[a]);
1441 /* single object duplicate, if dupflag==0, fully linked, else it uses the flags given */
1442 /* leaves selection of base/object unaltered */
1443 Base *ED_object_add_duplicate(Scene *scene, Base *base, int dupflag)
1447 clear_id_newpoins();
1448 clear_sca_new_poins(); /* sensor/contr/act */
1450 basen= object_add_duplicate_internal(scene, base, dupflag);
1452 DAG_scene_sort(scene);
1457 /* contextual operator dupli */
1458 static int duplicate_exec(bContext *C, wmOperator *op)
1460 Scene *scene= CTX_data_scene(C);
1461 int linked= RNA_boolean_get(op->ptr, "linked");
1462 int dupflag= (linked)? 0: U.dupflag;
1464 clear_id_newpoins();
1465 clear_sca_new_poins(); /* sensor/contr/act */
1467 CTX_DATA_BEGIN(C, Base*, base, selected_editable_bases) {
1468 Base *basen= object_add_duplicate_internal(scene, base, dupflag);
1470 /* note that this is safe to do with this context iterator,
1471 the list is made in advance */
1472 ED_base_object_select(base, BA_DESELECT);
1474 /* new object becomes active */
1476 ED_base_object_activate(C, basen);
1481 copy_object_set_idnew(C, dupflag);
1483 DAG_scene_sort(scene);
1484 ED_anim_dag_flush_update(C);
1486 WM_event_add_notifier(C, NC_SCENE|ND_OB_SELECT, scene);
1488 return OPERATOR_FINISHED;
1491 void OBJECT_OT_duplicate(wmOperatorType *ot)
1494 ot->name= "Duplicate";
1495 ot->description = "Duplicate selected objects.";
1496 ot->idname= "OBJECT_OT_duplicate";
1499 ot->exec= duplicate_exec;
1500 ot->poll= ED_operator_scene_editable;
1503 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1505 /* to give to transform */
1506 RNA_def_boolean(ot->srna, "linked", 0, "Linked", "Duplicate object but not object data, linking to the original data.");
1507 RNA_def_int(ot->srna, "mode", TFM_TRANSLATION, 0, INT_MAX, "Mode", "", 0, INT_MAX);
1510 /**************************** Join *************************/
1512 static int join_exec(bContext *C, wmOperator *op)
1514 Scene *scene= CTX_data_scene(C);
1515 Object *ob= CTX_data_active_object(C);
1518 BKE_report(op->reports, RPT_ERROR, "This data does not support joining in editmode.");
1519 return OPERATOR_CANCELLED;
1522 BKE_report(op->reports, RPT_ERROR, "Can't join unless there is an active object.");
1523 return OPERATOR_CANCELLED;
1525 else if(object_data_is_libdata(ob)) {
1526 BKE_report(op->reports, RPT_ERROR, "Can't edit external libdata.");
1527 return OPERATOR_CANCELLED;
1530 if(ob->type == OB_MESH)
1531 return join_mesh_exec(C, op);
1532 else if(ELEM(ob->type, OB_CURVE, OB_SURF))
1533 return join_curve_exec(C, op);
1534 else if(ob->type == OB_ARMATURE)
1535 return join_armature_exec(C, op);
1537 BKE_report(op->reports, RPT_ERROR, "This object type doesn't support joining.");
1539 return OPERATOR_CANCELLED;
1542 void OBJECT_OT_join(wmOperatorType *ot)
1546 ot->description = "Join selected objects into active object.";
1547 ot->idname= "OBJECT_OT_join";
1550 ot->exec= join_exec;
1551 ot->poll= ED_operator_scene_editable;
1554 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;