style cleanup: format 'for' loop macros the same as for loops, some renaming to BLI_a...
[blender-staging.git] / source / blender / editors / object / object_add.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
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.
8  *
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.
13  *
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.
17  *
18  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19  * All rights reserved.
20  *
21  * Contributor(s): Blender Foundation, 2002-2008 full recode
22  *
23  * ***** END GPL LICENSE BLOCK *****
24  */
25
26 /** \file blender/editors/object/object_add.c
27  *  \ingroup edobj
28  */
29
30
31 #include <stdlib.h>
32 #include <string.h>
33
34 #include "MEM_guardedalloc.h"
35
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"
50
51 #include "BLI_ghash.h"
52 #include "BLI_listbase.h"
53 #include "BLI_math.h"
54 #include "BLI_string.h"
55 #include "BLI_utildefines.h"
56
57 #include "BKE_anim.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"
69 #include "BKE_lamp.h"
70 #include "BKE_lattice.h"
71 #include "BKE_library.h"
72 #include "BKE_key.h"
73 #include "BKE_main.h"
74 #include "BKE_material.h"
75 #include "BKE_mball.h"
76 #include "BKE_mesh.h"
77 #include "BKE_modifier.h"
78 #include "BKE_nla.h"
79 #include "BKE_object.h"
80 #include "BKE_particle.h"
81 #include "BKE_report.h"
82 #include "BKE_sca.h"
83 #include "BKE_scene.h"
84 #include "BKE_speaker.h"
85 #include "BKE_texture.h"
86
87 #include "RNA_access.h"
88 #include "RNA_define.h"
89 #include "RNA_enum_types.h"
90
91 #include "WM_api.h"
92 #include "WM_types.h"
93
94 #include "ED_armature.h"
95 #include "ED_curve.h"
96 #include "ED_mball.h"
97 #include "ED_mesh.h"
98 #include "ED_node.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"
104
105 #include "UI_interface.h"
106 #include "UI_resources.h"
107
108 #include "object_intern.h"
109
110 /************************** Exported *****************************/
111
112 void ED_object_location_from_view(bContext *C, float *loc)
113 {
114         View3D *v3d= CTX_wm_view3d(C);
115         Scene *scene= CTX_data_scene(C);
116         float *cursor;
117         
118         cursor = give_cursor(scene, v3d);
119
120         copy_v3_v3(loc, cursor);
121 }
122
123 void ED_object_rotation_from_view(bContext *C, float *rot)
124 {
125         RegionView3D *rv3d= CTX_wm_region_view3d(C);
126         if (rv3d) {
127                 float quat[4];
128                 copy_qt_qt(quat, rv3d->viewquat);
129                 quat[0]= -quat[0];
130                 quat_to_eul(rot, quat);
131         }
132         else {
133                 zero_v3(rot);
134         }
135 }
136
137 void ED_object_base_init_transform(bContext *C, Base *base, float *loc, float *rot)
138 {
139         Object *ob= base->object;
140         Scene *scene= CTX_data_scene(C);
141         
142         if (!scene) return;
143         
144         if (loc)
145                 copy_v3_v3(ob->loc, loc);
146         
147         if (rot)
148                 copy_v3_v3(ob->rot, rot);
149         
150         where_is_object(scene, ob);
151 }
152
153 /* uses context to figure out transform for primitive */
154 /* returns standard diameter */
155 float ED_object_new_primitive_matrix(bContext *C, Object *obedit, float *loc, float *rot, float primmat[][4])
156 {
157         View3D *v3d = CTX_wm_view3d(C);
158         float mat[3][3], rmat[3][3], cmat[3][3], imat[3][3];
159         
160         unit_m4(primmat);
161         
162         eul_to_mat3(rmat, rot);
163         invert_m3(rmat);
164         
165         /* inverse transform for initial rotation and object */
166         copy_m3_m4(mat, obedit->obmat);
167         mul_m3_m3m3(cmat, rmat, mat);
168         invert_m3_m3(imat, cmat);
169         copy_m4_m3(primmat, imat);
170         
171         /* center */
172         copy_v3_v3(primmat[3], loc);
173         sub_v3_v3v3(primmat[3], primmat[3], obedit->obmat[3]);
174         invert_m3_m3(imat, mat);
175         mul_m3_v3(imat, primmat[3]);
176         
177         if (v3d) return v3d->grid;
178         return 1.0f;
179 }
180
181 /********************* Add Object Operator ********************/
182
183 void view_align_update(struct Main *UNUSED(main), struct Scene *UNUSED(scene), struct PointerRNA *ptr)
184 {
185         RNA_struct_idprops_unset(ptr, "rotation");
186 }
187
188 void ED_object_add_generic_props(wmOperatorType *ot, int do_editmode)
189 {
190         PropertyRNA *prop;
191         
192         /* note: this property gets hidden for add-camera operator */
193         prop = RNA_def_boolean(ot->srna, "view_align", 0, "Align to View", "Align the new object to the view");
194         RNA_def_property_update_runtime(prop, view_align_update);
195
196         if (do_editmode) {
197                 prop = RNA_def_boolean(ot->srna, "enter_editmode", 0, "Enter Editmode",
198                                       "Enter editmode when adding this object");
199                 RNA_def_property_flag(prop, PROP_HIDDEN|PROP_SKIP_SAVE);
200         }
201         
202         prop = RNA_def_float_vector_xyz(ot->srna, "location", 3, NULL, -FLT_MAX, FLT_MAX, "Location",
203                                        "Location for the newly added object", -FLT_MAX, FLT_MAX);
204         RNA_def_property_flag(prop, PROP_SKIP_SAVE);
205         prop = RNA_def_float_rotation(ot->srna, "rotation", 3, NULL, -FLT_MAX, FLT_MAX, "Rotation",
206                                      "Rotation for the newly added object", (float)-M_PI * 2.0f, (float)M_PI * 2.0f);
207         RNA_def_property_flag(prop, PROP_SKIP_SAVE);
208         
209         prop = RNA_def_boolean_layer_member(ot->srna, "layers", 20, NULL, "Layer", "");
210         RNA_def_property_flag(prop, PROP_HIDDEN|PROP_SKIP_SAVE);
211 }
212
213 static void object_add_generic_invoke_options(bContext *C, wmOperator *op)
214 {
215         if (RNA_struct_find_property(op->ptr, "enter_editmode")) /* optional */
216                 if (!RNA_struct_property_is_set(op->ptr, "enter_editmode"))
217                         RNA_boolean_set(op->ptr, "enter_editmode", U.flag & USER_ADD_EDITMODE);
218         
219         if (!RNA_struct_property_is_set(op->ptr, "location")) {
220                 float loc[3];
221                 
222                 ED_object_location_from_view(C, loc);
223                 RNA_float_set_array(op->ptr, "location", loc);
224         }
225          
226         if (!RNA_struct_property_is_set(op->ptr, "layers")) {
227                 View3D *v3d = CTX_wm_view3d(C);
228                 Scene *scene = CTX_data_scene(C);
229                 int a, values[20], layer;
230
231                 if (v3d) {
232                         layer = (v3d->scenelock && !v3d->localvd)? scene->layact: v3d->layact;
233                 }
234                 else {
235                         layer = scene->layact;
236                 }
237
238                 for (a=0; a<20; a++) {
239                         values[a]= (layer & (1<<a));
240                 }
241
242                 RNA_boolean_set_array(op->ptr, "layers", values);
243         }
244 }
245
246 int ED_object_add_generic_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
247 {
248         object_add_generic_invoke_options(C, op);
249         return op->type->exec(C, op);
250 }
251
252 int ED_object_add_generic_get_opts(bContext *C, wmOperator *op, float *loc,
253         float *rot, int *enter_editmode, unsigned int *layer, int *is_view_aligned)
254 {
255         View3D *v3d = CTX_wm_view3d(C);
256         int a, layer_values[20];
257         int view_align;
258         
259         *enter_editmode = FALSE;
260         if (RNA_struct_find_property(op->ptr, "enter_editmode") && RNA_boolean_get(op->ptr, "enter_editmode")) {
261                 *enter_editmode = TRUE;
262         }
263
264         if (RNA_struct_property_is_set(op->ptr, "layers")) {
265                 RNA_boolean_get_array(op->ptr, "layers", layer_values);
266                 *layer= 0;
267                 for (a=0; a<20; a++) {
268                         if (layer_values[a])
269                                 *layer |= (1 << a);
270                         else
271                                 *layer &= ~(1 << a);
272                 }
273         }
274         else {
275                 /* not set, use the scenes layers */
276                 Scene *scene = CTX_data_scene(C);
277                 *layer = scene->layact;
278         }
279
280         /* in local view we additionally add local view layers,
281          * not part of operator properties */
282         if (v3d && v3d->localvd)
283                 *layer |= v3d->lay;
284
285         if (RNA_struct_property_is_set(op->ptr, "rotation"))
286                 view_align = FALSE;
287         else if (RNA_struct_property_is_set(op->ptr, "view_align"))
288                 view_align = RNA_boolean_get(op->ptr, "view_align");
289         else {
290                 view_align = U.flag & USER_ADD_VIEWALIGNED;
291                 RNA_boolean_set(op->ptr, "view_align", view_align);
292         }
293         
294         if (view_align) {
295                 ED_object_rotation_from_view(C, rot);
296                 RNA_float_set_array(op->ptr, "rotation", rot);
297         }
298         else
299                 RNA_float_get_array(op->ptr, "rotation", rot);
300         
301         if (is_view_aligned)
302                 *is_view_aligned = view_align;
303         
304         RNA_float_get_array(op->ptr, "location", loc);
305
306         if (*layer == 0) {
307                 BKE_report(op->reports, RPT_ERROR, "Property 'layer' has no values set");
308                 return 0;
309         }
310
311         return 1;
312 }
313
314 /* for object add primitive operators */
315 /* do not call undo push in this function (users of this function have to) */
316 Object *ED_object_add_type(bContext *C, int type, float *loc, float *rot,
317         int enter_editmode, unsigned int layer)
318 {
319         Main *bmain= CTX_data_main(C);
320         Scene *scene= CTX_data_scene(C);
321         Object *ob;
322         
323         /* for as long scene has editmode... */
324         if (CTX_data_edit_object(C)) 
325                 ED_object_exit_editmode(C, EM_FREEDATA|EM_FREEUNDO|EM_WAITCURSOR|EM_DO_UNDO); /* freedata, and undo */
326         
327         /* deselects all, sets scene->basact */
328         ob= add_object(scene, type);
329         BASACT->lay = ob->lay = layer;
330         /* editor level activate, notifiers */
331         ED_base_object_activate(C, BASACT);
332
333         /* more editor stuff */
334         ED_object_base_init_transform(C, BASACT, loc, rot);
335
336         DAG_id_type_tag(bmain, ID_OB);
337         DAG_scene_sort(bmain, scene);
338         if (ob->data) {
339                 ED_render_id_flush_update(bmain, ob->data);
340         }
341
342         if (enter_editmode)
343                 ED_object_enter_editmode(C, EM_IGNORE_LAYER);
344
345         WM_event_add_notifier(C, NC_SCENE|ND_LAYER_CONTENT, scene);
346
347         return ob;
348 }
349
350 /* for object add operator */
351 static int object_add_exec(bContext *C, wmOperator *op)
352 {
353         int enter_editmode;
354         unsigned int layer;
355         float loc[3], rot[3];
356         
357         if (!ED_object_add_generic_get_opts(C, op, loc, rot, &enter_editmode, &layer, NULL))
358                 return OPERATOR_CANCELLED;
359
360         ED_object_add_type(C, RNA_enum_get(op->ptr, "type"), loc, rot, enter_editmode, layer);
361         
362         return OPERATOR_FINISHED;
363 }
364
365 void OBJECT_OT_add(wmOperatorType *ot)
366 {
367         /* identifiers */
368         ot->name = "Add Object";
369         ot->description = "Add an object to the scene";
370         ot->idname = "OBJECT_OT_add";
371         
372         /* api callbacks */
373         ot->invoke = ED_object_add_generic_invoke;
374         ot->exec = object_add_exec;
375         
376         ot->poll = ED_operator_objectmode;
377         
378         /* flags */
379         ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
380         
381         RNA_def_enum(ot->srna, "type", object_type_items, 0, "Type", "");
382
383         ED_object_add_generic_props(ot, TRUE);
384 }
385
386 /********************* Add Effector Operator ********************/
387 /* copy from rna_object_force.c*/
388 static EnumPropertyItem field_type_items[] = {
389         {PFIELD_FORCE, "FORCE", ICON_FORCE_FORCE, "Force", ""},
390         {PFIELD_WIND, "WIND", ICON_FORCE_WIND, "Wind", ""},
391         {PFIELD_VORTEX, "VORTEX", ICON_FORCE_VORTEX, "Vortex", ""},
392         {PFIELD_MAGNET, "MAGNET", ICON_FORCE_MAGNETIC, "Magnetic", ""},
393         {PFIELD_HARMONIC, "HARMONIC", ICON_FORCE_HARMONIC, "Harmonic", ""},
394         {PFIELD_CHARGE, "CHARGE", ICON_FORCE_CHARGE, "Charge", ""},
395         {PFIELD_LENNARDJ, "LENNARDJ", ICON_FORCE_LENNARDJONES, "Lennard-Jones", ""},
396         {PFIELD_TEXTURE, "TEXTURE", ICON_FORCE_TEXTURE, "Texture", ""},
397         {PFIELD_GUIDE, "GUIDE", ICON_FORCE_CURVE, "Curve Guide", ""},
398         {PFIELD_BOID, "BOID", ICON_FORCE_BOID, "Boid", ""},
399         {PFIELD_TURBULENCE, "TURBULENCE", ICON_FORCE_TURBULENCE, "Turbulence", ""},
400         {PFIELD_DRAG, "DRAG", ICON_FORCE_DRAG, "Drag", ""},
401         {0, NULL, 0, NULL, NULL}};
402
403 /* for effector add primitive operators */
404 static Object *effector_add_type(bContext *C, wmOperator *op, int type)
405 {
406         Object *ob;
407         int enter_editmode;
408         unsigned int layer;
409         float loc[3], rot[3];
410         float mat[4][4];
411         
412         object_add_generic_invoke_options(C, op);
413
414         if (!ED_object_add_generic_get_opts(C, op, loc, rot, &enter_editmode, &layer, NULL))
415                 return NULL;
416
417         if (type==PFIELD_GUIDE) {
418                 ob= ED_object_add_type(C, OB_CURVE, loc, rot, FALSE, layer);
419                 rename_id(&ob->id, "CurveGuide");
420
421                 ((Curve*)ob->data)->flag |= CU_PATH|CU_3D;
422                 ED_object_enter_editmode(C, 0);
423                 ED_object_new_primitive_matrix(C, ob, loc, rot, mat);
424                 BLI_addtail(object_editcurve_get(ob), add_nurbs_primitive(C, mat, CU_NURBS|CU_PRIM_PATH, 1));
425
426                 if (!enter_editmode)
427                         ED_object_exit_editmode(C, EM_FREEDATA);
428         }
429         else {
430                 ob= ED_object_add_type(C, OB_EMPTY, loc, rot, FALSE, layer);
431                 rename_id(&ob->id, "Field");
432
433                 switch (type) {
434                         case PFIELD_WIND:
435                         case PFIELD_VORTEX:
436                                 ob->empty_drawtype = OB_SINGLE_ARROW;
437                                 break;
438                 }
439         }
440
441         ob->pd= object_add_collision_fields(type);
442
443         DAG_scene_sort(CTX_data_main(C), CTX_data_scene(C));
444
445         return ob;
446 }
447
448 /* for object add operator */
449 static int effector_add_exec(bContext *C, wmOperator *op)
450 {
451         if (effector_add_type(C, op, RNA_enum_get(op->ptr, "type")) == NULL)
452                 return OPERATOR_CANCELLED;
453
454         return OPERATOR_FINISHED;
455 }
456
457 void OBJECT_OT_effector_add(wmOperatorType *ot)
458 {
459         /* identifiers */
460         ot->name = "Add Effector";
461         ot->description = "Add an empty object with a physics effector to the scene";
462         ot->idname = "OBJECT_OT_effector_add";
463         
464         /* api callbacks */
465         ot->invoke = WM_menu_invoke;
466         ot->exec = effector_add_exec;
467         
468         ot->poll = ED_operator_objectmode;
469         
470         /* flags */
471         ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
472         
473         ot->prop = RNA_def_enum(ot->srna, "type", field_type_items, 0, "Type", "");
474
475         ED_object_add_generic_props(ot, TRUE);
476 }
477
478 /* ***************** Add Camera *************** */
479
480 static int object_camera_add_exec(bContext *C, wmOperator *op)
481 {
482         View3D *v3d = CTX_wm_view3d(C);
483         Scene *scene= CTX_data_scene(C);
484         Object *ob;
485         int enter_editmode;
486         unsigned int layer;
487         float loc[3], rot[3];
488         
489         /* force view align for cameras */
490         RNA_boolean_set(op->ptr, "view_align", TRUE);
491         
492         object_add_generic_invoke_options(C, op);
493
494         if (!ED_object_add_generic_get_opts(C, op, loc, rot, &enter_editmode, &layer, NULL))
495                 return OPERATOR_CANCELLED;
496
497         ob= ED_object_add_type(C, OB_CAMERA, loc, rot, FALSE, layer);
498         
499         if (v3d) {
500                 if (v3d->camera == NULL)
501                         v3d->camera = ob;
502                 if (v3d->scenelock && scene->camera==NULL) {
503                         scene->camera = ob;
504                 }
505         }
506
507         return OPERATOR_FINISHED;
508 }
509
510 void OBJECT_OT_camera_add(wmOperatorType *ot)
511 {
512         PropertyRNA *prop;
513         
514         /* identifiers */
515         ot->name = "Add Camera";
516         ot->description = "Add a camera object to the scene";
517         ot->idname = "OBJECT_OT_camera_add";
518         
519         /* api callbacks */
520         ot->exec = object_camera_add_exec;
521         ot->poll = ED_operator_objectmode;
522         
523         /* flags */
524         ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
525                 
526         ED_object_add_generic_props(ot, TRUE);
527         
528         /* hide this for cameras, default */
529         prop= RNA_struct_type_find_property(ot->srna, "view_align");
530         RNA_def_property_flag(prop, PROP_HIDDEN);
531
532 }
533
534
535 /* ***************** add primitives *************** */
536 static int object_metaball_add_exec(bContext *C, wmOperator *op)
537 {
538         Object *obedit= CTX_data_edit_object(C);
539         /*MetaElem *elem;*/ /*UNUSED*/
540         int newob= 0;
541         int enter_editmode;
542         unsigned int layer;
543         float loc[3], rot[3];
544         float mat[4][4];
545         
546         object_add_generic_invoke_options(C, op); // XXX these props don't get set right when only exec() is called
547
548         if (!ED_object_add_generic_get_opts(C, op, loc, rot, &enter_editmode, &layer, NULL))
549                 return OPERATOR_CANCELLED;
550         
551         if (obedit==NULL || obedit->type!=OB_MBALL) {
552                 obedit= ED_object_add_type(C, OB_MBALL, loc, rot, TRUE, layer);
553                 newob = 1;
554         }
555         else DAG_id_tag_update(&obedit->id, OB_RECALC_DATA);
556         
557         ED_object_new_primitive_matrix(C, obedit, loc, rot, mat);
558         
559         /* elem= (MetaElem *) */ add_metaball_primitive(C, mat, RNA_enum_get(op->ptr, "type"), newob);
560
561         /* userdef */
562         if (newob && !enter_editmode) {
563                 ED_object_exit_editmode(C, EM_FREEDATA);
564         }
565         
566         WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, obedit);
567         
568         return OPERATOR_FINISHED;
569 }
570
571 static int object_metaball_add_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
572 {
573         Object *obedit= CTX_data_edit_object(C);
574         uiPopupMenu *pup;
575         uiLayout *layout;
576
577         object_add_generic_invoke_options(C, op);
578
579         pup= uiPupMenuBegin(C, op->type->name, ICON_NONE);
580         layout= uiPupMenuLayout(pup);
581         if (!obedit || obedit->type == OB_MBALL)
582                 uiItemsEnumO(layout, op->type->idname, "type");
583         else
584                 uiItemsEnumO(layout, "OBJECT_OT_metaball_add", "type");
585         uiPupMenuEnd(C, pup);
586
587         return OPERATOR_CANCELLED;
588 }
589
590 void OBJECT_OT_metaball_add(wmOperatorType *ot)
591 {
592         /* identifiers */
593         ot->name = "Add Metaball";
594         ot->description = "Add an metaball object to the scene";
595         ot->idname = "OBJECT_OT_metaball_add";
596
597         /* api callbacks */
598         ot->invoke = object_metaball_add_invoke;
599         ot->exec = object_metaball_add_exec;
600         ot->poll = ED_operator_scene_editable;
601
602         /* flags */
603         ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
604         
605         RNA_def_enum(ot->srna, "type", metaelem_type_items, 0, "Primitive", "");
606         ED_object_add_generic_props(ot, TRUE);
607 }
608
609 static int object_add_text_exec(bContext *C, wmOperator *op)
610 {
611         Object *obedit= CTX_data_edit_object(C);
612         int enter_editmode;
613         unsigned int layer;
614         float loc[3], rot[3];
615         
616         object_add_generic_invoke_options(C, op); // XXX these props don't get set right when only exec() is called
617         if (!ED_object_add_generic_get_opts(C, op, loc, rot, &enter_editmode, &layer, NULL))
618                 return OPERATOR_CANCELLED;
619         
620         if (obedit && obedit->type==OB_FONT)
621                 return OPERATOR_CANCELLED;
622
623         obedit= ED_object_add_type(C, OB_FONT, loc, rot, enter_editmode, layer);
624         
625         WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, obedit);
626         
627         return OPERATOR_FINISHED;
628 }
629
630 void OBJECT_OT_text_add(wmOperatorType *ot)
631 {
632         /* identifiers */
633         ot->name = "Add Text";
634         ot->description = "Add a text object to the scene";
635         ot->idname = "OBJECT_OT_text_add";
636         
637         /* api callbacks */
638         ot->invoke = ED_object_add_generic_invoke;
639         ot->exec = object_add_text_exec;
640         ot->poll = ED_operator_objectmode;
641         
642         /* flags */
643         ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
644         ED_object_add_generic_props(ot, TRUE);
645 }
646
647 static int object_armature_add_exec(bContext *C, wmOperator *op)
648 {
649         Object *obedit= CTX_data_edit_object(C);
650         View3D *v3d= CTX_wm_view3d(C);
651         RegionView3D *rv3d= CTX_wm_region_view3d(C);
652         int newob= 0;
653         int enter_editmode;
654         unsigned int layer;
655         float loc[3], rot[3];
656         
657         object_add_generic_invoke_options(C, op); // XXX these props don't get set right when only exec() is called
658         if (!ED_object_add_generic_get_opts(C, op, loc, rot, &enter_editmode, &layer, NULL))
659                 return OPERATOR_CANCELLED;
660         
661         if ((obedit==NULL) || (obedit->type != OB_ARMATURE)) {
662                 obedit= ED_object_add_type(C, OB_ARMATURE, loc, rot, TRUE, layer);
663                 ED_object_enter_editmode(C, 0);
664                 newob = 1;
665         }
666         else DAG_id_tag_update(&obedit->id, OB_RECALC_DATA);
667         
668         if (obedit==NULL) {
669                 BKE_report(op->reports, RPT_ERROR, "Cannot create editmode armature");
670                 return OPERATOR_CANCELLED;
671         }
672         
673         /* v3d and rv3d are allowed to be NULL */
674         add_primitive_bone(CTX_data_scene(C), v3d, rv3d);
675
676         /* userdef */
677         if (newob && !enter_editmode)
678                 ED_object_exit_editmode(C, EM_FREEDATA);
679         
680         WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, obedit);
681         
682         return OPERATOR_FINISHED;
683 }
684
685 void OBJECT_OT_armature_add(wmOperatorType *ot)
686 {       
687         /* identifiers */
688         ot->name = "Add Armature";
689         ot->description = "Add an armature object to the scene";
690         ot->idname = "OBJECT_OT_armature_add";
691         
692         /* api callbacks */
693         ot->invoke = ED_object_add_generic_invoke;
694         ot->exec = object_armature_add_exec;
695         ot->poll = ED_operator_objectmode;
696         
697         /* flags */
698         ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
699         ED_object_add_generic_props(ot, TRUE);
700 }
701
702 static const char *get_lamp_defname(int type)
703 {
704         switch (type) {
705                 case LA_LOCAL: return "Point";
706                 case LA_SUN: return "Sun";
707                 case LA_SPOT: return "Spot";
708                 case LA_HEMI: return "Hemi";
709                 case LA_AREA: return "Area";
710                 default:
711                         return "Lamp";
712         }
713 }
714
715 static int object_lamp_add_exec(bContext *C, wmOperator *op)
716 {
717         Scene *scene= CTX_data_scene(C);
718         Object *ob;
719         Lamp *la;
720         int type= RNA_enum_get(op->ptr, "type");
721         int enter_editmode;
722         unsigned int layer;
723         float loc[3], rot[3];
724         
725         object_add_generic_invoke_options(C, op);
726         if (!ED_object_add_generic_get_opts(C, op, loc, rot, &enter_editmode, &layer, NULL))
727                 return OPERATOR_CANCELLED;
728
729         ob= ED_object_add_type(C, OB_LAMP, loc, rot, FALSE, layer);
730         la= (Lamp*)ob->data;
731
732         la->type= type;
733         rename_id(&ob->id, get_lamp_defname(type));
734         rename_id(&la->id, get_lamp_defname(type));
735
736         if (scene_use_new_shading_nodes(scene)) {
737                 ED_node_shader_default(scene, &la->id);
738                 la->use_nodes= 1;
739         }
740         
741         return OPERATOR_FINISHED;
742 }
743
744 void OBJECT_OT_lamp_add(wmOperatorType *ot)
745 {       
746         static EnumPropertyItem lamp_type_items[] = {
747                 {LA_LOCAL, "POINT", ICON_LAMP_POINT, "Point", "Omnidirectional point light source"},
748                 {LA_SUN, "SUN", ICON_LAMP_SUN, "Sun", "Constant direction parallel ray light source"},
749                 {LA_SPOT, "SPOT", ICON_LAMP_SPOT, "Spot", "Directional cone light source"},
750                 {LA_HEMI, "HEMI", ICON_LAMP_HEMI, "Hemi", "180 degree constant light source"},
751                 {LA_AREA, "AREA", ICON_LAMP_AREA, "Area", "Directional area light source"},
752                 {0, NULL, 0, NULL, NULL}};
753
754         /* identifiers */
755         ot->name = "Add Lamp";
756         ot->description = "Add a lamp object to the scene";
757         ot->idname = "OBJECT_OT_lamp_add";
758         
759         /* api callbacks */
760         ot->invoke = WM_menu_invoke;
761         ot->exec = object_lamp_add_exec;
762         ot->poll = ED_operator_objectmode;
763         
764         /* flags */
765         ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
766
767         /* properties */
768         ot->prop = RNA_def_enum(ot->srna, "type", lamp_type_items, 0, "Type", "");
769
770         ED_object_add_generic_props(ot, FALSE);
771 }
772
773 static int group_instance_add_exec(bContext *C, wmOperator *op)
774 {
775         Group *group= BLI_findlink(&CTX_data_main(C)->group, RNA_enum_get(op->ptr, "group"));
776
777         int enter_editmode;
778         unsigned int layer;
779         float loc[3], rot[3];
780         
781         object_add_generic_invoke_options(C, op);
782         if (!ED_object_add_generic_get_opts(C, op, loc, rot, &enter_editmode, &layer, NULL))
783                 return OPERATOR_CANCELLED;
784
785         if (group) {
786                 Main *bmain= CTX_data_main(C);
787                 Scene *scene= CTX_data_scene(C);
788                 Object *ob= ED_object_add_type(C, OB_EMPTY, loc, rot, FALSE, layer);
789                 rename_id(&ob->id, group->id.name+2);
790                 ob->dup_group= group;
791                 ob->transflag |= OB_DUPLIGROUP;
792                 id_lib_extern(&group->id);
793
794                 /* works without this except if you try render right after, see: 22027 */
795                 DAG_scene_sort(bmain, scene);
796
797                 WM_event_add_notifier(C, NC_SCENE|ND_OB_ACTIVE, CTX_data_scene(C));
798
799                 return OPERATOR_FINISHED;
800         }
801
802         return OPERATOR_CANCELLED;
803 }
804
805 static int object_speaker_add_exec(bContext *C, wmOperator *op)
806 {
807         Object *ob;
808         int enter_editmode;
809         unsigned int layer;
810         float loc[3], rot[3];
811         Scene *scene = CTX_data_scene(C);
812
813         object_add_generic_invoke_options(C, op);
814         if (!ED_object_add_generic_get_opts(C, op, loc, rot, &enter_editmode, &layer, NULL))
815                 return OPERATOR_CANCELLED;
816
817         ob= ED_object_add_type(C, OB_SPEAKER, loc, rot, FALSE, layer);
818         
819         /* to make it easier to start using this immediately in NLA, a default sound clip is created
820          * ready to be moved around to retime the sound and/or make new sound clips
821          */
822         {
823                 /* create new data for NLA hierarchy */
824                 AnimData *adt = BKE_id_add_animdata(&ob->id);
825                 NlaTrack *nlt = add_nlatrack(adt, NULL);
826                 NlaStrip *strip = add_nla_soundstrip(CTX_data_scene(C), ob->data);
827                 strip->start = CFRA;
828                 strip->end += strip->start;
829                 
830                 /* hook them up */
831                 BKE_nlatrack_add_strip(nlt, strip);
832                 
833                 /* auto-name the strip, and give the track an interesting name  */
834                 strcpy(nlt->name, "SoundTrack");
835                 BKE_nlastrip_validate_name(adt, strip);
836                 
837                 WM_event_add_notifier(C, NC_ANIMATION|ND_NLA|NA_EDITED, NULL);
838         }
839
840         return OPERATOR_FINISHED;
841 }
842
843 void OBJECT_OT_speaker_add(wmOperatorType *ot)
844 {
845         /* identifiers */
846         ot->name = "Add Speaker";
847         ot->description = "Add a speaker object to the scene";
848         ot->idname = "OBJECT_OT_speaker_add";
849
850         /* api callbacks */
851         ot->exec = object_speaker_add_exec;
852         ot->poll = ED_operator_objectmode;
853
854         /* flags */
855         ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
856
857         ED_object_add_generic_props(ot, TRUE);
858 }
859
860 /* only used as menu */
861 void OBJECT_OT_group_instance_add(wmOperatorType *ot)
862 {
863         PropertyRNA *prop;
864
865         /* identifiers */
866         ot->name = "Add Group Instance";
867         ot->description = "Add a dupligroup instance";
868         ot->idname = "OBJECT_OT_group_instance_add";
869
870         /* api callbacks */
871         ot->invoke = WM_enum_search_invoke;
872         ot->exec = group_instance_add_exec;
873
874         ot->poll = ED_operator_objectmode;
875
876         /* flags */
877         ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
878
879         /* properties */
880         prop= RNA_def_enum(ot->srna, "group", DummyRNA_NULL_items, 0, "Group", "");
881         RNA_def_enum_funcs(prop, RNA_group_itemf);
882         ot->prop = prop;
883         ED_object_add_generic_props(ot, FALSE);
884 }
885
886 /**************************** Delete Object *************************/
887
888 /* remove base from a specific scene */
889 /* note: now unlinks constraints as well */
890 void ED_base_object_free_and_unlink(Main *bmain, Scene *scene, Base *base)
891 {
892         DAG_id_type_tag(bmain, ID_OB);
893         BLI_remlink(&scene->base, base);
894         free_libblock_us(&bmain->object, base->object);
895         if (scene->basact==base) scene->basact= NULL;
896         MEM_freeN(base);
897 }
898
899 static int object_delete_exec(bContext *C, wmOperator *op)
900 {
901         Main *bmain= CTX_data_main(C);
902         Scene *scene= CTX_data_scene(C);
903         const short use_global= RNA_boolean_get(op->ptr, "use_global");
904         /* int islamp= 0; */ /* UNUSED */
905         
906         if (CTX_data_edit_object(C)) 
907                 return OPERATOR_CANCELLED;
908         
909         CTX_DATA_BEGIN (C, Base*, base, selected_bases) {
910
911                 /* if (base->object->type==OB_LAMP) islamp= 1; */
912
913                 /* deselect object -- it could be used in other scenes */
914                 base->object->flag &= ~SELECT;
915
916                 /* remove from current scene only */
917                 ED_base_object_free_and_unlink(bmain, scene, base);
918
919                 if (use_global) {
920                         Scene *scene_iter;
921                         Base *base_other;
922
923                         for (scene_iter= bmain->scene.first; scene_iter; scene_iter= scene_iter->id.next) {
924                                 if (scene_iter != scene && !(scene_iter->id.lib)) {
925                                         base_other= object_in_scene(base->object, scene_iter);
926                                         if (base_other) {
927                                                 ED_base_object_free_and_unlink(bmain, scene_iter, base_other);
928                                         }
929                                 }
930                         }
931                 }
932                 /* end global */
933
934         }
935         CTX_DATA_END;
936
937         DAG_scene_sort(bmain, scene);
938         DAG_ids_flush_update(bmain, 0);
939         
940         WM_event_add_notifier(C, NC_SCENE|ND_OB_ACTIVE, scene);
941         WM_event_add_notifier(C, NC_SCENE|ND_LAYER_CONTENT, scene);
942         
943         return OPERATOR_FINISHED;
944 }
945
946 void OBJECT_OT_delete(wmOperatorType *ot)
947 {
948         /* identifiers */
949         ot->name = "Delete";
950         ot->description = "Delete selected objects";
951         ot->idname = "OBJECT_OT_delete";
952         
953         /* api callbacks */
954         ot->invoke = WM_operator_confirm;
955         ot->exec = object_delete_exec;
956         ot->poll = ED_operator_objectmode;
957         
958         /* flags */
959         ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
960
961         RNA_def_boolean(ot->srna, "use_global", 0, "Delete Globally", "Remove object from all scenes");
962 }
963
964 /**************************** Copy Utilities ******************************/
965
966 /* after copying objects, copied data should get new pointers */
967 static void copy_object_set_idnew(bContext *C, int dupflag)
968 {
969         Main *bmain= CTX_data_main(C);
970         Material *ma, *mao;
971         ID *id;
972         int a;
973         
974         /* XXX check object pointers */
975         CTX_DATA_BEGIN (C, Object*, ob, selected_editable_objects) {
976                 object_relink(ob);
977         }
978         CTX_DATA_END;
979         
980         /* materials */
981         if ( dupflag & USER_DUP_MAT) {
982                 mao= bmain->mat.first;
983                 while (mao) {
984                         if (mao->id.newid) {
985                                 
986                                 ma= (Material *)mao->id.newid;
987                                 
988                                 if (dupflag & USER_DUP_TEX) {
989                                         for (a=0; a<MAX_MTEX; a++) {
990                                                 if (ma->mtex[a]) {
991                                                         id= (ID *)ma->mtex[a]->tex;
992                                                         if (id) {
993                                                                 ID_NEW_US(ma->mtex[a]->tex)
994                                                                 else ma->mtex[a]->tex= copy_texture(ma->mtex[a]->tex);
995                                                                 id->us--;
996                                                         }
997                                                 }
998                                         }
999                                 }
1000 #if 0 // XXX old animation system
1001                                 id= (ID *)ma->ipo;
1002                                 if (id) {
1003                                         ID_NEW_US(ma->ipo)
1004                                         else ma->ipo= copy_ipo(ma->ipo);
1005                                         id->us--;
1006                                 }
1007 #endif // XXX old animation system
1008                         }
1009                         mao= mao->id.next;
1010                 }
1011         }
1012         
1013 #if 0 // XXX old animation system
1014         /* lamps */
1015         if ( dupflag & USER_DUP_IPO) {
1016                 Lamp *la= bmain->lamp.first;
1017                 while (la) {
1018                         if (la->id.newid) {
1019                                 Lamp *lan= (Lamp *)la->id.newid;
1020                                 id= (ID *)lan->ipo;
1021                                 if (id) {
1022                                         ID_NEW_US(lan->ipo)
1023                                         else lan->ipo= copy_ipo(lan->ipo);
1024                                         id->us--;
1025                                 }
1026                         }
1027                         la= la->id.next;
1028                 }
1029         }
1030         
1031         /* ipos */
1032         ipo= bmain->ipo.first;
1033         while (ipo) {
1034                 if (ipo->id.lib==NULL && ipo->id.newid) {
1035                         Ipo *ipon= (Ipo *)ipo->id.newid;
1036                         IpoCurve *icu;
1037                         for (icu= ipon->curve.first; icu; icu= icu->next) {
1038                                 if (icu->driver) {
1039                                         ID_NEW(icu->driver->ob);
1040                                 }
1041                         }
1042                 }
1043                 ipo= ipo->id.next;
1044         }
1045 #endif // XXX old animation system
1046         
1047         set_sca_new_poins();
1048         
1049         clear_id_newpoins();
1050 }
1051
1052 /********************* Make Duplicates Real ************************/
1053
1054 static void make_object_duplilist_real(bContext *C, Scene *scene, Base *base,
1055                                        const short use_base_parent,
1056                                        const short use_hierarchy)
1057 {
1058         ListBase *lb;
1059         DupliObject *dob;
1060         GHash *dupli_gh= NULL, *parent_gh= NULL;
1061         
1062         if (!(base->object->transflag & OB_DUPLI))
1063                 return;
1064         
1065         lb= object_duplilist(scene, base->object);
1066
1067         if (use_hierarchy || use_base_parent) {
1068                 dupli_gh= BLI_ghash_new(BLI_ghashutil_ptrhash, BLI_ghashutil_ptrcmp, "make_object_duplilist_real dupli_gh");
1069                 parent_gh= BLI_ghash_new(BLI_ghashutil_pairhash, BLI_ghashutil_paircmp, "make_object_duplilist_real parent_gh");
1070         }
1071         
1072         for (dob= lb->first; dob; dob= dob->next) {
1073                 Base *basen;
1074                 Object *ob= copy_object(dob->ob);
1075                 /* font duplis can have a totcol without material, we get them from parent
1076                  * should be implemented better...
1077                  */
1078                 if (ob->mat==NULL) ob->totcol= 0;
1079                 
1080                 basen= MEM_dupallocN(base);
1081                 basen->flag &= ~(OB_FROMDUPLI|OB_FROMGROUP);
1082                 ob->flag= basen->flag;
1083                 basen->lay= base->lay;
1084                 BLI_addhead(&scene->base, basen);       /* addhead: othwise eternal loop */
1085                 basen->object= ob;
1086                 
1087                 /* make sure apply works */
1088                 BKE_free_animdata(&ob->id);     
1089                 ob->adt = NULL;
1090                 
1091                 ob->parent= NULL;
1092                 ob->constraints.first= ob->constraints.last= NULL;
1093                 ob->disp.first= ob->disp.last= NULL;
1094                 ob->transflag &= ~OB_DUPLI;     
1095                 ob->lay= base->lay;
1096                 
1097                 copy_m4_m4(ob->obmat, dob->mat);
1098                 object_apply_mat4(ob, ob->obmat, FALSE, FALSE);
1099
1100                 if (dupli_gh)
1101                         BLI_ghash_insert(dupli_gh, dob, ob);
1102                 if (parent_gh)
1103                         BLI_ghash_insert(parent_gh, BLI_ghashutil_pairalloc(dob->ob, dob->index), ob);
1104         }
1105         
1106         if (use_hierarchy) {
1107                 for (dob= lb->first; dob; dob= dob->next) {
1108                         /* original parents */
1109                         Object *ob_src=     dob->ob;
1110                         Object *ob_src_par= ob_src->parent;
1111
1112                         Object *ob_dst=     BLI_ghash_lookup(dupli_gh, dob);
1113                         Object *ob_dst_par= NULL;
1114
1115                         /* find parent that was also made real */
1116                         if (ob_src_par) {
1117                                 GHashPair *pair = BLI_ghashutil_pairalloc(ob_src_par, dob->index);
1118                                 ob_dst_par = BLI_ghash_lookup(parent_gh, pair);
1119                                 BLI_ghashutil_pairfree(pair);
1120                         }
1121
1122                         if (ob_dst_par) {
1123                                 /* allow for all possible parent types */
1124                                 ob_dst->partype= ob_src->partype;
1125                                 BLI_strncpy(ob_dst->parsubstr, ob_src->parsubstr, sizeof(ob_dst->parsubstr));
1126                                 ob_dst->par1= ob_src->par1;
1127                                 ob_dst->par2= ob_src->par2;
1128                                 ob_dst->par3= ob_src->par3;
1129
1130                                 copy_m4_m4(ob_dst->parentinv, ob_src->parentinv);
1131
1132                                 ob_dst->parent= ob_dst_par;
1133                         }
1134                         else if (use_base_parent) {
1135                                 ob_dst->parent= base->object;
1136                                 ob_dst->partype= PAROBJECT;
1137                         }
1138
1139                         if (ob_dst->parent) {
1140                                 invert_m4_m4(ob_dst->parentinv, dob->mat);
1141
1142                                 /* note, this may be the parent of other objects, but it should
1143                                  * still work out ok */
1144                                 object_apply_mat4(ob_dst, dob->mat, FALSE, TRUE);
1145
1146                                 /* to set ob_dst->orig and in case theres any other discrepicies */
1147                                 DAG_id_tag_update(&ob_dst->id, OB_RECALC_OB);
1148                         }
1149                 }
1150         }
1151         else if (use_base_parent) {
1152                 /* since we are ignoring the internal hierarchy - parent all to the
1153                  * base object */
1154                 for (dob= lb->first; dob; dob= dob->next) {
1155                         /* original parents */
1156                         Object *ob_dst= BLI_ghash_lookup(dupli_gh, dob);
1157
1158                         ob_dst->parent= base->object;
1159                         ob_dst->partype= PAROBJECT;
1160
1161                         /* similer to the code above, see comments */
1162                         invert_m4_m4(ob_dst->parentinv, dob->mat);
1163                         object_apply_mat4(ob_dst, dob->mat, FALSE, TRUE);
1164                         DAG_id_tag_update(&ob_dst->id, OB_RECALC_OB);
1165
1166
1167                 }
1168         }
1169
1170         if (dupli_gh)
1171                 BLI_ghash_free(dupli_gh, NULL, NULL);
1172         if (parent_gh)
1173                 BLI_ghash_free(parent_gh, BLI_ghashutil_pairfree, NULL);
1174
1175         copy_object_set_idnew(C, 0);
1176         
1177         free_object_duplilist(lb);
1178         
1179         base->object->transflag &= ~OB_DUPLI;
1180 }
1181
1182 static int object_duplicates_make_real_exec(bContext *C, wmOperator *op)
1183 {
1184         Main *bmain= CTX_data_main(C);
1185         Scene *scene= CTX_data_scene(C);
1186
1187         const short use_base_parent= RNA_boolean_get(op->ptr, "use_base_parent");
1188         const short use_hierarchy= RNA_boolean_get(op->ptr, "use_hierarchy");
1189         
1190         clear_id_newpoins();
1191                 
1192         CTX_DATA_BEGIN (C, Base*, base, selected_editable_bases) {
1193                 make_object_duplilist_real(C, scene, base, use_base_parent, use_hierarchy);
1194
1195                 /* dependencies were changed */
1196                 WM_event_add_notifier(C, NC_OBJECT|ND_PARENT, base->object);
1197         }
1198         CTX_DATA_END;
1199
1200         DAG_scene_sort(bmain, scene);
1201         DAG_ids_flush_update(bmain, 0);
1202         WM_event_add_notifier(C, NC_SCENE, scene);
1203         WM_main_add_notifier(NC_OBJECT|ND_DRAW, NULL);
1204         
1205         return OPERATOR_FINISHED;
1206 }
1207
1208 void OBJECT_OT_duplicates_make_real(wmOperatorType *ot)
1209 {
1210         
1211         /* identifiers */
1212         ot->name = "Make Duplicates Real";
1213         ot->description = "Make dupli objects attached to this object real";
1214         ot->idname = "OBJECT_OT_duplicates_make_real";
1215         
1216         /* api callbacks */
1217         ot->exec = object_duplicates_make_real_exec;
1218         
1219         ot->poll = ED_operator_objectmode;
1220         
1221         /* flags */
1222         ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
1223
1224         RNA_def_boolean(ot->srna, "use_base_parent", 0, "Parent", "Parent newly created objects to the original duplicator");
1225         RNA_def_boolean(ot->srna, "use_hierarchy", 0, "Keep Hierarchy", "Maintain parent child relationships");
1226 }
1227
1228 /**************************** Convert **************************/
1229
1230 static EnumPropertyItem convert_target_items[]= {
1231         {OB_CURVE, "CURVE", ICON_OUTLINER_OB_CURVE, "Curve from Mesh/Text", ""},
1232         {OB_MESH, "MESH", ICON_OUTLINER_OB_MESH, "Mesh from Curve/Meta/Surf/Text", ""},
1233         {0, NULL, 0, NULL, NULL}};
1234
1235 static void curvetomesh(Scene *scene, Object *ob) 
1236 {
1237         if (ob->disp.first == NULL)
1238                 makeDispListCurveTypes(scene, ob, 0); /* force creation */
1239
1240         nurbs_to_mesh(ob); /* also does users */
1241
1242         if (ob->type == OB_MESH)
1243                 object_free_modifiers(ob);
1244 }
1245
1246 static int convert_poll(bContext *C)
1247 {
1248         Object *obact= CTX_data_active_object(C);
1249         Scene *scene= CTX_data_scene(C);
1250
1251         return (!scene->id.lib && obact && scene->obedit != obact && (obact->flag & SELECT) && !(obact->id.lib));
1252 }
1253
1254 /* Helper for convert_exec */
1255 static Base *duplibase_for_convert(Scene *scene, Base *base, Object *ob)
1256 {
1257         Object *obn;
1258         Base *basen;
1259
1260         if (ob == NULL) {
1261                 ob= base->object;
1262         }
1263
1264         obn= copy_object(ob);
1265         obn->recalc |= OB_RECALC_OB|OB_RECALC_DATA|OB_RECALC_TIME;
1266
1267         basen= MEM_mallocN(sizeof(Base), "duplibase");
1268         *basen= *base;
1269         BLI_addhead(&scene->base, basen);       /* addhead: otherwise eternal loop */
1270         basen->object= obn;
1271         basen->flag |= SELECT;
1272         obn->flag |= SELECT;
1273         base->flag &= ~SELECT;
1274         ob->flag &= ~SELECT;
1275
1276         return basen;
1277 }
1278
1279 static int convert_exec(bContext *C, wmOperator *op)
1280 {
1281         Main *bmain= CTX_data_main(C);
1282         Scene *scene= CTX_data_scene(C);
1283         Base *basen=NULL, *basact=NULL, *basedel=NULL;
1284         Object *ob, *ob1, *newob, *obact= CTX_data_active_object(C);
1285         DerivedMesh *dm;
1286         Curve *cu;
1287         Nurb *nu;
1288         MetaBall *mb;
1289         Mesh *me;
1290         const short target= RNA_enum_get(op->ptr, "target");
1291         const short keep_original= RNA_boolean_get(op->ptr, "keep_original");
1292         int a, mballConverted= 0;
1293
1294         /* don't forget multiple users! */
1295
1296         /* reset flags */
1297         CTX_DATA_BEGIN (C, Base*, base, selected_editable_bases) {
1298                 ob= base->object;
1299                 ob->flag &= ~OB_DONE;
1300
1301                 /* flag data thats not been edited (only needed for !keep_original) */
1302                 if (ob->data) {
1303                         ((ID *)ob->data)->flag |= LIB_DOIT;
1304                 }
1305         }
1306         CTX_DATA_END;
1307
1308         CTX_DATA_BEGIN (C, Base*, base, selected_editable_bases) {
1309                 ob= base->object;
1310
1311                 if (ob->flag & OB_DONE || !IS_TAGGED(ob->data)) {
1312                         if (ob->type != target) {
1313                                 base->flag &= ~SELECT;
1314                                 ob->flag &= ~SELECT;
1315                         }
1316
1317                         /* obdata already modified */
1318                         if (!IS_TAGGED(ob->data)) {
1319                                 /* When 2 objects with linked data are selected, converting both
1320                                  * would keep modifiers on all but the converted object [#26003] */
1321                                 if (ob->type == OB_MESH) {
1322                                         object_free_modifiers(ob);      /* after derivedmesh calls! */
1323                                 }
1324                         }
1325                 }
1326                 else if (ob->type==OB_MESH && target == OB_CURVE) {
1327                         ob->flag |= OB_DONE;
1328
1329                         if (keep_original) {
1330                                 basen= duplibase_for_convert(scene, base, NULL);
1331                                 newob= basen->object;
1332
1333                                 /* decrement original mesh's usage count  */
1334                                 me= newob->data;
1335                                 me->id.us--;
1336
1337                                 /* make a new copy of the mesh */
1338                                 newob->data= copy_mesh(me);
1339                         }
1340                         else {
1341                                 newob = ob;
1342                         }
1343
1344                         mesh_to_curve(scene, newob);
1345
1346                         if (newob->type==OB_CURVE)
1347                                 object_free_modifiers(newob);   /* after derivedmesh calls! */
1348                 }
1349                 else if (ob->type==OB_MESH && ob->modifiers.first) { /* converting a mesh with no modifiers causes a segfault */
1350                         ob->flag |= OB_DONE;
1351
1352                         if (keep_original) {
1353                                 basen= duplibase_for_convert(scene, base, NULL);
1354                                 newob= basen->object;
1355
1356                                 /* decrement original mesh's usage count  */
1357                                 me= newob->data;
1358                                 me->id.us--;
1359
1360                                 /* make a new copy of the mesh */
1361                                 newob->data= copy_mesh(me);
1362                         }
1363                         else {
1364                                 newob = ob;
1365                                 ob->recalc |= OB_RECALC_OB|OB_RECALC_DATA|OB_RECALC_TIME;
1366                         }
1367
1368                         /* make new mesh data from the original copy */
1369                         /* note: get the mesh from the original, not from the copy in some
1370                          * cases this doesnt give correct results (when MDEF is used for eg)
1371                          */
1372                         dm= mesh_get_derived_final(scene, newob, CD_MASK_MESH);
1373                         /* dm= mesh_create_derived_no_deform(ob1, NULL);        this was called original (instead of get_derived). man o man why! (ton) */
1374
1375                         DM_to_mesh(dm, newob->data, newob);
1376
1377                         /* re-tessellation is called by DM_to_mesh */
1378
1379                         dm->release(dm);
1380                         object_free_modifiers(newob);   /* after derivedmesh calls! */
1381                 }
1382                 else if (ob->type==OB_FONT) {
1383                         ob->flag |= OB_DONE;
1384
1385                         if (keep_original) {
1386                                 basen= duplibase_for_convert(scene, base, NULL);
1387                                 newob= basen->object;
1388
1389                                 /* decrement original curve's usage count  */
1390                                 ((Curve *)newob->data)->id.us--;
1391
1392                                 /* make a new copy of the curve */
1393                                 newob->data= copy_curve(ob->data);
1394                         }
1395                         else {
1396                                 newob= ob;
1397                         }
1398
1399                         cu= newob->data;
1400
1401                         if (!newob->disp.first)
1402                                 makeDispListCurveTypes(scene, newob, 0);
1403
1404                         newob->type= OB_CURVE;
1405                         cu->type= OB_CURVE;
1406
1407                         if (cu->vfont) {
1408                                 cu->vfont->id.us--;
1409                                 cu->vfont= NULL;
1410                         }
1411                         if (cu->vfontb) {
1412                                 cu->vfontb->id.us--;
1413                                 cu->vfontb= NULL;
1414                         }
1415                         if (cu->vfonti) {
1416                                 cu->vfonti->id.us--;
1417                                 cu->vfonti= NULL;
1418                         }
1419                         if (cu->vfontbi) {
1420                                 cu->vfontbi->id.us--;
1421                                 cu->vfontbi= NULL;
1422                         }
1423
1424                         if (!keep_original) {
1425                                 /* other users */
1426                                 if (cu->id.us>1) {
1427                                         for (ob1= bmain->object.first; ob1; ob1=ob1->id.next) {
1428                                                 if (ob1->data==ob->data) {
1429                                                         ob1->type= OB_CURVE;
1430                                                         ob1->recalc |= OB_RECALC_OB|OB_RECALC_DATA|OB_RECALC_TIME;
1431                                                 }
1432                                         }
1433                                 }
1434                         }
1435
1436                         for (nu=cu->nurb.first; nu; nu=nu->next)
1437                                 nu->charidx= 0;
1438
1439                         if (target == OB_MESH) {
1440                                 curvetomesh(scene, newob);
1441
1442                                 /* meshes doesn't use displist */
1443                                 freedisplist(&newob->disp);
1444                         }
1445                 }
1446                 else if (ELEM(ob->type, OB_CURVE, OB_SURF)) {
1447                         ob->flag |= OB_DONE;
1448
1449                         if (target == OB_MESH) {
1450                                 if (keep_original) {
1451                                         basen= duplibase_for_convert(scene, base, NULL);
1452                                         newob= basen->object;
1453
1454                                         /* decrement original curve's usage count  */
1455                                         ((Curve *)newob->data)->id.us--;
1456
1457                                         /* make a new copy of the curve */
1458                                         newob->data= copy_curve(ob->data);
1459                                 }
1460                                 else {
1461                                         newob= ob;
1462
1463                                         /* meshes doesn't use displist */
1464                                         freedisplist(&newob->disp);
1465                                 }
1466
1467                                 curvetomesh(scene, newob);
1468                         }
1469                 }
1470                 else if (ob->type==OB_MBALL && target == OB_MESH) {
1471                         Object *baseob;
1472
1473                         base->flag &= ~SELECT;
1474                         ob->flag &= ~SELECT;
1475
1476                         baseob= find_basis_mball(scene, ob);
1477
1478                         if (ob != baseob) {
1479                                 /* if motherball is converting it would be marked as done later */
1480                                 ob->flag |= OB_DONE;
1481                         }
1482
1483                         if (!baseob->disp.first) {
1484                                 makeDispListMBall(scene, baseob);
1485                         }
1486
1487                         if (!(baseob->flag & OB_DONE)) {
1488                                 baseob->flag |= OB_DONE;
1489
1490                                 basen= duplibase_for_convert(scene, base, baseob);
1491                                 newob= basen->object;
1492
1493                                 mb= newob->data;
1494                                 mb->id.us--;
1495
1496                                 newob->data= add_mesh("Mesh");
1497                                 newob->type= OB_MESH;
1498
1499                                 me= newob->data;
1500                                 me->totcol= mb->totcol;
1501                                 if (newob->totcol) {
1502                                         me->mat= MEM_dupallocN(mb->mat);
1503                                         for (a=0; a<newob->totcol; a++) id_us_plus((ID *)me->mat[a]);
1504                                 }
1505
1506                                 mball_to_mesh(&baseob->disp, newob->data);
1507
1508                                 if (obact->type == OB_MBALL) {
1509                                         basact= basen;
1510                                 }
1511
1512                                 mballConverted= 1;
1513                         }
1514                 }
1515                 else {
1516                         continue;
1517                 }
1518
1519                 /* tag obdata if it was been changed */
1520
1521                 /* If the original object is active then make this object active */
1522                 if (basen) {
1523                         if (ob == obact) {
1524                                 /* store new active base to update BASACT */
1525                                 basact= basen;
1526                         }
1527
1528                         basen= NULL;
1529                 }
1530
1531                 if (!keep_original && (ob->flag & OB_DONE)) {
1532                         DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
1533                         ((ID *)ob->data)->flag &= ~LIB_DOIT; /* flag not to convert this datablock again */
1534                 }
1535
1536                 /* delete original if needed */
1537                 if (basedel) {
1538                         if (!keep_original)
1539                                 ED_base_object_free_and_unlink(bmain, scene, basedel);  
1540
1541                         basedel = NULL;
1542                 }
1543         }
1544         CTX_DATA_END;
1545
1546         if (!keep_original) {
1547                 if (mballConverted) {
1548                         Base *base= scene->base.first, *tmpbase;
1549                         while (base) {
1550                                 ob= base->object;
1551                                 tmpbase= base;
1552                                 base= base->next;
1553
1554                                 if (ob->type == OB_MBALL) {
1555                                         ED_base_object_free_and_unlink(bmain, scene, tmpbase);
1556                                 }
1557                         }
1558                 }
1559
1560                 /* delete object should renew depsgraph */
1561                 DAG_scene_sort(bmain, scene);
1562         }
1563
1564 // XXX  ED_object_enter_editmode(C, 0);
1565 // XXX  exit_editmode(C, EM_FREEDATA|EM_WAITCURSOR); /* freedata, but no undo */
1566
1567         if (basact) {
1568                 /* active base was changed */
1569                 ED_base_object_activate(C, basact);
1570                 BASACT= basact;
1571         }
1572         else if (BASACT->object->flag & OB_DONE) {
1573                 WM_event_add_notifier(C, NC_OBJECT|ND_MODIFIER, BASACT->object);
1574                 WM_event_add_notifier(C, NC_OBJECT|ND_DATA, BASACT->object);
1575         }
1576
1577         DAG_scene_sort(bmain, scene);
1578         WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, scene);
1579         WM_event_add_notifier(C, NC_SCENE|ND_OB_SELECT, scene);
1580
1581         return OPERATOR_FINISHED;
1582 }
1583
1584
1585 void OBJECT_OT_convert(wmOperatorType *ot)
1586 {
1587         /* identifiers */
1588         ot->name = "Convert to";
1589         ot->description = "Convert selected objects to another type";
1590         ot->idname = "OBJECT_OT_convert";
1591         
1592         /* api callbacks */
1593         ot->invoke = WM_menu_invoke;
1594         ot->exec = convert_exec;
1595         ot->poll = convert_poll;
1596         
1597         /* flags */
1598         ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
1599
1600         /* properties */
1601         ot->prop = RNA_def_enum(ot->srna, "target", convert_target_items, OB_MESH, "Target", "Type of object to convert to");
1602         RNA_def_boolean(ot->srna, "keep_original", 0, "Keep Original", "Keep original objects instead of replacing them");
1603 }
1604
1605 /**************************** Duplicate ************************/
1606
1607 /* 
1608  * dupflag: a flag made from constants declared in DNA_userdef_types.h
1609  * The flag tells adduplicate() weather to copy data linked to the object, or to reference the existing data.
1610  * U.dupflag for default operations or you can construct a flag as python does
1611  * if the dupflag is 0 then no data will be copied (linked duplicate) */
1612
1613 /* used below, assumes id.new is correct */
1614 /* leaves selection of base/object unaltered */
1615 static Base *object_add_duplicate_internal(Main *bmain, Scene *scene, Base *base, int dupflag)
1616 {
1617         Base *basen= NULL;
1618         Material ***matarar;
1619         Object *ob, *obn;
1620         ID *id;
1621         int a, didit;
1622
1623         ob= base->object;
1624         if (ob->mode & OB_MODE_POSE) {
1625                 ; /* nothing? */
1626         }
1627         else {
1628                 obn= copy_object(ob);
1629                 obn->recalc |= OB_RECALC_OB|OB_RECALC_DATA|OB_RECALC_TIME;
1630                 
1631                 basen= MEM_mallocN(sizeof(Base), "duplibase");
1632                 *basen= *base;
1633                 BLI_addhead(&scene->base, basen);       /* addhead: prevent eternal loop */
1634                 basen->object= obn;
1635                 
1636                 if (basen->flag & OB_FROMGROUP) {
1637                         Group *group;
1638                         for (group= bmain->group.first; group; group= group->id.next) {
1639                                 if (object_in_group(ob, group))
1640                                         add_to_group(group, obn, scene, basen);
1641                         }
1642                 }
1643                 
1644                 /* duplicates using userflags */
1645                 if (dupflag & USER_DUP_ACT) {
1646                         BKE_copy_animdata_id_action(&obn->id);
1647                 }
1648                 
1649                 if (dupflag & USER_DUP_MAT) {
1650                         for (a=0; a<obn->totcol; a++) {
1651                                 id= (ID *)obn->mat[a];
1652                                 if (id) {
1653                                         ID_NEW_US(obn->mat[a])
1654                                         else obn->mat[a]= copy_material(obn->mat[a]);
1655                                         id->us--;
1656                                         
1657                                         if (dupflag & USER_DUP_ACT) {
1658                                                 BKE_copy_animdata_id_action(&obn->mat[a]->id);
1659                                         }
1660                                 }
1661                         }
1662                 }
1663                 if (dupflag & USER_DUP_PSYS) {
1664                         ParticleSystem *psys;
1665                         for (psys=obn->particlesystem.first; psys; psys=psys->next) {
1666                                 id= (ID*) psys->part;
1667                                 if (id) {
1668                                         ID_NEW_US(psys->part)
1669                                         else psys->part= psys_copy_settings(psys->part);
1670                                         
1671                                         if (dupflag & USER_DUP_ACT) {
1672                                                 BKE_copy_animdata_id_action(&psys->part->id);
1673                                         }
1674
1675                                         id->us--;
1676                                 }
1677                         }
1678                 }
1679                 
1680                 id= obn->data;
1681                 didit= 0;
1682                 
1683                 switch (obn->type) {
1684                         case OB_MESH:
1685                                 if (dupflag & USER_DUP_MESH) {
1686                                         ID_NEW_US2( obn->data )
1687                                         else {
1688                                                 obn->data= copy_mesh(obn->data);
1689                                                 
1690                                                 if (obn->fluidsimSettings) {
1691                                                         obn->fluidsimSettings->orgMesh = (Mesh *)obn->data;
1692                                                 }
1693                                                 
1694                                                 didit= 1;
1695                                         }
1696                                         id->us--;
1697                                 }
1698                                 break;
1699                         case OB_CURVE:
1700                                 if (dupflag & USER_DUP_CURVE) {
1701                                         ID_NEW_US2(obn->data )
1702                                         else {
1703                                                 obn->data= copy_curve(obn->data);
1704                                                 didit= 1;
1705                                         }
1706                                         id->us--;
1707                                 }
1708                                 break;
1709                         case OB_SURF:
1710                                 if (dupflag & USER_DUP_SURF) {
1711                                         ID_NEW_US2( obn->data )
1712                                         else {
1713                                                 obn->data= copy_curve(obn->data);
1714                                                 didit= 1;
1715                                         }
1716                                         id->us--;
1717                                 }
1718                                 break;
1719                         case OB_FONT:
1720                                 if (dupflag & USER_DUP_FONT) {
1721                                         ID_NEW_US2( obn->data )
1722                                         else {
1723                                                 obn->data= copy_curve(obn->data);
1724                                                 didit= 1;
1725                                         }
1726                                         id->us--;
1727                                 }
1728                                 break;
1729                         case OB_MBALL:
1730                                 if (dupflag & USER_DUP_MBALL) {
1731                                         ID_NEW_US2(obn->data )
1732                                         else {
1733                                                 obn->data= copy_mball(obn->data);
1734                                                 didit= 1;
1735                                         }
1736                                         id->us--;
1737                                 }
1738                                 break;
1739                         case OB_LAMP:
1740                                 if (dupflag & USER_DUP_LAMP) {
1741                                         ID_NEW_US2(obn->data )
1742                                         else {
1743                                                 obn->data= copy_lamp(obn->data);
1744                                                 didit= 1;
1745                                         }
1746                                         id->us--;
1747                                 }
1748                                 break;
1749                                 
1750                         case OB_ARMATURE:
1751                                 obn->recalc |= OB_RECALC_DATA;
1752                                 if (obn->pose) obn->pose->flag |= POSE_RECALC;
1753                                         
1754                                         if (dupflag & USER_DUP_ARM) {
1755                                                 ID_NEW_US2(obn->data )
1756                                                 else {
1757                                                         obn->data= copy_armature(obn->data);
1758                                                         armature_rebuild_pose(obn, obn->data);
1759                                                         didit= 1;
1760                                                 }
1761                                                 id->us--;
1762                                         }
1763                                                 
1764                                                 break;
1765                                 
1766                         case OB_LATTICE:
1767                                 if (dupflag!=0) {
1768                                         ID_NEW_US2(obn->data )
1769                                         else {
1770                                                 obn->data= copy_lattice(obn->data);
1771                                                 didit= 1;
1772                                         }
1773                                         id->us--;
1774                                 }
1775                                 break;
1776                         case OB_CAMERA:
1777                                 if (dupflag!=0) {
1778                                         ID_NEW_US2(obn->data )
1779                                         else {
1780                                                 obn->data= copy_camera(obn->data);
1781                                                 didit= 1;
1782                                         }
1783                                         id->us--;
1784                                 }
1785                                 break;
1786                         case OB_SPEAKER:
1787                                 if (dupflag!=0) {
1788                                         ID_NEW_US2(obn->data )
1789                                         else {
1790                                                 obn->data= copy_speaker(obn->data);
1791                                                 didit= 1;
1792                                         }
1793                                         id->us--;
1794                                 }
1795                                 break;
1796
1797                 }
1798
1799                 /* check if obdata is copied */
1800                 if (didit) {
1801                         Key *key = ob_get_key(obn);
1802                         
1803                         if (dupflag & USER_DUP_ACT) {
1804                                 bActuator *act;
1805
1806                                 BKE_copy_animdata_id_action((ID *)obn->data);
1807                                 if (key) {
1808                                         BKE_copy_animdata_id_action((ID*)key);
1809                                 }
1810
1811                                 /* Update the duplicated action in the action actuators */
1812                                 for (act = obn->actuators.first; act; act = act->next) {
1813                                         if (act->type == ACT_ACTION) {
1814                                                 bActionActuator* actact = (bActionActuator*) act->data;
1815                                                 if (ob->adt && actact->act == ob->adt->action) {
1816                                                         actact->act = obn->adt->action;
1817                                                 }
1818                                         }
1819                                 }
1820                         }
1821                         
1822                         if (dupflag & USER_DUP_MAT) {
1823                                 matarar= give_matarar(obn);
1824                                 if (matarar) {
1825                                         for (a=0; a<obn->totcol; a++) {
1826                                                 id= (ID *)(*matarar)[a];
1827                                                 if (id) {
1828                                                         ID_NEW_US( (*matarar)[a] )
1829                                                         else (*matarar)[a]= copy_material((*matarar)[a]);
1830                                                         
1831                                                         id->us--;
1832                                                 }
1833                                         }
1834                                 }
1835                         }
1836                 }
1837         }
1838         return basen;
1839 }
1840
1841 /* single object duplicate, if dupflag==0, fully linked, else it uses the flags given */
1842 /* leaves selection of base/object unaltered.
1843  * note: don't call this within a loop since clear_* funcs loop over the entire database. */
1844 Base *ED_object_add_duplicate(Main *bmain, Scene *scene, Base *base, int dupflag)
1845 {
1846         Base *basen;
1847         Object *ob;
1848
1849         clear_id_newpoins();
1850         clear_sca_new_poins();  /* sensor/contr/act */
1851
1852         basen= object_add_duplicate_internal(bmain, scene, base, dupflag);
1853         if (basen == NULL) {
1854                 return NULL;
1855         }
1856
1857         ob= basen->object;
1858
1859         /* link own references to the newly duplicated data [#26816] */
1860         object_relink(ob);
1861         set_sca_new_poins_ob(ob);
1862
1863         DAG_scene_sort(bmain, scene);
1864         if (ob->data) {
1865                 ED_render_id_flush_update(bmain, ob->data);
1866         }
1867
1868         return basen;
1869 }
1870
1871 /* contextual operator dupli */
1872 static int duplicate_exec(bContext *C, wmOperator *op)
1873 {
1874         Main *bmain= CTX_data_main(C);
1875         Scene *scene= CTX_data_scene(C);
1876         int linked= RNA_boolean_get(op->ptr, "linked");
1877         int dupflag= (linked)? 0: U.dupflag;
1878         
1879         clear_id_newpoins();
1880         clear_sca_new_poins();  /* sensor/contr/act */
1881         
1882         CTX_DATA_BEGIN (C, Base*, base, selected_editable_bases) {
1883                 Base *basen= object_add_duplicate_internal(bmain, scene, base, dupflag);
1884                 
1885                 /* note that this is safe to do with this context iterator,
1886                  * the list is made in advance */
1887                 ED_base_object_select(base, BA_DESELECT);
1888
1889                 if (basen == NULL) {
1890                         continue;
1891                 }
1892
1893                 /* new object becomes active */
1894                 if (BASACT==base)
1895                         ED_base_object_activate(C, basen);
1896
1897                 if (basen->object->data) {
1898                         DAG_id_tag_update(basen->object->data, 0);
1899                 }
1900         }
1901         CTX_DATA_END;
1902
1903         copy_object_set_idnew(C, dupflag);
1904
1905         DAG_scene_sort(bmain, scene);
1906         DAG_ids_flush_update(bmain, 0);
1907
1908         WM_event_add_notifier(C, NC_SCENE|ND_OB_SELECT, scene);
1909
1910         return OPERATOR_FINISHED;
1911 }
1912
1913 void OBJECT_OT_duplicate(wmOperatorType *ot)
1914 {
1915         PropertyRNA *prop;
1916         
1917         /* identifiers */
1918         ot->name = "Duplicate Objects";
1919         ot->description = "Duplicate selected objects";
1920         ot->idname = "OBJECT_OT_duplicate";
1921         
1922         /* api callbacks */
1923         ot->exec = duplicate_exec;
1924         ot->poll = ED_operator_objectmode;
1925         
1926         /* flags */
1927         ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
1928         
1929         /* to give to transform */
1930         RNA_def_boolean(ot->srna, "linked", 0, "Linked", "Duplicate object but not object data, linking to the original data");
1931         prop= RNA_def_enum(ot->srna, "mode", transform_mode_types, TFM_TRANSLATION, "Mode", "");
1932         RNA_def_property_flag(prop, PROP_HIDDEN);
1933 }
1934
1935 /* **************** add named object, for dragdrop ************* */
1936
1937
1938 static int add_named_exec(bContext *C, wmOperator *op)
1939 {
1940         Main *bmain= CTX_data_main(C);
1941         Scene *scene= CTX_data_scene(C);
1942         Base *basen, *base;
1943         Object *ob;
1944         int linked= RNA_boolean_get(op->ptr, "linked");
1945         int dupflag= (linked)? 0: U.dupflag;
1946         char name[MAX_ID_NAME-2];
1947
1948         /* find object, create fake base */
1949         RNA_string_get(op->ptr, "name", name);
1950         ob= (Object *)find_id("OB", name);
1951         if (ob==NULL) 
1952                 return OPERATOR_CANCELLED;
1953
1954         base= MEM_callocN(sizeof(Base), "duplibase");
1955         base->object= ob;
1956         base->flag= ob->flag;
1957
1958         /* prepare dupli */
1959         clear_id_newpoins();
1960         clear_sca_new_poins();  /* sensor/contr/act */
1961
1962         basen= object_add_duplicate_internal(bmain, scene, base, dupflag);
1963
1964         if (basen == NULL) {
1965                 MEM_freeN(base);
1966                 return OPERATOR_CANCELLED;
1967         }
1968
1969         basen->lay= basen->object->lay= scene->lay;
1970
1971         ED_object_location_from_view(C, basen->object->loc);
1972         ED_base_object_activate(C, basen);
1973
1974         copy_object_set_idnew(C, dupflag);
1975
1976         DAG_scene_sort(bmain, scene);
1977         DAG_ids_flush_update(bmain, 0);
1978
1979         MEM_freeN(base);
1980
1981         WM_event_add_notifier(C, NC_SCENE|ND_OB_SELECT, scene);
1982
1983         return OPERATOR_FINISHED;
1984 }
1985
1986 void OBJECT_OT_add_named(wmOperatorType *ot)
1987 {
1988         /* identifiers */
1989         ot->name = "Add Named Object";
1990         ot->description = "Add named object";
1991         ot->idname = "OBJECT_OT_add_named";
1992         
1993         /* api callbacks */
1994         ot->exec = add_named_exec;
1995         ot->poll = ED_operator_objectmode;
1996         
1997         /* flags */
1998         ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
1999         
2000         RNA_def_boolean(ot->srna, "linked", 0, "Linked", "Duplicate object but not object data, linking to the original data");
2001         RNA_def_string(ot->srna, "name", "Cube", MAX_ID_NAME-2, "Name", "Object name to add");
2002 }
2003
2004
2005
2006 /**************************** Join *************************/
2007 static int join_poll(bContext *C)
2008 {
2009         Object *ob= CTX_data_active_object(C);
2010         
2011         if (!ob || ob->id.lib) return 0;
2012         
2013         if (ELEM4(ob->type, OB_MESH, OB_CURVE, OB_SURF, OB_ARMATURE))
2014                 return ED_operator_screenactive(C);
2015         else
2016                 return 0;
2017 }
2018
2019
2020 static int join_exec(bContext *C, wmOperator *op)
2021 {
2022         Scene *scene= CTX_data_scene(C);
2023         Object *ob= CTX_data_active_object(C);
2024
2025         if (scene->obedit) {
2026                 BKE_report(op->reports, RPT_ERROR, "This data does not support joining in editmode");
2027                 return OPERATOR_CANCELLED;
2028         }
2029         else if (object_data_is_libdata(ob)) {
2030                 BKE_report(op->reports, RPT_ERROR, "Can't edit external libdata");
2031                 return OPERATOR_CANCELLED;
2032         }
2033
2034         if (ob->type == OB_MESH)
2035                 return join_mesh_exec(C, op);
2036         else if (ELEM(ob->type, OB_CURVE, OB_SURF))
2037                 return join_curve_exec(C, op);
2038         else if (ob->type == OB_ARMATURE)
2039                 return join_armature_exec(C, op);
2040         
2041         return OPERATOR_CANCELLED;
2042 }
2043
2044 void OBJECT_OT_join(wmOperatorType *ot)
2045 {
2046         /* identifiers */
2047         ot->name = "Join";
2048         ot->description = "Join selected objects into active object";
2049         ot->idname = "OBJECT_OT_join";
2050         
2051         /* api callbacks */
2052         ot->exec = join_exec;
2053         ot->poll = join_poll;
2054         
2055         /* flags */
2056         ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
2057 }
2058
2059 /**************************** Join as Shape Key*************************/
2060 static int join_shapes_poll(bContext *C)
2061 {
2062         Object *ob= CTX_data_active_object(C);
2063         
2064         if (!ob || ob->id.lib) return 0;
2065         
2066         /* only meshes supported at the moment */
2067         if (ob->type == OB_MESH)
2068                 return ED_operator_screenactive(C);
2069         else
2070                 return 0;
2071 }
2072
2073 static int join_shapes_exec(bContext *C, wmOperator *op)
2074 {
2075         Scene *scene= CTX_data_scene(C);
2076         Object *ob= CTX_data_active_object(C);
2077         
2078         if (scene->obedit) {
2079                 BKE_report(op->reports, RPT_ERROR, "This data does not support joining in editmode");
2080                 return OPERATOR_CANCELLED;
2081         }
2082         else if (object_data_is_libdata(ob)) {
2083                 BKE_report(op->reports, RPT_ERROR, "Can't edit external libdata");
2084                 return OPERATOR_CANCELLED;
2085         }
2086         
2087         if (ob->type == OB_MESH)
2088                 return join_mesh_shapes_exec(C, op);
2089         
2090         return OPERATOR_CANCELLED;
2091 }
2092
2093 void OBJECT_OT_join_shapes(wmOperatorType *ot)
2094 {
2095         /* identifiers */
2096         ot->name = "Join as Shapes";
2097         ot->description = "Merge selected objects to shapes of active object";
2098         ot->idname = "OBJECT_OT_join_shapes";
2099         
2100         /* api callbacks */
2101         ot->exec = join_shapes_exec;
2102         ot->poll = join_shapes_poll;
2103         
2104         /* flags */
2105         ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
2106 }