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