2 * ***** BEGIN GPL LICENSE BLOCK *****
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.
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.
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.
18 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19 * All rights reserved.
21 * Contributor(s): Blender Foundation, 2002-2008 full recode
23 * ***** END GPL LICENSE BLOCK *****
26 /** \file blender/editors/object/object_edit.c
36 #include <stddef.h> //for offsetof
38 #include "MEM_guardedalloc.h"
40 #include "BLI_blenlib.h"
42 #include "BLI_utildefines.h"
43 #include "BLI_ghash.h"
44 #include "BLI_string_utils.h"
46 #include "BLT_translation.h"
48 #include "DNA_armature_types.h"
49 #include "DNA_curve_types.h"
50 #include "DNA_gpencil_types.h"
51 #include "DNA_group_types.h"
52 #include "DNA_material_types.h"
53 #include "DNA_meta_types.h"
54 #include "DNA_property_types.h"
55 #include "DNA_scene_types.h"
56 #include "DNA_object_types.h"
57 #include "DNA_object_force_types.h"
58 #include "DNA_meshdata_types.h"
59 #include "DNA_vfont_types.h"
60 #include "DNA_mesh_types.h"
61 #include "DNA_lattice_types.h"
63 #include "IMB_imbuf_types.h"
66 #include "BKE_constraint.h"
67 #include "BKE_context.h"
68 #include "BKE_curve.h"
69 #include "BKE_effect.h"
70 #include "BKE_depsgraph.h"
71 #include "BKE_global.h"
72 #include "BKE_image.h"
73 #include "BKE_lattice.h"
74 #include "BKE_library.h"
76 #include "BKE_material.h"
77 #include "BKE_mball.h"
79 #include "BKE_object.h"
80 #include "BKE_pointcache.h"
81 #include "BKE_property.h"
83 #include "BKE_softbody.h"
84 #include "BKE_modifier.h"
85 #include "BKE_editlattice.h"
86 #include "BKE_editmesh.h"
87 #include "BKE_report.h"
88 #include "BKE_undo_system.h"
90 #include "ED_armature.h"
94 #include "ED_lattice.h"
95 #include "ED_object.h"
96 #include "ED_screen.h"
100 #include "RNA_access.h"
101 #include "RNA_define.h"
102 #include "RNA_enum_types.h"
104 /* for menu/popup icons etc etc*/
106 #include "UI_interface.h"
108 #include "WM_types.h"
110 #include "object_intern.h" // own include
112 /* ************* XXX **************** */
113 static void error(const char *UNUSED(arg)) {}
114 static void waitcursor(int UNUSED(val)) {}
115 static int pupmenu(const char *UNUSED(msg)) { return 0; }
118 static void error_libdata(void) {}
120 Object *ED_object_context(bContext *C)
122 return CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
125 /* find the correct active object per context
126 * note: context can be NULL when called from a enum with PROP_ENUM_NO_CONTEXT */
127 Object *ED_object_active_context(bContext *C)
131 ob = ED_object_context(C);
132 if (!ob) ob = CTX_data_active_object(C);
138 /* ********* clear/set restrict view *********/
139 static int object_hide_view_clear_exec(bContext *C, wmOperator *op)
141 Main *bmain = CTX_data_main(C);
142 ScrArea *sa = CTX_wm_area(C);
143 View3D *v3d = sa->spacedata.first;
144 Scene *scene = CTX_data_scene(C);
146 bool changed = false;
147 const bool select = RNA_boolean_get(op->ptr, "select");
149 /* XXX need a context loop to handle such cases */
150 for (base = FIRSTBASE; base; base = base->next) {
151 if ((base->lay & v3d->lay) && base->object->restrictflag & OB_RESTRICT_VIEW) {
152 if (!(base->object->restrictflag & OB_RESTRICT_SELECT)) {
153 SET_FLAG_FROM_TEST(base->flag, select, SELECT);
155 base->object->flag = base->flag;
156 base->object->restrictflag &= ~OB_RESTRICT_VIEW;
161 DAG_id_type_tag(bmain, ID_OB);
162 DAG_relations_tag_update(bmain);
163 WM_event_add_notifier(C, NC_SCENE | ND_OB_SELECT, scene);
166 return OPERATOR_FINISHED;
169 void OBJECT_OT_hide_view_clear(wmOperatorType *ot)
173 ot->name = "Clear Restrict View";
174 ot->description = "Reveal the object by setting the hide flag";
175 ot->idname = "OBJECT_OT_hide_view_clear";
178 ot->exec = object_hide_view_clear_exec;
179 ot->poll = ED_operator_view3d_active;
182 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
184 RNA_def_boolean(ot->srna, "select", true, "Select", "");
187 static int object_hide_view_set_exec(bContext *C, wmOperator *op)
189 Main *bmain = CTX_data_main(C);
190 Scene *scene = CTX_data_scene(C);
191 bool changed = false;
192 const bool unselected = RNA_boolean_get(op->ptr, "unselected");
194 CTX_DATA_BEGIN(C, Base *, base, visible_bases)
197 if (base->flag & SELECT) {
198 base->flag &= ~SELECT;
199 base->object->flag = base->flag;
200 base->object->restrictflag |= OB_RESTRICT_VIEW;
202 if (base == BASACT) {
203 ED_base_object_activate(C, NULL);
208 if (!(base->flag & SELECT)) {
209 base->object->restrictflag |= OB_RESTRICT_VIEW;
211 if (base == BASACT) {
212 ED_base_object_activate(C, NULL);
220 DAG_id_type_tag(bmain, ID_OB);
221 DAG_relations_tag_update(bmain);
223 WM_event_add_notifier(C, NC_SCENE | ND_OB_SELECT, scene);
227 return OPERATOR_FINISHED;
230 void OBJECT_OT_hide_view_set(wmOperatorType *ot)
233 ot->name = "Set Restrict View";
234 ot->description = "Hide the object by setting the hide flag";
235 ot->idname = "OBJECT_OT_hide_view_set";
238 ot->exec = object_hide_view_set_exec;
239 ot->poll = ED_operator_view3d_active;
242 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
244 RNA_def_boolean(ot->srna, "unselected", 0, "Unselected", "Hide unselected rather than selected objects");
248 /* 99% same as above except no need for scene refreshing (TODO, update render preview) */
249 static int object_hide_render_clear_exec(bContext *C, wmOperator *UNUSED(op))
251 bool changed = false;
253 /* XXX need a context loop to handle such cases */
254 CTX_DATA_BEGIN(C, Object *, ob, selected_editable_objects)
256 if (ob->restrictflag & OB_RESTRICT_RENDER) {
257 ob->restrictflag &= ~OB_RESTRICT_RENDER;
264 WM_event_add_notifier(C, NC_SPACE | ND_SPACE_OUTLINER, NULL);
266 return OPERATOR_FINISHED;
269 void OBJECT_OT_hide_render_clear(wmOperatorType *ot)
273 ot->name = "Clear Restrict Render";
274 ot->description = "Reveal the render object by setting the hide render flag";
275 ot->idname = "OBJECT_OT_hide_render_clear";
278 ot->exec = object_hide_render_clear_exec;
279 ot->poll = ED_operator_view3d_active;
282 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
285 static int object_hide_render_set_exec(bContext *C, wmOperator *op)
287 const bool unselected = RNA_boolean_get(op->ptr, "unselected");
289 CTX_DATA_BEGIN(C, Base *, base, visible_bases)
292 if (base->flag & SELECT) {
293 base->object->restrictflag |= OB_RESTRICT_RENDER;
297 if (!(base->flag & SELECT)) {
298 base->object->restrictflag |= OB_RESTRICT_RENDER;
303 WM_event_add_notifier(C, NC_SPACE | ND_SPACE_OUTLINER, NULL);
304 return OPERATOR_FINISHED;
307 void OBJECT_OT_hide_render_set(wmOperatorType *ot)
310 ot->name = "Set Restrict Render";
311 ot->description = "Hide the render object by setting the hide render flag";
312 ot->idname = "OBJECT_OT_hide_render_set";
315 ot->exec = object_hide_render_set_exec;
316 ot->poll = ED_operator_view3d_active;
319 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
321 RNA_def_boolean(ot->srna, "unselected", 0, "Unselected", "Hide unselected rather than selected objects");
324 /* ******************* toggle editmode operator ***************** */
326 static bool mesh_needs_keyindex(Main *bmain, const Mesh *me)
329 return false; /* will be added */
332 for (const Object *ob = bmain->object.first; ob; ob = ob->id.next) {
333 if ((ob->parent) && (ob->parent->data == me) && ELEM(ob->partype, PARVERT1, PARVERT3)) {
336 if (ob->data == me) {
337 for (const ModifierData *md = ob->modifiers.first; md; md = md->next) {
338 if (md->type == eModifierType_Hook) {
348 * Load EditMode data back into the object,
349 * optionally freeing the editmode data.
351 static bool ED_object_editmode_load_ex(Main *bmain, Object *obedit, const bool freedata)
353 if (obedit == NULL) {
357 if (obedit->type == OB_MESH) {
358 Mesh *me = obedit->data;
359 if (me->edit_btmesh == NULL) {
363 if (me->edit_btmesh->bm->totvert > MESH_MAX_VERTS) {
364 error("Too many vertices");
368 EDBM_mesh_load(obedit);
371 EDBM_mesh_free(me->edit_btmesh);
372 MEM_freeN(me->edit_btmesh);
373 me->edit_btmesh = NULL;
375 /* will be recalculated as needed. */
377 ED_mesh_mirror_spatial_table(NULL, NULL, NULL, NULL, 'e');
378 ED_mesh_mirror_topo_table(NULL, NULL, 'e');
381 else if (obedit->type == OB_ARMATURE) {
382 const bArmature *arm = obedit->data;
383 if (arm->edbo == NULL) {
386 ED_armature_from_edit(bmain, obedit->data);
388 ED_armature_edit_free(obedit->data);
390 /* TODO(sergey): Pose channels might have been changed, so need
391 * to inform dependency graph about this. But is it really the
392 * best place to do this?
394 DAG_relations_tag_update(bmain);
396 else if (ELEM(obedit->type, OB_CURVE, OB_SURF)) {
397 const Curve *cu = obedit->data;
398 if (cu->editnurb == NULL) {
401 ED_curve_editnurb_load(bmain, obedit);
403 ED_curve_editnurb_free(obedit);
406 else if (obedit->type == OB_FONT) {
407 const Curve *cu = obedit->data;
408 if (cu->editfont == NULL) {
411 ED_curve_editfont_load(obedit);
413 ED_curve_editfont_free(obedit);
416 else if (obedit->type == OB_LATTICE) {
417 const Lattice *lt = obedit->data;
418 if (lt->editlatt == NULL) {
421 BKE_editlattice_load(obedit);
423 BKE_editlattice_free(obedit);
426 else if (obedit->type == OB_MBALL) {
427 const MetaBall *mb = obedit->data;
428 if (mb->editelems == NULL) {
431 ED_mball_editmball_load(obedit);
433 ED_mball_editmball_free(obedit);
440 bool ED_object_editmode_load(Main *bmain, Object *obedit)
442 return ED_object_editmode_load_ex(bmain, obedit, false);
447 * - If #EM_FREEDATA isn't in the flag, use ED_object_editmode_load directly.
449 bool ED_object_editmode_exit_ex(Main *bmain, Scene *scene, Object *obedit, int flag)
451 const bool freedata = (flag & EM_FREEDATA) != 0;
453 if (flag & EM_WAITCURSOR) waitcursor(1);
455 if (ED_object_editmode_load_ex(bmain, obedit, freedata) == false) {
456 /* in rare cases (background mode) its possible active object
457 * is flagged for editmode, without 'obedit' being set [#35489] */
458 if (UNLIKELY(scene->basact && (scene->basact->object->mode & OB_MODE_EDIT))) {
459 scene->basact->object->mode &= ~OB_MODE_EDIT;
461 if (flag & EM_WAITCURSOR) waitcursor(0);
465 /* freedata only 0 now on file saves and render */
470 /* for example; displist make is different in editmode */
471 scene->obedit = NULL; // XXX for context
473 /* flag object caches as outdated */
474 BKE_ptcache_ids_from_object(&pidlist, obedit, scene, 0);
475 for (pid = pidlist.first; pid; pid = pid->next) {
476 if (pid->type != PTCACHE_TYPE_PARTICLES) /* particles don't need reset on geometry change */
477 pid->cache->flag |= PTCACHE_OUTDATED;
479 BLI_freelistN(&pidlist);
481 BKE_ptcache_object_reset(scene, obedit, PTCACHE_RESET_OUTDATED);
483 /* also flush ob recalc, doesn't take much overhead, but used for particles */
484 DAG_id_tag_update(&obedit->id, OB_RECALC_OB | OB_RECALC_DATA);
486 WM_main_add_notifier(NC_SCENE | ND_MODE | NS_MODE_OBJECT, scene);
488 obedit->mode &= ~OB_MODE_EDIT;
491 if (flag & EM_WAITCURSOR) waitcursor(0);
493 return (obedit->mode & OB_MODE_EDIT) == 0;
496 bool ED_object_editmode_exit(bContext *C, int flag)
498 Main *bmain = CTX_data_main(C);
499 Scene *scene = CTX_data_scene(C);
500 Object *obedit = CTX_data_edit_object(C);
501 return ED_object_editmode_exit_ex(bmain, scene, obedit, flag);
504 bool ED_object_editmode_enter(bContext *C, int flag)
506 Main *bmain = CTX_data_main(C);
507 Scene *scene = CTX_data_scene(C);
510 ScrArea *sa = CTX_wm_area(C);
514 if (ID_IS_LINKED(scene)) {
518 if (sa && sa->spacetype == SPACE_VIEW3D)
519 v3d = sa->spacedata.first;
521 if ((flag & EM_IGNORE_LAYER) == 0) {
522 base = CTX_data_active_base(C); /* active layer checked here for view3d */
524 if ((base == NULL) ||
525 (v3d && (base->lay & v3d->lay) == 0) ||
526 (!v3d && (base->lay & scene->lay) == 0))
532 base = scene->basact;
535 if (ELEM(NULL, base, base->object, base->object->data)) {
541 /* this checks actual object->data, for cases when other scenes have it in editmode context */
542 if (BKE_object_is_in_editmode(ob)) {
546 if (BKE_object_obdata_is_libdata(ob)) {
551 if (flag & EM_WAITCURSOR) waitcursor(1);
553 ob->restore_mode = ob->mode;
555 /* note, when switching scenes the object can have editmode data but
556 * not be scene->obedit: bug 22954, this avoids calling self eternally */
557 if ((ob->restore_mode & OB_MODE_EDIT) == 0)
558 ED_object_mode_toggle(C, ob->mode);
560 ob->mode = OB_MODE_EDIT;
562 if (ob->type == OB_MESH) {
565 scene->obedit = ob; /* context sees this */
567 const bool use_key_index = mesh_needs_keyindex(bmain, ob->data);
569 EDBM_mesh_make(ob, scene->toolsettings->selectmode, use_key_index);
571 em = BKE_editmesh_from_object(ob);
573 /* order doesn't matter */
574 EDBM_mesh_normals_update(em);
575 BKE_editmesh_tessface_calc(em);
578 WM_event_add_notifier(C, NC_SCENE | ND_MODE | NS_EDITMODE_MESH, scene);
580 else if (ob->type == OB_ARMATURE) {
583 ED_armature_to_edit(ob->data);
584 /* to ensure all goes in restposition and without striding */
585 DAG_id_tag_update(&ob->id, OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME); /* XXX: should this be OB_RECALC_DATA? */
587 WM_event_add_notifier(C, NC_SCENE | ND_MODE | NS_EDITMODE_ARMATURE, scene);
589 else if (ob->type == OB_FONT) {
590 scene->obedit = ob; /* XXX for context */
592 ED_curve_editfont_make(ob);
594 WM_event_add_notifier(C, NC_SCENE | ND_MODE | NS_EDITMODE_TEXT, scene);
596 else if (ob->type == OB_MBALL) {
597 scene->obedit = ob; /* XXX for context */
599 ED_mball_editmball_make(ob);
601 WM_event_add_notifier(C, NC_SCENE | ND_MODE | NS_EDITMODE_MBALL, scene);
603 else if (ob->type == OB_LATTICE) {
604 scene->obedit = ob; /* XXX for context */
606 BKE_editlattice_make(ob);
608 WM_event_add_notifier(C, NC_SCENE | ND_MODE | NS_EDITMODE_LATTICE, scene);
610 else if (ob->type == OB_SURF || ob->type == OB_CURVE) {
612 scene->obedit = ob; /* XXX for context */
613 ED_curve_editnurb_make(ob);
615 WM_event_add_notifier(C, NC_SCENE | ND_MODE | NS_EDITMODE_CURVE, scene);
619 DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
622 scene->obedit = NULL; /* XXX for context */
623 ob->mode &= ~OB_MODE_EDIT;
624 WM_event_add_notifier(C, NC_SCENE | ND_MODE | NS_MODE_OBJECT, scene);
627 if (flag & EM_WAITCURSOR) waitcursor(0);
629 return (ob->mode & OB_MODE_EDIT) != 0;
632 static int editmode_toggle_exec(bContext *C, wmOperator *op)
634 Main *bmain = CTX_data_main(C);
635 const int mode_flag = OB_MODE_EDIT;
636 const bool is_mode_set = (CTX_data_edit_object(C) != NULL);
637 Scene *scene = CTX_data_scene(C);
640 Object *ob = CTX_data_active_object(C);
641 if (!ED_object_mode_compat_set(C, ob, mode_flag, op->reports)) {
642 return OPERATOR_CANCELLED;
647 ED_object_editmode_enter(C, EM_WAITCURSOR);
650 ED_object_editmode_exit(C, EM_FREEDATA | EM_WAITCURSOR);
652 ED_space_image_uv_sculpt_update(bmain, CTX_wm_manager(C), scene);
654 return OPERATOR_FINISHED;
657 static int editmode_toggle_poll(bContext *C)
659 Object *ob = CTX_data_active_object(C);
661 /* covers proxies too */
662 if (ELEM(NULL, ob, ob->data) || ID_IS_LINKED(ob->data))
665 /* if hidden but in edit mode, we still display */
666 if ((ob->restrictflag & OB_RESTRICT_VIEW) && !(ob->mode & OB_MODE_EDIT))
669 return OB_TYPE_SUPPORT_EDITMODE(ob->type);
672 void OBJECT_OT_editmode_toggle(wmOperatorType *ot)
676 ot->name = "Toggle Editmode";
677 ot->description = "Toggle object's editmode";
678 ot->idname = "OBJECT_OT_editmode_toggle";
681 ot->exec = editmode_toggle_exec;
682 ot->poll = editmode_toggle_poll;
685 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
688 /* *************************** */
690 static int posemode_exec(bContext *C, wmOperator *op)
692 Base *base = CTX_data_active_base(C);
693 Object *ob = base->object;
694 const int mode_flag = OB_MODE_POSE;
695 bool is_mode_set = (ob->mode & mode_flag) != 0;
698 if (!ED_object_mode_compat_set(C, ob, mode_flag, op->reports)) {
699 return OPERATOR_CANCELLED;
703 if (ob->type == OB_ARMATURE) {
704 if (ob == CTX_data_edit_object(C)) {
705 ED_object_editmode_exit(C, EM_FREEDATA);
710 ED_object_posemode_exit(C, ob);
713 ED_object_posemode_enter(C, ob);
716 return OPERATOR_FINISHED;
719 return OPERATOR_PASS_THROUGH;
722 void OBJECT_OT_posemode_toggle(wmOperatorType *ot)
725 ot->name = "Toggle Pose Mode";
726 ot->idname = "OBJECT_OT_posemode_toggle";
727 ot->description = "Enable or disable posing/selecting bones";
730 ot->exec = posemode_exec;
731 ot->poll = ED_operator_object_active_editable;
734 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
737 static void copymenu_properties(Scene *scene, View3D *v3d, Object *ob)
739 //XXX no longer used - to be removed - replaced by game_properties_copy_exec
745 prop = ob->prop.first;
751 str = MEM_callocN(50 + 33 * tot, "copymenu prop");
754 strcpy(str, "Copy Property %t|Replace All|Merge All|%l");
756 strcpy(str, "Copy Property %t|Clear All (no properties on active)");
759 prop = ob->prop.first;
763 strcat(str, prop->name);
769 if (nr == 1 || nr == 2) {
770 for (base = FIRSTBASE; base; base = base->next) {
771 if ((base != BASACT) && (TESTBASELIB(v3d, base))) {
772 if (nr == 1) { /* replace */
773 BKE_bproperty_copy_list(&base->object->prop, &ob->prop);
776 for (prop = ob->prop.first; prop; prop = prop->next) {
777 BKE_bproperty_object_set(base->object, prop);
784 prop = BLI_findlink(&ob->prop, nr - 4); /* account for first 3 menu items & menu index starting at 1*/
787 for (base = FIRSTBASE; base; base = base->next) {
788 if ((base != BASACT) && (TESTBASELIB(v3d, base))) {
789 BKE_bproperty_object_set(base->object, prop);
798 static void copymenu_logicbricks(Scene *scene, View3D *v3d, Object *ob)
800 //XXX no longer used - to be removed - replaced by logicbricks_copy_exec
803 for (base = FIRSTBASE; base; base = base->next) {
804 if (base->object != ob) {
805 if (TESTBASELIB(v3d, base)) {
807 /* first: free all logic */
808 free_sensors(&base->object->sensors);
809 unlink_controllers(&base->object->controllers);
810 free_controllers(&base->object->controllers);
811 unlink_actuators(&base->object->actuators);
812 free_actuators(&base->object->actuators);
814 /* now copy it, this also works without logicbricks! */
815 clear_sca_new_poins_ob(ob);
816 copy_sensors(&base->object->sensors, &ob->sensors, 0);
817 copy_controllers(&base->object->controllers, &ob->controllers, 0);
818 copy_actuators(&base->object->actuators, &ob->actuators, 0);
819 set_sca_new_poins_ob(base->object);
821 /* some menu settings */
822 base->object->scavisflag = ob->scavisflag;
823 base->object->scaflag = ob->scaflag;
825 /* set the initial state */
826 base->object->state = ob->state;
827 base->object->init_state = ob->init_state;
833 /* both pointers should exist */
834 static void copy_texture_space(Object *to, Object *ob)
836 float *poin1 = NULL, *poin2 = NULL;
839 if (ob->type == OB_MESH) {
840 texflag = ((Mesh *)ob->data)->texflag;
841 poin2 = ((Mesh *)ob->data)->loc;
843 else if (ELEM(ob->type, OB_CURVE, OB_SURF, OB_FONT)) {
844 texflag = ((Curve *)ob->data)->texflag;
845 poin2 = ((Curve *)ob->data)->loc;
847 else if (ob->type == OB_MBALL) {
848 texflag = ((MetaBall *)ob->data)->texflag;
849 poin2 = ((MetaBall *)ob->data)->loc;
854 if (to->type == OB_MESH) {
855 ((Mesh *)to->data)->texflag = texflag;
856 poin1 = ((Mesh *)to->data)->loc;
858 else if (ELEM(to->type, OB_CURVE, OB_SURF, OB_FONT)) {
859 ((Curve *)to->data)->texflag = texflag;
860 poin1 = ((Curve *)to->data)->loc;
862 else if (to->type == OB_MBALL) {
863 ((MetaBall *)to->data)->texflag = texflag;
864 poin1 = ((MetaBall *)to->data)->loc;
869 memcpy(poin1, poin2, 9 * sizeof(float)); /* this was noted in DNA_mesh, curve, mball */
871 if (to->type == OB_MESH) {
874 else if (to->type == OB_MBALL) {
875 BKE_mball_texspace_calc(to);
878 BKE_curve_texspace_calc(to->data);
883 /* UNUSED, keep in case we want to copy functionality for use elsewhere */
884 static void copy_attr(Main *bmain, Scene *scene, View3D *v3d, short event)
890 bool do_depgraph_update = false;
892 if (ID_IS_LINKED(scene)) return;
894 if (!(ob = OBACT)) return;
896 if (scene->obedit) { // XXX get from context
897 /* obedit_copymenu(); */
901 copymenu_properties(scene, v3d, ob);
904 else if (event == 10) {
905 copymenu_logicbricks(scene, v3d, ob);
908 else if (event == 24) {
909 /* moved to BKE_object_link_modifiers */
910 /* copymenu_modifiers(bmain, scene, v3d, ob); */
914 for (base = FIRSTBASE; base; base = base->next) {
915 if (base != BASACT) {
916 if (TESTBASELIB(v3d, base)) {
917 DAG_id_tag_update(&base->object->id, OB_RECALC_DATA);
919 if (event == 1) { /* loc */
920 copy_v3_v3(base->object->loc, ob->loc);
921 copy_v3_v3(base->object->dloc, ob->dloc);
923 else if (event == 2) { /* rot */
924 copy_v3_v3(base->object->rot, ob->rot);
925 copy_v3_v3(base->object->drot, ob->drot);
927 copy_qt_qt(base->object->quat, ob->quat);
928 copy_qt_qt(base->object->dquat, ob->dquat);
930 else if (event == 3) { /* size */
931 copy_v3_v3(base->object->size, ob->size);
932 copy_v3_v3(base->object->dscale, ob->dscale);
934 else if (event == 4) { /* drawtype */
935 base->object->dt = ob->dt;
936 base->object->dtx = ob->dtx;
937 base->object->empty_drawtype = ob->empty_drawtype;
938 base->object->empty_drawsize = ob->empty_drawsize;
940 else if (event == 5) { /* time offs */
941 base->object->sf = ob->sf;
943 else if (event == 6) { /* dupli */
944 base->object->dupon = ob->dupon;
945 base->object->dupoff = ob->dupoff;
946 base->object->dupsta = ob->dupsta;
947 base->object->dupend = ob->dupend;
949 base->object->transflag &= ~OB_DUPLI;
950 base->object->transflag |= (ob->transflag & OB_DUPLI);
952 base->object->dup_group = ob->dup_group;
954 id_us_plus(&ob->dup_group->id);
956 else if (event == 7) { /* mass */
957 base->object->mass = ob->mass;
959 else if (event == 8) { /* damping */
960 base->object->damping = ob->damping;
961 base->object->rdamping = ob->rdamping;
963 else if (event == 11) { /* all physical attributes */
964 base->object->gameflag = ob->gameflag;
965 base->object->inertia = ob->inertia;
966 base->object->formfactor = ob->formfactor;
967 base->object->damping = ob->damping;
968 base->object->rdamping = ob->rdamping;
969 base->object->min_vel = ob->min_vel;
970 base->object->max_vel = ob->max_vel;
971 base->object->min_angvel = ob->min_angvel;
972 base->object->max_angvel = ob->max_angvel;
973 if (ob->gameflag & OB_BOUNDS) {
974 base->object->collision_boundtype = ob->collision_boundtype;
976 base->object->margin = ob->margin;
977 base->object->bsoft = copy_bulletsoftbody(ob->bsoft, 0);
980 else if (event == 17) { /* tex space */
981 copy_texture_space(base->object, ob);
983 else if (event == 18) { /* font settings */
985 if (base->object->type == ob->type) {
987 cu1 = base->object->data;
989 cu1->spacemode = cu->spacemode;
990 cu1->align_y = cu->align_y;
991 cu1->spacing = cu->spacing;
992 cu1->linedist = cu->linedist;
993 cu1->shear = cu->shear;
994 cu1->fsize = cu->fsize;
997 cu1->textoncurve = cu->textoncurve;
998 cu1->wordspace = cu->wordspace;
999 cu1->ulpos = cu->ulpos;
1000 cu1->ulheight = cu->ulheight;
1002 id_us_min(&cu1->vfont->id);
1003 cu1->vfont = cu->vfont;
1004 id_us_plus((ID *)cu1->vfont);
1006 id_us_min(&cu1->vfontb->id);
1007 cu1->vfontb = cu->vfontb;
1008 id_us_plus((ID *)cu1->vfontb);
1010 id_us_min(&cu1->vfonti->id);
1011 cu1->vfonti = cu->vfonti;
1012 id_us_plus((ID *)cu1->vfonti);
1014 id_us_min(&cu1->vfontbi->id);
1015 cu1->vfontbi = cu->vfontbi;
1016 id_us_plus((ID *)cu1->vfontbi);
1018 BLI_strncpy(cu1->family, cu->family, sizeof(cu1->family));
1020 DAG_id_tag_update(&base->object->id, OB_RECALC_DATA);
1023 else if (event == 19) { /* bevel settings */
1025 if (ELEM(base->object->type, OB_CURVE, OB_FONT)) {
1027 cu1 = base->object->data;
1029 cu1->bevobj = cu->bevobj;
1030 cu1->taperobj = cu->taperobj;
1031 cu1->width = cu->width;
1032 cu1->bevresol = cu->bevresol;
1033 cu1->ext1 = cu->ext1;
1034 cu1->ext2 = cu->ext2;
1036 DAG_id_tag_update(&base->object->id, OB_RECALC_DATA);
1039 else if (event == 25) { /* curve resolution */
1041 if (ELEM(base->object->type, OB_CURVE, OB_FONT)) {
1043 cu1 = base->object->data;
1045 cu1->resolu = cu->resolu;
1046 cu1->resolu_ren = cu->resolu_ren;
1048 nu = cu1->nurb.first;
1051 nu->resolu = cu1->resolu;
1055 DAG_id_tag_update(&base->object->id, OB_RECALC_DATA);
1058 else if (event == 21) {
1059 if (base->object->type == OB_MESH) {
1060 ModifierData *md = modifiers_findByType(ob, eModifierType_Subsurf);
1063 ModifierData *tmd = modifiers_findByType(base->object, eModifierType_Subsurf);
1066 tmd = modifier_new(eModifierType_Subsurf);
1067 BLI_addtail(&base->object->modifiers, tmd);
1070 modifier_copyData(md, tmd);
1071 DAG_id_tag_update(&base->object->id, OB_RECALC_DATA);
1075 else if (event == 22) {
1076 /* Copy the constraint channels over */
1077 BKE_constraints_copy(&base->object->constraints, &ob->constraints, true);
1079 do_depgraph_update = true;
1081 else if (event == 23) {
1082 base->object->softflag = ob->softflag;
1083 if (base->object->soft) sbFree(base->object->soft);
1085 base->object->soft = copy_softbody(ob->soft, 0);
1087 if (!modifiers_findByType(base->object, eModifierType_Softbody)) {
1088 BLI_addhead(&base->object->modifiers, modifier_new(eModifierType_Softbody));
1091 else if (event == 26) {
1092 #if 0 // XXX old animation system
1093 BKE_nlastrip_copy(s(&base->object->nlastrips, &ob->nlastrips);
1094 #endif // XXX old animation system
1096 else if (event == 27) { /* autosmooth */
1097 if (base->object->type == OB_MESH) {
1098 Mesh *me = ob->data;
1099 Mesh *cme = base->object->data;
1100 cme->smoothresh = me->smoothresh;
1101 if (me->flag & ME_AUTOSMOOTH)
1102 cme->flag |= ME_AUTOSMOOTH;
1104 cme->flag &= ~ME_AUTOSMOOTH;
1107 else if (event == 28) { /* UV orco */
1108 if (ELEM(base->object->type, OB_CURVE, OB_SURF)) {
1110 cu1 = base->object->data;
1112 if (cu->flag & CU_UV_ORCO)
1113 cu1->flag |= CU_UV_ORCO;
1115 cu1->flag &= ~CU_UV_ORCO;
1118 else if (event == 29) { /* protected bits */
1119 base->object->protectflag = ob->protectflag;
1121 else if (event == 30) { /* index object */
1122 base->object->index = ob->index;
1124 else if (event == 31) { /* object color */
1125 copy_v4_v4(base->object->col, ob->col);
1131 if (do_depgraph_update)
1132 DAG_relations_tag_update(bmain);
1135 static void UNUSED_FUNCTION(copy_attr_menu) (Main *bmain, Scene *scene, View3D *v3d)
1141 if (!(ob = OBACT)) return;
1143 if (scene->obedit) { /* XXX get from context */
1144 /* if (ob->type == OB_MESH) */
1145 /* XXX mesh_copy_menu(); */
1151 /* If you change this menu, don't forget to update the menu in header_view3d.c
1152 * view3d_edit_object_copyattrmenu() and in toolbox.c
1156 "Copy Attributes %t|Location %x1|Rotation %x2|Size %x3|Draw Options %x4|"
1157 "Time Offset %x5|Dupli %x6|Object Color %x31|%l|Mass %x7|Damping %x8|All Physical Attributes %x11|Properties %x9|"
1158 "Logic Bricks %x10|Protected Transform %x29|%l");
1160 strcat(str, "|Object Constraints %x22");
1161 strcat(str, "|NLA Strips %x26");
1163 /* XXX if (OB_TYPE_SUPPORT_MATERIAL(ob->type)) { */
1164 /* strcat(str, "|Texture Space %x17"); */
1167 if (ob->type == OB_FONT) strcat(str, "|Font Settings %x18|Bevel Settings %x19");
1168 if (ob->type == OB_CURVE) strcat(str, "|Bevel Settings %x19|UV Orco %x28");
1170 if ((ob->type == OB_FONT) || (ob->type == OB_CURVE)) {
1171 strcat(str, "|Curve Resolution %x25");
1174 if (ob->type == OB_MESH) {
1175 strcat(str, "|Subsurf Settings %x21|AutoSmooth %x27");
1178 if (ob->soft) strcat(str, "|Soft Body Settings %x23");
1180 strcat(str, "|Pass Index %x30");
1182 if (ob->type == OB_MESH || ob->type == OB_CURVE || ob->type == OB_LATTICE || ob->type == OB_SURF) {
1183 strcat(str, "|Modifiers ... %x24");
1186 event = pupmenu(str);
1187 if (event <= 0) return;
1189 copy_attr(bmain, scene, v3d, event);
1192 /* ******************* force field toggle operator ***************** */
1194 void ED_object_check_force_modifiers(Main *bmain, Scene *scene, Object *object)
1196 PartDeflect *pd = object->pd;
1197 ModifierData *md = modifiers_findByType(object, eModifierType_Surface);
1199 /* add/remove modifier as needed */
1201 if (pd && (pd->shape == PFIELD_SHAPE_SURFACE) && !ELEM(pd->forcefield, 0, PFIELD_GUIDE, PFIELD_TEXTURE)) {
1202 if (ELEM(object->type, OB_MESH, OB_SURF, OB_FONT, OB_CURVE)) {
1203 ED_object_modifier_add(NULL, bmain, scene, object, NULL, eModifierType_Surface);
1208 if (!pd || (pd->shape != PFIELD_SHAPE_SURFACE) || ELEM(pd->forcefield, 0, PFIELD_GUIDE, PFIELD_TEXTURE)) {
1209 ED_object_modifier_remove(NULL, bmain, object, md);
1214 static int forcefield_toggle_exec(bContext *C, wmOperator *UNUSED(op))
1216 Object *ob = CTX_data_active_object(C);
1219 ob->pd = object_add_collision_fields(PFIELD_FORCE);
1220 else if (ob->pd->forcefield == 0)
1221 ob->pd->forcefield = PFIELD_FORCE;
1223 ob->pd->forcefield = 0;
1225 ED_object_check_force_modifiers(CTX_data_main(C), CTX_data_scene(C), ob);
1226 WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, ob);
1227 WM_event_add_notifier(C, NC_OBJECT | ND_MODIFIER, ob);
1229 return OPERATOR_FINISHED;
1232 void OBJECT_OT_forcefield_toggle(wmOperatorType *ot)
1236 ot->name = "Toggle Force Field";
1237 ot->description = "Toggle object's force field";
1238 ot->idname = "OBJECT_OT_forcefield_toggle";
1241 ot->exec = forcefield_toggle_exec;
1242 ot->poll = ED_operator_object_active_editable;
1245 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1248 /* ********************************************** */
1251 /* For the objects with animation: update paths for those that have got them
1252 * This should selectively update paths that exist...
1254 * To be called from various tools that do incremental updates
1256 void ED_objects_recalculate_paths(bContext *C, Scene *scene)
1258 Main *bmain = CTX_data_main(C);
1259 ListBase targets = {NULL, NULL};
1261 /* loop over objects in scene */
1262 CTX_DATA_BEGIN(C, Object *, ob, selected_editable_objects)
1264 /* set flag to force recalc, then grab path(s) from object */
1265 ob->avs.recalc |= ANIMVIZ_RECALC_PATHS;
1266 animviz_get_object_motionpaths(ob, &targets);
1270 /* recalculate paths, then free */
1271 animviz_calc_motionpaths(bmain, scene, &targets);
1272 BLI_freelistN(&targets);
1276 /* show popup to determine settings */
1277 static int object_calculate_paths_invoke(bContext *C, wmOperator *op, const wmEvent *UNUSED(event))
1279 Object *ob = CTX_data_active_object(C);
1282 return OPERATOR_CANCELLED;
1284 /* set default settings from existing/stored settings */
1286 bAnimVizSettings *avs = &ob->avs;
1288 RNA_int_set(op->ptr, "start_frame", avs->path_sf);
1289 RNA_int_set(op->ptr, "end_frame", avs->path_ef);
1292 /* show popup dialog to allow editing of range... */
1293 /* FIXME: hardcoded dimensions here are just arbitrary */
1294 return WM_operator_props_dialog_popup(C, op, 10 * UI_UNIT_X, 10 * UI_UNIT_Y);
1297 /* Calculate/recalculate whole paths (avs.path_sf to avs.path_ef) */
1298 static int object_calculate_paths_exec(bContext *C, wmOperator *op)
1300 Scene *scene = CTX_data_scene(C);
1301 int start = RNA_int_get(op->ptr, "start_frame");
1302 int end = RNA_int_get(op->ptr, "end_frame");
1304 /* set up path data for bones being calculated */
1305 CTX_DATA_BEGIN(C, Object *, ob, selected_editable_objects)
1307 bAnimVizSettings *avs = &ob->avs;
1309 /* grab baking settings from operator settings */
1310 avs->path_sf = start;
1313 /* verify that the selected object has the appropriate settings */
1314 animviz_verify_motionpaths(op->reports, scene, ob, NULL);
1318 /* calculate the paths for objects that have them (and are tagged to get refreshed) */
1319 ED_objects_recalculate_paths(C, scene);
1321 /* notifiers for updates */
1322 WM_event_add_notifier(C, NC_OBJECT | ND_TRANSFORM, NULL);
1324 return OPERATOR_FINISHED;
1327 void OBJECT_OT_paths_calculate(wmOperatorType *ot)
1330 ot->name = "Calculate Object Paths";
1331 ot->idname = "OBJECT_OT_paths_calculate";
1332 ot->description = "Calculate motion paths for the selected objects";
1335 ot->invoke = object_calculate_paths_invoke;
1336 ot->exec = object_calculate_paths_exec;
1337 ot->poll = ED_operator_object_active_editable;
1340 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1343 RNA_def_int(ot->srna, "start_frame", 1, MINAFRAME, MAXFRAME, "Start",
1344 "First frame to calculate object paths on", MINFRAME, MAXFRAME / 2.0);
1345 RNA_def_int(ot->srna, "end_frame", 250, MINAFRAME, MAXFRAME, "End",
1346 "Last frame to calculate object paths on", MINFRAME, MAXFRAME / 2.0);
1351 static int object_update_paths_poll(bContext *C)
1353 if (ED_operator_object_active_editable(C)) {
1354 Object *ob = ED_object_active_context(C);
1355 return (ob->avs.path_bakeflag & MOTIONPATH_BAKE_HAS_PATHS) != 0;
1361 static int object_update_paths_exec(bContext *C, wmOperator *UNUSED(op))
1363 Scene *scene = CTX_data_scene(C);
1366 return OPERATOR_CANCELLED;
1368 /* calculate the paths for objects that have them (and are tagged to get refreshed) */
1369 ED_objects_recalculate_paths(C, scene);
1371 /* notifiers for updates */
1372 WM_event_add_notifier(C, NC_OBJECT | ND_TRANSFORM, NULL);
1374 return OPERATOR_FINISHED;
1377 void OBJECT_OT_paths_update(wmOperatorType *ot)
1380 ot->name = "Update Object Paths";
1381 ot->idname = "OBJECT_OT_paths_update";
1382 ot->description = "Recalculate paths for selected objects";
1385 ot->exec = object_update_paths_exec;
1386 ot->poll = object_update_paths_poll;
1389 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1394 /* Helper for ED_objects_clear_paths() */
1395 static void object_clear_mpath(Object *ob)
1398 animviz_free_motionpath(ob->mpath);
1400 ob->avs.path_bakeflag &= ~MOTIONPATH_BAKE_HAS_PATHS;
1404 /* Clear motion paths for all objects */
1405 void ED_objects_clear_paths(bContext *C, bool only_selected)
1407 if (only_selected) {
1408 /* loop over all selected + sedtiable objects in scene */
1409 CTX_DATA_BEGIN(C, Object *, ob, selected_editable_objects)
1411 object_clear_mpath(ob);
1416 /* loop over all edtiable objects in scene */
1417 CTX_DATA_BEGIN(C, Object *, ob, editable_objects)
1419 object_clear_mpath(ob);
1425 /* operator callback for this */
1426 static int object_clear_paths_exec(bContext *C, wmOperator *op)
1428 bool only_selected = RNA_boolean_get(op->ptr, "only_selected");
1430 /* use the backend function for this */
1431 ED_objects_clear_paths(C, only_selected);
1433 /* notifiers for updates */
1434 WM_event_add_notifier(C, NC_OBJECT | ND_TRANSFORM, NULL);
1436 return OPERATOR_FINISHED;
1439 /* operator callback/wrapper */
1440 static int object_clear_paths_invoke(bContext *C, wmOperator *op, const wmEvent *evt)
1442 if ((evt->shift) && !RNA_struct_property_is_set(op->ptr, "only_selected")) {
1443 RNA_boolean_set(op->ptr, "only_selected", true);
1445 return object_clear_paths_exec(C, op);
1448 void OBJECT_OT_paths_clear(wmOperatorType *ot)
1451 ot->name = "Clear Object Paths";
1452 ot->idname = "OBJECT_OT_paths_clear";
1453 ot->description = "Clear path caches for all objects, hold Shift key for selected objects only";
1456 ot->invoke = object_clear_paths_invoke;
1457 ot->exec = object_clear_paths_exec;
1458 ot->poll = ED_operator_object_active_editable;
1461 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1464 ot->prop = RNA_def_boolean(ot->srna, "only_selected", false, "Only Selected",
1465 "Only clear paths from selected objects");
1466 RNA_def_property_flag(ot->prop, PROP_SKIP_SAVE);
1470 /********************** Smooth/Flat *********************/
1472 static int shade_smooth_exec(bContext *C, wmOperator *op)
1477 int clear = (STREQ(op->idname, "OBJECT_OT_shade_flat"));
1478 bool done = false, linked_data = false;
1480 CTX_DATA_BEGIN(C, Object *, ob, selected_editable_objects)
1484 if (data && ID_IS_LINKED(data)) {
1489 if (ob->type == OB_MESH) {
1490 BKE_mesh_smooth_flag_set(ob, !clear);
1492 DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
1493 WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, ob);
1497 else if (ELEM(ob->type, OB_SURF, OB_CURVE)) {
1500 for (nu = cu->nurb.first; nu; nu = nu->next) {
1501 if (!clear) nu->flag |= ME_SMOOTH;
1502 else nu->flag &= ~ME_SMOOTH;
1505 DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
1506 WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, ob);
1514 BKE_report(op->reports, RPT_WARNING, "Can't edit linked mesh or curve data");
1516 return (done) ? OPERATOR_FINISHED : OPERATOR_CANCELLED;
1519 static int shade_poll(bContext *C)
1521 return (CTX_data_edit_object(C) == NULL);
1524 void OBJECT_OT_shade_flat(wmOperatorType *ot)
1527 ot->name = "Shade Flat";
1528 ot->description = "Render and display faces uniform, using Face Normals";
1529 ot->idname = "OBJECT_OT_shade_flat";
1532 ot->poll = shade_poll;
1533 ot->exec = shade_smooth_exec;
1536 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1539 void OBJECT_OT_shade_smooth(wmOperatorType *ot)
1542 ot->name = "Shade Smooth";
1543 ot->description = "Render and display faces smooth, using interpolated Vertex Normals";
1544 ot->idname = "OBJECT_OT_shade_smooth";
1547 ot->poll = shade_poll;
1548 ot->exec = shade_smooth_exec;
1551 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1554 /* ********************** */
1556 static void UNUSED_FUNCTION(image_aspect) (Scene *scene, View3D *v3d)
1558 /* all selected objects with an image map: scale in image aspect */
1566 if (scene->obedit) return; // XXX get from context
1567 if (ID_IS_LINKED(scene)) return;
1569 for (base = FIRSTBASE; base; base = base->next) {
1570 if (TESTBASELIB(v3d, base)) {
1574 for (a = 1; a <= ob->totcol; a++) {
1575 ma = give_current_material(ob, a);
1577 for (b = 0; b < MAX_MTEX; b++) {
1578 if (ma->mtex[b] && ma->mtex[b]->tex) {
1579 tex = ma->mtex[b]->tex;
1580 if (tex->type == TEX_IMAGE && tex->ima) {
1581 ImBuf *ibuf = BKE_image_acquire_ibuf(tex->ima, NULL, NULL);
1585 if (ob->type == OB_MESH) {
1587 BKE_mesh_texspace_get(ob->data, NULL, NULL, size);
1588 space = size[0] / size[1];
1590 else if (ELEM(ob->type, OB_CURVE, OB_FONT, OB_SURF)) {
1592 BKE_curve_texspace_get(ob->data, NULL, NULL, size);
1593 space = size[0] / size[1];
1596 x = ibuf->x / space;
1599 if (x > y) ob->size[0] = ob->size[1] * x / y;
1600 else ob->size[1] = ob->size[0] * y / x;
1603 DAG_id_tag_update(&ob->id, OB_RECALC_OB);
1605 BKE_image_release_ibuf(tex->ima, ibuf, NULL);
1618 static const EnumPropertyItem *object_mode_set_itemsf(
1619 bContext *C, PointerRNA *UNUSED(ptr), PropertyRNA *UNUSED(prop), bool *r_free)
1621 const EnumPropertyItem *input = rna_enum_object_mode_items;
1622 EnumPropertyItem *item = NULL;
1627 if (!C) /* needed for docs */
1628 return rna_enum_object_mode_items;
1630 ob = CTX_data_active_object(C);
1632 const bool use_mode_particle_edit = (BLI_listbase_is_empty(&ob->particlesystem) == false) ||
1633 (ob->soft != NULL) ||
1634 (modifiers_findByType(ob, eModifierType_Cloth) != NULL);
1635 while (input->identifier) {
1636 if ((input->value == OB_MODE_EDIT && OB_TYPE_SUPPORT_EDITMODE(ob->type)) ||
1637 (input->value == OB_MODE_POSE && (ob->type == OB_ARMATURE)) ||
1638 (input->value == OB_MODE_PARTICLE_EDIT && use_mode_particle_edit) ||
1639 (ELEM(input->value, OB_MODE_SCULPT, OB_MODE_VERTEX_PAINT,
1640 OB_MODE_WEIGHT_PAINT, OB_MODE_TEXTURE_PAINT) && (ob->type == OB_MESH)) ||
1641 (input->value == OB_MODE_OBJECT))
1643 RNA_enum_item_add(&item, &totitem, input);
1649 /* We need at least this one! */
1650 RNA_enum_items_add_value(&item, &totitem, input, OB_MODE_OBJECT);
1653 /* On top of all the rest, GPencil Stroke Edit Mode
1654 * is available if there's a valid gp datablock...
1656 gpd = CTX_data_gpencil_data(C);
1658 RNA_enum_items_add_value(&item, &totitem, rna_enum_object_mode_items, OB_MODE_GPENCIL);
1661 RNA_enum_item_end(&item, &totitem);
1668 static int object_mode_set_poll(bContext *C)
1670 /* Since Grease Pencil editmode is also handled here,
1671 * we have a special exception for allowing this operator
1672 * to still work in that case when there's no active object
1673 * so that users can exit editmode this way as per normal.
1675 if (ED_operator_object_active_editable(C))
1678 return (CTX_data_gpencil_data(C) != NULL);
1681 static int object_mode_set_exec(bContext *C, wmOperator *op)
1683 Object *ob = CTX_data_active_object(C);
1684 bGPdata *gpd = CTX_data_gpencil_data(C);
1685 eObjectMode mode = RNA_enum_get(op->ptr, "mode");
1686 eObjectMode restore_mode = (ob) ? ob->mode : OB_MODE_OBJECT;
1687 const bool toggle = RNA_boolean_get(op->ptr, "toggle");
1690 /* GP Mode is not bound to a specific object. Therefore,
1691 * we don't want it to be actually saved on any objects,
1692 * as weirdness can happen if you select other objects,
1693 * or load old files.
1695 * Instead, we use the following 2 rules to ensure that
1696 * the mode selector works as expected:
1697 * 1) If there's no object, we want to enter editmode.
1698 * (i.e. with no object, we're in object mode)
1699 * 2) Otherwise, exit stroke editmode, so that we can
1700 * enter another mode...
1702 if (!ob || (gpd->flag & GP_DATA_STROKE_EDITMODE)) {
1703 WM_operator_name_call(C, "GPENCIL_OT_editmode_toggle", WM_OP_EXEC_REGION_WIN, NULL);
1707 if (!ob || !ED_object_mode_compat_test(ob, mode))
1708 return OPERATOR_PASS_THROUGH;
1710 if (ob->mode != mode) {
1711 /* we should be able to remove this call, each operator calls */
1712 ED_object_mode_compat_set(C, ob, mode, op->reports);
1715 /* Exit current mode if it's not the mode we're setting */
1716 if (mode != OB_MODE_OBJECT && (ob->mode != mode || toggle)) {
1717 /* Enter new mode */
1718 ED_object_mode_toggle(C, mode);
1722 /* Special case for Object mode! */
1723 if (mode == OB_MODE_OBJECT && restore_mode == OB_MODE_OBJECT && ob->restore_mode != OB_MODE_OBJECT) {
1724 ED_object_mode_toggle(C, ob->restore_mode);
1726 else if (ob->mode == mode) {
1727 /* For toggling, store old mode so we know what to go back to */
1728 ob->restore_mode = restore_mode;
1730 else if (ob->restore_mode != OB_MODE_OBJECT && ob->restore_mode != mode) {
1731 ED_object_mode_toggle(C, ob->restore_mode);
1735 return OPERATOR_FINISHED;
1738 void OBJECT_OT_mode_set(wmOperatorType *ot)
1743 ot->name = "Set Object Mode";
1744 ot->description = "Sets the object interaction mode";
1745 ot->idname = "OBJECT_OT_mode_set";
1748 ot->exec = object_mode_set_exec;
1750 ot->poll = object_mode_set_poll; //ED_operator_object_active_editable;
1753 ot->flag = 0; /* no register/undo here, leave it to operators being called */
1755 ot->prop = RNA_def_enum(ot->srna, "mode", rna_enum_object_mode_items, OB_MODE_OBJECT, "Mode", "");
1756 RNA_def_enum_funcs(ot->prop, object_mode_set_itemsf);
1757 RNA_def_property_flag(ot->prop, PROP_SKIP_SAVE);
1759 prop = RNA_def_boolean(ot->srna, "toggle", 0, "Toggle", "");
1760 RNA_def_property_flag(prop, PROP_SKIP_SAVE);
1763 /************************ Game Properties ***********************/
1765 static int game_property_new_exec(bContext *C, wmOperator *op)
1767 Object *ob = CTX_data_active_object(C);
1769 char name[MAX_NAME];
1770 int type = RNA_enum_get(op->ptr, "type");
1772 prop = BKE_bproperty_new(type);
1773 BLI_addtail(&ob->prop, prop);
1775 RNA_string_get(op->ptr, "name", name);
1776 if (name[0] != '\0') {
1777 BLI_strncpy(prop->name, name, sizeof(prop->name));
1780 BLI_uniquename(&ob->prop, prop, DATA_("Property"), '.', offsetof(bProperty, name), sizeof(prop->name));
1782 WM_event_add_notifier(C, NC_LOGIC, NULL);
1783 return OPERATOR_FINISHED;
1787 void OBJECT_OT_game_property_new(wmOperatorType *ot)
1790 ot->name = "New Game Property";
1791 ot->description = "Create a new property available to the game engine";
1792 ot->idname = "OBJECT_OT_game_property_new";
1795 ot->exec = game_property_new_exec;
1796 ot->poll = ED_operator_object_active_editable;
1799 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1801 RNA_def_enum(ot->srna, "type", rna_enum_gameproperty_type_items, GPROP_FLOAT, "Type", "Type of game property to add");
1802 RNA_def_string(ot->srna, "name", NULL, MAX_NAME, "Name", "Name of the game property to add");
1805 static int game_property_remove_exec(bContext *C, wmOperator *op)
1807 Object *ob = CTX_data_active_object(C);
1809 int index = RNA_int_get(op->ptr, "index");
1812 return OPERATOR_CANCELLED;
1814 prop = BLI_findlink(&ob->prop, index);
1817 BLI_remlink(&ob->prop, prop);
1818 BKE_bproperty_free(prop);
1820 WM_event_add_notifier(C, NC_LOGIC, NULL);
1821 return OPERATOR_FINISHED;
1824 return OPERATOR_CANCELLED;
1828 void OBJECT_OT_game_property_remove(wmOperatorType *ot)
1831 ot->name = "Remove Game Property";
1832 ot->description = "Remove game property";
1833 ot->idname = "OBJECT_OT_game_property_remove";
1836 ot->exec = game_property_remove_exec;
1837 ot->poll = ED_operator_object_active_editable;
1840 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1842 RNA_def_int(ot->srna, "index", 0, 0, INT_MAX, "Index", "Property index to remove ", 0, INT_MAX);
1845 #define GAME_PROPERTY_MOVE_UP 1
1846 #define GAME_PROPERTY_MOVE_DOWN -1
1848 static int game_property_move(bContext *C, wmOperator *op)
1850 Object *ob = CTX_data_active_object(C);
1852 bProperty *otherprop = NULL;
1853 const int index = RNA_int_get(op->ptr, "index");
1854 const int dir = RNA_enum_get(op->ptr, "direction");
1857 return OPERATOR_CANCELLED;
1859 prop = BLI_findlink(&ob->prop, index);
1862 return OPERATOR_CANCELLED;
1864 if (dir == GAME_PROPERTY_MOVE_UP) {
1865 otherprop = prop->prev;
1867 else if (dir == GAME_PROPERTY_MOVE_DOWN) {
1868 otherprop = prop->next;
1874 if (prop && otherprop) {
1875 BLI_listbase_swaplinks(&ob->prop, prop, otherprop);
1877 WM_event_add_notifier(C, NC_LOGIC, NULL);
1878 return OPERATOR_FINISHED;
1881 return OPERATOR_CANCELLED;
1885 void OBJECT_OT_game_property_move(wmOperatorType *ot)
1887 static const EnumPropertyItem direction_property_move[] = {
1888 {GAME_PROPERTY_MOVE_UP, "UP", 0, "Up", ""},
1889 {GAME_PROPERTY_MOVE_DOWN, "DOWN", 0, "Down", ""},
1890 {0, NULL, 0, NULL, NULL}
1895 ot->name = "Move Game Property";
1896 ot->description = "Move game property";
1897 ot->idname = "OBJECT_OT_game_property_move";
1900 ot->exec = game_property_move;
1901 ot->poll = ED_operator_object_active_editable;
1904 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1907 prop = RNA_def_int(ot->srna, "index", 0, 0, INT_MAX, "Index", "Property index to move", 0, INT_MAX);
1908 RNA_def_property_flag(prop, PROP_HIDDEN);
1909 RNA_def_enum(ot->srna, "direction", direction_property_move, 0, "Direction",
1910 "Direction for moving the property");
1913 #undef GAME_PROPERTY_MOVE_UP
1914 #undef GAME_PROPERTY_MOVE_DOWN
1916 #define COPY_PROPERTIES_REPLACE 1
1917 #define COPY_PROPERTIES_MERGE 2
1918 #define COPY_PROPERTIES_COPY 3
1920 static const EnumPropertyItem game_properties_copy_operations[] = {
1921 {COPY_PROPERTIES_REPLACE, "REPLACE", 0, "Replace Properties", ""},
1922 {COPY_PROPERTIES_MERGE, "MERGE", 0, "Merge Properties", ""},
1923 {COPY_PROPERTIES_COPY, "COPY", 0, "Copy a Property", ""},
1924 {0, NULL, 0, NULL, NULL}
1927 static const EnumPropertyItem *gameprops_itemf(bContext *C, PointerRNA *UNUSED(ptr), PropertyRNA *UNUSED(prop), bool *r_free)
1929 Object *ob = ED_object_active_context(C);
1930 EnumPropertyItem tmp = {0, "", 0, "", ""};
1931 EnumPropertyItem *item = NULL;
1936 return DummyRNA_NULL_items;
1938 for (a = 1, prop = ob->prop.first; prop; prop = prop->next, a++) {
1940 tmp.identifier = prop->name;
1941 tmp.name = prop->name;
1942 RNA_enum_item_add(&item, &totitem, &tmp);
1945 RNA_enum_item_end(&item, &totitem);
1951 static int game_property_copy_exec(bContext *C, wmOperator *op)
1953 Object *ob = ED_object_active_context(C);
1955 int type = RNA_enum_get(op->ptr, "operation");
1956 int propid = RNA_enum_get(op->ptr, "property");
1958 if (propid > 0) { /* copy */
1959 prop = BLI_findlink(&ob->prop, propid - 1);
1962 CTX_DATA_BEGIN(C, Object *, ob_iter, selected_editable_objects)
1965 BKE_bproperty_object_set(ob_iter, prop);
1971 CTX_DATA_BEGIN(C, Object *, ob_iter, selected_editable_objects)
1973 if (ob != ob_iter) {
1974 if (type == COPY_PROPERTIES_REPLACE) {
1975 BKE_bproperty_copy_list(&ob_iter->prop, &ob->prop);
1978 /* merge - the default when calling with no argument */
1979 for (prop = ob->prop.first; prop; prop = prop->next) {
1980 BKE_bproperty_object_set(ob_iter, prop);
1988 return OPERATOR_FINISHED;
1991 void OBJECT_OT_game_property_copy(wmOperatorType *ot)
1995 ot->name = "Copy Game Property";
1996 ot->idname = "OBJECT_OT_game_property_copy";
1997 ot->description = "Copy/merge/replace a game property from active object to all selected objects";
2000 ot->exec = game_property_copy_exec;
2001 ot->poll = ED_operator_object_active_editable;
2004 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2006 RNA_def_enum(ot->srna, "operation", game_properties_copy_operations, 3, "Operation", "");
2007 prop = RNA_def_enum(ot->srna, "property", DummyRNA_NULL_items, 0, "Property", "Properties to copy");
2008 RNA_def_property_flag(prop, PROP_SKIP_SAVE | PROP_ENUM_NO_TRANSLATE);
2009 RNA_def_enum_funcs(prop, gameprops_itemf);
2013 static int game_property_clear_exec(bContext *C, wmOperator *UNUSED(op))
2015 CTX_DATA_BEGIN(C, Object *, ob_iter, selected_editable_objects)
2017 BKE_bproperty_free_list(&ob_iter->prop);
2021 WM_event_add_notifier(C, NC_LOGIC, NULL);
2022 return OPERATOR_FINISHED;
2024 void OBJECT_OT_game_property_clear(wmOperatorType *ot)
2027 ot->name = "Clear Game Properties";
2028 ot->idname = "OBJECT_OT_game_property_clear";
2029 ot->description = "Remove all game properties from all selected objects";
2032 ot->exec = game_property_clear_exec;
2033 ot->poll = ED_operator_object_active_editable;
2036 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2039 /************************ Copy Logic Bricks ***********************/
2041 static int logicbricks_copy_exec(bContext *C, wmOperator *UNUSED(op))
2043 Object *ob = ED_object_active_context(C);
2045 CTX_DATA_BEGIN(C, Object *, ob_iter, selected_editable_objects)
2047 if (ob != ob_iter) {
2048 /* first: free all logic */
2049 free_sensors(&ob_iter->sensors);
2050 unlink_controllers(&ob_iter->controllers);
2051 free_controllers(&ob_iter->controllers);
2052 unlink_actuators(&ob_iter->actuators);
2053 free_actuators(&ob_iter->actuators);
2055 /* now copy it, this also works without logicbricks! */
2056 clear_sca_new_poins_ob(ob);
2057 copy_sensors(&ob_iter->sensors, &ob->sensors, 0);
2058 copy_controllers(&ob_iter->controllers, &ob->controllers, 0);
2059 copy_actuators(&ob_iter->actuators, &ob->actuators, 0);
2060 set_sca_new_poins_ob(ob_iter);
2062 /* some menu settings */
2063 ob_iter->scavisflag = ob->scavisflag;
2064 ob_iter->scaflag = ob->scaflag;
2066 /* set the initial state */
2067 ob_iter->state = ob->state;
2068 ob_iter->init_state = ob->init_state;
2070 if (ob_iter->totcol == ob->totcol) {
2071 ob_iter->actcol = ob->actcol;
2072 WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, ob_iter);
2078 WM_event_add_notifier(C, NC_LOGIC, NULL);
2080 return OPERATOR_FINISHED;
2083 void OBJECT_OT_logic_bricks_copy(wmOperatorType *ot)
2086 ot->name = "Copy Logic Bricks to Selected";
2087 ot->description = "Copy logic bricks to other selected objects";
2088 ot->idname = "OBJECT_OT_logic_bricks_copy";
2091 ot->exec = logicbricks_copy_exec;
2092 ot->poll = ED_operator_object_active_editable;
2095 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2098 static int game_physics_copy_exec(bContext *C, wmOperator *UNUSED(op))
2100 Object *ob = ED_object_active_context(C);
2102 CTX_DATA_BEGIN(C, Object *, ob_iter, selected_editable_objects)
2104 if (ob != ob_iter) {
2105 ob_iter->gameflag = ob->gameflag;
2106 ob_iter->gameflag2 = ob->gameflag2;
2107 ob_iter->inertia = ob->inertia;
2108 ob_iter->formfactor = ob->formfactor;
2109 ob_iter->damping = ob->damping;
2110 ob_iter->rdamping = ob->rdamping;
2111 ob_iter->min_vel = ob->min_vel;
2112 ob_iter->max_vel = ob->max_vel;
2113 ob_iter->min_angvel = ob->min_angvel;
2114 ob_iter->max_angvel = ob->max_angvel;
2115 ob_iter->obstacleRad = ob->obstacleRad;
2116 ob_iter->mass = ob->mass;
2117 copy_v3_v3(ob_iter->anisotropicFriction, ob->anisotropicFriction);
2118 ob_iter->collision_boundtype = ob->collision_boundtype;
2119 ob_iter->margin = ob->margin;
2120 ob_iter->bsoft = copy_bulletsoftbody(ob->bsoft, 0);
2121 if (ob->restrictflag & OB_RESTRICT_RENDER)
2122 ob_iter->restrictflag |= OB_RESTRICT_RENDER;
2124 ob_iter->restrictflag &= ~OB_RESTRICT_RENDER;
2126 ob_iter->col_group = ob->col_group;
2127 ob_iter->col_mask = ob->col_mask;
2132 return OPERATOR_FINISHED;
2135 void OBJECT_OT_game_physics_copy(struct wmOperatorType *ot)
2138 ot->name = "Copy Game Physics Properties to Selected";
2139 ot->description = "Copy game physics properties to other selected objects";
2140 ot->idname = "OBJECT_OT_game_physics_copy";
2143 ot->exec = game_physics_copy_exec;
2144 ot->poll = ED_operator_object_active_editable;
2147 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2150 /* generic utility function */
2152 bool ED_object_editmode_calc_active_center(Object *obedit, const bool select_only, float r_center[3])
2154 switch (obedit->type) {
2157 BMEditMesh *em = BKE_editmesh_from_object(obedit);
2158 BMEditSelection ese;
2160 if (BM_select_history_active_get(em->bm, &ese)) {
2161 BM_editselection_center(&ese, r_center);
2168 bArmature *arm = obedit->data;
2169 EditBone *ebo = arm->act_edbone;
2171 if (ebo && (!select_only || (ebo->flag & (BONE_SELECTED | BONE_ROOTSEL)))) {
2172 copy_v3_v3(r_center, ebo->head);
2181 Curve *cu = obedit->data;
2183 if (ED_curve_active_center(cu, r_center)) {
2190 MetaBall *mb = obedit->data;
2191 MetaElem *ml_act = mb->lastelem;
2193 if (ml_act && (!select_only || (ml_act->flag & SELECT))) {
2194 copy_v3_v3(r_center, &ml_act->x);
2201 BPoint *actbp = BKE_lattice_active_point_get(obedit->data);
2204 copy_v3_v3(r_center, actbp->vec);