Blender V5.3
blenkernel/intern/paint_canvas.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "BLI_listbase.hh"
10#include "BLI_string.hh"
11
12#include "DNA_brush_types.h"
13#include "DNA_material_types.h"
14#include "DNA_mesh_types.h"
15#include "DNA_scene_types.h"
16
17#include "BKE_attribute.hh"
18#include "BKE_image.hh"
19#include "BKE_material.hh"
20#include "BKE_mesh.hh"
21#include "BKE_paint.hh"
22
23#include "IMB_imbuf_types.hh"
24
25#include <sstream>
26
27namespace blender {
28
31{
33 if (mat == nullptr) {
34 return nullptr;
35 }
36 if (mat->texpaintslot == nullptr) {
37 return nullptr;
38 }
39 if (mat->paint_active_slot >= mat->tot_slots) {
40 return nullptr;
41 }
42
43 TexPaintSlot *slot = &mat->texpaintslot[mat->paint_active_slot];
44 return slot;
45}
46
47} // namespace bke::paint::canvas
48
49using namespace blender::bke::paint::canvas;
50
51std::optional<CanvasImageData> BKE_paint_canvas_image_get(const ImagePaintSettings &settings,
52 Object &object)
53{
54 if (settings.mode == PAINT_CANVAS_SOURCE_MATERIAL) {
55 TexPaintSlot *slot = get_active_slot(&object);
56 if (slot == nullptr || slot->ima == nullptr) {
57 return std::nullopt;
58 }
59 return std::make_optional<CanvasImageData>({slot->ima, slot->image_user});
60 }
61 if (settings.mode == PAINT_CANVAS_SOURCE_IMAGE) {
62 if (settings.canvas == nullptr) {
63 return std::nullopt;
64 }
65 return std::make_optional<CanvasImageData>({settings.canvas, ImageUser{}});
66 }
67 return std::nullopt;
68}
69
70static bool has_uv_map_attribute(const Mesh &mesh, const StringRef name)
71{
72 return bke::mesh::is_uv_map(mesh.attributes().lookup_meta_data(name));
73}
74
75std::optional<StringRef> BKE_paint_canvas_uvmap_name_get(const ImagePaintSettings &settings,
76 Object *ob)
77{
78 switch (settings.mode) {
80 /* Use active uv map of the object. */
81 if (ob->type != OB_MESH) {
82 return std::nullopt;
83 }
84
85 const Mesh *mesh = id_cast<Mesh *>(ob->data);
86 if (!has_uv_map_attribute(*mesh, mesh->active_uv_map_name())) {
87 return std::nullopt;
88 }
89 return mesh->active_uv_map_name();
90 }
92 /* Use uv map of the canvas. */
93 TexPaintSlot *slot = get_active_slot(ob);
94 if (slot == nullptr) {
95 break;
96 }
97
98 if (ob->type != OB_MESH) {
99 return std::nullopt;
100 }
101
102 if (slot->uvname == nullptr) {
103 return std::nullopt;
104 }
105
106 const Mesh *mesh = id_cast<Mesh *>(ob->data);
107 if (!has_uv_map_attribute(*mesh, slot->uvname)) {
108 return std::nullopt;
109 }
110 return slot->uvname;
111 }
112 }
113 return std::nullopt;
114}
115
117{
118 std::stringstream ss;
119 ss << "UV_MAP:" << BKE_paint_canvas_uvmap_name_get(settings, ob).value_or("");
120 const Brush *brush = settings.paint.brush;
121 const bool is_mask_brush = brush && brush->image_brush_type == IMAGE_PAINT_BRUSH_TYPE_MASK;
122
123 Image *image = nullptr;
124 ImageUser tile_user;
125
126 if (is_mask_brush) {
127 image = settings.stencil;
128 tile_user = ImageUser{};
129 }
130 else if (std::optional<CanvasImageData> canvas_image_data = BKE_paint_canvas_image_get(settings,
131 *ob))
132 {
133 image = canvas_image_data->first;
134
135 if (std::holds_alternative<ImageUser *>(canvas_image_data->second)) {
136 tile_user = *std::get<ImageUser *>(canvas_image_data->second);
137 }
138 else {
139 tile_user = std::get<ImageUser>(canvas_image_data->second);
140 }
141 }
142
143 if (image) {
144 ss << ",SEAM_MARGIN:" << image->seam_margin;
145 for (ImageTile &image_tile : image->tiles) {
146 tile_user.tile = image_tile.tile_number;
147 ImBuf *image_buffer = BKE_image_acquire_ibuf(image, &tile_user, nullptr);
148 if (!image_buffer) {
149 continue;
150 }
151 ss << ",TILE_" << image_tile.tile_number;
152 ss << "(" << image_buffer->x << "," << image_buffer->y << ")";
153 BKE_image_release_ibuf(image, image_buffer, nullptr);
154 }
155 }
156
157 return ss.str();
158}
159
160} // namespace blender
General operations, lookup, etc. for materials.
bool is_uv_map(const AttributeMetaData &meta_data)
static TexPaintSlot * get_active_slot(Object *ob)
BMesh Iterators.
Material * BKE_object_material_get(Object *ob, short act)
@ IMAGE_PAINT_BRUSH_TYPE_MASK
std::optional< StringRef > BKE_paint_canvas_uvmap_name_get(const ImagePaintSettings &settings, Object *ob)
std::optional< CanvasImageData > BKE_paint_canvas_image_get(const ImagePaintSettings &settings, Object &object)
ImBuf * BKE_image_acquire_ibuf(Image *ima, ImageUser *iuser, void **r_lock)
static bool has_uv_map_attribute(const Mesh &mesh, const StringRef name)
@ PAINT_CANVAS_SOURCE_IMAGE
@ PAINT_CANVAS_SOURCE_MATERIAL
std::string BKE_paint_canvas_key_get(ImagePaintSettings &settings, Object *ob)
void BKE_image_release_ibuf(Image *ima, ImBuf *ibuf, void *lock)
struct Brush * brush
struct ImageUser * image_user