Merge branch 'master' into blender2.8
[blender.git] / source / blender / editors / object / object_add.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19  * All rights reserved.
20  *
21  * Contributor(s): Blender Foundation, 2002-2008 full recode
22  *
23  * ***** END GPL LICENSE BLOCK *****
24  */
25
26 /** \file blender/editors/object/object_add.c
27  *  \ingroup edobj
28  */
29
30
31 #include <stdlib.h>
32 #include <string.h>
33
34 #include "MEM_guardedalloc.h"
35
36 #include "DNA_anim_types.h"
37 #include "DNA_camera_types.h"
38 #include "DNA_curve_types.h"
39 #include "DNA_group_types.h"
40 #include "DNA_lamp_types.h"
41 #include "DNA_key_types.h"
42 #include "DNA_material_types.h"
43 #include "DNA_mesh_types.h"
44 #include "DNA_meta_types.h"
45 #include "DNA_object_fluidsim.h"
46 #include "DNA_object_force.h"
47 #include "DNA_object_types.h"
48 #include "DNA_scene_types.h"
49 #include "DNA_vfont_types.h"
50 #include "DNA_actuator_types.h"
51 #include "DNA_gpencil_types.h"
52
53 #include "BLI_utildefines.h"
54 #include "BLI_ghash.h"
55 #include "BLI_listbase.h"
56 #include "BLI_math.h"
57 #include "BLI_string.h"
58
59 #include "BLT_translation.h"
60
61 #include "BKE_action.h"
62 #include "BKE_anim.h"
63 #include "BKE_animsys.h"
64 #include "BKE_armature.h"
65 #include "BKE_camera.h"
66 #include "BKE_collection.h"
67 #include "BKE_context.h"
68 #include "BKE_constraint.h"
69 #include "BKE_curve.h"
70 #include "BKE_depsgraph.h"
71 #include "BKE_DerivedMesh.h"
72 #include "BKE_displist.h"
73 #include "BKE_effect.h"
74 #include "BKE_font.h"
75 #include "BKE_group.h"
76 #include "BKE_lamp.h"
77 #include "BKE_lattice.h"
78 #include "BKE_layer.h"
79 #include "BKE_library.h"
80 #include "BKE_library_query.h"
81 #include "BKE_library_remap.h"
82 #include "BKE_key.h"
83 #include "BKE_main.h"
84 #include "BKE_material.h"
85 #include "BKE_mball.h"
86 #include "BKE_mesh.h"
87 #include "BKE_nla.h"
88 #include "BKE_object.h"
89 #include "BKE_particle.h"
90 #include "BKE_report.h"
91 #include "BKE_sca.h"
92 #include "BKE_scene.h"
93 #include "BKE_screen.h"
94 #include "BKE_speaker.h"
95 #include "BKE_texture.h"
96
97 #include "RNA_access.h"
98 #include "RNA_define.h"
99 #include "RNA_enum_types.h"
100
101 #include "WM_api.h"
102 #include "WM_types.h"
103
104 #include "ED_armature.h"
105 #include "ED_curve.h"
106 #include "ED_mball.h"
107 #include "ED_mesh.h"
108 #include "ED_node.h"
109 #include "ED_object.h"
110 #include "ED_physics.h"
111 #include "ED_render.h"
112 #include "ED_screen.h"
113 #include "ED_transform.h"
114 #include "ED_view3d.h"
115
116 #include "UI_resources.h"
117
118 #include "GPU_lamp.h"
119
120 #include "object_intern.h"
121
122 /* this is an exact copy of the define in rna_lamp.c
123  * kept here because of linking order.
124  * Icons are only defined here */
125 EnumPropertyItem rna_enum_lamp_type_items[] = {
126         {LA_LOCAL, "POINT", ICON_LAMP_POINT, "Point", "Omnidirectional point light source"},
127         {LA_SUN, "SUN", ICON_LAMP_SUN, "Sun", "Constant direction parallel ray light source"},
128         {LA_SPOT, "SPOT", ICON_LAMP_SPOT, "Spot", "Directional cone light source"},
129         {LA_HEMI, "HEMI", ICON_LAMP_HEMI, "Hemi", "180 degree constant light source"},
130         {LA_AREA, "AREA", ICON_LAMP_AREA, "Area", "Directional area light source"},
131         {0, NULL, 0, NULL, NULL}
132 };
133
134 /* copy from rna_object_force.c */
135 static EnumPropertyItem field_type_items[] = {
136         {PFIELD_FORCE, "FORCE", ICON_FORCE_FORCE, "Force", ""},
137         {PFIELD_WIND, "WIND", ICON_FORCE_WIND, "Wind", ""},
138         {PFIELD_VORTEX, "VORTEX", ICON_FORCE_VORTEX, "Vortex", ""},
139         {PFIELD_MAGNET, "MAGNET", ICON_FORCE_MAGNETIC, "Magnetic", ""},
140         {PFIELD_HARMONIC, "HARMONIC", ICON_FORCE_HARMONIC, "Harmonic", ""},
141         {PFIELD_CHARGE, "CHARGE", ICON_FORCE_CHARGE, "Charge", ""},
142         {PFIELD_LENNARDJ, "LENNARDJ", ICON_FORCE_LENNARDJONES, "Lennard-Jones", ""},
143         {PFIELD_TEXTURE, "TEXTURE", ICON_FORCE_TEXTURE, "Texture", ""},
144         {PFIELD_GUIDE, "GUIDE", ICON_FORCE_CURVE, "Curve Guide", ""},
145         {PFIELD_BOID, "BOID", ICON_FORCE_BOID, "Boid", ""},
146         {PFIELD_TURBULENCE, "TURBULENCE", ICON_FORCE_TURBULENCE, "Turbulence", ""},
147         {PFIELD_DRAG, "DRAG", ICON_FORCE_DRAG, "Drag", ""},
148         {PFIELD_SMOKEFLOW, "SMOKE", ICON_FORCE_SMOKEFLOW, "Smoke Flow", ""},
149         {0, NULL, 0, NULL, NULL}
150 };
151
152 /************************** Exported *****************************/
153
154 void ED_object_location_from_view(bContext *C, float loc[3])
155 {
156         View3D *v3d = CTX_wm_view3d(C);
157         Scene *scene = CTX_data_scene(C);
158         const float *cursor;
159
160         cursor = ED_view3d_cursor3d_get(scene, v3d);
161
162         copy_v3_v3(loc, cursor);
163 }
164
165 void ED_object_rotation_from_view(bContext *C, float rot[3], const char align_axis)
166 {
167         RegionView3D *rv3d = CTX_wm_region_view3d(C);
168
169         BLI_assert(align_axis >= 'X' && align_axis <= 'Z');
170
171         if (rv3d) {
172                 float quat[4];
173
174                 switch (align_axis) {
175                         case 'X':
176                         {
177                                 float quat_y[4];
178                                 axis_angle_to_quat(quat_y, rv3d->viewinv[1], -M_PI_2);
179                                 mul_qt_qtqt(quat, rv3d->viewquat, quat_y);
180                                 quat[0] = -quat[0];
181
182                                 quat_to_eul(rot, quat);
183                                 break;
184                         }
185                         case 'Y':
186                         {
187                                 copy_qt_qt(quat, rv3d->viewquat);
188                                 quat[0] = -quat[0];
189
190                                 quat_to_eul(rot, quat);
191                                 rot[0] -= (float)M_PI_2;
192                                 break;
193                         }
194                         case 'Z':
195                         {
196                                 copy_qt_qt(quat, rv3d->viewquat);
197                                 quat[0] = -quat[0];
198
199                                 quat_to_eul(rot, quat);
200                                 break;
201                         }
202                 }
203
204         }
205         else {
206                 zero_v3(rot);
207         }
208 }
209
210 void ED_object_base_init_transform(bContext *C, Base *base, const float loc[3], const float rot[3])
211 {
212         Object *ob = base->object;
213         Scene *scene = CTX_data_scene(C);
214
215         if (!scene) return;
216
217         if (loc)
218                 copy_v3_v3(ob->loc, loc);
219
220         if (rot)
221                 copy_v3_v3(ob->rot, rot);
222
223         BKE_object_where_is_calc(scene, ob);
224 }
225
226 /* Uses context to figure out transform for primitive.
227  * Returns standard diameter. */
228 float ED_object_new_primitive_matrix(
229         bContext *C, Object *obedit,
230         const float loc[3], const float rot[3], float primmat[4][4])
231 {
232         Scene *scene = CTX_data_scene(C);
233         View3D *v3d = CTX_wm_view3d(C);
234         float mat[3][3], rmat[3][3], cmat[3][3], imat[3][3];
235
236         unit_m4(primmat);
237
238         eul_to_mat3(rmat, rot);
239         invert_m3(rmat);
240
241         /* inverse transform for initial rotation and object */
242         copy_m3_m4(mat, obedit->obmat);
243         mul_m3_m3m3(cmat, rmat, mat);
244         invert_m3_m3(imat, cmat);
245         copy_m4_m3(primmat, imat);
246
247         /* center */
248         copy_v3_v3(primmat[3], loc);
249         sub_v3_v3v3(primmat[3], primmat[3], obedit->obmat[3]);
250         invert_m3_m3(imat, mat);
251         mul_m3_v3(imat, primmat[3]);
252
253         {
254                 const float dia = v3d ? ED_view3d_grid_scale(scene, v3d, NULL) : ED_scene_grid_scale(scene, NULL);
255                 return dia;
256         }
257
258         // return 1.0f;
259 }
260
261 /********************* Add Object Operator ********************/
262
263 static void view_align_update(struct Main *UNUSED(main), struct Scene *UNUSED(scene), struct PointerRNA *ptr)
264 {
265         RNA_struct_idprops_unset(ptr, "rotation");
266 }
267
268 void ED_object_add_unit_props(wmOperatorType *ot)
269 {
270         RNA_def_float_distance(ot->srna, "radius", 1.0f, 0.0, OBJECT_ADD_SIZE_MAXF, "Radius", "", 0.001, 100.00);
271 }
272
273 void ED_object_add_generic_props(wmOperatorType *ot, bool do_editmode)
274 {
275         PropertyRNA *prop;
276
277         /* note: this property gets hidden for add-camera operator */
278         prop = RNA_def_boolean(ot->srna, "view_align", 0, "Align to View", "Align the new object to the view");
279         RNA_def_property_update_runtime(prop, view_align_update);
280
281         if (do_editmode) {
282                 prop = RNA_def_boolean(ot->srna, "enter_editmode", 0, "Enter Editmode",
283                                        "Enter editmode when adding this object");
284                 RNA_def_property_flag(prop, PROP_HIDDEN | PROP_SKIP_SAVE);
285         }
286
287         prop = RNA_def_float_vector_xyz(ot->srna, "location", 3, NULL, -OBJECT_ADD_SIZE_MAXF, OBJECT_ADD_SIZE_MAXF,
288                                         "Location", "Location for the newly added object", -1000.0f, 1000.0f);
289         RNA_def_property_flag(prop, PROP_SKIP_SAVE);
290         prop = RNA_def_float_rotation(ot->srna, "rotation", 3, NULL, -OBJECT_ADD_SIZE_MAXF, OBJECT_ADD_SIZE_MAXF,
291                                       "Rotation", "Rotation for the newly added object",
292                                       DEG2RADF(-360.0f), DEG2RADF(360.0f));
293         RNA_def_property_flag(prop, PROP_SKIP_SAVE);
294
295         prop = RNA_def_boolean_layer_member(ot->srna, "layers", 20, NULL, "Layer", "");
296         RNA_def_property_flag(prop, PROP_HIDDEN | PROP_SKIP_SAVE);
297 }
298
299 void ED_object_add_mesh_props(wmOperatorType *ot)
300 {
301         RNA_def_boolean(ot->srna, "calc_uvs", false, "Generate UVs", "Generate a default UV map");
302 }
303
304 bool ED_object_add_generic_get_opts(bContext *C, wmOperator *op, const char view_align_axis,
305                                     float loc[3], float rot[3],
306                                     bool *enter_editmode, unsigned int *layer, bool *is_view_aligned)
307 {
308         View3D *v3d = CTX_wm_view3d(C);
309         unsigned int _layer;
310         PropertyRNA *prop;
311
312         /* Switch to Edit mode? optional prop */
313         if ((prop = RNA_struct_find_property(op->ptr, "enter_editmode"))) {
314                 bool _enter_editmode;
315                 if (!enter_editmode)
316                         enter_editmode = &_enter_editmode;
317
318                 if (RNA_property_is_set(op->ptr, prop) && enter_editmode)
319                         *enter_editmode = RNA_property_boolean_get(op->ptr, prop);
320                 else {
321                         *enter_editmode = (U.flag & USER_ADD_EDITMODE) != 0;
322                         RNA_property_boolean_set(op->ptr, prop, *enter_editmode);
323                 }
324         }
325
326         /* Get layers! */
327         {
328                 int a, layer_values[20];
329                 if (!layer)
330                         layer = &_layer;
331
332                 prop = RNA_struct_find_property(op->ptr, "layers");
333                 if (RNA_property_is_set(op->ptr, prop)) {
334                         RNA_property_boolean_get_array(op->ptr, prop, layer_values);
335                         *layer = 0;
336                         for (a = 0; a < 20; a++) {
337                                 if (layer_values[a])
338                                         *layer |= (1 << a);
339                         }
340                 }
341                 else {
342                         Scene *scene = CTX_data_scene(C);
343                         *layer = BKE_screen_view3d_layer_active_ex(v3d, scene, false);
344                         for (a = 0; a < 20; a++) {
345                                 layer_values[a] = (*layer & (1 << a)) != 0;
346                         }
347                         RNA_property_boolean_set_array(op->ptr, prop, layer_values);
348                 }
349
350                 /* in local view we additionally add local view layers,
351                  * not part of operator properties */
352                 if (v3d && v3d->localvd)
353                         *layer |= v3d->lay;
354         }
355
356         /* Location! */
357         {
358                 float _loc[3];
359                 if (!loc)
360                         loc = _loc;
361
362                 if (RNA_struct_property_is_set(op->ptr, "location")) {
363                         RNA_float_get_array(op->ptr, "location", loc);
364                 }
365                 else {
366                         ED_object_location_from_view(C, loc);
367                         RNA_float_set_array(op->ptr, "location", loc);
368                 }
369         }
370
371         /* Rotation! */
372         {
373                 bool _is_view_aligned;
374                 float _rot[3];
375                 if (!is_view_aligned)
376                         is_view_aligned = &_is_view_aligned;
377                 if (!rot)
378                         rot = _rot;
379
380                 if (RNA_struct_property_is_set(op->ptr, "rotation"))
381                         *is_view_aligned = false;
382                 else if (RNA_struct_property_is_set(op->ptr, "view_align"))
383                         *is_view_aligned = RNA_boolean_get(op->ptr, "view_align");
384                 else {
385                         *is_view_aligned = (U.flag & USER_ADD_VIEWALIGNED) != 0;
386                         RNA_boolean_set(op->ptr, "view_align", *is_view_aligned);
387                 }
388
389                 if (*is_view_aligned) {
390                         ED_object_rotation_from_view(C, rot, view_align_axis);
391                         RNA_float_set_array(op->ptr, "rotation", rot);
392                 }
393                 else
394                         RNA_float_get_array(op->ptr, "rotation", rot);
395         }
396
397         if (layer && *layer == 0) {
398                 BKE_report(op->reports, RPT_ERROR, "Property 'layer' has no values set");
399                 return false;
400         }
401
402         return true;
403 }
404
405 /* For object add primitive operators.
406  * Do not call undo push in this function (users of this function have to). */
407 Object *ED_object_add_type(
408         bContext *C,
409         int type, const char *name,
410         const float loc[3], const float rot[3],
411         bool enter_editmode, unsigned int UNUSED(layer))
412 {
413         Main *bmain = CTX_data_main(C);
414         Scene *scene = CTX_data_scene(C);
415         SceneLayer *sl = CTX_data_scene_layer(C);
416         Object *ob;
417
418         /* for as long scene has editmode... */
419         if (CTX_data_edit_object(C)) 
420                 ED_object_editmode_exit(C, EM_FREEDATA | EM_FREEUNDO | EM_WAITCURSOR | EM_DO_UNDO);  /* freedata, and undo */
421
422         /* deselects all, sets scene->basact */
423         ob = BKE_object_add(bmain, scene, sl, type, name);
424         /* editor level activate, notifiers */
425         ED_object_base_activate(C, sl->basact);
426
427         /* more editor stuff */
428         ED_object_base_init_transform(C, sl->basact, loc, rot);
429
430         /* Ignore collisions by default for non-mesh objects */
431         if (type != OB_MESH) {
432                 ob->body_type = OB_BODY_TYPE_NO_COLLISION;
433                 ob->gameflag &= ~(OB_SENSOR | OB_RIGID_BODY | OB_SOFT_BODY | OB_COLLISION | OB_CHARACTER | OB_OCCLUDER | OB_DYNAMIC | OB_NAVMESH); /* copied from rna_object.c */
434         }
435
436         DAG_id_type_tag(bmain, ID_OB);
437         DAG_relations_tag_update(bmain);
438         if (ob->data) {
439                 ED_render_id_flush_update(bmain, ob->data);
440         }
441
442         if (enter_editmode)
443                 ED_object_editmode_enter(C, EM_IGNORE_LAYER);
444
445         WM_event_add_notifier(C, NC_SCENE | ND_LAYER_CONTENT, scene);
446
447         /* TODO(sergey): Use proper flag for tagging here. */
448         DAG_id_tag_update(&scene->id, 0);
449
450         return ob;
451 }
452
453 /* for object add operator */
454 static int object_add_exec(bContext *C, wmOperator *op)
455 {
456         Object *ob;
457         bool enter_editmode;
458         unsigned int layer;
459         float loc[3], rot[3], radius;
460
461         WM_operator_view3d_unit_defaults(C, op);
462         if (!ED_object_add_generic_get_opts(C, op, 'Z', loc, rot, &enter_editmode, &layer, NULL))
463                 return OPERATOR_CANCELLED;
464
465         radius = RNA_float_get(op->ptr, "radius");
466         ob = ED_object_add_type(C, RNA_enum_get(op->ptr, "type"), NULL, loc, rot, enter_editmode, layer);
467
468         if (ob->type == OB_LATTICE) {
469                 /* lattice is a special case!
470                  * we never want to scale the obdata since that is the rest-state */
471                 copy_v3_fl(ob->size, radius);
472         }
473         else {
474                 BKE_object_obdata_size_init(ob, radius);
475         }
476
477         return OPERATOR_FINISHED;
478 }
479
480 void OBJECT_OT_add(wmOperatorType *ot)
481 {
482         /* identifiers */
483         ot->name = "Add Object";
484         ot->description = "Add an object to the scene";
485         ot->idname = "OBJECT_OT_add";
486
487         /* api callbacks */
488         ot->exec = object_add_exec;
489         ot->poll = ED_operator_objectmode;
490
491         /* flags */
492         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
493
494         /* properties */
495         ED_object_add_unit_props(ot);
496         RNA_def_enum(ot->srna, "type", rna_enum_object_type_items, 0, "Type", "");
497
498         ED_object_add_generic_props(ot, true);
499 }
500
501 /********************* Add Effector Operator ********************/
502
503 /* for object add operator */
504 static int effector_add_exec(bContext *C, wmOperator *op)
505 {
506         Object *ob;
507         int type;
508         bool enter_editmode;
509         unsigned int layer;
510         float loc[3], rot[3];
511         float mat[4][4];
512         float dia;
513
514         WM_operator_view3d_unit_defaults(C, op);
515         if (!ED_object_add_generic_get_opts(C, op, 'Z', loc, rot, &enter_editmode, &layer, NULL))
516                 return OPERATOR_CANCELLED;
517
518         type = RNA_enum_get(op->ptr, "type");
519         dia = RNA_float_get(op->ptr, "radius");
520
521         if (type == PFIELD_GUIDE) {
522                 Curve *cu;
523                 const char *name = CTX_DATA_(BLT_I18NCONTEXT_ID_OBJECT, "CurveGuide");
524                 ob = ED_object_add_type(C, OB_CURVE, name, loc, rot, false, layer);
525
526                 cu = ob->data;
527                 cu->flag |= CU_PATH | CU_3D;
528                 ED_object_editmode_enter(C, 0);
529                 ED_object_new_primitive_matrix(C, ob, loc, rot, mat);
530                 BLI_addtail(&cu->editnurb->nurbs, ED_curve_add_nurbs_primitive(C, ob, mat, CU_NURBS | CU_PRIM_PATH, dia));
531                 if (!enter_editmode)
532                         ED_object_editmode_exit(C, EM_FREEDATA);
533         }
534         else {
535                 const char *name = CTX_DATA_(BLT_I18NCONTEXT_ID_OBJECT, "Field");
536                 ob = ED_object_add_type(C, OB_EMPTY, name, loc, rot, false, layer);
537                 BKE_object_obdata_size_init(ob, dia);
538                 if (ELEM(type, PFIELD_WIND, PFIELD_VORTEX))
539                         ob->empty_drawtype = OB_SINGLE_ARROW;
540         }
541
542         ob->pd = object_add_collision_fields(type);
543
544         DAG_relations_tag_update(CTX_data_main(C));
545
546         return OPERATOR_FINISHED;
547 }
548
549 void OBJECT_OT_effector_add(wmOperatorType *ot)
550 {
551         /* identifiers */
552         ot->name = "Add Effector";
553         ot->description = "Add an empty object with a physics effector to the scene";
554         ot->idname = "OBJECT_OT_effector_add";
555
556         /* api callbacks */
557         ot->exec = effector_add_exec;
558         ot->poll = ED_operator_objectmode;
559
560         /* flags */
561         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
562
563         /* properties */
564         ot->prop = RNA_def_enum(ot->srna, "type", field_type_items, 0, "Type", "");
565
566         ED_object_add_unit_props(ot);
567         ED_object_add_generic_props(ot, true);
568 }
569
570 /********************* Add Camera Operator ********************/
571
572 static int object_camera_add_exec(bContext *C, wmOperator *op)
573 {
574         View3D *v3d = CTX_wm_view3d(C);
575         Scene *scene = CTX_data_scene(C);
576         Object *ob;
577         Camera *cam;
578         bool enter_editmode;
579         unsigned int layer;
580         float loc[3], rot[3];
581
582         /* force view align for cameras */
583         RNA_boolean_set(op->ptr, "view_align", true);
584
585         if (!ED_object_add_generic_get_opts(C, op, 'Z', loc, rot, &enter_editmode, &layer, NULL))
586                 return OPERATOR_CANCELLED;
587
588         ob = ED_object_add_type(C, OB_CAMERA, NULL, loc, rot, false, layer);
589
590         if (v3d) {
591                 if (v3d->camera == NULL)
592                         v3d->camera = ob;
593                 if (v3d->scenelock && scene->camera == NULL) {
594                         scene->camera = ob;
595                 }
596         }
597
598         cam = ob->data;
599         cam->drawsize = v3d ? ED_view3d_grid_scale(scene, v3d, NULL) : ED_scene_grid_scale(scene, NULL);
600
601         return OPERATOR_FINISHED;
602 }
603
604 void OBJECT_OT_camera_add(wmOperatorType *ot)
605 {
606         PropertyRNA *prop;
607
608         /* identifiers */
609         ot->name = "Add Camera";
610         ot->description = "Add a camera object to the scene";
611         ot->idname = "OBJECT_OT_camera_add";
612
613         /* api callbacks */
614         ot->exec = object_camera_add_exec;
615         ot->poll = ED_operator_objectmode;
616
617         /* flags */
618         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
619
620         ED_object_add_generic_props(ot, true);
621
622         /* hide this for cameras, default */
623         prop = RNA_struct_type_find_property(ot->srna, "view_align");
624         RNA_def_property_flag(prop, PROP_HIDDEN);
625 }
626
627
628 /********************* Add Metaball Operator ********************/
629
630 static int object_metaball_add_exec(bContext *C, wmOperator *op)
631 {
632         Object *obedit = CTX_data_edit_object(C);
633         bool newob = false;
634         bool enter_editmode;
635         unsigned int layer;
636         float loc[3], rot[3];
637         float mat[4][4];
638         float dia;
639
640         WM_operator_view3d_unit_defaults(C, op);
641         if (!ED_object_add_generic_get_opts(C, op, 'Z', loc, rot, &enter_editmode, &layer, NULL))
642                 return OPERATOR_CANCELLED;
643
644         if (obedit == NULL || obedit->type != OB_MBALL) {
645                 obedit = ED_object_add_type(C, OB_MBALL, NULL, loc, rot, true, layer);
646                 newob = true;
647         }
648         else {
649                 DAG_id_tag_update(&obedit->id, OB_RECALC_DATA);
650         }
651
652         ED_object_new_primitive_matrix(C, obedit, loc, rot, mat);
653         dia = RNA_float_get(op->ptr, "radius");
654
655         ED_mball_add_primitive(C, obedit, mat, dia, RNA_enum_get(op->ptr, "type"));
656
657         /* userdef */
658         if (newob && !enter_editmode) {
659                 ED_object_editmode_exit(C, EM_FREEDATA);
660         }
661
662         WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, obedit);
663
664         return OPERATOR_FINISHED;
665 }
666
667 void OBJECT_OT_metaball_add(wmOperatorType *ot)
668 {
669         /* identifiers */
670         ot->name = "Add Metaball";
671         ot->description = "Add an metaball object to the scene";
672         ot->idname = "OBJECT_OT_metaball_add";
673
674         /* api callbacks */
675         ot->invoke = WM_menu_invoke;
676         ot->exec = object_metaball_add_exec;
677         ot->poll = ED_operator_scene_editable;
678
679         /* flags */
680         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
681
682         ot->prop = RNA_def_enum(ot->srna, "type", rna_enum_metaelem_type_items, 0, "Primitive", "");
683
684         ED_object_add_unit_props(ot);
685         ED_object_add_generic_props(ot, true);
686 }
687
688 /********************* Add Text Operator ********************/
689
690 static int object_add_text_exec(bContext *C, wmOperator *op)
691 {
692         Object *obedit = CTX_data_edit_object(C);
693         bool enter_editmode;
694         unsigned int layer;
695         float loc[3], rot[3];
696
697         WM_operator_view3d_unit_defaults(C, op);
698         if (!ED_object_add_generic_get_opts(C, op, 'Z', loc, rot, &enter_editmode, &layer, NULL))
699                 return OPERATOR_CANCELLED;
700
701         if (obedit && obedit->type == OB_FONT)
702                 return OPERATOR_CANCELLED;
703
704         obedit = ED_object_add_type(C, OB_FONT, NULL, loc, rot, enter_editmode, layer);
705         BKE_object_obdata_size_init(obedit, RNA_float_get(op->ptr, "radius"));
706
707         WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, obedit);
708
709         return OPERATOR_FINISHED;
710 }
711
712 void OBJECT_OT_text_add(wmOperatorType *ot)
713 {
714         /* identifiers */
715         ot->name = "Add Text";
716         ot->description = "Add a text object to the scene";
717         ot->idname = "OBJECT_OT_text_add";
718
719         /* api callbacks */
720         ot->exec = object_add_text_exec;
721         ot->poll = ED_operator_objectmode;
722
723         /* flags */
724         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
725
726         /* properties */
727         ED_object_add_unit_props(ot);
728         ED_object_add_generic_props(ot, true);
729 }
730
731 /********************* Add Armature Operator ********************/
732
733 static int object_armature_add_exec(bContext *C, wmOperator *op)
734 {
735         Object *obedit = CTX_data_edit_object(C);
736         RegionView3D *rv3d = CTX_wm_region_view3d(C);
737         bool newob = false;
738         bool enter_editmode;
739         unsigned int layer;
740         float loc[3], rot[3], dia;
741         bool view_aligned = rv3d && (U.flag & USER_ADD_VIEWALIGNED);
742
743         WM_operator_view3d_unit_defaults(C, op);
744         if (!ED_object_add_generic_get_opts(C, op, 'Z', loc, rot, &enter_editmode, &layer, NULL))
745                 return OPERATOR_CANCELLED;
746
747         if ((obedit == NULL) || (obedit->type != OB_ARMATURE)) {
748                 obedit = ED_object_add_type(C, OB_ARMATURE, NULL, loc, rot, true, layer);
749                 ED_object_editmode_enter(C, 0);
750                 newob = true;
751         }
752         else {
753                 DAG_id_tag_update(&obedit->id, OB_RECALC_DATA);
754         }
755
756         if (obedit == NULL) {
757                 BKE_report(op->reports, RPT_ERROR, "Cannot create editmode armature");
758                 return OPERATOR_CANCELLED;
759         }
760
761         dia = RNA_float_get(op->ptr, "radius");
762         ED_armature_edit_bone_add_primitive(obedit, dia, view_aligned);
763
764         /* userdef */
765         if (newob && !enter_editmode)
766                 ED_object_editmode_exit(C, EM_FREEDATA);
767
768         WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, obedit);
769
770         return OPERATOR_FINISHED;
771 }
772
773 void OBJECT_OT_armature_add(wmOperatorType *ot)
774 {
775         /* identifiers */
776         ot->name = "Add Armature";
777         ot->description = "Add an armature object to the scene";
778         ot->idname = "OBJECT_OT_armature_add";
779
780         /* api callbacks */
781         ot->exec = object_armature_add_exec;
782         ot->poll = ED_operator_objectmode;
783
784         /* flags */
785         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
786
787         /* properties */
788         ED_object_add_unit_props(ot);
789         ED_object_add_generic_props(ot, true);
790 }
791
792 /********************* Add Empty Operator ********************/
793
794 static int object_empty_add_exec(bContext *C, wmOperator *op)
795 {
796         Object *ob;
797         int type = RNA_enum_get(op->ptr, "type");
798         unsigned int layer;
799         float loc[3], rot[3];
800
801         WM_operator_view3d_unit_defaults(C, op);
802         if (!ED_object_add_generic_get_opts(C, op, 'Z', loc, rot, NULL, &layer, NULL))
803                 return OPERATOR_CANCELLED;
804
805         ob = ED_object_add_type(C, OB_EMPTY, NULL, loc, rot, false, layer);
806
807         BKE_object_empty_draw_type_set(ob, type);
808         BKE_object_obdata_size_init(ob, RNA_float_get(op->ptr, "radius"));
809
810         return OPERATOR_FINISHED;
811 }
812
813 void OBJECT_OT_empty_add(wmOperatorType *ot)
814 {
815         /* identifiers */
816         ot->name = "Add Empty";
817         ot->description = "Add an empty object to the scene";
818         ot->idname = "OBJECT_OT_empty_add";
819
820         /* api callbacks */
821         ot->invoke = WM_menu_invoke;
822         ot->exec = object_empty_add_exec;
823         ot->poll = ED_operator_objectmode;
824
825         /* flags */
826         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
827
828         /* properties */
829         ot->prop = RNA_def_enum(ot->srna, "type", rna_enum_object_empty_drawtype_items, 0, "Type", "");
830
831         ED_object_add_unit_props(ot);
832         ED_object_add_generic_props(ot, false);
833 }
834
835 static int empty_drop_named_image_invoke(bContext *C, wmOperator *op, const wmEvent *event)
836 {
837         Scene *scene = CTX_data_scene(C);
838
839         BaseLegacy *base = NULL;
840         Image *ima = NULL;
841         Object *ob = NULL;
842
843         ima = (Image *)WM_operator_drop_load_path(C, op, ID_IM);
844         if (!ima) {
845                 return OPERATOR_CANCELLED;
846         }
847         /* handled below */
848         id_us_min((ID *)ima);
849
850         base = ED_view3d_give_base_under_cursor(C, event->mval);
851
852         /* if empty under cursor, then set object */
853         if (base && base->object->type == OB_EMPTY) {
854                 ob = base->object;
855                 WM_event_add_notifier(C, NC_SCENE | ND_OB_ACTIVE, scene);
856         }
857         else {
858                 /* add new empty */
859                 unsigned int layer;
860                 float rot[3];
861
862                 if (!ED_object_add_generic_get_opts(C, op, 'Z', NULL, rot, NULL, &layer, NULL))
863                         return OPERATOR_CANCELLED;
864
865                 ob = ED_object_add_type(C, OB_EMPTY, NULL, NULL, rot, false, layer);
866
867                 /* add under the mouse */
868                 ED_object_location_from_view(C, ob->loc);
869                 ED_view3d_cursor3d_position(C, ob->loc, event->mval);
870         }
871
872         BKE_object_empty_draw_type_set(ob, OB_EMPTY_IMAGE);
873
874         id_us_min(ob->data);
875         ob->data = ima;
876         id_us_plus(ob->data);
877
878         return OPERATOR_FINISHED;
879 }
880
881 void OBJECT_OT_drop_named_image(wmOperatorType *ot)
882 {
883         PropertyRNA *prop;
884
885         /* identifiers */
886         ot->name = "Add Empty Image/Drop Image To Empty";
887         ot->description = "Add an empty image type to scene with data";
888         ot->idname = "OBJECT_OT_drop_named_image";
889
890         /* api callbacks */
891         ot->invoke = empty_drop_named_image_invoke;
892         ot->poll = ED_operator_objectmode;
893
894         /* flags */
895         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
896
897         /* properties */
898         prop = RNA_def_string(ot->srna, "filepath", NULL, FILE_MAX, "Filepath", "Path to image file");
899         RNA_def_property_flag(prop, PROP_HIDDEN | PROP_SKIP_SAVE);
900         RNA_def_boolean(ot->srna, "relative_path", true, "Relative Path", "Select the file relative to the blend file");
901         RNA_def_property_flag(prop, PROP_HIDDEN | PROP_SKIP_SAVE);
902         prop = RNA_def_string(ot->srna, "name", NULL, MAX_ID_NAME - 2, "Name", "Image name to assign");
903         RNA_def_property_flag(prop, PROP_HIDDEN | PROP_SKIP_SAVE);
904         ED_object_add_generic_props(ot, false);
905 }
906
907 /********************* Add Lamp Operator ********************/
908
909 static const char *get_lamp_defname(int type)
910 {
911         switch (type) {
912                 case LA_LOCAL: return CTX_DATA_(BLT_I18NCONTEXT_ID_LAMP, "Point");
913                 case LA_SUN: return CTX_DATA_(BLT_I18NCONTEXT_ID_LAMP, "Sun");
914                 case LA_SPOT: return CTX_DATA_(BLT_I18NCONTEXT_ID_LAMP, "Spot");
915                 case LA_HEMI: return CTX_DATA_(BLT_I18NCONTEXT_ID_LAMP, "Hemi");
916                 case LA_AREA: return CTX_DATA_(BLT_I18NCONTEXT_ID_LAMP, "Area");
917                 default:
918                         return CTX_DATA_(BLT_I18NCONTEXT_ID_LAMP, "Lamp");
919         }
920 }
921
922 static int object_lamp_add_exec(bContext *C, wmOperator *op)
923 {
924         Scene *scene = CTX_data_scene(C);
925         Object *ob;
926         Lamp *la;
927         int type = RNA_enum_get(op->ptr, "type");
928         unsigned int layer;
929         float loc[3], rot[3];
930
931         WM_operator_view3d_unit_defaults(C, op);
932         if (!ED_object_add_generic_get_opts(C, op, 'Z', loc, rot, NULL, &layer, NULL))
933                 return OPERATOR_CANCELLED;
934
935         ob = ED_object_add_type(C, OB_LAMP, get_lamp_defname(type), loc, rot, false, layer);
936         BKE_object_obdata_size_init(ob, RNA_float_get(op->ptr, "radius"));
937
938         la = (Lamp *)ob->data;
939         la->type = type;
940
941         if (BKE_scene_use_new_shading_nodes(scene)) {
942                 ED_node_shader_default(C, &la->id);
943                 la->use_nodes = true;
944         }
945
946         return OPERATOR_FINISHED;
947 }
948
949 void OBJECT_OT_lamp_add(wmOperatorType *ot)
950 {
951         /* identifiers */
952         ot->name = "Add Lamp";
953         ot->description = "Add a lamp object to the scene";
954         ot->idname = "OBJECT_OT_lamp_add";
955
956         /* api callbacks */
957         ot->invoke = WM_menu_invoke;
958         ot->exec = object_lamp_add_exec;
959         ot->poll = ED_operator_objectmode;
960
961         /* flags */
962         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
963
964         /* properties */
965         ot->prop = RNA_def_enum(ot->srna, "type", rna_enum_lamp_type_items, 0, "Type", "");
966         RNA_def_property_translation_context(ot->prop, BLT_I18NCONTEXT_ID_LAMP);
967
968         ED_object_add_unit_props(ot);
969         ED_object_add_generic_props(ot, false);
970 }
971
972 /********************* Add Group Instance Operator ********************/
973
974 static int group_instance_add_exec(bContext *C, wmOperator *op)
975 {
976         Group *group;
977         unsigned int layer;
978         float loc[3], rot[3];
979         
980         if (RNA_struct_property_is_set(op->ptr, "name")) {
981                 char name[MAX_ID_NAME - 2];
982                 
983                 RNA_string_get(op->ptr, "name", name);
984                 group = (Group *)BKE_libblock_find_name(ID_GR, name);
985                 
986                 if (0 == RNA_struct_property_is_set(op->ptr, "location")) {
987                         wmEvent *event = CTX_wm_window(C)->eventstate;
988                         ARegion *ar = CTX_wm_region(C);
989                         const int mval[2] = {event->x - ar->winrct.xmin,
990                                              event->y - ar->winrct.ymin};
991                         ED_object_location_from_view(C, loc);
992                         ED_view3d_cursor3d_position(C, loc, mval);
993                         RNA_float_set_array(op->ptr, "location", loc);
994                 }
995         }
996         else
997                 group = BLI_findlink(&CTX_data_main(C)->group, RNA_enum_get(op->ptr, "group"));
998
999         if (!ED_object_add_generic_get_opts(C, op, 'Z', loc, rot, NULL, &layer, NULL))
1000                 return OPERATOR_CANCELLED;
1001
1002         if (group) {
1003                 Main *bmain = CTX_data_main(C);
1004                 Scene *scene = CTX_data_scene(C);
1005                 Object *ob = ED_object_add_type(C, OB_EMPTY, group->id.name + 2, loc, rot, false, layer);
1006                 ob->dup_group = group;
1007                 ob->transflag |= OB_DUPLIGROUP;
1008                 id_us_plus(&group->id);
1009
1010                 /* works without this except if you try render right after, see: 22027 */
1011                 DAG_relations_tag_update(bmain);
1012
1013                 WM_event_add_notifier(C, NC_SCENE | ND_OB_ACTIVE, scene);
1014
1015                 return OPERATOR_FINISHED;
1016         }
1017
1018         return OPERATOR_CANCELLED;
1019 }
1020
1021 /* only used as menu */
1022 void OBJECT_OT_group_instance_add(wmOperatorType *ot)
1023 {
1024         PropertyRNA *prop;
1025
1026         /* identifiers */
1027         ot->name = "Add Group Instance";
1028         ot->description = "Add a dupligroup instance";
1029         ot->idname = "OBJECT_OT_group_instance_add";
1030
1031         /* api callbacks */
1032         ot->invoke = WM_enum_search_invoke;
1033         ot->exec = group_instance_add_exec;
1034         ot->poll = ED_operator_objectmode;
1035
1036         /* flags */
1037         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1038
1039         /* properties */
1040         RNA_def_string(ot->srna, "name", "Group", MAX_ID_NAME - 2, "Name", "Group name to add");
1041         prop = RNA_def_enum(ot->srna, "group", DummyRNA_NULL_items, 0, "Group", "");
1042         RNA_def_enum_funcs(prop, RNA_group_itemf);
1043         RNA_def_property_flag(prop, PROP_ENUM_NO_TRANSLATE);
1044         ot->prop = prop;
1045         ED_object_add_generic_props(ot, false);
1046 }
1047
1048 /********************* Add Speaker Operator ********************/
1049
1050 static int object_speaker_add_exec(bContext *C, wmOperator *op)
1051 {
1052         Object *ob;
1053         unsigned int layer;
1054         float loc[3], rot[3];
1055         Scene *scene = CTX_data_scene(C);
1056
1057         if (!ED_object_add_generic_get_opts(C, op, 'Z', loc, rot, NULL, &layer, NULL))
1058                 return OPERATOR_CANCELLED;
1059
1060         ob = ED_object_add_type(C, OB_SPEAKER, NULL, loc, rot, false, layer);
1061
1062         /* to make it easier to start using this immediately in NLA, a default sound clip is created
1063          * ready to be moved around to retime the sound and/or make new sound clips
1064          */
1065         {
1066                 /* create new data for NLA hierarchy */
1067                 AnimData *adt = BKE_animdata_add_id(&ob->id);
1068                 NlaTrack *nlt = add_nlatrack(adt, NULL);
1069                 NlaStrip *strip = add_nla_soundstrip(scene, ob->data);
1070                 strip->start = CFRA;
1071                 strip->end += strip->start;
1072
1073                 /* hook them up */
1074                 BKE_nlatrack_add_strip(nlt, strip);
1075
1076                 /* auto-name the strip, and give the track an interesting name  */
1077                 BLI_strncpy(nlt->name, DATA_("SoundTrack"), sizeof(nlt->name));
1078                 BKE_nlastrip_validate_name(adt, strip);
1079
1080                 WM_event_add_notifier(C, NC_ANIMATION | ND_NLA | NA_EDITED, NULL);
1081         }
1082
1083         return OPERATOR_FINISHED;
1084 }
1085
1086 void OBJECT_OT_speaker_add(wmOperatorType *ot)
1087 {
1088         /* identifiers */
1089         ot->name = "Add Speaker";
1090         ot->description = "Add a speaker object to the scene";
1091         ot->idname = "OBJECT_OT_speaker_add";
1092
1093         /* api callbacks */
1094         ot->exec = object_speaker_add_exec;
1095         ot->poll = ED_operator_objectmode;
1096
1097         /* flags */
1098         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1099
1100         ED_object_add_generic_props(ot, true);
1101 }
1102
1103 /**************************** Delete Object *************************/
1104
1105 static void object_delete_check_glsl_update(Object *ob)
1106 {
1107         /* some objects could affect on GLSL shading, make sure GLSL settings
1108          * are being tagged to be updated when object is removing from scene
1109          */
1110         if (ob->type == OB_LAMP) {
1111                 if (ob->gpulamp.first)
1112                         GPU_lamp_free(ob);
1113         }
1114 }
1115
1116 /* remove base from a specific scene */
1117 /* note: now unlinks constraints as well */
1118 void ED_base_object_free_and_unlink(Main *bmain, Scene *scene, Object *ob)
1119 {
1120         if (BKE_library_ID_is_indirectly_used(bmain, ob) &&
1121             ID_REAL_USERS(ob) <= 1 && ID_EXTRA_USERS(ob) == 0)
1122         {
1123                 /* We cannot delete indirectly used object... */
1124                 printf("WARNING, undeletable object '%s', should have been catched before reaching this function!",
1125                        ob->id.name + 2);
1126                 return;
1127         }
1128
1129         object_delete_check_glsl_update(ob);
1130         BKE_collections_object_remove(bmain, scene, ob, true);
1131         DAG_id_type_tag(bmain, ID_OB);
1132 }
1133
1134 static int object_delete_exec(bContext *C, wmOperator *op)
1135 {
1136         Main *bmain = CTX_data_main(C);
1137         Scene *scene = CTX_data_scene(C);
1138         wmWindowManager *wm = CTX_wm_manager(C);
1139         wmWindow *win;
1140         const bool use_global = RNA_boolean_get(op->ptr, "use_global");
1141         bool changed = false;
1142
1143         if (CTX_data_edit_object(C)) 
1144                 return OPERATOR_CANCELLED;
1145
1146         CTX_DATA_BEGIN (C, Object *, ob, selected_objects)
1147         {
1148                 const bool is_indirectly_used = BKE_library_ID_is_indirectly_used(bmain, ob);
1149                 if (ob->id.tag & LIB_TAG_INDIRECT) {
1150                         /* Can this case ever happen? */
1151                         BKE_reportf(op->reports, RPT_WARNING, "Cannot delete indirectly linked object '%s'", ob->id.name + 2);
1152                         continue;
1153                 }
1154                 else if (is_indirectly_used && ID_REAL_USERS(ob) <= 1 && ID_EXTRA_USERS(ob) == 0) {
1155                         BKE_reportf(op->reports, RPT_WARNING,
1156                                 "Cannot delete object '%s' from scene '%s', indirectly used objects need at least one user",
1157                                 ob->id.name + 2, scene->id.name + 2);
1158                         continue;
1159                 }
1160                 /* remove from Grease Pencil parent */
1161                 for (bGPdata *gpd = bmain->gpencil.first; gpd; gpd = gpd->id.next) {
1162                         for (bGPDlayer *gpl = gpd->layers.first; gpl; gpl = gpl->next) {
1163                                 if (gpl->parent != NULL) {
1164                                         if (gpl->parent == ob) {
1165                                                 gpl->parent = NULL;
1166                                         }
1167                                 }
1168                         }
1169                 }
1170
1171                 /* remove from current scene only */
1172                 ED_base_object_free_and_unlink(bmain, scene, ob);
1173                 changed = true;
1174
1175                 if (use_global) {
1176                         Scene *scene_iter;
1177                         for (scene_iter = bmain->scene.first; scene_iter; scene_iter = scene_iter->id.next) {
1178                                 if (scene_iter != scene && !ID_IS_LINKED_DATABLOCK(scene_iter)) {
1179                                         if (is_indirectly_used && ID_REAL_USERS(ob) <= 1 && ID_EXTRA_USERS(ob) == 0) {
1180                                                 BKE_reportf(op->reports, RPT_WARNING,
1181                                                             "Cannot delete object '%s' from scene '%s', indirectly used objects need at least one user",
1182                                                             ob->id.name + 2, scene_iter->id.name + 2);
1183                                                 break;
1184                                         }
1185                                         ED_base_object_free_and_unlink(bmain, scene_iter, ob);
1186                                 }
1187                         }
1188                 }
1189                 /* end global */
1190         }
1191         CTX_DATA_END;
1192
1193         if (!changed)
1194                 return OPERATOR_CANCELLED;
1195
1196         /* delete has to handle all open scenes */
1197         BKE_main_id_tag_listbase(&bmain->scene, LIB_TAG_DOIT, true);
1198         for (win = wm->windows.first; win; win = win->next) {
1199                 scene = win->screen->scene;
1200                 
1201                 if (scene->id.tag & LIB_TAG_DOIT) {
1202                         scene->id.tag &= ~LIB_TAG_DOIT;
1203                         
1204                         DAG_relations_tag_update(bmain);
1205
1206                         WM_event_add_notifier(C, NC_SCENE | ND_OB_ACTIVE, scene);
1207                         WM_event_add_notifier(C, NC_SCENE | ND_LAYER_CONTENT, scene);
1208                 }
1209         }
1210
1211         return OPERATOR_FINISHED;
1212 }
1213
1214 void OBJECT_OT_delete(wmOperatorType *ot)
1215 {
1216         /* identifiers */
1217         ot->name = "Delete";
1218         ot->description = "Delete selected objects";
1219         ot->idname = "OBJECT_OT_delete";
1220
1221         /* api callbacks */
1222         ot->invoke = WM_operator_confirm;
1223         ot->exec = object_delete_exec;
1224         ot->poll = ED_operator_objectmode;
1225
1226         /* flags */
1227         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1228
1229         RNA_def_boolean(ot->srna, "use_global", 0, "Delete Globally", "Remove object from all scenes");
1230 }
1231
1232 /**************************** Copy Utilities ******************************/
1233
1234 /* after copying objects, copied data should get new pointers */
1235 static void copy_object_set_idnew(bContext *C)
1236 {
1237         Main *bmain = CTX_data_main(C);
1238
1239         CTX_DATA_BEGIN (C, Object *, ob, selected_editable_objects)
1240         {
1241                 BKE_libblock_relink_to_newid(&ob->id);
1242         }
1243         CTX_DATA_END;
1244
1245         set_sca_new_poins();
1246
1247         BKE_main_id_clear_newpoins(bmain);
1248 }
1249
1250 /********************* Make Duplicates Real ************************/
1251
1252 /**
1253  * \note regarding hashing dupli-objects when using OB_DUPLIGROUP, skip the first member of #DupliObject.persistent_id
1254  * since its a unique index and we only want to know if the group objects are from the same dupli-group instance.
1255  */
1256 static unsigned int dupliobject_group_hash(const void *ptr)
1257 {
1258         const DupliObject *dob = ptr;
1259         unsigned int hash = BLI_ghashutil_ptrhash(dob->ob);
1260         unsigned int i;
1261         for (i = 1; (i < MAX_DUPLI_RECUR) && dob->persistent_id[i] != INT_MAX; i++) {
1262                 hash ^= (dob->persistent_id[i] ^ i);
1263         }
1264         return hash;
1265 }
1266
1267 /**
1268  * \note regarding hashing dupli-objects when NOT using OB_DUPLIGROUP, include the first member of #DupliObject.persistent_id
1269  * since its the index of the vertex/face the object is instantiated on and we want to identify objects on the same vertex/face.
1270  */
1271 static unsigned int dupliobject_hash(const void *ptr)
1272 {
1273         const DupliObject *dob = ptr;
1274         unsigned int hash = BLI_ghashutil_ptrhash(dob->ob);
1275         hash ^= (dob->persistent_id[0] ^ 0);
1276         return hash;
1277 }
1278
1279 /* Compare function that matches dupliobject_group_hash */
1280 static bool dupliobject_group_cmp(const void *a_, const void *b_)
1281 {
1282         const DupliObject *a = a_;
1283         const DupliObject *b = b_;
1284         unsigned int i;
1285
1286         if (a->ob != b->ob) {
1287                 return true;
1288         }
1289
1290         for (i = 1; (i < MAX_DUPLI_RECUR); i++) {
1291                 if (a->persistent_id[i] != b->persistent_id[i]) {
1292                         return true;
1293                 }
1294                 else if (a->persistent_id[i] == INT_MAX) {
1295                         break;
1296                 }
1297         }
1298
1299         /* matching */
1300         return false;
1301 }
1302
1303 /* Compare function that matches dupliobject_hash */
1304 static bool dupliobject_cmp(const void *a_, const void *b_)
1305 {
1306         const DupliObject *a = a_;
1307         const DupliObject *b = b_;
1308
1309         if (a->ob != b->ob) {
1310                 return true;
1311         }
1312
1313         if (a->persistent_id[0] != b->persistent_id[0]) {
1314                 return true;
1315         }
1316
1317         /* matching */
1318         return false;
1319 }
1320
1321 static void make_object_duplilist_real(bContext *C, Scene *scene, Base *base,
1322                                        const bool use_base_parent,
1323                                        const bool use_hierarchy)
1324 {
1325         Main *bmain = CTX_data_main(C);
1326         SceneLayer *sl = CTX_data_scene_layer(C);
1327         ListBase *lb;
1328         DupliObject *dob;
1329         GHash *dupli_gh = NULL, *parent_gh = NULL;
1330         Object *object;
1331
1332         if (!(base->object->transflag & OB_DUPLI))
1333                 return;
1334
1335         lb = object_duplilist(bmain->eval_ctx, scene, base->object);
1336
1337         if (use_hierarchy || use_base_parent) {
1338                 dupli_gh = BLI_ghash_ptr_new(__func__);
1339                 if (use_hierarchy) {
1340                         if (base->object->transflag & OB_DUPLIGROUP) {
1341                                 parent_gh = BLI_ghash_new(dupliobject_group_hash, dupliobject_group_cmp, __func__);
1342                         }
1343                         else {
1344                                 parent_gh = BLI_ghash_new(dupliobject_hash, dupliobject_cmp, __func__);
1345                         }
1346                 }
1347         }
1348
1349         for (dob = lb->first; dob; dob = dob->next) {
1350                 Base *basen;
1351                 Object *ob = ID_NEW_SET(dob->ob, BKE_object_copy(bmain, dob->ob));
1352
1353                 /* font duplis can have a totcol without material, we get them from parent
1354                  * should be implemented better...
1355                  */
1356                 if (ob->mat == NULL) ob->totcol = 0;
1357
1358                 BKE_collection_object_add_from(scene, dob->ob, ob);
1359                 basen = BKE_scene_layer_base_find(sl, ob);
1360
1361                 BKE_scene_object_base_flag_sync_from_base(basen);
1362
1363                 /* make sure apply works */
1364                 BKE_animdata_free(&ob->id, true);
1365                 ob->adt = NULL;
1366
1367                 /* Proxies are not to be copied. */
1368                 ob->proxy_from = NULL;
1369                 ob->proxy_group = NULL;
1370                 ob->proxy = NULL;
1371
1372                 ob->parent = NULL;
1373                 BKE_constraints_free(&ob->constraints);
1374                 ob->curve_cache = NULL;
1375                 ob->transflag &= ~OB_DUPLI;
1376
1377                 copy_m4_m4(ob->obmat, dob->mat);
1378                 BKE_object_apply_mat4(ob, ob->obmat, false, false);
1379
1380                 if (dupli_gh) {
1381                         BLI_ghash_insert(dupli_gh, dob, ob);
1382                 }
1383                 if (parent_gh) {
1384                         void **val;
1385                         /* Due to nature of hash/comparison of this ghash, a lot of duplis may be considered as 'the same',
1386                          * this avoids trying to insert same key several time and raise asserts in debug builds... */
1387                         if (!BLI_ghash_ensure_p(parent_gh, dob, &val)) {
1388                                 *val = ob;
1389                         }
1390                 }
1391
1392                 /* Remap new object to itself, and clear again newid pointer of orig object. */
1393                 BKE_libblock_relink_to_newid(&ob->id);
1394                 set_sca_new_poins_ob(ob);
1395                 BKE_id_clear_newpoin(&dob->ob->id);
1396
1397                 DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
1398         }
1399
1400         if (use_hierarchy) {
1401                 for (dob = lb->first; dob; dob = dob->next) {
1402                         /* original parents */
1403                         Object *ob_src =     dob->ob;
1404                         Object *ob_src_par = ob_src->parent;
1405
1406                         Object *ob_dst =     BLI_ghash_lookup(dupli_gh, dob);
1407                         Object *ob_dst_par = NULL;
1408
1409                         /* find parent that was also made real */
1410                         if (ob_src_par) {
1411                                 /* OK to keep most of the members uninitialized,
1412                                  * they won't be read, this is simply for a hash lookup. */
1413                                 DupliObject dob_key;
1414                                 dob_key.ob = ob_src_par;
1415                                 if (base->object->transflag & OB_DUPLIGROUP) {
1416                                         memcpy(&dob_key.persistent_id[1],
1417                                                    &dob->persistent_id[1],
1418                                                    sizeof(dob->persistent_id[1]) * (MAX_DUPLI_RECUR - 1));
1419                                 }
1420                                 else {
1421                                         dob_key.persistent_id[0] = dob->persistent_id[0];
1422                                 }
1423                                 ob_dst_par = BLI_ghash_lookup(parent_gh, &dob_key);
1424                         }
1425
1426                         if (ob_dst_par) {
1427                                 /* allow for all possible parent types */
1428                                 ob_dst->partype = ob_src->partype;
1429                                 BLI_strncpy(ob_dst->parsubstr, ob_src->parsubstr, sizeof(ob_dst->parsubstr));
1430                                 ob_dst->par1 = ob_src->par1;
1431                                 ob_dst->par2 = ob_src->par2;
1432                                 ob_dst->par3 = ob_src->par3;
1433
1434                                 copy_m4_m4(ob_dst->parentinv, ob_src->parentinv);
1435
1436                                 ob_dst->parent = ob_dst_par;
1437                         }
1438                         else if (use_base_parent) {
1439                                 ob_dst->parent = base->object;
1440                                 ob_dst->partype = PAROBJECT;
1441                         }
1442
1443                         if (ob_dst->parent) {
1444                                 invert_m4_m4(ob_dst->parentinv, dob->mat);
1445
1446                                 /* note, this may be the parent of other objects, but it should
1447                                  * still work out ok */
1448                                 BKE_object_apply_mat4(ob_dst, dob->mat, false, true);
1449
1450                                 /* to set ob_dst->orig and in case theres any other discrepicies */
1451                                 DAG_id_tag_update(&ob_dst->id, OB_RECALC_OB);
1452                         }
1453                 }
1454         }
1455         else if (use_base_parent) {
1456                 /* since we are ignoring the internal hierarchy - parent all to the
1457                  * base object */
1458                 for (dob = lb->first; dob; dob = dob->next) {
1459                         /* original parents */
1460                         Object *ob_dst = BLI_ghash_lookup(dupli_gh, dob);
1461
1462                         ob_dst->parent = base->object;
1463                         ob_dst->partype = PAROBJECT;
1464
1465                         /* similer to the code above, see comments */
1466                         invert_m4_m4(ob_dst->parentinv, dob->mat);
1467                         BKE_object_apply_mat4(ob_dst, dob->mat, false, true);
1468                         DAG_id_tag_update(&ob_dst->id, OB_RECALC_OB);
1469                 }
1470         }
1471
1472         if (base->object->transflag & OB_DUPLIGROUP && base->object->dup_group) {
1473                 for (object = bmain->object.first; object; object = object->id.next) {
1474                         if (object->proxy_group == base->object) {
1475                                 object->proxy = NULL;
1476                                 object->proxy_from = NULL;
1477                                 DAG_id_tag_update(&object->id, OB_RECALC_OB);
1478                         }
1479                 }
1480         }
1481
1482         if (dupli_gh)
1483                 BLI_ghash_free(dupli_gh, NULL, NULL);
1484         if (parent_gh)
1485                 BLI_ghash_free(parent_gh, NULL, NULL);
1486
1487         free_object_duplilist(lb);
1488
1489         base->object->transflag &= ~OB_DUPLI;
1490 }
1491
1492 static int object_duplicates_make_real_exec(bContext *C, wmOperator *op)
1493 {
1494         Main *bmain = CTX_data_main(C);
1495         Scene *scene = CTX_data_scene(C);
1496
1497         const bool use_base_parent = RNA_boolean_get(op->ptr, "use_base_parent");
1498         const bool use_hierarchy = RNA_boolean_get(op->ptr, "use_hierarchy");
1499
1500         BKE_main_id_clear_newpoins(bmain);
1501
1502         CTX_DATA_BEGIN (C, Base *, base, selected_editable_bases)
1503         {
1504                 make_object_duplilist_real(C, scene, base, use_base_parent, use_hierarchy);
1505
1506                 /* dependencies were changed */
1507                 WM_event_add_notifier(C, NC_OBJECT | ND_PARENT, base->object);
1508         }
1509         CTX_DATA_END;
1510
1511         DAG_relations_tag_update(bmain);
1512         WM_event_add_notifier(C, NC_SCENE, scene);
1513         WM_main_add_notifier(NC_OBJECT | ND_DRAW, NULL);
1514
1515         return OPERATOR_FINISHED;
1516 }
1517
1518 void OBJECT_OT_duplicates_make_real(wmOperatorType *ot)
1519 {
1520         /* identifiers */
1521         ot->name = "Make Duplicates Real";
1522         ot->description = "Make dupli objects attached to this object real";
1523         ot->idname = "OBJECT_OT_duplicates_make_real";
1524
1525         /* api callbacks */
1526         ot->exec = object_duplicates_make_real_exec;
1527
1528         ot->poll = ED_operator_objectmode;
1529
1530         /* flags */
1531         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1532
1533         RNA_def_boolean(ot->srna, "use_base_parent", 0, "Parent", "Parent newly created objects to the original duplicator");
1534         RNA_def_boolean(ot->srna, "use_hierarchy", 0, "Keep Hierarchy", "Maintain parent child relationships");
1535 }
1536
1537 /**************************** Convert **************************/
1538
1539 static EnumPropertyItem convert_target_items[] = {
1540         {OB_CURVE, "CURVE", ICON_OUTLINER_OB_CURVE, "Curve from Mesh/Text", ""},
1541         {OB_MESH, "MESH", ICON_OUTLINER_OB_MESH, "Mesh from Curve/Meta/Surf/Text", ""},
1542         {0, NULL, 0, NULL, NULL}
1543 };
1544
1545 static void convert_ensure_curve_cache(Main *bmain, Scene *scene, Object *ob)
1546 {
1547         if (ob->curve_cache == NULL) {
1548                 /* Force creation. This is normally not needed but on operator
1549                  * redo we might end up with an object which isn't evaluated yet.
1550                  */
1551                 if (ELEM(ob->type, OB_SURF, OB_CURVE, OB_FONT)) {
1552                         BKE_displist_make_curveTypes(scene, ob, false);
1553                 }
1554                 else if (ob->type == OB_MBALL) {
1555                         BKE_displist_make_mball(bmain->eval_ctx, scene, ob);
1556                 }
1557         }
1558 }
1559
1560 static void curvetomesh(Main *bmain, Scene *scene, Object *ob)
1561 {
1562         convert_ensure_curve_cache(bmain, scene, ob);
1563         BKE_mesh_from_nurbs(ob); /* also does users */
1564
1565         if (ob->type == OB_MESH) {
1566                 BKE_object_free_modifiers(ob);
1567
1568                 /* Game engine defaults for mesh objects */
1569                 ob->body_type = OB_BODY_TYPE_STATIC;
1570                 ob->gameflag = OB_PROP | OB_COLLISION;
1571         }
1572 }
1573
1574 static int convert_poll(bContext *C)
1575 {
1576         Object *obact = CTX_data_active_object(C);
1577         Scene *scene = CTX_data_scene(C);
1578
1579         return (!ID_IS_LINKED_DATABLOCK(scene) && obact && scene->obedit != obact &&
1580                 (obact->flag & SELECT) && !ID_IS_LINKED_DATABLOCK(obact));
1581 }
1582
1583 /* Helper for convert_exec */
1584 static Base *duplibase_for_convert(Main *bmain, Scene *scene, SceneLayer *sl, Base *base, Object *ob)
1585 {
1586         Object *obn;
1587         Base *basen;
1588
1589         if (ob == NULL) {
1590                 ob = base->object;
1591         }
1592
1593         obn = BKE_object_copy(bmain, ob);
1594         DAG_id_tag_update(&ob->id, OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME);
1595         BKE_collection_object_add_from(scene, ob, obn);
1596
1597         basen = BKE_scene_layer_base_find(sl, obn);
1598         ED_object_base_select(basen, BA_SELECT);
1599         ED_object_base_select(basen, BA_DESELECT);
1600         return basen;
1601 }
1602
1603 static int convert_exec(bContext *C, wmOperator *op)
1604 {
1605         Main *bmain = CTX_data_main(C);
1606         Scene *scene = CTX_data_scene(C);
1607         SceneLayer *sl = CTX_data_scene_layer(C);
1608         Base *basen = NULL, *basact = NULL;
1609         Object *ob, *ob1, *newob, *obact = CTX_data_active_object(C);
1610         DerivedMesh *dm;
1611         Curve *cu;
1612         Nurb *nu;
1613         MetaBall *mb;
1614         Mesh *me;
1615         const short target = RNA_enum_get(op->ptr, "target");
1616         const bool keep_original = RNA_boolean_get(op->ptr, "keep_original");
1617         int a, mballConverted = 0;
1618
1619         /* don't forget multiple users! */
1620
1621         {
1622                 BaseLegacy *base;
1623
1624                 for (base = scene->base.first; base; base = base->next) {
1625                         ob = base->object;
1626                         ob->flag &= ~OB_DONE;
1627
1628                         /* flag data thats not been edited (only needed for !keep_original) */
1629                         if (ob->data) {
1630                                 ((ID *)ob->data)->tag |= LIB_TAG_DOIT;
1631                         }
1632
1633                         /* possible metaball basis is not in this scene */
1634                         if (ob->type == OB_MBALL && target == OB_MESH) {
1635                                 if (BKE_mball_is_basis(ob) == false) {
1636                                         Object *ob_basis;
1637                                         ob_basis = BKE_mball_basis_find(scene, ob);
1638                                         if (ob_basis) {
1639                                                 ob_basis->flag &= ~OB_DONE;
1640                                         }
1641                                 }
1642                         }
1643                 }
1644         }
1645
1646         ListBase selected_editable_bases = CTX_data_collection_get(C, "selected_editable_bases");
1647
1648         /* Ensure we get all meshes calculated with a sufficient data-mask,
1649          * needed since re-evaluating single modifiers causes bugs if they depend
1650          * on other objects data masks too, see: T50950. */
1651         {
1652                 for (CollectionPointerLink *link = selected_editable_bases.first; link; link = link->next) {
1653                         Base *base = link->ptr.data;
1654                         DAG_id_tag_update(&base->object->id, OB_RECALC_DATA);
1655                 }
1656
1657                 uint64_t customdata_mask_prev = scene->customdata_mask;
1658                 scene->customdata_mask |= CD_MASK_MESH;
1659                 BKE_scene_update_tagged(bmain->eval_ctx, bmain, scene);
1660                 scene->customdata_mask = customdata_mask_prev;
1661         }
1662
1663         for (CollectionPointerLink *link = selected_editable_bases.first; link; link = link->next) {
1664                 Base *base = link->ptr.data;
1665                 ob = base->object;
1666
1667                 if (ob->flag & OB_DONE || !IS_TAGGED(ob->data)) {
1668                         if (ob->type != target) {
1669                                 base->flag &= ~SELECT;
1670                                 ob->flag &= ~SELECT;
1671                         }
1672
1673                         /* obdata already modified */
1674                         if (!IS_TAGGED(ob->data)) {
1675                                 /* When 2 objects with linked data are selected, converting both
1676                                  * would keep modifiers on all but the converted object [#26003] */
1677                                 if (ob->type == OB_MESH) {
1678                                         BKE_object_free_modifiers(ob);  /* after derivedmesh calls! */
1679                                 }
1680                         }
1681                 }
1682                 else if (ob->type == OB_MESH && target == OB_CURVE) {
1683                         ob->flag |= OB_DONE;
1684
1685                         if (keep_original) {
1686                                 basen = duplibase_for_convert(bmain, scene, sl, base, NULL);
1687                                 newob = basen->object;
1688
1689                                 /* decrement original mesh's usage count  */
1690                                 me = newob->data;
1691                                 id_us_min(&me->id);
1692
1693                                 /* make a new copy of the mesh */
1694                                 newob->data = BKE_mesh_copy(bmain, me);
1695                         }
1696                         else {
1697                                 newob = ob;
1698                         }
1699
1700                         BKE_mesh_to_curve(scene, newob);
1701
1702                         if (newob->type == OB_CURVE) {
1703                                 BKE_object_free_modifiers(newob);   /* after derivedmesh calls! */
1704                                 ED_rigidbody_object_remove(bmain, scene, newob);
1705                         }
1706                 }
1707                 else if (ob->type == OB_MESH) {
1708                         ob->flag |= OB_DONE;
1709
1710                         if (keep_original) {
1711                                 basen = duplibase_for_convert(bmain, scene, sl, base, NULL);
1712                                 newob = basen->object;
1713
1714                                 /* decrement original mesh's usage count  */
1715                                 me = newob->data;
1716                                 id_us_min(&me->id);
1717
1718                                 /* make a new copy of the mesh */
1719                                 newob->data = BKE_mesh_copy(bmain, me);
1720                         }
1721                         else {
1722                                 newob = ob;
1723                                 DAG_id_tag_update(&ob->id, OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME);
1724                         }
1725
1726                         /* make new mesh data from the original copy */
1727                         /* note: get the mesh from the original, not from the copy in some
1728                          * cases this doesnt give correct results (when MDEF is used for eg)
1729                          */
1730                         dm = mesh_get_derived_final(scene, newob, CD_MASK_MESH);
1731
1732                         DM_to_mesh(dm, newob->data, newob, CD_MASK_MESH, true);
1733
1734                         /* re-tessellation is called by DM_to_mesh */
1735
1736                         BKE_object_free_modifiers(newob);   /* after derivedmesh calls! */
1737                 }
1738                 else if (ob->type == OB_FONT) {
1739                         ob->flag |= OB_DONE;
1740
1741                         if (keep_original) {
1742                                 basen = duplibase_for_convert(bmain, scene, sl, base, NULL);
1743                                 newob = basen->object;
1744
1745                                 /* decrement original curve's usage count  */
1746                                 id_us_min(&((Curve *)newob->data)->id);
1747
1748                                 /* make a new copy of the curve */
1749                                 newob->data = BKE_curve_copy(bmain, ob->data);
1750                         }
1751                         else {
1752                                 newob = ob;
1753                         }
1754
1755                         cu = newob->data;
1756
1757                         /* TODO(sergey): Ideally DAG will create nurbs list for a curve data
1758                          *               datablock, but for until we've got granular update
1759                          *               lets take care by selves.
1760                          */
1761                         BKE_vfont_to_curve(bmain, newob, FO_EDIT);
1762
1763                         newob->type = OB_CURVE;
1764                         cu->type = OB_CURVE;
1765
1766                         if (cu->vfont) {
1767                                 id_us_min(&cu->vfont->id);
1768                                 cu->vfont = NULL;
1769                         }
1770                         if (cu->vfontb) {
1771                                 id_us_min(&cu->vfontb->id);
1772                                 cu->vfontb = NULL;
1773                         }
1774                         if (cu->vfonti) {
1775                                 id_us_min(&cu->vfonti->id);
1776                                 cu->vfonti = NULL;
1777                         }
1778                         if (cu->vfontbi) {
1779                                 id_us_min(&cu->vfontbi->id);
1780                                 cu->vfontbi = NULL;
1781                         }
1782
1783                         if (!keep_original) {
1784                                 /* other users */
1785                                 if (cu->id.us > 1) {
1786                                         for (ob1 = bmain->object.first; ob1; ob1 = ob1->id.next) {
1787                                                 if (ob1->data == ob->data) {
1788                                                         ob1->type = OB_CURVE;
1789                                                         DAG_id_tag_update(&ob1->id, OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME);
1790                                                 }
1791                                         }
1792                                 }
1793                         }
1794
1795                         for (nu = cu->nurb.first; nu; nu = nu->next)
1796                                 nu->charidx = 0;
1797
1798                         cu->flag &= ~CU_3D;
1799                         BKE_curve_curve_dimension_update(cu);
1800
1801                         if (target == OB_MESH) {
1802                                 curvetomesh(bmain, scene, newob);
1803
1804                                 /* meshes doesn't use displist */
1805                                 BKE_object_free_curve_cache(newob);
1806                         }
1807                 }
1808                 else if (ELEM(ob->type, OB_CURVE, OB_SURF)) {
1809                         ob->flag |= OB_DONE;
1810
1811                         if (target == OB_MESH) {
1812                                 if (keep_original) {
1813                                         basen = duplibase_for_convert(bmain, scene, sl, base, NULL);
1814                                         newob = basen->object;
1815
1816                                         /* decrement original curve's usage count  */
1817                                         id_us_min(&((Curve *)newob->data)->id);
1818
1819                                         /* make a new copy of the curve */
1820                                         newob->data = BKE_curve_copy(bmain, ob->data);
1821                                 }
1822                                 else {
1823                                         newob = ob;
1824                                 }
1825
1826                                 curvetomesh(bmain, scene, newob);
1827
1828                                 /* meshes doesn't use displist */
1829                                 BKE_object_free_curve_cache(newob);
1830                         }
1831                 }
1832                 else if (ob->type == OB_MBALL && target == OB_MESH) {
1833                         Object *baseob;
1834
1835                         base->flag &= ~SELECT;
1836                         ob->flag &= ~SELECT;
1837
1838                         baseob = BKE_mball_basis_find(scene, ob);
1839
1840                         if (ob != baseob) {
1841                                 /* if motherball is converting it would be marked as done later */
1842                                 ob->flag |= OB_DONE;
1843                         }
1844
1845                         if (!(baseob->flag & OB_DONE)) {
1846                                 baseob->flag |= OB_DONE;
1847
1848                                 basen = duplibase_for_convert(bmain, scene, sl, base, baseob);
1849                                 newob = basen->object;
1850
1851                                 mb = newob->data;
1852                                 id_us_min(&mb->id);
1853
1854                                 newob->data = BKE_mesh_add(bmain, "Mesh");
1855                                 newob->type = OB_MESH;
1856
1857                                 me = newob->data;
1858                                 me->totcol = mb->totcol;
1859                                 if (newob->totcol) {
1860                                         me->mat = MEM_dupallocN(mb->mat);
1861                                         for (a = 0; a < newob->totcol; a++) id_us_plus((ID *)me->mat[a]);
1862                                 }
1863
1864                                 convert_ensure_curve_cache(bmain, scene, baseob);
1865                                 BKE_mesh_from_metaball(&baseob->curve_cache->disp, newob->data);
1866
1867                                 if (obact->type == OB_MBALL) {
1868                                         basact = basen;
1869                                 }
1870
1871                                 mballConverted = 1;
1872                         }
1873                 }
1874                 else {
1875                         continue;
1876                 }
1877
1878                 /* tag obdata if it was been changed */
1879
1880                 /* If the original object is active then make this object active */
1881                 if (basen) {
1882                         if (ob == obact) {
1883                                 /* store new active base to update BASACT */
1884                                 basact = basen;
1885                         }
1886
1887                         basen = NULL;
1888                 }
1889
1890                 if (!keep_original && (ob->flag & OB_DONE)) {
1891                         DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
1892                         ((ID *)ob->data)->tag &= ~LIB_TAG_DOIT; /* flag not to convert this datablock again */
1893                 }
1894         }
1895         BLI_freelistN(&selected_editable_bases);
1896
1897         if (!keep_original) {
1898                 if (mballConverted) {
1899                         FOREACH_SCENE_OBJECT(scene, ob_mball)
1900                         {
1901                                 if (ob->type == OB_MBALL) {
1902                                         if (ob->flag & OB_DONE) {
1903                                                 Object *ob_basis = NULL;
1904                                                 if (BKE_mball_is_basis(ob_mball) ||
1905                                                     ((ob_basis = BKE_mball_basis_find(scene, ob_mball)) && (ob_basis->flag & OB_DONE)))
1906                                                 {
1907                                                         ED_base_object_free_and_unlink(bmain, scene, ob_mball);
1908                                                 }
1909                                         }
1910                                 }
1911                         }
1912                         FOREACH_SCENE_OBJECT_END
1913                 }
1914
1915                 /* delete object should renew depsgraph */
1916                 DAG_relations_tag_update(bmain);
1917         }
1918
1919 // XXX  ED_object_editmode_enter(C, 0);
1920 // XXX  exit_editmode(C, EM_FREEDATA|EM_WAITCURSOR); /* freedata, but no undo */
1921
1922         if (basact) {
1923                 /* active base was changed */
1924                 ED_object_base_activate(C, basact);
1925                 BASACT_NEW = basact;
1926         }
1927         else if (BASACT_NEW->object->flag & OB_DONE) {
1928                 WM_event_add_notifier(C, NC_OBJECT | ND_MODIFIER, BASACT_NEW->object);
1929                 WM_event_add_notifier(C, NC_OBJECT | ND_DATA, BASACT_NEW->object);
1930         }
1931
1932         DAG_relations_tag_update(bmain);
1933         WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, scene);
1934         WM_event_add_notifier(C, NC_SCENE | ND_OB_SELECT, scene);
1935
1936         return OPERATOR_FINISHED;
1937 }
1938
1939
1940 void OBJECT_OT_convert(wmOperatorType *ot)
1941 {
1942         /* identifiers */
1943         ot->name = "Convert to";
1944         ot->description = "Convert selected objects to another type";
1945         ot->idname = "OBJECT_OT_convert";
1946
1947         /* api callbacks */
1948         ot->invoke = WM_menu_invoke;
1949         ot->exec = convert_exec;
1950         ot->poll = convert_poll;
1951
1952         /* flags */
1953         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1954
1955         /* properties */
1956         ot->prop = RNA_def_enum(ot->srna, "target", convert_target_items, OB_MESH, "Target", "Type of object to convert to");
1957         RNA_def_boolean(ot->srna, "keep_original", 0, "Keep Original", "Keep original objects instead of replacing them");
1958 }
1959
1960 /**************************** Duplicate ************************/
1961
1962 /* 
1963  * dupflag: a flag made from constants declared in DNA_userdef_types.h
1964  * The flag tells adduplicate() whether to copy data linked to the object, or to reference the existing data.
1965  * U.dupflag for default operations or you can construct a flag as python does
1966  * if the dupflag is 0 then no data will be copied (linked duplicate) */
1967
1968 /* used below, assumes id.new is correct */
1969 /* leaves selection of base/object unaltered */
1970 /* Does set ID->newid pointers. */
1971 static Base *object_add_duplicate_internal(Main *bmain, Scene *scene, SceneLayer *sl, Object *ob, int dupflag)
1972 {
1973 #define ID_NEW_REMAP_US(a)      if (      (a)->id.newid) { (a) = (void *)(a)->id.newid;       (a)->id.us++; }
1974 #define ID_NEW_REMAP_US2(a)     if (((ID *)a)->newid)    { (a) = ((ID  *)a)->newid;     ((ID *)a)->us++;    }
1975
1976         Base *basen = NULL;
1977         Material ***matarar;
1978         Object *obn;
1979         ID *id;
1980         int a, didit;
1981
1982         if (ob->mode & OB_MODE_POSE) {
1983                 ; /* nothing? */
1984         }
1985         else {
1986                 obn = ID_NEW_SET(ob, BKE_object_copy(bmain, ob));
1987                 DAG_id_tag_update(&obn->id, OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME);
1988
1989                 BKE_collection_object_add_from(scene, ob, obn);
1990                 basen = BKE_scene_layer_base_find(sl, obn);
1991
1992                 /* 1) duplis should end up in same group as the original
1993                  * 2) Rigid Body sim participants MUST always be part of a group...
1994                  */
1995                 // XXX: is 2) really a good measure here?
1996                 if ((ob->flag & OB_FROMGROUP) != 0 || ob->rigidbody_object || ob->rigidbody_constraint) {
1997                         Group *group;
1998                         for (group = bmain->group.first; group; group = group->id.next) {
1999                                 if (BKE_group_object_exists(group, ob))
2000                                         BKE_group_object_add(group, obn);
2001                         }
2002                 }
2003
2004                 /* duplicates using userflags */
2005                 if (dupflag & USER_DUP_ACT) {
2006                         BKE_animdata_copy_id_action(&obn->id, true);
2007                 }
2008
2009                 if (dupflag & USER_DUP_MAT) {
2010                         for (a = 0; a < obn->totcol; a++) {
2011                                 id = (ID *)obn->mat[a];
2012                                 if (id) {
2013                                         ID_NEW_REMAP_US(obn->mat[a])
2014                                         else {
2015                                                 obn->mat[a] = ID_NEW_SET(obn->mat[a], BKE_material_copy(bmain, obn->mat[a]));
2016                                         }
2017                                         id_us_min(id);
2018
2019                                         if (dupflag & USER_DUP_ACT) {
2020                                                 BKE_animdata_copy_id_action(&obn->mat[a]->id, true);
2021                                         }
2022                                 }
2023                         }
2024                 }
2025                 if (dupflag & USER_DUP_PSYS) {
2026                         ParticleSystem *psys;
2027                         for (psys = obn->particlesystem.first; psys; psys = psys->next) {
2028                                 id = (ID *) psys->part;
2029                                 if (id) {
2030                                         ID_NEW_REMAP_US(psys->part)
2031                                         else {
2032                                                 psys->part = ID_NEW_SET(psys->part, BKE_particlesettings_copy(bmain, psys->part));
2033                                         }
2034
2035                                         if (dupflag & USER_DUP_ACT) {
2036                                                 BKE_animdata_copy_id_action(&psys->part->id, true);
2037                                         }
2038
2039                                         id_us_min(id);
2040                                 }
2041                         }
2042                 }
2043
2044                 id = obn->data;
2045                 didit = 0;
2046
2047                 switch (obn->type) {
2048                         case OB_MESH:
2049                                 if (dupflag & USER_DUP_MESH) {
2050                                         ID_NEW_REMAP_US2(obn->data)
2051                                         else {
2052                                                 obn->data = ID_NEW_SET(obn->data, BKE_mesh_copy(bmain, obn->data));
2053                                                 didit = 1;
2054                                         }
2055                                         id_us_min(id);
2056                                 }
2057                                 break;
2058                         case OB_CURVE:
2059                                 if (dupflag & USER_DUP_CURVE) {
2060                                         ID_NEW_REMAP_US2(obn->data)
2061                                         else {
2062                                                 obn->data = ID_NEW_SET(obn->data, BKE_curve_copy(bmain, obn->data));
2063                                                 didit = 1;
2064                                         }
2065                                         id_us_min(id);
2066                                 }
2067                                 break;
2068                         case OB_SURF:
2069                                 if (dupflag & USER_DUP_SURF) {
2070                                         ID_NEW_REMAP_US2(obn->data)
2071                                         else {
2072                                                 obn->data = ID_NEW_SET(obn->data, BKE_curve_copy(bmain, obn->data));
2073                                                 didit = 1;
2074                                         }
2075                                         id_us_min(id);
2076                                 }
2077                                 break;
2078                         case OB_FONT:
2079                                 if (dupflag & USER_DUP_FONT) {
2080                                         ID_NEW_REMAP_US2(obn->data)
2081                                         else {
2082                                                 obn->data = ID_NEW_SET(obn->data, BKE_curve_copy(bmain, obn->data));
2083                                                 didit = 1;
2084                                         }
2085                                         id_us_min(id);
2086                                 }
2087                                 break;
2088                         case OB_MBALL:
2089                                 if (dupflag & USER_DUP_MBALL) {
2090                                         ID_NEW_REMAP_US2(obn->data)
2091                                         else {
2092                                                 obn->data = ID_NEW_SET(obn->data, BKE_mball_copy(bmain, obn->data));
2093                                                 didit = 1;
2094                                         }
2095                                         id_us_min(id);
2096                                 }
2097                                 break;
2098                         case OB_LAMP:
2099                                 if (dupflag & USER_DUP_LAMP) {
2100                                         ID_NEW_REMAP_US2(obn->data)
2101                                         else {
2102                                                 obn->data = ID_NEW_SET(obn->data, BKE_lamp_copy(bmain, obn->data));
2103                                                 didit = 1;
2104                                         }
2105                                         id_us_min(id);
2106                                 }
2107                                 break;
2108                         case OB_ARMATURE:
2109                                 DAG_id_tag_update(&obn->id, OB_RECALC_DATA);
2110                                 if (obn->pose)
2111                                         BKE_pose_tag_recalc(bmain, obn->pose);
2112                                 if (dupflag & USER_DUP_ARM) {
2113                                         ID_NEW_REMAP_US2(obn->data)
2114                                         else {
2115                                                 obn->data = ID_NEW_SET(obn->data, BKE_armature_copy(bmain, obn->data));
2116                                                 BKE_pose_rebuild(obn, obn->data);
2117                                                 didit = 1;
2118                                         }
2119                                         id_us_min(id);
2120                                 }
2121                                 break;
2122                         case OB_LATTICE:
2123                                 if (dupflag != 0) {
2124                                         ID_NEW_REMAP_US2(obn->data)
2125                                         else {
2126                                                 obn->data = ID_NEW_SET(obn->data, BKE_lattice_copy(bmain, obn->data));
2127                                                 didit = 1;
2128                                         }
2129                                         id_us_min(id);
2130                                 }
2131                                 break;
2132                         case OB_CAMERA:
2133                                 if (dupflag != 0) {
2134                                         ID_NEW_REMAP_US2(obn->data)
2135                                         else {
2136                                                 obn->data = ID_NEW_SET(obn->data, BKE_camera_copy(bmain, obn->data));
2137                                                 didit = 1;
2138                                         }
2139                                         id_us_min(id);
2140                                 }
2141                                 break;
2142                         case OB_SPEAKER:
2143                                 if (dupflag != 0) {
2144                                         ID_NEW_REMAP_US2(obn->data)
2145                                         else {
2146                                                 obn->data = ID_NEW_SET(obn->data, BKE_speaker_copy(bmain, obn->data));
2147                                                 didit = 1;
2148                                         }
2149                                         id_us_min(id);
2150                                 }
2151                                 break;
2152                 }
2153
2154                 /* check if obdata is copied */
2155                 if (didit) {
2156                         Key *key = BKE_key_from_object(obn);
2157
2158                         if (dupflag & USER_DUP_ACT) {
2159                                 bActuator *act;
2160
2161                                 BKE_animdata_copy_id_action((ID *)obn->data, true);
2162                                 if (key) {
2163                                         BKE_animdata_copy_id_action((ID *)key, true);
2164                                 }
2165
2166                                 /* Update the duplicated action in the action actuators */
2167                                 /* XXX TODO this code is all wrong! actact->act is user-refcounted (see readfile.c),
2168                                  * and what about other ID pointers of other BGE logic bricks,
2169                                  * and since this is object-level, why is it only ran if obdata was duplicated??? -mont29 */
2170                                 for (act = obn->actuators.first; act; act = act->next) {
2171                                         if (act->type == ACT_ACTION) {
2172                                                 bActionActuator *actact = (bActionActuator *) act->data;
2173                                                 if (ob->adt && actact->act == ob->adt->action) {
2174                                                         actact->act = obn->adt->action;
2175                                                 }
2176                                         }
2177                                 }
2178                         }
2179
2180                         if (dupflag & USER_DUP_MAT) {
2181                                 matarar = give_matarar(obn);
2182                                 if (matarar) {
2183                                         for (a = 0; a < obn->totcol; a++) {
2184                                                 id = (ID *)(*matarar)[a];
2185                                                 if (id) {
2186                                                         ID_NEW_REMAP_US((*matarar)[a])
2187                                                         else {
2188                                                                 (*matarar)[a] = ID_NEW_SET((*matarar)[a], BKE_material_copy(bmain, (*matarar)[a]));
2189                                                         }
2190                                                         id_us_min(id);
2191                                                 }
2192                                         }
2193                                 }
2194                         }
2195                 }
2196         }
2197         return basen;
2198
2199 #undef ID_NEW_REMAP_US
2200 #undef ID_NEW_REMAP_US2
2201 }
2202
2203 /* single object duplicate, if dupflag==0, fully linked, else it uses the flags given */
2204 /* leaves selection of base/object unaltered.
2205  * note: don't call this within a loop since clear_* funcs loop over the entire database.
2206  * note: caller must do DAG_relations_tag_update(bmain);
2207  *       this is not done automatic since we may duplicate many objects in a batch */
2208 Base *ED_object_add_duplicate(Main *bmain, Scene *scene, SceneLayer *sl, Base *base, int dupflag)
2209 {
2210         Base *basen;
2211         Object *ob;
2212
2213         clear_sca_new_poins();  /* BGE logic */
2214
2215         basen = object_add_duplicate_internal(bmain, scene, sl, base->object, dupflag);
2216         if (basen == NULL) {
2217                 return NULL;
2218         }
2219
2220         ob = basen->object;
2221
2222         /* link own references to the newly duplicated data [#26816] */
2223         BKE_libblock_relink_to_newid(&ob->id);
2224         set_sca_new_poins_ob(ob);
2225
2226         /* DAG_relations_tag_update(bmain); */ /* caller must do */
2227
2228         if (ob->data) {
2229                 ED_render_id_flush_update(bmain, ob->data);
2230         }
2231
2232         BKE_main_id_clear_newpoins(bmain);
2233
2234         return basen;
2235 }
2236
2237 /* contextual operator dupli */
2238 static int duplicate_exec(bContext *C, wmOperator *op)
2239 {
2240         Main *bmain = CTX_data_main(C);
2241         Scene *scene = CTX_data_scene(C);
2242         SceneLayer *sl = CTX_data_scene_layer(C);
2243         const bool linked = RNA_boolean_get(op->ptr, "linked");
2244         int dupflag = (linked) ? 0 : U.dupflag;
2245
2246         clear_sca_new_poins();  /* BGE logic */
2247
2248         CTX_DATA_BEGIN (C, Base *, base, selected_bases)
2249         {
2250                 Base *basen = object_add_duplicate_internal(bmain, scene, sl, base->object, dupflag);
2251
2252                 /* note that this is safe to do with this context iterator,
2253                  * the list is made in advance */
2254                 ED_object_base_select(base, BA_DESELECT);
2255
2256                 if (basen == NULL) {
2257                         continue;
2258                 }
2259
2260                 /* new object becomes active */
2261                 if (BASACT_NEW == base)
2262                         ED_object_base_activate(C, basen);
2263
2264                 if (basen->object->data) {
2265                         DAG_id_tag_update(basen->object->data, 0);
2266                 }
2267         }
2268         CTX_DATA_END;
2269
2270         copy_object_set_idnew(C);
2271
2272         BKE_main_id_clear_newpoins(bmain);
2273
2274         DAG_relations_tag_update(bmain);
2275
2276         WM_event_add_notifier(C, NC_SCENE | ND_OB_SELECT, scene);
2277
2278         return OPERATOR_FINISHED;
2279 }
2280
2281 void OBJECT_OT_duplicate(wmOperatorType *ot)
2282 {
2283         PropertyRNA *prop;
2284
2285         /* identifiers */
2286         ot->name = "Duplicate Objects";
2287         ot->description = "Duplicate selected objects";
2288         ot->idname = "OBJECT_OT_duplicate";
2289
2290         /* api callbacks */
2291         ot->exec = duplicate_exec;
2292         ot->poll = ED_operator_objectmode;
2293
2294         /* flags */
2295         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2296
2297         /* to give to transform */
2298         RNA_def_boolean(ot->srna, "linked", 0, "Linked", "Duplicate object but not object data, linking to the original data");
2299         prop = RNA_def_enum(ot->srna, "mode", rna_enum_transform_mode_types, TFM_TRANSLATION, "Mode", "");
2300         RNA_def_property_flag(prop, PROP_HIDDEN);
2301 }
2302
2303 /* **************** add named object, for dragdrop ************* */
2304
2305 static int add_named_exec(bContext *C, wmOperator *op)
2306 {
2307         wmWindow *win = CTX_wm_window(C);
2308         const wmEvent *event = win ? win->eventstate : NULL;
2309         Main *bmain = CTX_data_main(C);
2310         Scene *scene = CTX_data_scene(C);
2311         SceneLayer *sl = CTX_data_scene_layer(C);
2312         Base *basen;
2313         Object *ob;
2314         const bool linked = RNA_boolean_get(op->ptr, "linked");
2315         int dupflag = (linked) ? 0 : U.dupflag;
2316         char name[MAX_ID_NAME - 2];
2317
2318         /* find object, create fake base */
2319         RNA_string_get(op->ptr, "name", name);
2320         ob = (Object *)BKE_libblock_find_name(ID_OB, name);
2321
2322         if (ob == NULL) {
2323                 BKE_report(op->reports, RPT_ERROR, "Object not found");
2324                 return OPERATOR_CANCELLED;
2325         }
2326
2327         /* prepare dupli */
2328         clear_sca_new_poins();  /* BGE logic */
2329
2330         basen = object_add_duplicate_internal(bmain, scene, sl, ob, dupflag);
2331         BKE_scene_object_base_flag_sync_from_object(basen);
2332
2333         if (basen == NULL) {
2334                 BKE_report(op->reports, RPT_ERROR, "Object could not be duplicated");
2335                 return OPERATOR_CANCELLED;
2336         }
2337
2338         basen->object->restrictflag &= ~OB_RESTRICT_VIEW;
2339
2340         if (event) {
2341                 ARegion *ar = CTX_wm_region(C);
2342                 const int mval[2] = {event->x - ar->winrct.xmin,
2343                                      event->y - ar->winrct.ymin};
2344                 ED_object_location_from_view(C, basen->object->loc);
2345                 ED_view3d_cursor3d_position(C, basen->object->loc, mval);
2346         }
2347         
2348         ED_object_base_select(basen, BA_SELECT);
2349         ED_object_base_activate(C, basen);
2350
2351         copy_object_set_idnew(C);
2352
2353         BKE_main_id_clear_newpoins(bmain);
2354
2355         DAG_relations_tag_update(bmain);
2356
2357         WM_event_add_notifier(C, NC_SCENE | ND_OB_SELECT | ND_OB_ACTIVE, scene);
2358
2359         return OPERATOR_FINISHED;
2360 }
2361
2362 void OBJECT_OT_add_named(wmOperatorType *ot)
2363 {
2364         /* identifiers */
2365         ot->name = "Add Named Object";
2366         ot->description = "Add named object";
2367         ot->idname = "OBJECT_OT_add_named";
2368
2369         /* api callbacks */
2370         ot->exec = add_named_exec;
2371         ot->poll = ED_operator_objectmode;
2372
2373         /* flags */
2374         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2375
2376         RNA_def_boolean(ot->srna, "linked", 0, "Linked", "Duplicate object but not object data, linking to the original data");
2377         RNA_def_string(ot->srna, "name", NULL, MAX_ID_NAME - 2, "Name", "Object name to add");
2378 }
2379
2380 /**************************** Join *************************/
2381
2382 static int join_poll(bContext *C)
2383 {
2384         Object *ob = CTX_data_active_object(C);
2385
2386         if (!ob || ID_IS_LINKED_DATABLOCK(ob)) return 0;
2387
2388         if (ELEM(ob->type, OB_MESH, OB_CURVE, OB_SURF, OB_ARMATURE))
2389                 return ED_operator_screenactive(C);
2390         else
2391                 return 0;
2392 }
2393
2394 static int join_exec(bContext *C, wmOperator *op)
2395 {
2396         Scene *scene = CTX_data_scene(C);
2397         Object *ob = CTX_data_active_object(C);
2398
2399         if (scene->obedit) {
2400                 BKE_report(op->reports, RPT_ERROR, "This data does not support joining in edit mode");
2401                 return OPERATOR_CANCELLED;
2402         }
2403         else if (BKE_object_obdata_is_libdata(ob)) {
2404                 BKE_report(op->reports, RPT_ERROR, "Cannot edit external libdata");
2405                 return OPERATOR_CANCELLED;
2406         }
2407
2408         if (ob->type == OB_MESH)
2409                 return join_mesh_exec(C, op);
2410         else if (ELEM(ob->type, OB_CURVE, OB_SURF))
2411                 return join_curve_exec(C, op);
2412         else if (ob->type == OB_ARMATURE)
2413                 return join_armature_exec(C, op);
2414
2415         return OPERATOR_CANCELLED;
2416 }
2417
2418 void OBJECT_OT_join(wmOperatorType *ot)
2419 {
2420         /* identifiers */
2421         ot->name = "Join";
2422         ot->description = "Join selected objects into active object";
2423         ot->idname = "OBJECT_OT_join";
2424
2425         /* api callbacks */
2426         ot->exec = join_exec;
2427         ot->poll = join_poll;
2428
2429         /* flags */
2430         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2431 }
2432
2433 /**************************** Join as Shape Key*************************/
2434
2435 static int join_shapes_poll(bContext *C)
2436 {
2437         Object *ob = CTX_data_active_object(C);
2438
2439         if (!ob || ID_IS_LINKED_DATABLOCK(ob)) return 0;
2440
2441         /* only meshes supported at the moment */
2442         if (ob->type == OB_MESH)
2443                 return ED_operator_screenactive(C);
2444         else
2445                 return 0;
2446 }
2447
2448 static int join_shapes_exec(bContext *C, wmOperator *op)
2449 {
2450         Scene *scene = CTX_data_scene(C);
2451         Object *ob = CTX_data_active_object(C);
2452
2453         if (scene->obedit) {
2454                 BKE_report(op->reports, RPT_ERROR, "This data does not support joining in edit mode");
2455                 return OPERATOR_CANCELLED;
2456         }
2457         else if (BKE_object_obdata_is_libdata(ob)) {
2458                 BKE_report(op->reports, RPT_ERROR, "Cannot edit external libdata");
2459                 return OPERATOR_CANCELLED;
2460         }
2461
2462         if (ob->type == OB_MESH)
2463                 return join_mesh_shapes_exec(C, op);
2464
2465         return OPERATOR_CANCELLED;
2466 }
2467
2468 void OBJECT_OT_join_shapes(wmOperatorType *ot)
2469 {
2470         /* identifiers */
2471         ot->name = "Join as Shapes";
2472         ot->description = "Merge selected objects to shapes of active object";
2473         ot->idname = "OBJECT_OT_join_shapes";
2474
2475         /* api callbacks */
2476         ot->exec = join_shapes_exec;
2477         ot->poll = join_shapes_poll;
2478
2479         /* flags */
2480         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2481 }