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