Blender V5.3
view3d_ops.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2008 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "DNA_object_types.h"
10#include "DNA_screen_types.h"
11#include "DNA_space_types.h"
12
13#include "BLI_listbase.hh"
14#include "BLI_path_utils.hh"
15
16#include "BKE_appdir.hh"
18#include "BKE_blendfile.hh"
19#include "BKE_context.hh"
20#include "BKE_lib_id.hh"
21#include "BKE_report.hh"
22
23#include "BLO_readfile.hh"
24
25#include "RNA_access.hh"
26#include "RNA_define.hh"
27
28#include "WM_api.hh"
29#include "WM_types.hh"
30
31#include "ED_outliner.hh"
32#include "ED_screen.hh"
33#include "ED_transform.hh"
34
35#include "view3d_intern.hh"
36#include "view3d_navigate.hh"
37
38#ifdef WIN32
39# include "BLI_math_base_c.hh" /* M_PI */
40#endif
41
42namespace blender {
43
44/* -------------------------------------------------------------------- */
47
48static void view3d_copybuffer_filepath_get(char filepath[FILE_MAX], size_t filepath_maxncpy)
49{
50 BLI_path_join(filepath, filepath_maxncpy, BKE_tempdir_base(), "copybuffer.blend");
51}
52
54
55/* -------------------------------------------------------------------- */
58
60{
61 using namespace blender::bke::blendfile;
62
63 Main *bmain = CTX_data_main(C);
64 PartialWriteContext copybuffer{*bmain};
65
67 Object *obact_copy = nullptr;
68
69 /* context, selection, could be generalized */
70 CTX_DATA_BEGIN (C, Object *, ob, selected_objects) {
71 if (ID_IS_LINKED(&ob->id)) {
72 /* Direct link/append of linked IDs is not supported currently, so neither is their
73 * copy/pasting. */
76 "Cannot copy the linked object '%s' to the internal clipboard",
77 BKE_id_name(ob->id));
78 continue;
79 }
80 if (ID_IS_PACKED(&ob->id)) {
81 /* Direct link/append of packed IDs is not supported currently, so neither is their
82 * copy/pasting. */
83 /* NOTE: In practice, always shadowed currently by the more general check on linked status
84 * above. */
87 "Cannot copy the linked packed object '%s' to the internal clipboard",
88 BKE_id_name(ob->id));
89 continue;
90 }
91 ID *ob_id_copy = copybuffer.id_add(
92 &ob->id,
94 (PartialWriteContext::IDAddOperations::SET_FAKE_USER |
95 PartialWriteContext::IDAddOperations::SET_CLIPBOARD_MARK |
96 PartialWriteContext::IDAddOperations::ADD_DEPENDENCIES)},
97 nullptr);
98
99 if (obact && (obact == ob)) {
100 obact_copy = reinterpret_cast<Object *>(ob_id_copy);
101 }
102 }
104
105 /* Explicitly adding an object to the copy/paste buffer _may_ add others as dependencies (e.g. a
106 * parent object). So count to total amount of objects added, to get a matching number with the
107 * one reported by the "paste" operation. */
108 int num_copied = 0;
109
110 /* Count & mark the active as done (when set). */
111 for (Object &ob : copybuffer.bmain.objects) {
113 num_copied += 1;
114 }
115
116 if (num_copied == 0) {
117 BKE_report(op->reports, RPT_INFO, "No valid selected objects to copy");
118 return OPERATOR_CANCELLED;
119 }
120
121 if (obact_copy) {
122 obact_copy->flag |= OB_FLAG_ACTIVE_CLIPBOARD;
123 }
124
125 char filepath[FILE_MAX];
126 view3d_copybuffer_filepath_get(filepath, sizeof(filepath));
127 copybuffer.write_as_copypaste_buffer(filepath, *op->reports);
128
129 BKE_reportf(op->reports, RPT_INFO, "Copied %d selected object(s)", num_copied);
130
131 return OPERATOR_FINISHED;
132}
133
135{
136 /* identifiers */
137 ot->name = "Copy Objects";
138 ot->idname = "VIEW3D_OT_copybuffer";
139 ot->description = "Copy the selected objects to the internal clipboard";
140
141 /* API callbacks. */
144}
145
147
148/* -------------------------------------------------------------------- */
151
153{
154 char filepath[FILE_MAX];
155 int flag = 0;
156
157 if (RNA_boolean_get(op->ptr, "autoselect")) {
159 }
160 if (RNA_boolean_get(op->ptr, "active_collection")) {
162 }
163
164 view3d_copybuffer_filepath_get(filepath, sizeof(filepath));
165
166 const int num_pasted = BKE_copybuffer_paste(C, filepath, flag, op->reports, FILTER_ID_OB);
167 if (num_pasted == 0) {
168 BKE_report(op->reports, RPT_INFO, "No objects to paste");
169 return OPERATOR_CANCELLED;
170 }
171
175
176 BKE_reportf(op->reports, RPT_INFO, "%d object(s) pasted", num_pasted);
177
178 return OPERATOR_FINISHED;
179}
180
182{
183
184 /* identifiers */
185 ot->name = "Paste Objects";
186 ot->idname = "VIEW3D_OT_pastebuffer";
187 ot->description = "Paste objects from the internal clipboard";
188
189 /* API callbacks. */
192
193 /* flags */
195
196 RNA_def_boolean(ot->srna, "autoselect", true, "Select", "Select pasted objects");
198 "active_collection",
199 true,
200 "Active Collection",
201 "Put pasted objects in the active collection");
202}
203
205
206/* -------------------------------------------------------------------- */
209
211{
219#ifdef WITH_INPUT_NDOF
220 WM_operatortype_append(VIEW3D_OT_ndof_orbit_zoom);
221 WM_operatortype_append(VIEW3D_OT_ndof_orbit);
222 WM_operatortype_append(VIEW3D_OT_ndof_pan);
223 WM_operatortype_append(VIEW3D_OT_ndof_all);
224#endif /* WITH_INPUT_NDOF */
264
266
274
276
280
283
284#ifdef WITH_XR_OPENXR
286#endif
287
289}
290
292{
293 WM_keymap_ensure(keyconf, "3D View Generic", SPACE_VIEW3D, RGN_TYPE_WINDOW);
294
295 /* only for region 3D window */
296 WM_keymap_ensure(keyconf, "3D View", SPACE_VIEW3D, RGN_TYPE_WINDOW);
297
298 fly_modal_keymap(keyconf);
299 walk_modal_keymap(keyconf);
301 viewmove_modal_keymap(keyconf);
302 viewzoom_modal_keymap(keyconf);
303 viewdolly_modal_keymap(keyconf);
304 viewplace_modal_keymap(keyconf);
306}
307
309
310} // namespace blender
#define CTX_DATA_BEGIN(C, Type, instance, member)
#define CTX_DATA_END
#define FILE_MAX
#define BLI_path_join(...)
external readfile function prototypes.
#define ID_IS_PACKED(_id)
Definition DNA_ID.h:703
#define FILTER_ID_OB
Definition DNA_ID.h:1216
#define ID_IS_LINKED(_id)
Definition DNA_ID.h:697
Object is a sort of wrapper for general info.
#define C
Definition RandGen.cpp:29
#define NC_WINDOW
Definition WM_types.hh:388
#define ND_OB_SELECT
Definition WM_types.hh:461
#define NC_SCENE
Definition WM_types.hh:391
ID * id_add(const ID *id, IDAddOptions options, FunctionRef< IDAddOperations(LibraryIDLinkCallbackData *cb_data, IDAddOptions options)> dependencies_filter_cb=nullptr)
bool write_as_copypaste_buffer(const char *write_filepath, ReportList &reports)
BMesh Iterators.
void VIEW3D_OT_view_axis(wmOperatorType *ot)
void VIEW3D_OT_view_selected(wmOperatorType *ot)
void viewdolly_modal_keymap(wmKeyConfig *keyconf)
void VIEW3D_OT_view_roll_set(wmOperatorType *ot)
void view3d_operatortypes()
void VIEW3D_OT_view_orbit(wmOperatorType *ot)
void WM_operatortype_append(void(*opfunc)(wmOperatorType *))
bool ED_operator_scene_editable(bContext *C)
void VIEW3D_OT_view_center_lock(wmOperatorType *ot)
void VIEW3D_OT_snap_cursor_to_active(wmOperatorType *ot)
void VIEW3D_OT_smoothview(wmOperatorType *ot)
void VIEW3D_OT_clear_render_border(wmOperatorType *ot)
void VIEW3D_OT_object_as_camera(wmOperatorType *ot)
void VIEW3D_OT_view_persportho(wmOperatorType *ot)
void VIEW3D_OT_zoom_border(wmOperatorType *ot)
void VIEW3D_OT_clip_border(wmOperatorType *ot)
void VIEW3D_OT_localview(wmOperatorType *ot)
void VIEW3D_OT_localview_remove_from(wmOperatorType *ot)
void viewrotate_modal_keymap(wmKeyConfig *keyconf)
void viewmove_modal_keymap(wmKeyConfig *keyconf)
@ OB_FLAG_ACTIVE_CLIPBOARD
void VIEW3D_OT_view_lock_clear(wmOperatorType *ot)
void VIEW3D_OT_snap_selected_to_active(wmOperatorType *ot)
void VIEW3D_OT_toggle_shading(wmOperatorType *ot)
void VIEW3D_OT_view_lock_to_active(wmOperatorType *ot)
void VIEW3D_OT_object_mode_pie_or_toggle(wmOperatorType *ot)
void VIEW3D_OT_select_circle(wmOperatorType *ot)
void VIEW3D_OT_view_all(wmOperatorType *ot)
void VIEW3D_OT_move(wmOperatorType *ot)
void VIEW3D_OT_camera_to_view_selected(wmOperatorType *ot)
const char * BKE_id_name(const ID &id)
void VIEW3D_OT_rotate(wmOperatorType *ot)
@ BLO_LIBLINK_APPEND_SET_OB_ACTIVE_CLIPBOARD
bool ED_operator_scene(bContext *C)
void VIEW3D_OT_view_center_camera(wmOperatorType *ot)
void VIEW3D_OT_view_camera(wmOperatorType *ot)
void vr_location_scouting_capture_review_modal_keymap(wmKeyConfig *keyconf)
void ED_outliner_select_sync_from_object_tag(bContext *C)
void VIEW3D_OT_select(wmOperatorType *ot)
static wmOperatorStatus view3d_copybuffer_exec(bContext *C, wmOperator *op)
Definition view3d_ops.cc:59
void VIEW3D_OT_navigate(wmOperatorType *ot)
void VIEW3D_OT_zoom(wmOperatorType *ot)
void VIEW3D_OT_snap_selected_to_grid(wmOperatorType *ot)
void VIEW3D_OT_bone_select_menu(wmOperatorType *ot)
void VIEW3D_OT_cursor3d(wmOperatorType *ot)
void VIEW3D_OT_render_border(wmOperatorType *ot)
void BKE_report(ReportList *reports, eReportType type, const char *message)
Definition report.cc:162
void viewzoom_modal_keymap(wmKeyConfig *keyconf)
void view3d_keymap(wmKeyConfig *keyconf)
void VIEW3D_OT_ruler_remove(wmOperatorType *ot)
@ FILE_ACTIVE_COLLECTION
void VIEW3D_OT_dolly(wmOperatorType *ot)
void viewplace_modal_keymap(wmKeyConfig *keyconf)
void VIEW3D_OT_zoom_camera_1_to_1(wmOperatorType *ot)
void VIEW3D_OT_select_lasso(wmOperatorType *ot)
wmKeyMap * WM_keymap_ensure(wmKeyConfig *keyconf, const char *idname, int spaceid, int regionid)
Definition wm_keymap.cc:935
void VIEW3D_OT_toggle_matcap_flip(wmOperatorType *ot)
void VIEW3D_OT_snap_selected_to_cursor(wmOperatorType *ot)
bool RNA_boolean_get(PointerRNA *ptr, const char *name)
void VIEW3D_OT_view_center_pick(wmOperatorType *ot)
void VIEW3D_OT_vr_location_scouting_capture_review(wmOperatorType *ot)
const char * BKE_tempdir_base() ATTR_WARN_UNUSED_RESULT ATTR_RETURNS_NONNULL
Definition appdir.cc:1288
void VIEW3D_OT_fly(wmOperatorType *ot)
void VIEW3D_OT_snap_cursor_to_grid(wmOperatorType *ot)
void WM_event_add_notifier(const bContext *C, uint type, void *reference)
void VIEW3D_OT_view_flip(wmOperatorType *ot)
void VIEW3D_OT_view_pan(wmOperatorType *ot)
static wmOperatorStatus view3d_pastebuffer_exec(bContext *C, wmOperator *op)
void BKE_reportf(ReportList *reports, eReportType type, const char *format,...) ATTR_PRINTF_FORMAT(3
Definition report.cc:191
Main * CTX_data_main(const bContext *C)
void VIEW3D_OT_toggle_xray(wmOperatorType *ot)
void VIEW3D_OT_camera_to_view(wmOperatorType *ot)
void VIEW3D_OT_interactive_add(wmOperatorType *ot)
void VIEW3D_OT_view_center_cursor(wmOperatorType *ot)
void VIEW3D_OT_ruler_add(wmOperatorType *ot)
int BKE_copybuffer_paste(bContext *C, const char *libname, int flag, ReportList *reports, uint64_t id_types_mask)
void VIEW3D_OT_camera_background_image_remove(wmOperatorType *ot)
static void VIEW3D_OT_copybuffer(wmOperatorType *ot)
void VIEW3D_OT_snap_cursor_to_center(wmOperatorType *ot)
void VIEW3D_OT_view_roll(wmOperatorType *ot)
void VIEW3D_OT_select_box(wmOperatorType *ot)
void VIEW3D_OT_walk(wmOperatorType *ot)
@ OPTYPE_REGISTER
Definition WM_types.hh:188
@ OPTYPE_UNDO
Definition WM_types.hh:190
void fly_modal_keymap(wmKeyConfig *keyconf)
PropertyRNA * RNA_def_boolean(StructOrFunctionRNA *cont_, const char *identifier, const bool default_value, const char *ui_name, const char *ui_description)
static void VIEW3D_OT_pastebuffer(wmOperatorType *ot)
void VIEW3D_OT_snap_cursor_to_selected(wmOperatorType *ot)
void VIEW3D_OT_drop_world(wmOperatorType *ot)
Object * CTX_data_active_object(const bContext *C)
void VIEW3D_OT_camera_background_image_add(wmOperatorType *ot)
void walk_modal_keymap(wmKeyConfig *keyconf)
void VIEW3D_OT_select_menu(wmOperatorType *ot)
static void view3d_copybuffer_filepath_get(char filepath[FILE_MAX], size_t filepath_maxncpy)
Definition view3d_ops.cc:48
ListBaseT< Object > objects
Definition BKE_main.hh:388
wmOperatorStatus(*) exec(bContext *C, wmOperator *op) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1125
const char * description
Definition WM_types.hh:1115
bool(*) poll(bContext *C) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1165
struct ReportList * reports