Blender V5.3
project_splash_ops.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2026 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "BLI_fileops.hh"
10#include "BLI_path_utils.hh"
11
13#include "BKE_context.hh"
14#include "BKE_report.hh"
15
16#include "DNA_space_types.h"
17
18#include "IMB_imbuf.hh"
19#include "IMB_imbuf_types.hh"
20
21#include "RNA_access.hh"
22
23#include "WM_api.hh"
24#include "WM_types.hh"
25
26#include "project_intern.hh"
27
28namespace blender {
29
30static bool project_poll(bContext *C)
31{
33 CTX_data_main(C), [](const bke::BlenderProject *project) { return project != nullptr; });
34}
35
37{
38 char filepath[FILE_MAX];
39 BLI_path_join(filepath,
40 sizeof(filepath),
41 project.get_root_path().c_str(),
43 "splash.jpg");
44 return filepath;
45}
46
47static void splash_image_path_set(Main *bmain, const StringRef filepath)
48{
50 if (project) {
51 project->set_splash_image_path(
53 }
54 });
55
57}
58
59/* -------------------------------------------------------------------- */
62
64{
65 Main *bmain = CTX_data_main(C);
66 const std::string splash_filepath = BKE_blender_project_read_callback(
67 bmain, [](const bke::BlenderProject *project) {
68 return project ? splash_image_default_filepath(*project) : std::string();
69 });
70 if (splash_filepath.empty()) {
71 return OPERATOR_CANCELLED;
72 }
73
74 char filepath[FILE_MAX];
75 RNA_string_get(op->ptr, "filepath", filepath);
76
77 /* Use the existing image as is, re-encoding would only lose quality. */
78 if (BLI_path_cmp_normalized(filepath, splash_filepath.c_str()) == 0) {
79 splash_image_path_set(bmain, splash_filepath);
80 return OPERATOR_FINISHED;
81 }
82
83 ImBuf *ibuf = WM_project_splash_image_load(filepath);
84 if (ibuf == nullptr) {
85 BKE_reportf(op->reports, RPT_ERROR, "Failed to load image \"%s\"", filepath);
86 return OPERATOR_CANCELLED;
87 }
88
89 ibuf->ftype = IMB_FTYPE_JPG;
90 ibuf->foptions.quality = 95;
92 const bool ok = BLI_file_ensure_parent_dir_exists(splash_filepath.c_str()) &&
93 IMB_save_image(ibuf, splash_filepath.c_str(), ImBufFlags::ByteData);
94 IMB_freeImBuf(ibuf);
95
96 if (!ok) {
97 BKE_reportf(op->reports, RPT_ERROR, "Failed to write \"%s\"", splash_filepath.c_str());
98 return OPERATOR_CANCELLED;
99 }
100
101 splash_image_path_set(bmain, splash_filepath);
102 return OPERATOR_FINISHED;
103}
104
106 wmOperator *op,
107 const wmEvent * /*event*/)
108{
109 /* Default to the existing splash.jpg, to easily restore after clear. */
110 const std::string splash_filepath = BKE_blender_project_read_callback(
112 return project ? splash_image_default_filepath(*project) : std::string();
113 });
114 if (!splash_filepath.empty() && BLI_is_file(splash_filepath.c_str())) {
115 RNA_string_set(op->ptr, "filepath", splash_filepath.c_str());
116 }
117
120}
121
123{
124 ot->name = "Create Splash Artwork";
125 ot->description =
126 "Set the project splash artwork from an image file.\n"
127 "The image will be cropped, scaled and saved as \".blender_project/splash.jpg\".\n"
128 "For optimal results use a 2:1 aspect ratio";
129 ot->idname = "PROJECT_OT_splash_image_create";
130
131 ot->poll = project_poll;
134
135 ot->flag = OPTYPE_INTERNAL;
136
144}
145
147
148/* -------------------------------------------------------------------- */
151
153{
156 return project && !project->get_splash_image_path().is_empty();
157 });
158}
159
161{
163 if (project) {
164 project->set_splash_image_path("");
165 }
166 });
167
169 return OPERATOR_FINISHED;
170}
171
173{
174 ot->name = "Clear Splash Artwork";
175 ot->description =
176 "Clear the project custom splash artwork.\n"
177 "The image file is not deleted from disk";
178 ot->idname = "PROJECT_OT_splash_image_clear";
179
182
183 ot->flag = OPTYPE_INTERNAL;
184}
185
187
188} // namespace blender
#define BLENDER_PROJECT_CONFIG_DIR
File and directory operations.
#define FILE_MAX
#define BLI_path_join(...)
#define C
Definition RandGen.cpp:29
#define NC_WINDOW
Definition WM_types.hh:388
BMesh Iterators.
void WM_main_add_notifier(uint type, void *reference)
void PROJECT_OT_splash_image_create(wmOperatorType *ot)
void RNA_string_set(PointerRNA *ptr, const char *name, const char *value)
static bool project_poll(bContext *C)
auto BKE_blender_project_read_callback(const Main *bmain, Fn lambda)
void WM_operator_properties_filesel(wmOperatorType *ot, const int filter, const short type, const eFileSel_Action action, const eFileSel_Flag flag, const short display, const short sort)
bool IMB_save_image(ImBuf *ibuf, const char *filepath, ImBufFlags flags)
Definition writeimage.cc:40
void IMB_freeImBuf(ImBuf *ibuf)
int BLI_path_cmp_normalized(const char *p1, const char *p2) ATTR_NONNULL(1
@ WM_FILESEL_FILEPATH
Definition WM_api.hh:1332
static void splash_image_path_set(Main *bmain, const StringRef filepath)
static wmOperatorStatus project_splash_image_clear_exec(bContext *C, wmOperator *)
static bool project_splash_image_clear_poll(bContext *C)
static wmOperatorStatus project_splash_image_create_exec(bContext *C, wmOperator *op)
void WM_event_add_notifier(const bContext *C, uint type, void *reference)
ImBuf * WM_project_splash_image_load(const StringRefNull filepath, const int width, const bool add_logo)
static wmOperatorStatus project_splash_image_create_invoke(bContext *C, wmOperator *op, const wmEvent *)
void PROJECT_OT_splash_image_clear(wmOperatorType *ot)
void BKE_reportf(ReportList *reports, eReportType type, const char *format,...) ATTR_PRINTF_FORMAT(3
Definition report.cc:191
@ FILE_OPENFILE
Definition WM_api.hh:1341
Main * CTX_data_main(const bContext *C)
static std::string splash_image_default_filepath(const bke::BlenderProject &project)
@ OPTYPE_INTERNAL
Definition WM_types.hh:210
std::string RNA_string_get(PointerRNA *ptr, const char *name)
bool BLI_is_file(const char *path) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition storage.cc:469
bool BLI_file_ensure_parent_dir_exists(const char *filepath) ATTR_NONNULL(1)
Definition fileops_c.cc:454
void WM_event_add_fileselect(bContext *C, wmOperator *op)
auto BKE_blender_project_write_callback(const Main *bmain, Fn lambda)
std::string BKE_blender_project_path_make_relative(StringRef path, bool path_is_template, const bke::BlenderProject &project)
ImbFormatOptions foptions
ImColorMode color_mode
eImbFileType ftype
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
wmOperatorStatus(*) invoke(bContext *C, wmOperator *op, const wmEvent *event) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1141
struct ReportList * reports