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