style cleanup: editors / mesh & object
[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
1236 static void curvetomesh(Scene *scene, Object *ob) 
1237 {
1238         if (ob->disp.first == NULL)
1239                 makeDispListCurveTypes(scene, ob, 0);  /* force creation */
1240
1241         nurbs_to_mesh(ob); /* also does users */
1242
1243         if (ob->type == OB_MESH)
1244                 object_free_modifiers(ob);
1245 }
1246
1247 static int convert_poll(bContext *C)
1248 {
1249         Object *obact = CTX_data_active_object(C);
1250         Scene *scene = CTX_data_scene(C);
1251
1252         return (!scene->id.lib && obact && scene->obedit != obact && (obact->flag & SELECT) && !(obact->id.lib));
1253 }
1254
1255 /* Helper for convert_exec */
1256 static Base *duplibase_for_convert(Scene *scene, Base *base, Object *ob)
1257 {
1258         Object *obn;
1259         Base *basen;
1260
1261         if (ob == NULL) {
1262                 ob = base->object;
1263         }
1264
1265         obn = copy_object(ob);
1266         obn->recalc |= OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME;
1267
1268         basen = MEM_mallocN(sizeof(Base), "duplibase");
1269         *basen = *base;
1270         BLI_addhead(&scene->base, basen);   /* addhead: otherwise eternal loop */
1271         basen->object = obn;
1272         basen->flag |= SELECT;
1273         obn->flag |= SELECT;
1274         base->flag &= ~SELECT;
1275         ob->flag &= ~SELECT;
1276
1277         return basen;
1278 }
1279
1280 static int convert_exec(bContext *C, wmOperator *op)
1281 {
1282         Main *bmain = CTX_data_main(C);
1283         Scene *scene = CTX_data_scene(C);
1284         Base *basen = NULL, *basact = NULL, *basedel = NULL;
1285         Object *ob, *ob1, *newob, *obact = CTX_data_active_object(C);
1286         DerivedMesh *dm;
1287         Curve *cu;
1288         Nurb *nu;
1289         MetaBall *mb;
1290         Mesh *me;
1291         const short target = RNA_enum_get(op->ptr, "target");
1292         const short keep_original = RNA_boolean_get(op->ptr, "keep_original");
1293         int a, mballConverted = 0;
1294
1295         /* don't forget multiple users! */
1296
1297         /* reset flags */
1298         CTX_DATA_BEGIN (C, Base *, base, selected_editable_bases) {
1299                 ob = base->object;
1300                 ob->flag &= ~OB_DONE;
1301
1302                 /* flag data thats not been edited (only needed for !keep_original) */
1303                 if (ob->data) {
1304                         ((ID *)ob->data)->flag |= LIB_DOIT;
1305                 }
1306         }
1307         CTX_DATA_END;
1308
1309         CTX_DATA_BEGIN (C, Base *, base, selected_editable_bases) {
1310                 ob = base->object;
1311
1312                 if (ob->flag & OB_DONE || !IS_TAGGED(ob->data)) {
1313                         if (ob->type != target) {
1314                                 base->flag &= ~SELECT;
1315                                 ob->flag &= ~SELECT;
1316                         }
1317
1318                         /* obdata already modified */
1319                         if (!IS_TAGGED(ob->data)) {
1320                                 /* When 2 objects with linked data are selected, converting both
1321                                  * would keep modifiers on all but the converted object [#26003] */
1322                                 if (ob->type == OB_MESH) {
1323                                         object_free_modifiers(ob);  /* after derivedmesh calls! */
1324                                 }
1325                         }
1326                 }
1327                 else if (ob->type == OB_MESH && target == OB_CURVE) {
1328                         ob->flag |= OB_DONE;
1329
1330                         if (keep_original) {
1331                                 basen = duplibase_for_convert(scene, base, NULL);
1332                                 newob = basen->object;
1333
1334                                 /* decrement original mesh's usage count  */
1335                                 me = newob->data;
1336                                 me->id.us--;
1337
1338                                 /* make a new copy of the mesh */
1339                                 newob->data = copy_mesh(me);
1340                         }
1341                         else {
1342                                 newob = ob;
1343                         }
1344
1345                         mesh_to_curve(scene, newob);
1346
1347                         if (newob->type == OB_CURVE)
1348                                 object_free_modifiers(newob);   /* after derivedmesh calls! */
1349                 }
1350                 else if (ob->type == OB_MESH && ob->modifiers.first) { /* converting a mesh with no modifiers causes a segfault */
1351                         ob->flag |= OB_DONE;
1352
1353                         if (keep_original) {
1354                                 basen = duplibase_for_convert(scene, base, NULL);
1355                                 newob = basen->object;
1356
1357                                 /* decrement original mesh's usage count  */
1358                                 me = newob->data;
1359                                 me->id.us--;
1360
1361                                 /* make a new copy of the mesh */
1362                                 newob->data = copy_mesh(me);
1363                         }
1364                         else {
1365                                 newob = ob;
1366                                 ob->recalc |= OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME;
1367                         }
1368
1369                         /* make new mesh data from the original copy */
1370                         /* note: get the mesh from the original, not from the copy in some
1371                          * cases this doesnt give correct results (when MDEF is used for eg)
1372                          */
1373                         dm = mesh_get_derived_final(scene, newob, CD_MASK_MESH);
1374                         /* dm= mesh_create_derived_no_deform(ob1, NULL);        this was called original (instead of get_derived). man o man why! (ton) */
1375
1376                         DM_to_mesh(dm, newob->data, newob);
1377
1378                         /* re-tessellation is called by DM_to_mesh */
1379
1380                         dm->release(dm);
1381                         object_free_modifiers(newob);   /* after derivedmesh calls! */
1382                 }
1383                 else if (ob->type == OB_FONT) {
1384                         ob->flag |= OB_DONE;
1385
1386                         if (keep_original) {
1387                                 basen = duplibase_for_convert(scene, base, NULL);
1388                                 newob = basen->object;
1389
1390                                 /* decrement original curve's usage count  */
1391                                 ((Curve *)newob->data)->id.us--;
1392
1393                                 /* make a new copy of the curve */
1394                                 newob->data = copy_curve(ob->data);
1395                         }
1396                         else {
1397                                 newob = ob;
1398                         }
1399
1400                         cu = newob->data;
1401
1402                         if (!newob->disp.first)
1403                                 makeDispListCurveTypes(scene, newob, 0);
1404
1405                         newob->type = OB_CURVE;
1406                         cu->type = OB_CURVE;
1407
1408                         if (cu->vfont) {
1409                                 cu->vfont->id.us--;
1410                                 cu->vfont = NULL;
1411                         }
1412                         if (cu->vfontb) {
1413                                 cu->vfontb->id.us--;
1414                                 cu->vfontb = NULL;
1415                         }
1416                         if (cu->vfonti) {
1417                                 cu->vfonti->id.us--;
1418                                 cu->vfonti = NULL;
1419                         }
1420                         if (cu->vfontbi) {
1421                                 cu->vfontbi->id.us--;
1422                                 cu->vfontbi = NULL;
1423                         }
1424
1425                         if (!keep_original) {
1426                                 /* other users */
1427                                 if (cu->id.us > 1) {
1428                                         for (ob1 = bmain->object.first; ob1; ob1 = ob1->id.next) {
1429                                                 if (ob1->data == ob->data) {
1430                                                         ob1->type = OB_CURVE;
1431                                                         ob1->recalc |= OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME;
1432                                                 }
1433                                         }
1434                                 }
1435                         }
1436
1437                         for (nu = cu->nurb.first; nu; nu = nu->next)
1438                                 nu->charidx = 0;
1439
1440                         if (target == OB_MESH) {
1441                                 curvetomesh(scene, newob);
1442
1443                                 /* meshes doesn't use displist */
1444                                 freedisplist(&newob->disp);
1445                         }
1446                 }
1447                 else if (ELEM(ob->type, OB_CURVE, OB_SURF)) {
1448                         ob->flag |= OB_DONE;
1449
1450                         if (target == OB_MESH) {
1451                                 if (keep_original) {
1452                                         basen = duplibase_for_convert(scene, base, NULL);
1453                                         newob = basen->object;
1454
1455                                         /* decrement original curve's usage count  */
1456                                         ((Curve *)newob->data)->id.us--;
1457
1458                                         /* make a new copy of the curve */
1459                                         newob->data = copy_curve(ob->data);
1460                                 }
1461                                 else {
1462                                         newob = ob;
1463
1464                                         /* meshes doesn't use displist */
1465                                         freedisplist(&newob->disp);
1466                                 }
1467
1468                                 curvetomesh(scene, newob);
1469                         }
1470                 }
1471                 else if (ob->type == OB_MBALL && target == OB_MESH) {
1472                         Object *baseob;
1473
1474                         base->flag &= ~SELECT;
1475                         ob->flag &= ~SELECT;
1476
1477                         baseob = find_basis_mball(scene, ob);
1478
1479                         if (ob != baseob) {
1480                                 /* if motherball is converting it would be marked as done later */
1481                                 ob->flag |= OB_DONE;
1482                         }
1483
1484                         if (!baseob->disp.first) {
1485                                 makeDispListMBall(scene, baseob);
1486                         }
1487
1488                         if (!(baseob->flag & OB_DONE)) {
1489                                 baseob->flag |= OB_DONE;
1490
1491                                 basen = duplibase_for_convert(scene, base, baseob);
1492                                 newob = basen->object;
1493
1494                                 mb = newob->data;
1495                                 mb->id.us--;
1496
1497                                 newob->data = add_mesh("Mesh");
1498                                 newob->type = OB_MESH;
1499
1500                                 me = newob->data;
1501                                 me->totcol = mb->totcol;
1502                                 if (newob->totcol) {
1503                                         me->mat = MEM_dupallocN(mb->mat);
1504                                         for (a = 0; a < newob->totcol; a++) id_us_plus((ID *)me->mat[a]);
1505                                 }
1506
1507                                 mball_to_mesh(&baseob->disp, newob->data);
1508
1509                                 if (obact->type == OB_MBALL) {
1510                                         basact = basen;
1511                                 }
1512
1513                                 mballConverted = 1;
1514                         }
1515                 }
1516                 else {
1517                         continue;
1518                 }
1519
1520                 /* tag obdata if it was been changed */
1521
1522                 /* If the original object is active then make this object active */
1523                 if (basen) {
1524                         if (ob == obact) {
1525                                 /* store new active base to update BASACT */
1526                                 basact = basen;
1527                         }
1528
1529                         basen = NULL;
1530                 }
1531
1532                 if (!keep_original && (ob->flag & OB_DONE)) {
1533                         DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
1534                         ((ID *)ob->data)->flag &= ~LIB_DOIT; /* flag not to convert this datablock again */
1535                 }
1536
1537                 /* delete original if needed */
1538                 if (basedel) {
1539                         if (!keep_original)
1540                                 ED_base_object_free_and_unlink(bmain, scene, basedel);  
1541
1542                         basedel = NULL;
1543                 }
1544         }
1545         CTX_DATA_END;
1546
1547         if (!keep_original) {
1548                 if (mballConverted) {
1549                         Base *base = scene->base.first, *tmpbase;
1550                         while (base) {
1551                                 ob = base->object;
1552                                 tmpbase = base;
1553                                 base = base->next;
1554
1555                                 if (ob->type == OB_MBALL) {
1556                                         ED_base_object_free_and_unlink(bmain, scene, tmpbase);
1557                                 }
1558                         }
1559                 }
1560
1561                 /* delete object should renew depsgraph */
1562                 DAG_scene_sort(bmain, scene);
1563         }
1564
1565 // XXX  ED_object_enter_editmode(C, 0);
1566 // XXX  exit_editmode(C, EM_FREEDATA|EM_WAITCURSOR); /* freedata, but no undo */
1567
1568         if (basact) {
1569                 /* active base was changed */
1570                 ED_base_object_activate(C, basact);
1571                 BASACT = basact;
1572         }
1573         else if (BASACT->object->flag & OB_DONE) {
1574                 WM_event_add_notifier(C, NC_OBJECT | ND_MODIFIER, BASACT->object);
1575                 WM_event_add_notifier(C, NC_OBJECT | ND_DATA, BASACT->object);
1576         }
1577
1578         DAG_scene_sort(bmain, scene);
1579         WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, scene);
1580         WM_event_add_notifier(C, NC_SCENE | ND_OB_SELECT, scene);
1581
1582         return OPERATOR_FINISHED;
1583 }
1584
1585
1586 void OBJECT_OT_convert(wmOperatorType *ot)
1587 {
1588         /* identifiers */
1589         ot->name = "Convert to";
1590         ot->description = "Convert selected objects to another type";
1591         ot->idname = "OBJECT_OT_convert";
1592         
1593         /* api callbacks */
1594         ot->invoke = WM_menu_invoke;
1595         ot->exec = convert_exec;
1596         ot->poll = convert_poll;
1597         
1598         /* flags */
1599         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1600
1601         /* properties */
1602         ot->prop = RNA_def_enum(ot->srna, "target", convert_target_items, OB_MESH, "Target", "Type of object to convert to");
1603         RNA_def_boolean(ot->srna, "keep_original", 0, "Keep Original", "Keep original objects instead of replacing them");
1604 }
1605
1606 /**************************** Duplicate ************************/
1607
1608 /* 
1609  * dupflag: a flag made from constants declared in DNA_userdef_types.h
1610  * The flag tells adduplicate() weather to copy data linked to the object, or to reference the existing data.
1611  * U.dupflag for default operations or you can construct a flag as python does
1612  * if the dupflag is 0 then no data will be copied (linked duplicate) */
1613
1614 /* used below, assumes id.new is correct */
1615 /* leaves selection of base/object unaltered */
1616 static Base *object_add_duplicate_internal(Main *bmain, Scene *scene, Base *base, int dupflag)
1617 {
1618         Base *basen = NULL;
1619         Material ***matarar;
1620         Object *ob, *obn;
1621         ID *id;
1622         int a, didit;
1623
1624         ob = base->object;
1625         if (ob->mode & OB_MODE_POSE) {
1626                 ; /* nothing? */
1627         }
1628         else {
1629                 obn = copy_object(ob);
1630                 obn->recalc |= OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME;
1631                 
1632                 basen = MEM_mallocN(sizeof(Base), "duplibase");
1633                 *basen = *base;
1634                 BLI_addhead(&scene->base, basen);   /* addhead: prevent eternal loop */
1635                 basen->object = obn;
1636                 
1637                 if (basen->flag & OB_FROMGROUP) {
1638                         Group *group;
1639                         for (group = bmain->group.first; group; group = group->id.next) {
1640                                 if (object_in_group(ob, group))
1641                                         add_to_group(group, obn, scene, basen);
1642                         }
1643                 }
1644                 
1645                 /* duplicates using userflags */
1646                 if (dupflag & USER_DUP_ACT) {
1647                         BKE_copy_animdata_id_action(&obn->id);
1648                 }
1649                 
1650                 if (dupflag & USER_DUP_MAT) {
1651                         for (a = 0; a < obn->totcol; a++) {
1652                                 id = (ID *)obn->mat[a];
1653                                 if (id) {
1654                                         ID_NEW_US(obn->mat[a])
1655                                         else obn->mat[a] = copy_material(obn->mat[a]);
1656                                         id->us--;
1657                                         
1658                                         if (dupflag & USER_DUP_ACT) {
1659                                                 BKE_copy_animdata_id_action(&obn->mat[a]->id);
1660                                         }
1661                                 }
1662                         }
1663                 }
1664                 if (dupflag & USER_DUP_PSYS) {
1665                         ParticleSystem *psys;
1666                         for (psys = obn->particlesystem.first; psys; psys = psys->next) {
1667                                 id = (ID *) psys->part;
1668                                 if (id) {
1669                                         ID_NEW_US(psys->part)
1670                                         else psys->part = psys_copy_settings(psys->part);
1671                                         
1672                                         if (dupflag & USER_DUP_ACT) {
1673                                                 BKE_copy_animdata_id_action(&psys->part->id);
1674                                         }
1675
1676                                         id->us--;
1677                                 }
1678                         }
1679                 }
1680                 
1681                 id = obn->data;
1682                 didit = 0;
1683                 
1684                 switch (obn->type) {
1685                         case OB_MESH:
1686                                 if (dupflag & USER_DUP_MESH) {
1687                                         ID_NEW_US2(obn->data)
1688                                         else {
1689                                                 obn->data = copy_mesh(obn->data);
1690                                                 
1691                                                 if (obn->fluidsimSettings) {
1692                                                         obn->fluidsimSettings->orgMesh = (Mesh *)obn->data;
1693                                                 }
1694                                                 
1695                                                 didit = 1;
1696                                         }
1697                                         id->us--;
1698                                 }
1699                                 break;
1700                         case OB_CURVE:
1701                                 if (dupflag & USER_DUP_CURVE) {
1702                                         ID_NEW_US2(obn->data)
1703                                         else {
1704                                                 obn->data = copy_curve(obn->data);
1705                                                 didit = 1;
1706                                         }
1707                                         id->us--;
1708                                 }
1709                                 break;
1710                         case OB_SURF:
1711                                 if (dupflag & USER_DUP_SURF) {
1712                                         ID_NEW_US2(obn->data)
1713                                         else {
1714                                                 obn->data = copy_curve(obn->data);
1715                                                 didit = 1;
1716                                         }
1717                                         id->us--;
1718                                 }
1719                                 break;
1720                         case OB_FONT:
1721                                 if (dupflag & USER_DUP_FONT) {
1722                                         ID_NEW_US2(obn->data)
1723                                         else {
1724                                                 obn->data = copy_curve(obn->data);
1725                                                 didit = 1;
1726                                         }
1727                                         id->us--;
1728                                 }
1729                                 break;
1730                         case OB_MBALL:
1731                                 if (dupflag & USER_DUP_MBALL) {
1732                                         ID_NEW_US2(obn->data)
1733                                         else {
1734                                                 obn->data = copy_mball(obn->data);
1735                                                 didit = 1;
1736                                         }
1737                                         id->us--;
1738                                 }
1739                                 break;
1740                         case OB_LAMP:
1741                                 if (dupflag & USER_DUP_LAMP) {
1742                                         ID_NEW_US2(obn->data)
1743                                         else {
1744                                                 obn->data = copy_lamp(obn->data);
1745                                                 didit = 1;
1746                                         }
1747                                         id->us--;
1748                                 }
1749                                 break;
1750                                 
1751                         case OB_ARMATURE:
1752                                 obn->recalc |= OB_RECALC_DATA;
1753                                 if (obn->pose) obn->pose->flag |= POSE_RECALC;
1754                                         
1755                                 if (dupflag & USER_DUP_ARM) {
1756                                         ID_NEW_US2(obn->data)
1757                                         else {
1758                                                 obn->data = copy_armature(obn->data);
1759                                                 armature_rebuild_pose(obn, obn->data);
1760                                                 didit = 1;
1761                                         }
1762                                         id->us--;
1763                                 }
1764                                                 
1765                                 break;
1766                                 
1767                         case OB_LATTICE:
1768                                 if (dupflag != 0) {
1769                                         ID_NEW_US2(obn->data)
1770                                         else {
1771                                                 obn->data = copy_lattice(obn->data);
1772                                                 didit = 1;
1773                                         }
1774                                         id->us--;
1775                                 }
1776                                 break;
1777                         case OB_CAMERA:
1778                                 if (dupflag != 0) {
1779                                         ID_NEW_US2(obn->data)
1780                                         else {
1781                                                 obn->data = copy_camera(obn->data);
1782                                                 didit = 1;
1783                                         }
1784                                         id->us--;
1785                                 }
1786                                 break;
1787                         case OB_SPEAKER:
1788                                 if (dupflag != 0) {
1789                                         ID_NEW_US2(obn->data)
1790                                         else {
1791                                                 obn->data = copy_speaker(obn->data);
1792                                                 didit = 1;
1793                                         }
1794                                         id->us--;
1795                                 }
1796                                 break;
1797
1798                 }
1799
1800                 /* check if obdata is copied */
1801                 if (didit) {
1802                         Key *key = ob_get_key(obn);
1803                         
1804                         if (dupflag & USER_DUP_ACT) {
1805                                 bActuator *act;
1806
1807                                 BKE_copy_animdata_id_action((ID *)obn->data);
1808                                 if (key) {
1809                                         BKE_copy_animdata_id_action((ID *)key);
1810                                 }
1811
1812                                 /* Update the duplicated action in the action actuators */
1813                                 for (act = obn->actuators.first; act; act = act->next) {
1814                                         if (act->type == ACT_ACTION) {
1815                                                 bActionActuator *actact = (bActionActuator *) act->data;
1816                                                 if (ob->adt && actact->act == ob->adt->action) {
1817                                                         actact->act = obn->adt->action;
1818                                                 }
1819                                         }
1820                                 }
1821                         }
1822                         
1823                         if (dupflag & USER_DUP_MAT) {
1824                                 matarar = give_matarar(obn);
1825                                 if (matarar) {
1826                                         for (a = 0; a < obn->totcol; a++) {
1827                                                 id = (ID *)(*matarar)[a];
1828                                                 if (id) {
1829                                                         ID_NEW_US( (*matarar)[a])
1830                                                         else (*matarar)[a] = copy_material((*matarar)[a]);
1831                                                         
1832                                                         id->us--;
1833                                                 }
1834                                         }
1835                                 }
1836                         }
1837                 }
1838         }
1839         return basen;
1840 }
1841
1842 /* single object duplicate, if dupflag==0, fully linked, else it uses the flags given */
1843 /* leaves selection of base/object unaltered.
1844  * note: don't call this within a loop since clear_* funcs loop over the entire database. */
1845 Base *ED_object_add_duplicate(Main *bmain, Scene *scene, Base *base, int dupflag)
1846 {
1847         Base *basen;
1848         Object *ob;
1849
1850         clear_id_newpoins();
1851         clear_sca_new_poins();  /* sensor/contr/act */
1852
1853         basen = object_add_duplicate_internal(bmain, scene, base, dupflag);
1854         if (basen == NULL) {
1855                 return NULL;
1856         }
1857
1858         ob = basen->object;
1859
1860         /* link own references to the newly duplicated data [#26816] */
1861         object_relink(ob);
1862         set_sca_new_poins_ob(ob);
1863
1864         DAG_scene_sort(bmain, scene);
1865         if (ob->data) {
1866                 ED_render_id_flush_update(bmain, ob->data);
1867         }
1868
1869         return basen;
1870 }
1871
1872 /* contextual operator dupli */
1873 static int duplicate_exec(bContext *C, wmOperator *op)
1874 {
1875         Main *bmain = CTX_data_main(C);
1876         Scene *scene = CTX_data_scene(C);
1877         int linked = RNA_boolean_get(op->ptr, "linked");
1878         int dupflag = (linked) ? 0 : U.dupflag;
1879         
1880         clear_id_newpoins();
1881         clear_sca_new_poins();  /* sensor/contr/act */
1882         
1883         CTX_DATA_BEGIN (C, Base *, base, selected_editable_bases) {
1884                 Base *basen = object_add_duplicate_internal(bmain, scene, base, dupflag);
1885                 
1886                 /* note that this is safe to do with this context iterator,
1887                  * the list is made in advance */
1888                 ED_base_object_select(base, BA_DESELECT);
1889
1890                 if (basen == NULL) {
1891                         continue;
1892                 }
1893
1894                 /* new object becomes active */
1895                 if (BASACT == base)
1896                         ED_base_object_activate(C, basen);
1897
1898                 if (basen->object->data) {
1899                         DAG_id_tag_update(basen->object->data, 0);
1900                 }
1901         }
1902         CTX_DATA_END;
1903
1904         copy_object_set_idnew(C, dupflag);
1905
1906         DAG_scene_sort(bmain, scene);
1907         DAG_ids_flush_update(bmain, 0);
1908
1909         WM_event_add_notifier(C, NC_SCENE | ND_OB_SELECT, scene);
1910
1911         return OPERATOR_FINISHED;
1912 }
1913
1914 void OBJECT_OT_duplicate(wmOperatorType *ot)
1915 {
1916         PropertyRNA *prop;
1917         
1918         /* identifiers */
1919         ot->name = "Duplicate Objects";
1920         ot->description = "Duplicate selected objects";
1921         ot->idname = "OBJECT_OT_duplicate";
1922         
1923         /* api callbacks */
1924         ot->exec = duplicate_exec;
1925         ot->poll = ED_operator_objectmode;
1926         
1927         /* flags */
1928         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1929         
1930         /* to give to transform */
1931         RNA_def_boolean(ot->srna, "linked", 0, "Linked", "Duplicate object but not object data, linking to the original data");
1932         prop = RNA_def_enum(ot->srna, "mode", transform_mode_types, TFM_TRANSLATION, "Mode", "");
1933         RNA_def_property_flag(prop, PROP_HIDDEN);
1934 }
1935
1936 /* **************** add named object, for dragdrop ************* */
1937
1938
1939 static int add_named_exec(bContext *C, wmOperator *op)
1940 {
1941         Main *bmain = CTX_data_main(C);
1942         Scene *scene = CTX_data_scene(C);
1943         Base *basen, *base;
1944         Object *ob;
1945         int linked = RNA_boolean_get(op->ptr, "linked");
1946         int dupflag = (linked) ? 0 : U.dupflag;
1947         char name[MAX_ID_NAME - 2];
1948
1949         /* find object, create fake base */
1950         RNA_string_get(op->ptr, "name", name);
1951         ob = (Object *)find_id("OB", name);
1952         if (ob == NULL)
1953                 return OPERATOR_CANCELLED;
1954
1955         base = MEM_callocN(sizeof(Base), "duplibase");
1956         base->object = ob;
1957         base->flag = ob->flag;
1958
1959         /* prepare dupli */
1960         clear_id_newpoins();
1961         clear_sca_new_poins();  /* sensor/contr/act */
1962
1963         basen = object_add_duplicate_internal(bmain, scene, base, dupflag);
1964
1965         if (basen == NULL) {
1966                 MEM_freeN(base);
1967                 return OPERATOR_CANCELLED;
1968         }
1969
1970         basen->lay = basen->object->lay = scene->lay;
1971
1972         ED_object_location_from_view(C, basen->object->loc);
1973         ED_base_object_activate(C, basen);
1974
1975         copy_object_set_idnew(C, dupflag);
1976
1977         DAG_scene_sort(bmain, scene);
1978         DAG_ids_flush_update(bmain, 0);
1979
1980         MEM_freeN(base);
1981
1982         WM_event_add_notifier(C, NC_SCENE | ND_OB_SELECT, scene);
1983
1984         return OPERATOR_FINISHED;
1985 }
1986
1987 void OBJECT_OT_add_named(wmOperatorType *ot)
1988 {
1989         /* identifiers */
1990         ot->name = "Add Named Object";
1991         ot->description = "Add named object";
1992         ot->idname = "OBJECT_OT_add_named";
1993         
1994         /* api callbacks */
1995         ot->exec = add_named_exec;
1996         ot->poll = ED_operator_objectmode;
1997         
1998         /* flags */
1999         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2000         
2001         RNA_def_boolean(ot->srna, "linked", 0, "Linked", "Duplicate object but not object data, linking to the original data");
2002         RNA_def_string(ot->srna, "name", "Cube", MAX_ID_NAME - 2, "Name", "Object name to add");
2003 }
2004
2005
2006
2007 /**************************** Join *************************/
2008 static int join_poll(bContext *C)
2009 {
2010         Object *ob = CTX_data_active_object(C);
2011         
2012         if (!ob || ob->id.lib) return 0;
2013         
2014         if (ELEM4(ob->type, OB_MESH, OB_CURVE, OB_SURF, OB_ARMATURE))
2015                 return ED_operator_screenactive(C);
2016         else
2017                 return 0;
2018 }
2019
2020
2021 static int join_exec(bContext *C, wmOperator *op)
2022 {
2023         Scene *scene = CTX_data_scene(C);
2024         Object *ob = CTX_data_active_object(C);
2025
2026         if (scene->obedit) {
2027                 BKE_report(op->reports, RPT_ERROR, "This data does not support joining in editmode");
2028                 return OPERATOR_CANCELLED;
2029         }
2030         else if (object_data_is_libdata(ob)) {
2031                 BKE_report(op->reports, RPT_ERROR, "Can't edit external libdata");
2032                 return OPERATOR_CANCELLED;
2033         }
2034
2035         if (ob->type == OB_MESH)
2036                 return join_mesh_exec(C, op);
2037         else if (ELEM(ob->type, OB_CURVE, OB_SURF))
2038                 return join_curve_exec(C, op);
2039         else if (ob->type == OB_ARMATURE)
2040                 return join_armature_exec(C, op);
2041         
2042         return OPERATOR_CANCELLED;
2043 }
2044
2045 void OBJECT_OT_join(wmOperatorType *ot)
2046 {
2047         /* identifiers */
2048         ot->name = "Join";
2049         ot->description = "Join selected objects into active object";
2050         ot->idname = "OBJECT_OT_join";
2051         
2052         /* api callbacks */
2053         ot->exec = join_exec;
2054         ot->poll = join_poll;
2055         
2056         /* flags */
2057         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2058 }
2059
2060 /**************************** Join as Shape Key*************************/
2061 static int join_shapes_poll(bContext *C)
2062 {
2063         Object *ob = CTX_data_active_object(C);
2064         
2065         if (!ob || ob->id.lib) return 0;
2066         
2067         /* only meshes supported at the moment */
2068         if (ob->type == OB_MESH)
2069                 return ED_operator_screenactive(C);
2070         else
2071                 return 0;
2072 }
2073
2074 static int join_shapes_exec(bContext *C, wmOperator *op)
2075 {
2076         Scene *scene = CTX_data_scene(C);
2077         Object *ob = CTX_data_active_object(C);
2078         
2079         if (scene->obedit) {
2080                 BKE_report(op->reports, RPT_ERROR, "This data does not support joining in editmode");
2081                 return OPERATOR_CANCELLED;
2082         }
2083         else if (object_data_is_libdata(ob)) {
2084                 BKE_report(op->reports, RPT_ERROR, "Can't edit external libdata");
2085                 return OPERATOR_CANCELLED;
2086         }
2087         
2088         if (ob->type == OB_MESH)
2089                 return join_mesh_shapes_exec(C, op);
2090         
2091         return OPERATOR_CANCELLED;
2092 }
2093
2094 void OBJECT_OT_join_shapes(wmOperatorType *ot)
2095 {
2096         /* identifiers */
2097         ot->name = "Join as Shapes";
2098         ot->description = "Merge selected objects to shapes of active object";
2099         ot->idname = "OBJECT_OT_join_shapes";
2100         
2101         /* api callbacks */
2102         ot->exec = join_shapes_exec;
2103         ot->poll = join_shapes_poll;
2104         
2105         /* flags */
2106         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2107 }