Fix T82758: Convert Proxy to Override: Local constraints aren't saved.
[blender.git] / source / blender / editors / space_view3d / view3d_ops.c
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2008 Blender Foundation.
17  * All rights reserved.
18  */
19
20 /** \file
21  * \ingroup spview3d
22  */
23
24 #include <math.h>
25 #include <stdlib.h>
26
27 #include "DNA_collection_types.h"
28 #include "DNA_object_types.h"
29 #include "DNA_scene_types.h"
30 #include "DNA_screen_types.h"
31 #include "DNA_space_types.h"
32 #include "DNA_view3d_types.h"
33
34 #include "BLI_blenlib.h"
35 #include "BLI_utildefines.h"
36
37 #include "BKE_appdir.h"
38 #include "BKE_blender_copybuffer.h"
39 #include "BKE_collection.h"
40 #include "BKE_context.h"
41 #include "BKE_main.h"
42 #include "BKE_report.h"
43
44 #include "RNA_access.h"
45 #include "RNA_define.h"
46
47 #include "WM_api.h"
48 #include "WM_types.h"
49
50 #include "ED_outliner.h"
51 #include "ED_screen.h"
52 #include "ED_select_utils.h"
53 #include "ED_transform.h"
54
55 #include "view3d_intern.h"
56
57 #ifdef WIN32
58 #  include "BLI_math_base.h" /* M_PI */
59 #endif
60
61 /* ************************** copy paste ***************************** */
62
63 static int view3d_copybuffer_exec(bContext *C, wmOperator *op)
64 {
65   Main *bmain = CTX_data_main(C);
66   char str[FILE_MAX];
67   int num_copied = 0;
68
69   BKE_copybuffer_begin(bmain);
70
71   /* context, selection, could be generalized */
72   CTX_DATA_BEGIN (C, Object *, ob, selected_objects) {
73     if ((ob->id.tag & LIB_TAG_DOIT) == 0) {
74       BKE_copybuffer_tag_ID(&ob->id);
75       num_copied++;
76     }
77   }
78   CTX_DATA_END;
79
80   BLI_join_dirfile(str, sizeof(str), BKE_tempdir_base(), "copybuffer.blend");
81   BKE_copybuffer_save(bmain, str, op->reports);
82
83   BKE_reportf(op->reports, RPT_INFO, "Copied %d selected object(s)", num_copied);
84
85   return OPERATOR_FINISHED;
86 }
87
88 static void VIEW3D_OT_copybuffer(wmOperatorType *ot)
89 {
90   /* identifiers */
91   ot->name = "Copy Objects";
92   ot->idname = "VIEW3D_OT_copybuffer";
93   ot->description = "Selected objects are copied to the clipboard";
94
95   /* api callbacks */
96   ot->exec = view3d_copybuffer_exec;
97   ot->poll = ED_operator_scene;
98 }
99
100 static int view3d_pastebuffer_exec(bContext *C, wmOperator *op)
101 {
102   char str[FILE_MAX];
103   short flag = 0;
104
105   if (RNA_boolean_get(op->ptr, "autoselect")) {
106     flag |= FILE_AUTOSELECT;
107   }
108   if (RNA_boolean_get(op->ptr, "active_collection")) {
109     flag |= FILE_ACTIVE_COLLECTION;
110   }
111
112   BLI_join_dirfile(str, sizeof(str), BKE_tempdir_base(), "copybuffer.blend");
113
114   const int num_pasted = BKE_copybuffer_paste(C, str, flag, op->reports, FILTER_ID_OB);
115   if (num_pasted == 0) {
116     BKE_report(op->reports, RPT_INFO, "No objects to paste");
117     return OPERATOR_CANCELLED;
118   }
119
120   WM_event_add_notifier(C, NC_WINDOW, NULL);
121   ED_outliner_select_sync_from_object_tag(C);
122
123   BKE_reportf(op->reports, RPT_INFO, "%d object(s) pasted", num_pasted);
124
125   return OPERATOR_FINISHED;
126 }
127
128 static void VIEW3D_OT_pastebuffer(wmOperatorType *ot)
129 {
130
131   /* identifiers */
132   ot->name = "Paste Objects";
133   ot->idname = "VIEW3D_OT_pastebuffer";
134   ot->description = "Objects from the clipboard are pasted";
135
136   /* api callbacks */
137   ot->exec = view3d_pastebuffer_exec;
138   ot->poll = ED_operator_scene_editable;
139
140   /* flags */
141   ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
142
143   RNA_def_boolean(ot->srna, "autoselect", true, "Select", "Select pasted objects");
144   RNA_def_boolean(ot->srna,
145                   "active_collection",
146                   true,
147                   "Active Collection",
148                   "Put pasted objects in the active collection");
149 }
150
151 /* ************************** registration **********************************/
152
153 void view3d_operatortypes(void)
154 {
155   WM_operatortype_append(VIEW3D_OT_rotate);
156   WM_operatortype_append(VIEW3D_OT_move);
157   WM_operatortype_append(VIEW3D_OT_zoom);
158   WM_operatortype_append(VIEW3D_OT_zoom_camera_1_to_1);
159   WM_operatortype_append(VIEW3D_OT_dolly);
160 #ifdef WITH_INPUT_NDOF
161   WM_operatortype_append(VIEW3D_OT_ndof_orbit_zoom);
162   WM_operatortype_append(VIEW3D_OT_ndof_orbit);
163   WM_operatortype_append(VIEW3D_OT_ndof_pan);
164   WM_operatortype_append(VIEW3D_OT_ndof_all);
165 #endif /* WITH_INPUT_NDOF */
166   WM_operatortype_append(VIEW3D_OT_view_all);
167   WM_operatortype_append(VIEW3D_OT_view_axis);
168   WM_operatortype_append(VIEW3D_OT_view_camera);
169   WM_operatortype_append(VIEW3D_OT_view_orbit);
170   WM_operatortype_append(VIEW3D_OT_view_roll);
171   WM_operatortype_append(VIEW3D_OT_view_pan);
172   WM_operatortype_append(VIEW3D_OT_view_persportho);
173   WM_operatortype_append(VIEW3D_OT_background_image_add);
174   WM_operatortype_append(VIEW3D_OT_background_image_remove);
175   WM_operatortype_append(VIEW3D_OT_view_selected);
176   WM_operatortype_append(VIEW3D_OT_view_lock_clear);
177   WM_operatortype_append(VIEW3D_OT_view_lock_to_active);
178   WM_operatortype_append(VIEW3D_OT_view_center_cursor);
179   WM_operatortype_append(VIEW3D_OT_view_center_pick);
180   WM_operatortype_append(VIEW3D_OT_view_center_camera);
181   WM_operatortype_append(VIEW3D_OT_view_center_lock);
182   WM_operatortype_append(VIEW3D_OT_select);
183   WM_operatortype_append(VIEW3D_OT_select_box);
184   WM_operatortype_append(VIEW3D_OT_clip_border);
185   WM_operatortype_append(VIEW3D_OT_select_circle);
186   WM_operatortype_append(VIEW3D_OT_smoothview);
187   WM_operatortype_append(VIEW3D_OT_render_border);
188   WM_operatortype_append(VIEW3D_OT_clear_render_border);
189   WM_operatortype_append(VIEW3D_OT_zoom_border);
190   WM_operatortype_append(VIEW3D_OT_cursor3d);
191   WM_operatortype_append(VIEW3D_OT_select_lasso);
192   WM_operatortype_append(VIEW3D_OT_select_menu);
193   WM_operatortype_append(VIEW3D_OT_camera_to_view);
194   WM_operatortype_append(VIEW3D_OT_camera_to_view_selected);
195   WM_operatortype_append(VIEW3D_OT_object_as_camera);
196   WM_operatortype_append(VIEW3D_OT_localview);
197   WM_operatortype_append(VIEW3D_OT_localview_remove_from);
198   WM_operatortype_append(VIEW3D_OT_fly);
199   WM_operatortype_append(VIEW3D_OT_walk);
200   WM_operatortype_append(VIEW3D_OT_navigate);
201   WM_operatortype_append(VIEW3D_OT_copybuffer);
202   WM_operatortype_append(VIEW3D_OT_pastebuffer);
203
204   WM_operatortype_append(VIEW3D_OT_object_mode_pie_or_toggle);
205
206   WM_operatortype_append(VIEW3D_OT_snap_selected_to_grid);
207   WM_operatortype_append(VIEW3D_OT_snap_selected_to_cursor);
208   WM_operatortype_append(VIEW3D_OT_snap_selected_to_active);
209   WM_operatortype_append(VIEW3D_OT_snap_cursor_to_grid);
210   WM_operatortype_append(VIEW3D_OT_snap_cursor_to_center);
211   WM_operatortype_append(VIEW3D_OT_snap_cursor_to_selected);
212   WM_operatortype_append(VIEW3D_OT_snap_cursor_to_active);
213
214   WM_operatortype_append(VIEW3D_OT_interactive_add);
215
216   WM_operatortype_append(VIEW3D_OT_toggle_shading);
217   WM_operatortype_append(VIEW3D_OT_toggle_xray);
218   WM_operatortype_append(VIEW3D_OT_toggle_matcap_flip);
219
220   WM_operatortype_append(VIEW3D_OT_ruler_add);
221   WM_operatortype_append(VIEW3D_OT_ruler_remove);
222
223   transform_operatortypes();
224 }
225
226 void view3d_keymap(wmKeyConfig *keyconf)
227 {
228   WM_keymap_ensure(keyconf, "3D View Generic", SPACE_VIEW3D, 0);
229
230   /* only for region 3D window */
231   WM_keymap_ensure(keyconf, "3D View", SPACE_VIEW3D, 0);
232
233   fly_modal_keymap(keyconf);
234   walk_modal_keymap(keyconf);
235   viewrotate_modal_keymap(keyconf);
236   viewmove_modal_keymap(keyconf);
237   viewzoom_modal_keymap(keyconf);
238   viewdolly_modal_keymap(keyconf);
239   viewplace_modal_keymap(keyconf);
240 }