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