Blender V5.3
object_bake_api.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2004 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <cmath>
10#include <sys/stat.h>
11
12#include "MEM_guardedalloc.h"
13
14#include "DNA_material_types.h"
15#include "DNA_mesh_types.h"
16#include "DNA_meshdata_types.h"
17#include "DNA_modifier_types.h"
18#include "DNA_object_types.h"
19
20#include "RNA_access.hh"
21#include "RNA_define.hh"
22#include "RNA_enum_types.hh"
23
24#include "BLI_listbase.hh"
25#include "BLI_math_geom_c.hh"
26#include "BLI_path_utils.hh"
27#include "BLI_string_ref.hh"
28#include "BLI_string_utf8.hh"
29
30#include "BLT_translation.hh"
31
32#include "BKE_attribute.h"
33#include "BKE_attribute.hh"
34#include "BKE_callbacks.hh"
35#include "BKE_context.hh"
36#include "BKE_editmesh.hh"
37#include "BKE_global.hh"
38#include "BKE_image.hh"
39#include "BKE_image_format.hh"
40#include "BKE_image_gpu.hh"
41#include "BKE_layer.hh"
42#include "BKE_lib_id.hh"
43#include "BKE_main.hh"
44#include "BKE_material.hh"
45#include "BKE_mesh.hh"
46#include "BKE_modifier.hh"
47#include "BKE_node.hh"
48#include "BKE_node_runtime.hh"
49#include "BKE_object.hh"
50#include "BKE_report.hh"
51#include "BKE_scene.hh"
52#include "BKE_screen.hh"
53
54#include "DEG_depsgraph.hh"
57
58#include "RE_engine.h"
59#include "RE_pipeline.h"
60
62#include "IMB_imbuf.hh"
63#include "IMB_imbuf_types.hh"
64#include "IMB_partial_update.hh"
65
66#include "WM_api.hh"
67#include "WM_types.hh"
68
69#include "ED_mesh.hh"
70#include "ED_object.hh"
71#include "ED_render.hh"
72#include "ED_screen.hh"
73#include "ED_uvedit.hh"
74
75#include "object_intern.hh"
76
77namespace blender::ed::object {
78
79/* prototypes */
80static void bake_set_props(wmOperator *op, Scene *scene);
81
83 /* Data to work on. */
89
90 /* Baking settings. */
92
95 int margin;
97
101
106
107 std::string uv_layer;
109
110 /* Settings for external image saving. */
115 int width;
117 const char *identifier;
118
119 /* Baking render session. */
121
122 /* Progress Callbacks. */
123 float *progress;
125
126 /* To check for job cancellation. */
127 bool *stop;
128
129 /* Operator state. */
132};
133
134/* callbacks */
135
136static void bake_progress_update(void *bjv, float progress)
137{
138 BakeAPIRender *bj = static_cast<BakeAPIRender *>(bjv);
139
140 if (bj->progress && *bj->progress != progress) {
141 *bj->progress = progress;
142
143 /* make jobs timer to send notifier */
144 *(bj->do_update) = true;
145 }
146}
147
149static wmOperatorStatus bake_modal(bContext *C, wmOperator * /*op*/, const wmEvent *event)
150{
151 /* no running blender, remove handler and pass through */
154 }
155
156 /* running render */
157 switch (event->type) {
158 case EVT_ESCKEY: {
159 G.is_break = true;
161 }
162 default: {
163 break;
164 }
165 }
167}
168
169/* This could be bake_break but we would be required to use const_cast. */
171{
172 /* bkr can be null when called via bake_exec. */
173 return G.is_break || (bkr != nullptr && (bkr->stop && *(bkr->stop)));
174}
175
180static bool bake_break(void *rjv)
181{
182 const BakeAPIRender *bkr = static_cast<BakeAPIRender *>(rjv);
183 return bake_has_been_canceled(bkr);
184}
185
187{
188 if (area && area->spacetype == SPACE_IMAGE) { /* in case the user changed while baking */
189 SpaceImage *sima = area->spacedata.first_as<SpaceImage>();
190 if (sima) {
191 sima->image = image;
192 }
193 }
194}
195
196/* Bias almost-flat normals in tangent space to be flat to avoid artifacts in byte textures.
197 * For some types of normal baking, especially bevels, you can end up with a small amount
198 * of noise in the result. Since the border between pixel value 127 and 128 is exactly 0.5,
199 * the tiniest amount of deviation will flip between those two, and increasing samples won't
200 * help - you always end up with visible "dents" in the resulting normal map.
201 * Therefore, this function snaps values that are less than half a quantization level away
202 * from 0.5 to 0.5, so that they consistently become pixel value 128.
203 * This only makes sense for byte textures of course, and is not used when baking to float
204 * textures (which includes 16-bit formats). Also, it's only applied to the first two channels,
205 * since on flat surfaces the Z channel will be close enough to 1.0 to reliably end up on 255.
206 */
208 float *rect, int channels, int width, int height, int stride)
209{
210 BLI_assert(channels >= 3);
211
212 for (int y = 0; y < height; y++) {
213 float *pixels = rect + size_t(stride) * y * channels;
214 for (int x = 0; x < width; x++, pixels += channels) {
215 if (fabsf(pixels[0] - 0.5f) < 1.0f / 255.0f) {
216 pixels[0] = 0.5f + 1e-5f;
217 }
218 if (fabsf(pixels[1] - 0.5f) < 1.0f / 255.0f) {
219 pixels[1] = 0.5f + 1e-5f;
220 }
221 }
222 }
223}
224
226 const int image_tile_number,
227 BakePixel pixel_array[],
228 float *buffer,
229 const int width,
230 const int height,
231 const int margin,
232 const char margin_type,
233 const bool is_clear,
234 const bool is_noncolor,
235 const bool is_tangent_normal,
236 Mesh const *mesh_eval,
237 const StringRef uv_layer,
238 const float uv_offset[2])
239{
240 ImBuf *ibuf;
241 void *lock;
242 bool is_float;
243 char *mask_buffer = nullptr;
244 const size_t pixels_num = size_t(width) * size_t(height);
245
246 ImageUser iuser;
247 BKE_imageuser_default(&iuser);
248 iuser.tile = image_tile_number;
249 ibuf = BKE_image_acquire_ibuf(image, &iuser, &lock);
250
251 if (!ibuf) {
252 return false;
253 }
254
255 if (margin > 0 || !is_clear) {
256 mask_buffer = MEM_new_array_zeroed<char>(pixels_num, "Bake Mask");
257 RE_bake_mask_fill(pixel_array, pixels_num, mask_buffer);
258 }
259
260 is_float = (ibuf->float_data() != nullptr);
261
262 /* colormanagement conversions */
263 if (!is_noncolor) {
264 const char *from_colorspace;
265 const char *to_colorspace;
266
268
269 if (is_float) {
270 to_colorspace = IMB_colormanagement_get_float_colorspace(ibuf);
271 }
272 else {
273 to_colorspace = IMB_colormanagement_get_byte_colorspace(ibuf);
274 }
275
276 if (from_colorspace != to_colorspace) {
278 buffer, ibuf->x, ibuf->y, ibuf->channels, from_colorspace, to_colorspace, false);
279 }
280 }
281 else if (!is_float && is_tangent_normal) {
282 /* bias neutral values when converting tangent-space normal maps to byte textures */
283 bias_tangent_normal_pixels(buffer, ibuf->channels, ibuf->x, ibuf->y, ibuf->x);
284 }
285
286 /* populates the ImBuf */
287 if (is_clear) {
288 if (is_float) {
290 ibuf->float_data_for_write(), buffer, ibuf->channels, ibuf->x, ibuf->y);
291 }
292 else {
294 buffer,
295 ibuf->channels,
296 ibuf->dither,
297 false,
298 ibuf->x,
299 ibuf->y,
300 ibuf->x);
301 }
302 }
303 else {
304 if (is_float) {
306 ibuf->float_data_for_write(), buffer, ibuf->channels, ibuf->x, ibuf->y, mask_buffer);
307 }
308 else {
310 buffer,
311 ibuf->channels,
312 ibuf->dither,
313 ibuf->x,
314 ibuf->y,
315 mask_buffer);
316 }
317 }
318
319 /* margins */
320 if (margin > 0) {
321 RE_bake_margin(ibuf, mask_buffer, margin, margin_type, mesh_eval, uv_layer, uv_offset);
322 }
323
325 IMB_mark_dirty(ibuf);
326
327 BKE_image_release_ibuf(image, ibuf, nullptr);
328
329 if (mask_buffer) {
330 MEM_delete(mask_buffer);
331 }
332
333 return true;
334}
335
336/* force OpenGL reload */
338{
339 for (int i = 0; i < targets->images_num; i++) {
340 Image *ima = targets->images[i].image;
341
342 if (ima) {
343 DEG_id_tag_update(&ima->id, 0);
344 }
345 }
346}
347
348static bool write_external_bake_pixels(const char *filepath,
349 BakePixel pixel_array[],
350 float *buffer,
351 const int width,
352 const int height,
353 const int margin,
354 const int margin_type,
355 ImageFormatData const *im_format,
356 const bool is_noncolor,
357 const bool is_tangent_normal,
358 Mesh const *mesh_eval,
359 const StringRef uv_layer,
360 const float uv_offset[2])
361{
362 ImBuf *ibuf = nullptr;
363 bool ok = false;
364 bool is_float = im_format->depth > 8;
365
366 /* create a new ImBuf */
367 ibuf = IMB_allocImBuf(width, height, is_float ? ImBufFlags::FloatData : ImBufFlags::ByteData);
368 ibuf->color_mode = im_format->color_mode;
369
370 if (!ibuf) {
371 return false;
372 }
373
374 /* populates the ImBuf */
375 if (is_float) {
377 ibuf->float_data_for_write(), buffer, ibuf->channels, ibuf->x, ibuf->y);
378 }
379 else {
380 if (!is_noncolor) {
381 const char *from_colorspace = IMB_colormanagement_role_colorspace_name_get(
383 const char *to_colorspace = IMB_colormanagement_get_byte_colorspace(ibuf);
385 buffer, ibuf->x, ibuf->y, ibuf->channels, from_colorspace, to_colorspace, false);
386 }
387 else if (is_tangent_normal) {
388 /* bias neutral values when converting tangent-space normal maps to byte textures */
389 bias_tangent_normal_pixels(buffer, ibuf->channels, ibuf->x, ibuf->y, ibuf->x);
390 }
391
393 buffer,
394 ibuf->channels,
395 ibuf->dither,
396 false,
397 ibuf->x,
398 ibuf->y,
399 ibuf->x);
400 }
401
402 /* margins */
403 if (margin > 0) {
404 char *mask_buffer = nullptr;
405 const size_t pixels_num = size_t(width) * size_t(height);
406
407 mask_buffer = MEM_new_array_zeroed<char>(pixels_num, "Bake Mask");
408 RE_bake_mask_fill(pixel_array, pixels_num, mask_buffer);
409 RE_bake_margin(ibuf, mask_buffer, margin, margin_type, mesh_eval, uv_layer, uv_offset);
410
411 if (mask_buffer) {
412 MEM_delete(mask_buffer);
413 }
414 }
415
416 if ((ok = BKE_imbuf_write(ibuf, filepath, im_format))) {
417#ifndef WIN32
418 chmod(filepath, S_IRUSR | S_IWUSR);
419#endif
420 // printf("%s saving bake map: '%s'\n", __func__, filepath);
421 }
422
423 /* garbage collection */
424 IMB_freeImBuf(ibuf);
425
426 return ok;
427}
428
429static bool is_noncolor_pass(eScenePassType pass_type)
430{
431 return ELEM(pass_type,
439}
440
441/* if all is good tag image and return true */
442static bool bake_object_check(const Main &bmain,
443 const Scene *scene,
444 ViewLayer *view_layer,
445 Object *ob,
446 const eBakeTarget target,
447 ReportList *reports)
448{
449 BKE_view_layer_synced_ensure(bmain, scene, view_layer);
450 Base *base = BKE_view_layer_base_find(view_layer, ob);
451
452 if (base == nullptr) {
453 BKE_reportf(reports, RPT_ERROR, "Object \"%s\" is not in view layer", ob->id.name + 2);
454 return false;
455 }
456
457 if (!(base->flag & BASE_ENABLED_RENDER)) {
458 BKE_reportf(reports, RPT_ERROR, "Object \"%s\" is not enabled for rendering", ob->id.name + 2);
459 return false;
460 }
461
462 if (ob->type != OB_MESH) {
463 BKE_reportf(reports, RPT_ERROR, "Object \"%s\" is not a mesh", ob->id.name + 2);
464 return false;
465 }
466
467 Mesh *mesh = id_cast<Mesh *>(ob->data);
468
469 if (mesh->faces_num == 0) {
470 BKE_reportf(reports, RPT_ERROR, "No faces found in the object \"%s\"", ob->id.name + 2);
471 return false;
472 }
473
474 if (target == R_BAKE_TARGET_VERTEX_COLORS) {
475 if (!BKE_id_attributes_color_find(&mesh->id, mesh->active_color_attribute)) {
476 BKE_reportf(reports,
477 RPT_ERROR,
478 "Mesh does not have an active color attribute \"%s\"",
479 mesh->id.name + 2);
480 return false;
481 }
482 }
483 else if (target == R_BAKE_TARGET_IMAGE_TEXTURES) {
484 if (!mesh->uv_map_names().contains(mesh->active_uv_map_name())) {
486 reports, RPT_ERROR, "No active UV layer found in the object \"%s\"", ob->id.name + 2);
487 return false;
488 }
489
490 for (int i = 0; i < ob->totcol; i++) {
491 const bNodeTree *ntree = nullptr;
492 const bNode *node = nullptr;
493 const int mat_nr = i + 1;
494 Image *image;
495 ED_object_get_active_image(ob, mat_nr, &image, nullptr, &node, &ntree);
496
497 /* Don't bake to unselected images. */
498 if (node && !node->is_selected()) {
499 image = nullptr;
500 }
501
502 if (image) {
503
504 if (node) {
506 /* we don't return false since this may be a false positive
507 * this can't be RPT_ERROR though, otherwise it prevents
508 * multiple highpoly objects to be baked at once */
509 BKE_reportf(reports,
510 RPT_INFO,
511 "Circular dependency for image \"%s\" from object \"%s\"",
512 image->id.name + 2,
513 ob->id.name + 2);
514 }
515 }
516
517 for (ImageTile &tile : image->tiles) {
518 ImageUser iuser;
519 BKE_imageuser_default(&iuser);
520 iuser.tile = tile.tile_number;
521
522 void *lock;
523 ImBuf *ibuf = BKE_image_acquire_ibuf(image, &iuser, &lock);
524
525 if (ibuf) {
527 }
528 else {
529 BKE_reportf(reports,
530 RPT_ERROR,
531 "Uninitialized image \"%s\" from object \"%s\"",
532 image->id.name + 2,
533 ob->id.name + 2);
534
536 return false;
537 }
538 }
539 }
540 else {
541 Material *mat = BKE_object_material_get(ob, mat_nr);
542 if (mat != nullptr) {
543 BKE_reportf(reports,
544 RPT_INFO,
545 "No active and selected image texture node found in material \"%s\" (%d) "
546 "for object \"%s\"",
547 mat->id.name + 2,
548 i,
549 ob->id.name + 2);
550 }
551 else {
552 BKE_reportf(reports,
553 RPT_INFO,
554 "No active image found in material slot (%d) for object \"%s\"",
555 i,
556 ob->id.name + 2);
557 }
558 continue;
559 }
560
561 image->id.tag |= ID_TAG_DOIT;
562 }
563 }
564
565 return true;
566}
567
569 const int pass_filter,
570 ReportList *reports)
571{
572 switch (pass_type) {
574 if ((pass_filter & R_BAKE_PASS_FILTER_EMIT) != 0) {
575 return true;
576 }
577
578 if (((pass_filter & R_BAKE_PASS_FILTER_DIRECT) != 0) ||
579 ((pass_filter & R_BAKE_PASS_FILTER_INDIRECT) != 0))
580 {
581 if (((pass_filter & R_BAKE_PASS_FILTER_DIFFUSE) != 0) ||
582 ((pass_filter & R_BAKE_PASS_FILTER_GLOSSY) != 0) ||
583 ((pass_filter & R_BAKE_PASS_FILTER_TRANSM) != 0) ||
584 ((pass_filter & R_BAKE_PASS_FILTER_SUBSURFACE) != 0))
585 {
586 return true;
587 }
588
589 BKE_report(reports,
590 RPT_ERROR,
591 "Combined bake pass requires Emission, or a light pass with "
592 "Direct or Indirect contributions enabled");
593
594 return false;
595 }
596 BKE_report(reports,
597 RPT_ERROR,
598 "Combined bake pass requires Emission, or a light pass with "
599 "Direct or Indirect contributions enabled");
600 return false;
605 if (((pass_filter & R_BAKE_PASS_FILTER_COLOR) != 0) ||
606 ((pass_filter & R_BAKE_PASS_FILTER_DIRECT) != 0) ||
607 ((pass_filter & R_BAKE_PASS_FILTER_INDIRECT) != 0))
608 {
609 return true;
610 }
611 else {
612 BKE_report(reports,
613 RPT_ERROR,
614 "Bake pass requires Direct, Indirect, or Color contributions to be enabled");
615 return false;
616 }
617 break;
618 default:
619 return true;
620 break;
621 }
622}
623
624/* before even getting in the bake function we check for some basic errors */
625static bool bake_objects_check(Main *bmain,
626 const Scene *scene,
627 ViewLayer *view_layer,
628 Object *ob,
629 Span<PointerRNA> selected_objects,
630 ReportList *reports,
631 const bool is_selected_to_active,
632 const eBakeTarget target)
633{
634 /* error handling and tag (in case multiple materials share the same image) */
636
637 if (is_selected_to_active) {
638 int tot_objects = 0;
639
640 if (!bake_object_check(*bmain, scene, view_layer, ob, target, reports)) {
641 return false;
642 }
643
644 for (const PointerRNA &ptr : selected_objects) {
645 Object *ob_iter = static_cast<Object *>(ptr.data);
646 if (ob_iter == ob) {
647 continue;
648 }
649
650 if (ELEM(ob_iter->type, OB_MESH, OB_FONT, OB_CURVES_LEGACY, OB_SURF, OB_MBALL) == false) {
651 BKE_reportf(reports,
652 RPT_ERROR,
653 "Object \"%s\" is not a mesh or cannot be converted to a mesh (Curve, Text, "
654 "Surface or Metaball)",
655 ob_iter->id.name + 2);
656 return false;
657 }
658 tot_objects += 1;
659 }
660
661 if (tot_objects == 0) {
662 BKE_report(reports, RPT_ERROR, "No valid selected objects");
663 return false;
664 }
665 }
666 else {
667 if (selected_objects.is_empty()) {
668 BKE_report(reports, RPT_ERROR, "No valid selected objects");
669 return false;
670 }
671
672 for (const PointerRNA &ptr : selected_objects) {
674 *bmain, scene, view_layer, static_cast<Object *>(ptr.data), target, reports))
675 {
676 return false;
677 }
678 }
679 }
680 return true;
681}
682
683/* it needs to be called after bake_objects_check since the image tagging happens there */
684static void bake_targets_clear(Main *bmain, const bool is_tangent)
685{
686 for (Image &image : bmain->images) {
687 if ((image.id.tag & ID_TAG_DOIT) != 0) {
688 RE_bake_ibuf_clear(&image, is_tangent);
689 }
690 }
691}
692
693/* create new mesh with edit mode changes and modifiers applied */
694static Mesh *bake_mesh_new_from_object(Depsgraph *depsgraph,
695 Object *object,
696 const bool preserve_origindex)
697{
698 Mesh *mesh = BKE_mesh_new_from_object(depsgraph, object, false, preserve_origindex, true);
699
700 if (mesh->normals_domain() == bke::MeshNormalDomain::Corner) {
702 }
703
704 return mesh;
705}
706
707/* Image Bake Targets */
708
710 BakeTargets *targets,
711 Object *ob,
712 ReportList *reports)
713{
714 int materials_num = ob->totcol;
715
716 if (materials_num == 0) {
717 if (bkr->save_mode == R_BAKE_SAVE_INTERNAL) {
719 reports, RPT_ERROR, "No active image found, add a material or bake to an external file");
720 return false;
721 }
722 if (bkr->is_split_materials) {
724 reports,
725 RPT_ERROR,
726 "No active image found, add a material or bake without the Split Materials option");
727 return false;
728 }
729 }
730
731 /* Allocate material mapping. */
732 targets->materials_num = materials_num;
734
735 /* Error handling and tag (in case multiple materials share the same image). */
737
738 targets->images = nullptr;
739
740 for (int i = 0; i < materials_num; i++) {
741 Image *image;
742 const bNode *node = nullptr;
743 ED_object_get_active_image(ob, i + 1, &image, nullptr, &node, nullptr);
744
745 /* Don't bake to unselected images. */
746 if (node && !node->is_selected()) {
747 image = nullptr;
748 }
749
750 targets->material_to_image[i] = image;
751
752 /* Some materials have no image, we just ignore those cases.
753 * Also setup each image only once. */
754 if (image && !(image->id.tag & ID_TAG_DOIT)) {
755 for (ImageTile &tile : image->tiles) {
756 /* Add bake image. */
757 targets->images = static_cast<BakeImage *>(
758 MEM_realloc_zeroed(targets->images, sizeof(BakeImage) * (targets->images_num + 1)));
759 targets->images[targets->images_num].image = image;
760 targets->images[targets->images_num].tile_number = tile.tile_number;
761 targets->images_num++;
762 }
763
764 image->id.tag |= ID_TAG_DOIT;
765 }
766 }
767
768 return true;
769}
770
772 BakeTargets *targets,
773 Object *ob,
774 ReportList *reports)
775{
776 if (!bake_targets_init_image_textures(bkr, targets, ob, reports)) {
777 return false;
778 }
779
780 /* Saving to image datablocks. */
781 for (int i = 0; i < targets->images_num; i++) {
782 BakeImage *bk_image = &targets->images[i];
783
784 ImageUser iuser;
785 BKE_imageuser_default(&iuser);
786 iuser.tile = bk_image->tile_number;
787
788 void *lock;
789 ImBuf *ibuf = BKE_image_acquire_ibuf(bk_image->image, &iuser, &lock);
790
791 if (ibuf) {
792 bk_image->width = ibuf->x;
793 bk_image->height = ibuf->y;
794 bk_image->offset = targets->pixels_num;
795 BKE_image_get_tile_uv(bk_image->image, bk_image->tile_number, bk_image->uv_offset);
796
797 targets->pixels_num += size_t(ibuf->x) * size_t(ibuf->y);
798 }
799 else {
800 BKE_image_release_ibuf(bk_image->image, ibuf, lock);
801 BKE_reportf(reports, RPT_ERROR, "Uninitialized image %s", bk_image->image->id.name + 2);
802 return false;
803 }
804 BKE_image_release_ibuf(bk_image->image, ibuf, lock);
805 }
806
807 return true;
808}
809
811 BakeTargets *targets,
812 Object *ob,
813 BakePixel *pixel_array,
814 ReportList *reports,
815 Mesh *mesh_eval)
816{
817 bool all_ok = true;
818 const bool is_tangent_normal = (bkr->pass_type == SCE_PASS_NORMAL) &&
820
821 for (int i = 0; i < targets->images_num; i++) {
822 BakeImage *bk_image = &targets->images[i];
823 const bool ok = write_internal_bake_pixels(bk_image->image,
824 bk_image->tile_number,
825 pixel_array + bk_image->offset,
826 targets->result +
827 bk_image->offset * targets->channels_num,
828 bk_image->width,
829 bk_image->height,
830 bkr->margin,
831 bkr->margin_type,
832 bkr->is_clear,
833 targets->is_noncolor,
834 is_tangent_normal,
835 mesh_eval,
836 bkr->uv_layer,
837 bk_image->uv_offset);
838
839 /* might be read by UI to set active image for display */
840 bake_update_image(bkr->area, bk_image->image);
841
842 if (!ok) {
843 BKE_reportf(reports,
844 RPT_ERROR,
845 "Problem saving the bake map internally for object \"%s\"",
846 ob->id.name + 2);
847 all_ok = false;
848 }
849 else {
851 reports, RPT_INFO, "Baking map saved to internal image, save it externally or pack it");
852 }
853 }
854
855 return all_ok;
856}
857
859 BakeTargets *targets,
860 Object *ob,
861 ReportList *reports)
862{
863 if (!bake_targets_init_image_textures(bkr, targets, ob, reports)) {
864 return false;
865 }
866
867 /* Saving to disk. */
868 for (int i = 0; i < targets->images_num; i++) {
869 BakeImage *bk_image = &targets->images[i];
870
871 bk_image->width = bkr->width;
872 bk_image->height = bkr->height;
873 bk_image->offset = targets->pixels_num;
874
875 targets->pixels_num += size_t(bkr->width) * size_t(bkr->height);
876
877 if (!bkr->is_split_materials) {
878 break;
879 }
880 }
881
882 if (!bkr->is_split_materials) {
883 /* saving a single image */
884 for (int i = 0; i < targets->materials_num; i++) {
885 targets->material_to_image[i] = targets->images[0].image;
886 }
887 }
888
889 return true;
890}
891
893 BakeTargets *targets,
894 Object *ob,
895 Object *ob_eval,
896 Mesh *mesh_eval,
897 BakePixel *pixel_array,
898 ReportList *reports)
899{
900 bool all_ok = true;
901 const bool is_tangent_normal = (bkr->pass_type == SCE_PASS_NORMAL) &&
903
904 for (int i = 0; i < targets->images_num; i++) {
905 BakeImage *bk_image = &targets->images[i];
906
907 BakeData *bake = &bkr->scene->r.bake;
908 char filepath[FILE_MAX];
909
911 filepath,
912 bkr->filepath,
914 nullptr,
915 0,
916 bake->im_format.imtype,
917 true,
918 false,
919 nullptr);
920 BLI_assert_msg(errors.is_empty(),
921 "Path parsing errors should only occur when a variable map is provided.");
922 UNUSED_VARS_NDEBUG(errors);
923
924 if (bkr->is_automatic_name) {
925 BLI_path_suffix(filepath, FILE_MAX, ob->id.name + 2, "_");
926 BLI_path_suffix(filepath, FILE_MAX, bkr->identifier, "_");
927 }
928
929 if (bkr->is_split_materials) {
930 if (ob_eval->mat[i]) {
931 BLI_path_suffix(filepath, FILE_MAX, ob_eval->mat[i]->id.name + 2, "_");
932 }
933 else if (mesh_eval->mat[i]) {
934 BLI_path_suffix(filepath, FILE_MAX, mesh_eval->mat[i]->id.name + 2, "_");
935 }
936 else {
937 /* if everything else fails, use the material index */
938 char tmp[5];
939 SNPRINTF_UTF8(tmp, "%d", i % 1000);
940 BLI_path_suffix(filepath, FILE_MAX, tmp, "_");
941 }
942 }
943
944 if (bk_image->tile_number) {
945 char tmp[12];
946 SNPRINTF_UTF8(tmp, "%d", bk_image->tile_number);
947 BLI_path_suffix(filepath, FILE_MAX, tmp, "_");
948 }
949
950 /* save it externally */
951 const bool ok = write_external_bake_pixels(filepath,
952 pixel_array + bk_image->offset,
953 targets->result +
954 bk_image->offset * targets->channels_num,
955 bk_image->width,
956 bk_image->height,
957 bkr->margin,
958 bkr->margin_type,
959 &bake->im_format,
960 targets->is_noncolor,
961 is_tangent_normal,
962 mesh_eval,
963 bkr->uv_layer,
964 bk_image->uv_offset);
965
966 if (!ok) {
967 BKE_reportf(reports, RPT_ERROR, "Problem saving baked map in \"%s\"", filepath);
968 all_ok = false;
969 }
970 else {
971 BKE_reportf(reports, RPT_INFO, "Baking map written to \"%s\"", filepath);
972 }
973
974 if (!bkr->is_split_materials) {
975 break;
976 }
977 }
978
979 return all_ok;
980}
981
982/* Vertex Color Bake Targets */
983
985 BakeTargets *targets,
986 Object *ob,
987 ReportList *reports)
988{
989 if (ob->type != OB_MESH) {
990 BKE_report(reports, RPT_ERROR, "Color attribute baking is only supported for mesh objects");
991 return false;
992 }
993
994 Mesh *mesh = id_cast<Mesh *>(ob->data);
995 if (!BKE_id_attributes_color_find(&mesh->id, mesh->active_color_attribute)) {
996 BKE_report(reports, RPT_ERROR, "No active color attribute to bake to");
997 return false;
998 }
999
1000 /* Ensure mesh and editmesh topology are in sync. */
1001 editmode_load(bmain, ob);
1002
1003 targets->images = MEM_new_zeroed<BakeImage>(__func__);
1004 targets->images_num = 1;
1005
1006 targets->material_to_image = MEM_new_array_zeroed<Image *>(ob->totcol, __func__);
1007 targets->materials_num = ob->totcol;
1008
1009 BakeImage *bk_image = &targets->images[0];
1010 bk_image->width = std::ceil(std::sqrt(mesh->corners_num));
1011 bk_image->height = bk_image->width;
1012 bk_image->offset = 0;
1013 bk_image->image = nullptr;
1014
1015 targets->pixels_num = bk_image->width * bk_image->height;
1016
1017 return true;
1018}
1019
1020static int find_original_loop(const OffsetIndices<int> orig_faces,
1021 const Span<int> orig_corner_verts,
1022 const int *vert_origindex,
1023 const int *poly_origindex,
1024 const int poly_eval,
1025 const int vert_eval)
1026{
1027 /* Get original vertex and face index. There is currently no loop mapping
1028 * in modifier stack evaluation. */
1029 const int vert_orig = vert_origindex[vert_eval];
1030 const int poly_orig_index = poly_origindex[poly_eval];
1031
1032 if (vert_orig == ORIGINDEX_NONE || poly_orig_index == ORIGINDEX_NONE) {
1033 return ORIGINDEX_NONE;
1034 }
1035
1036 /* Find matching loop with original vertex in original face. */
1037 const IndexRange orig_face = orig_faces[poly_orig_index];
1038 const int *poly_verts_orig = &orig_corner_verts[orig_face.start()];
1039 for (int j = 0; j < orig_face.size(); ++j) {
1040 if (poly_verts_orig[j] == vert_orig) {
1041 return orig_face.start() + j;
1042 }
1043 }
1044
1045 return ORIGINDEX_NONE;
1046}
1047
1049 Object *ob,
1050 Mesh *mesh_eval,
1051 BakePixel *pixel_array)
1052{
1053 Mesh *mesh = id_cast<Mesh *>(ob->data);
1054 const int pixels_num = targets->pixels_num;
1055
1056 /* Initialize blank pixels. */
1057 for (int i = 0; i < pixels_num; i++) {
1058 BakePixel *pixel = &pixel_array[i];
1059
1060 pixel->primitive_id = -1;
1061 pixel->object_id = 0;
1062 pixel->seed = 0;
1063 pixel->du_dx = 0.0f;
1064 pixel->du_dy = 0.0f;
1065 pixel->dv_dx = 0.0f;
1066 pixel->dv_dy = 0.0f;
1067 pixel->uv[0] = 0.0f;
1068 pixel->uv[1] = 0.0f;
1069 pixel->is_margin = false;
1070 }
1071
1072 /* Populate through adjacent triangles, first triangle wins. */
1073 const int corner_tris_num = poly_to_tri_count(mesh_eval->faces_num, mesh_eval->corners_num);
1074 int3 *corner_tris = MEM_new_array_uninitialized<int3>(corner_tris_num, __func__);
1075
1076 const Span<int> corner_verts = mesh_eval->corner_verts();
1077 bke::mesh::corner_tris_calc(mesh_eval->vert_positions(),
1078 mesh_eval->faces(),
1079 corner_verts,
1080 {corner_tris, corner_tris_num});
1081 const Span<int> tri_faces = mesh_eval->corner_tri_faces();
1082
1083 /* For mapping back to original mesh in case there are modifiers. */
1084 const int *vert_origindex = static_cast<const int *>(
1086 const int *poly_origindex = static_cast<const int *>(
1088 const OffsetIndices orig_faces = mesh->faces();
1089 const Span<int> orig_corner_verts = mesh->corner_verts();
1090
1091 for (int i = 0; i < corner_tris_num; i++) {
1092 const int3 &tri = corner_tris[i];
1093 const int face_i = tri_faces[i];
1094
1095 for (int j = 0; j < 3; j++) {
1096 uint l = tri[j];
1097 const int v = corner_verts[l];
1098
1099 /* Map back to original loop if there are modifiers. */
1100 if (vert_origindex != nullptr && poly_origindex != nullptr) {
1102 orig_faces, orig_corner_verts, vert_origindex, poly_origindex, face_i, v);
1103 if (l == ORIGINDEX_NONE || l >= mesh->corners_num) {
1104 continue;
1105 }
1106 }
1107
1108 BakePixel *pixel = &pixel_array[l];
1109
1110 if (pixel->primitive_id != -1) {
1111 continue;
1112 }
1113
1114 pixel->primitive_id = i;
1115
1116 /* Seed is the vertex, so that sampling noise is coherent for the same
1117 * vertex, but different corners can still have different normals,
1118 * materials and UVs. */
1119 pixel->seed = v;
1120
1121 /* Barycentric coordinates. */
1122 if (j == 0) {
1123 pixel->uv[0] = 1.0f;
1124 pixel->uv[1] = 0.0f;
1125 }
1126 else if (j == 1) {
1127 pixel->uv[0] = 0.0f;
1128 pixel->uv[1] = 1.0f;
1129 }
1130 else if (j == 2) {
1131 pixel->uv[0] = 0.0f;
1132 pixel->uv[1] = 0.0f;
1133 }
1134 }
1135 }
1136
1137 MEM_delete(corner_tris);
1138}
1139
1140static void bake_result_add_to_rgba(float rgba[4], const float *result, const int channels_num)
1141{
1142 if (channels_num == 4) {
1143 add_v4_v4(rgba, result);
1144 }
1145 else if (channels_num == 3) {
1146 add_v3_v3(rgba, result);
1147 rgba[3] += 1.0f;
1148 }
1149 else {
1150 rgba[0] += result[0];
1151 rgba[1] += result[0];
1152 rgba[2] += result[0];
1153 rgba[3] += 1.0f;
1154 }
1155}
1156
1158 const int num,
1159 const bool is_noncolor,
1160 ColorGeometry4b *byte_colors)
1161{
1162 if (is_noncolor) {
1163 for (int i = 0; i < num; i++) {
1164 unit_float_to_uchar_clamp_v4(&byte_colors->r, float_colors[i]);
1165 }
1166 }
1167 else {
1168 for (int i = 0; i < num; i++) {
1169 linearrgb_to_srgb_uchar4(&byte_colors[i].r, float_colors[i]);
1170 }
1171 }
1172}
1173
1175{
1176 Mesh *mesh = id_cast<Mesh *>(ob->data);
1177 const StringRef attr_name = mesh->active_color_attribute;
1178 const bke::AttrDomain domain = [&]() {
1180 return BM_data_layer_lookup(*bm, attr_name).domain;
1181 }
1182 bke::AttributeAccessor attributes = mesh->attributes();
1183 return attributes.lookup_meta_data(attr_name)->domain;
1184 }();
1185
1186 const int channels_num = targets->channels_num;
1187 const bool is_noncolor = targets->is_noncolor;
1188 const float *result = targets->result;
1189
1190 if (domain == bke::AttrDomain::Point) {
1191 const int totvert = mesh->verts_num;
1192 const int totloop = mesh->corners_num;
1193
1194 ColorGeometry4f *mcol = MEM_new_array_zeroed<ColorGeometry4f>(totvert, __func__);
1195
1196 /* Accumulate float vertex colors in scene linear color space. */
1197 int *num_loops_for_vertex = MEM_new_array_zeroed<int>(mesh->verts_num, "num_loops_for_vertex");
1198
1199 const Span<int> corner_verts = mesh->corner_verts();
1200 for (int i = 0; i < totloop; i++) {
1201 const int v = corner_verts[i];
1202 bake_result_add_to_rgba(mcol[v], &result[i * channels_num], channels_num);
1203 num_loops_for_vertex[v]++;
1204 }
1205
1206 /* Normalize for number of loops. */
1207 for (int i = 0; i < totvert; i++) {
1208 if (num_loops_for_vertex[i] > 0) {
1209 mul_v4_fl(mcol[i], 1.0f / num_loops_for_vertex[i]);
1210 }
1211 }
1212
1214 const BMDataLayerLookup attr = BM_data_layer_lookup(*bm, attr_name);
1215 BMVert *v;
1216 BMIter viter;
1217 int i = 0;
1218 BM_ITER_MESH (v, &viter, bm, BM_VERTS_OF_MESH) {
1219 void *data = BM_ELEM_CD_GET_VOID_P(v, attr.offset);
1220 if (attr.type == bke::AttrType::ColorFloat) {
1221 memcpy(data, &mcol[i], sizeof(MPropCol));
1222 }
1223 else if (attr.type == bke::AttrType::ColorByte) {
1225 &mcol[i], 1, is_noncolor, static_cast<ColorGeometry4b *>(data));
1226 }
1227 i++;
1228 }
1229 }
1230 else {
1231 bke::MutableAttributeAccessor attributes = mesh->attributes_for_write();
1232 bke::GSpanAttributeWriter attr = attributes.lookup_for_write_span(attr_name);
1233 if (attr.span.type().is<ColorGeometry4f>()) {
1234 memcpy(attr.span.data(), mcol, sizeof(ColorGeometry4f) * mesh->verts_num);
1235 }
1236 else if (attr.span.type().is<ColorGeometry4b>()) {
1238 mcol, totvert, is_noncolor, static_cast<ColorGeometry4b *>(attr.span.data()));
1239 }
1240 attr.finish();
1241 }
1242
1243 MEM_delete(mcol);
1244
1245 MEM_SAFE_DELETE(num_loops_for_vertex);
1246 }
1247 else if (domain == bke::AttrDomain::Corner) {
1249 const BMDataLayerLookup attr = BM_data_layer_lookup(*bm, attr_name);
1250 BMFace *f;
1251 BMIter fiter;
1252 int i = 0;
1253 BM_ITER_MESH (f, &fiter, bm, BM_FACES_OF_MESH) {
1254 BMLoop *l;
1255 BMIter liter;
1256 BM_ITER_ELEM (l, &liter, f, BM_LOOPS_OF_FACE) {
1258 zero_v4(color);
1259 bake_result_add_to_rgba(color, &result[i * channels_num], channels_num);
1260 i++;
1261
1262 void *data = BM_ELEM_CD_GET_VOID_P(l, attr.offset);
1263 if (attr.type == bke::AttrType::ColorFloat) {
1264 memcpy(data, &color, sizeof(MPropCol));
1265 }
1266 else if (attr.type == bke::AttrType::ColorByte) {
1268 &color, 1, is_noncolor, static_cast<ColorGeometry4b *>(data));
1269 }
1270 }
1271 }
1272 }
1273 else {
1274 bke::MutableAttributeAccessor attributes = mesh->attributes_for_write();
1275 bke::GSpanAttributeWriter attr = attributes.lookup_for_write_span(attr_name);
1276 if (attr.span.type().is<ColorGeometry4f>()) {
1277 MutableSpan colors = attr.span.typed<ColorGeometry4f>();
1278 for (int i = 0; i < mesh->corners_num; i++) {
1279 zero_v4(colors[i]);
1280 bake_result_add_to_rgba(colors[i], &result[i * channels_num], channels_num);
1281 }
1282 }
1283 else if (attr.span.type().is<ColorGeometry4b>()) {
1284 MutableSpan colors = attr.span.typed<ColorGeometry4b>();
1285 for (int i = 0; i < mesh->corners_num; i++) {
1287 zero_v4(color);
1288 bake_result_add_to_rgba(color, &result[i * channels_num], channels_num);
1289 convert_float_color_to_byte_color(&color, 1, is_noncolor, &colors[i]);
1290 }
1291 }
1292 attr.finish();
1293 }
1294 }
1295
1297
1298 return true;
1299}
1300
1301/* Bake Targets */
1302
1303static bool bake_targets_init(const BakeAPIRender *bkr,
1304 BakeTargets *targets,
1305 Object *ob,
1306 Object *ob_eval,
1307 ReportList *reports)
1308{
1310 if (bkr->save_mode == R_BAKE_SAVE_INTERNAL) {
1311 if (!bake_targets_init_internal(bkr, targets, ob_eval, reports)) {
1312 return false;
1313 }
1314 }
1315 else if (bkr->save_mode == R_BAKE_SAVE_EXTERNAL) {
1316 if (!bake_targets_init_external(bkr, targets, ob_eval, reports)) {
1317 return false;
1318 }
1319 }
1320 }
1321 else if (bkr->target == R_BAKE_TARGET_VERTEX_COLORS) {
1322 if (!bake_targets_init_vertex_colors(bkr->main, targets, ob, reports)) {
1323 return false;
1324 }
1325 }
1326
1327 if (targets->pixels_num == 0) {
1328 return false;
1329 }
1330
1331 targets->is_noncolor = is_noncolor_pass(bkr->pass_type);
1332 targets->channels_num = RE_pass_depth(bkr->pass_type);
1333 targets->result = MEM_new_array_zeroed<float>(targets->channels_num * targets->pixels_num,
1334 "bake return pixels");
1335
1336 return true;
1337}
1338
1340 BakeTargets *targets,
1341 Object *ob,
1342 Mesh *mesh_eval,
1343 BakePixel *pixel_array)
1344{
1345 if (bkr->target == R_BAKE_TARGET_VERTEX_COLORS) {
1346 bake_targets_populate_pixels_color_attributes(targets, ob, mesh_eval, pixel_array);
1347 }
1348 else {
1350 mesh_eval, pixel_array, targets->pixels_num, targets, bkr->uv_layer, bkr->margin);
1351 }
1352}
1353
1354static bool bake_targets_output(const BakeAPIRender *bkr,
1355 BakeTargets *targets,
1356 Object *ob,
1357 Object *ob_eval,
1358 Mesh *mesh_eval,
1359 BakePixel *pixel_array,
1360 ReportList *reports)
1361{
1363 if (bkr->save_mode == R_BAKE_SAVE_INTERNAL) {
1364 return bake_targets_output_internal(bkr, targets, ob, pixel_array, reports, mesh_eval);
1365 }
1366 if (bkr->save_mode == R_BAKE_SAVE_EXTERNAL) {
1368 bkr, targets, ob, ob_eval, mesh_eval, pixel_array, reports);
1369 }
1370 }
1371 else if (bkr->target == R_BAKE_TARGET_VERTEX_COLORS) {
1372 return bake_targets_output_vertex_colors(targets, ob);
1373 }
1374
1375 return false;
1376}
1377
1378static void bake_targets_free(BakeTargets *targets)
1379{
1380 MEM_SAFE_DELETE(targets->images);
1381 MEM_SAFE_DELETE(targets->material_to_image);
1382 MEM_SAFE_DELETE(targets->result);
1383}
1384
1385/* Main Bake Logic */
1386
1387static bool bake(const BakeAPIRender *bkr,
1388 Object *ob_low,
1389 const Span<PointerRNA> selected_objects,
1390 ReportList *reports)
1391{
1392 Render *re = bkr->render;
1393 Main *bmain = bkr->main;
1394 Scene *scene = bkr->scene;
1395 ViewLayer *view_layer = bkr->view_layer;
1396
1397 /* We build a depsgraph for the baking,
1398 * so we don't need to change the original data to adjust visibility and modifiers. */
1399 Depsgraph *depsgraph = DEG_graph_new(bmain, scene, view_layer, DAG_EVAL_RENDER);
1400
1401 /* Ensure meshes are generated even for objects with animated visibility, see: #107426. */
1403
1405
1406 bool ok = false;
1407
1408 Object *ob_cage = nullptr;
1409 Object *ob_cage_eval = nullptr;
1410 Object *ob_low_eval = nullptr;
1411
1412 BakeHighPolyData *highpoly = nullptr;
1413 int highpoly_num = 0;
1414
1415 Mesh *me_low_eval = nullptr;
1416 Mesh *me_cage_eval = nullptr;
1417
1418 MultiresModifierData *mmd_low = nullptr;
1419 MultiresModifierFlag mmd_flags_low = {};
1420
1421 BakePixel *pixel_array_low = nullptr;
1422 BakePixel *pixel_array_high = nullptr;
1423
1424 BakeTargets targets = {nullptr};
1425
1426 const bool preserve_origindex = (bkr->target == R_BAKE_TARGET_VERTEX_COLORS);
1427 const bool check_valid_uv_map = (bkr->target == R_BAKE_TARGET_IMAGE_TEXTURES);
1428
1429 RE_bake_engine_set_engine_parameters(re, bmain, scene);
1430
1431 if (!RE_bake_has_engine(re)) {
1432 BKE_report(reports, RPT_ERROR, "Current render engine does not support baking");
1433 goto cleanup;
1434 }
1435
1436 if (!bkr->uv_layer.empty()) {
1437 Mesh *mesh = id_cast<Mesh *>(ob_low->data);
1438 const bke::AttributeAccessor attributes = mesh->attributes();
1439 if (!bke::mesh::is_uv_map(attributes.lookup_meta_data(bkr->uv_layer))) {
1440 BKE_reportf(reports,
1441 RPT_ERROR,
1442 "No UV layer named \"%s\" found in the object \"%s\"",
1443 bkr->uv_layer.c_str(),
1444 ob_low->id.name + 2);
1445 goto cleanup;
1446 }
1447 }
1448
1449 if (bkr->is_selected_to_active) {
1450 highpoly_num = 0;
1451
1452 for (const PointerRNA &ptr : selected_objects) {
1453 Object *ob_iter = static_cast<Object *>(ptr.data);
1454
1455 if (ob_iter == ob_low) {
1456 continue;
1457 }
1458
1459 highpoly_num++;
1460 }
1461
1462 if (bkr->is_cage && bkr->custom_cage[0] != '\0') {
1463 ob_cage = static_cast<Object *>(
1464 BLI_findstring(&bmain->objects, bkr->custom_cage, offsetof(ID, name) + 2));
1465
1466 if (ob_cage == nullptr || ob_cage->type != OB_MESH) {
1467 BKE_report(reports, RPT_ERROR, "No valid cage object");
1468 goto cleanup;
1469 }
1470 else {
1471 ob_cage_eval = DEG_get_evaluated(depsgraph, ob_cage);
1472 if (ob_cage_eval) {
1473 ob_cage_eval->visibility_flag |= OB_HIDE_RENDER;
1476 }
1477 }
1478 }
1479 }
1480
1481 /* for multires bake, use linear UV subdivision to match low res UVs */
1484 {
1485 mmd_low = reinterpret_cast<MultiresModifierData *>(
1487 if (mmd_low) {
1488 mmd_flags_low = mmd_low->flags;
1490 }
1491 }
1492
1493 /* Make sure depsgraph is up to date. */
1494 BKE_scene_graph_update_tagged(depsgraph, bmain);
1495 ob_low_eval = DEG_get_evaluated(depsgraph, ob_low);
1496
1497 /* get the mesh as it arrives in the renderer */
1498 me_low_eval = bake_mesh_new_from_object(depsgraph, ob_low_eval, preserve_origindex);
1499
1500 /* Ensure cage object was evaluated by the depsgraph. */
1501 if (ob_cage && (ob_cage_eval == nullptr || (ob_cage_eval->id.orig_id != &ob_cage->id))) {
1502 BKE_reportf(reports,
1503 RPT_ERROR,
1504 "Cage object \"%s\" not found in evaluated scene, it may be hidden",
1505 ob_cage->id.name + 2);
1506 goto cleanup;
1507 }
1508
1509 /* Initialize bake targets. */
1510 if (!bake_targets_init(bkr, &targets, ob_low, ob_low_eval, reports)) {
1511 goto cleanup;
1512 }
1513
1514 /* Populate the pixel array with the face data. Except if we use a cage, then
1515 * it is populated later with the cage mesh (smoothed version of the mesh). */
1516 pixel_array_low = MEM_new_array_uninitialized<BakePixel>(targets.pixels_num,
1517 "bake pixels low poly");
1518 if ((bkr->is_selected_to_active && (ob_cage == nullptr) && bkr->is_cage) == false) {
1519 if (check_valid_uv_map && me_low_eval->uv_map_names().is_empty()) {
1520 BKE_reportf(reports,
1521 RPT_ERROR,
1522 "No UV map found in the evaluated object \"%s\"",
1523 ob_low->id.name + 2);
1524 goto cleanup;
1525 }
1526 bake_targets_populate_pixels(bkr, &targets, ob_low, me_low_eval, pixel_array_low);
1527 }
1528
1529 if (bkr->is_selected_to_active) {
1530 int i = 0;
1531
1532 /* prepare cage mesh */
1533 if (ob_cage) {
1534 me_cage_eval = bake_mesh_new_from_object(depsgraph, ob_cage_eval, preserve_origindex);
1535 if ((me_low_eval->faces_num != me_cage_eval->faces_num) ||
1536 (me_low_eval->corners_num != me_cage_eval->corners_num))
1537 {
1538 BKE_report(reports,
1539 RPT_ERROR,
1540 "Invalid cage object, the cage mesh must have the same number "
1541 "of faces as the active object");
1542 goto cleanup;
1543 }
1544 }
1545 else if (bkr->is_cage) {
1546 bool is_changed = false;
1547
1548 ModifierData *md = ob_low_eval->modifiers.first();
1549 while (md) {
1550 ModifierData *md_next = md->next;
1551
1552 /* Edge Split cannot be applied in the cage,
1553 * the cage is supposed to have interpolated normals
1554 * between the faces unless the geometry is physically
1555 * split. So we create a copy of the low poly mesh without
1556 * the eventual edge split. */
1557
1558 if (md->type == eModifierType_EdgeSplit) {
1559 BLI_remlink(&ob_low_eval->modifiers, md);
1561 is_changed = true;
1562 }
1563 md = md_next;
1564 }
1565
1566 if (is_changed) {
1567 /* Make sure object is evaluated with the new modifier settings.
1568 *
1569 * NOTE: Since the dependency graph was fully evaluated prior to bake, and we only made
1570 * single modification to this object all the possible dependencies for evaluation are
1571 * already up to date. This means we can do a cheap single object update
1572 * (as an opposite of full depsgraph update). */
1573 BKE_object_eval_reset(ob_low_eval);
1574 BKE_object_handle_data_update(depsgraph, scene, ob_low_eval);
1575 }
1576
1577 me_cage_eval = BKE_mesh_new_from_object(
1578 nullptr, ob_low_eval, false, preserve_origindex, true);
1579 if (check_valid_uv_map && me_cage_eval->uv_map_names().is_empty()) {
1580 BKE_reportf(reports,
1581 RPT_ERROR,
1582 "No UV map found in the evaluated object \"%s\"",
1583 ob_low->id.name + 2);
1584 goto cleanup;
1585 }
1586 bake_targets_populate_pixels(bkr, &targets, ob_low, me_cage_eval, pixel_array_low);
1587 }
1588
1589 highpoly = MEM_new_array_zeroed<BakeHighPolyData>(highpoly_num, "bake high poly objects");
1590
1591 /* populate highpoly array */
1592 for (const PointerRNA &ptr : selected_objects) {
1593 Object *ob_iter = static_cast<Object *>(ptr.data);
1594
1595 if (ob_iter == ob_low) {
1596 continue;
1597 }
1598
1599 Object *ob_eval = DEG_get_evaluated(depsgraph, ob_iter);
1600 if (ob_eval->id.orig_id != &ob_iter->id) {
1601 BKE_reportf(reports,
1602 RPT_ERROR,
1603 "Object \"%s\" not found in evaluated scene, it may be hidden",
1604 ob_iter->id.name + 2);
1605 goto cleanup;
1606 }
1607
1608 ob_eval->visibility_flag &= ~OB_HIDE_RENDER;
1610
1611 Mesh *mesh_eval = BKE_mesh_new_from_object(nullptr, ob_eval, false, false, true);
1612
1613 /* Initialize `highpoly` data. */
1614 highpoly[i].ob = ob_iter;
1615 highpoly[i].ob_eval = ob_eval;
1616 highpoly[i].mesh = mesh_eval;
1617
1618 /* Low-poly to high-poly transformation matrix. */
1619 copy_m4_m4(highpoly[i].obmat, highpoly[i].ob->object_to_world().ptr());
1620 invert_m4_m4(highpoly[i].imat, highpoly[i].obmat);
1621
1622 highpoly[i].is_flip_object = is_negative_m4(highpoly[i].ob->object_to_world().ptr());
1623 i++;
1624
1625 /* NOTE(@ideasman42): While ideally this should never happen,
1626 * it's possible the `visibility_flag` assignment in this function
1627 * is overridden by animated visibility, see: #107426.
1628 *
1629 * There is also the potential that scripts called from depsgraph callbacks
1630 * change this value too, so we can't guarantee the mesh will be available.
1631 * Use an error here instead of a warning so users don't accidentally perform
1632 * a bake which seems to succeed with invalid results.
1633 * If visibility could be forced/overridden - it would help avoid the problem. */
1634 if (mesh_eval == nullptr) [[unlikely]] {
1636 reports,
1637 RPT_ERROR,
1638 "Failed to access mesh from object \"%s\", ensure it's visible while rendering",
1639 ob_iter->id.name + 2);
1640 goto cleanup;
1641 }
1642 }
1643
1644 BLI_assert(i == highpoly_num);
1645
1646 if (ob_cage != nullptr) {
1647 ob_cage_eval->visibility_flag |= OB_HIDE_RENDER;
1650 }
1651 ob_low_eval->visibility_flag |= OB_HIDE_RENDER;
1653
1654 /* populate the pixel arrays with the corresponding face data for each high poly object */
1655 pixel_array_high = MEM_new_array_uninitialized<BakePixel>(targets.pixels_num,
1656 "bake pixels high poly");
1657
1659 me_low_eval,
1660 pixel_array_low,
1661 pixel_array_high,
1662 highpoly,
1663 highpoly_num,
1664 targets.pixels_num,
1665 ob_cage != nullptr,
1666 bkr->cage_extrusion,
1667 bkr->max_ray_distance,
1668 ob_low_eval->object_to_world().ptr(),
1669 (ob_cage ? ob_cage->object_to_world().ptr() : ob_low_eval->object_to_world().ptr()),
1670 me_cage_eval))
1671 {
1672 BKE_report(reports, RPT_ERROR, "Error handling selected objects");
1673 goto cleanup;
1674 }
1675
1676 /* the baking itself */
1677 for (i = 0; i < highpoly_num; i++) {
1678 ok = RE_bake_engine(re,
1679 depsgraph,
1680 highpoly[i].ob_eval,
1681 i,
1682 pixel_array_high,
1683 &targets,
1684 bkr->pass_type,
1685 bkr->pass_filter,
1686 targets.result);
1687 if (!ok) {
1689 reports, RPT_ERROR, "Error baking from object \"%s\"", highpoly[i].ob->id.name + 2);
1690 goto cleanup;
1691 }
1692 }
1693 }
1694 else {
1695 /* If low poly is not renderable it should have failed long ago. */
1696 BLI_assert((ob_low_eval->visibility_flag & OB_HIDE_RENDER) == 0);
1697
1698 if (RE_bake_has_engine(re)) {
1699 ok = RE_bake_engine(re,
1700 depsgraph,
1701 ob_low_eval,
1702 0,
1703 pixel_array_low,
1704 &targets,
1705 bkr->pass_type,
1706 bkr->pass_filter,
1707 targets.result);
1708 }
1709 else {
1710 BKE_report(reports, RPT_ERROR, "Current render engine does not support baking");
1711 goto cleanup;
1712 }
1713 }
1714
1715 /* normal space conversion
1716 * the normals are expected to be in world space, +X +Y +Z */
1717 if (ok && bkr->pass_type == SCE_PASS_NORMAL) {
1718 switch (bkr->normal_space) {
1719 case R_BAKE_SPACE_WORLD: {
1720 /* Cycles internal format */
1721 if ((bkr->normal_swizzle[0] == R_BAKE_POSX) && (bkr->normal_swizzle[1] == R_BAKE_POSY) &&
1722 (bkr->normal_swizzle[2] == R_BAKE_POSZ))
1723 {
1724 break;
1725 }
1726 RE_bake_normal_world_to_world(pixel_array_low,
1727 targets.pixels_num,
1728 targets.channels_num,
1729 targets.result,
1730 bkr->normal_swizzle);
1731 break;
1732 }
1733 case R_BAKE_SPACE_OBJECT: {
1734 RE_bake_normal_world_to_object(pixel_array_low,
1735 targets.pixels_num,
1736 targets.channels_num,
1737 targets.result,
1738 ob_low_eval,
1739 bkr->normal_swizzle);
1740 break;
1741 }
1742 case R_BAKE_SPACE_TANGENT: {
1743 if (bkr->is_selected_to_active) {
1744 RE_bake_normal_world_to_tangent(pixel_array_low,
1745 targets.pixels_num,
1746 targets.channels_num,
1747 targets.result,
1748 me_low_eval,
1749 bkr->normal_swizzle,
1750 ob_low_eval->object_to_world().ptr());
1751 }
1752 else {
1753 /* From multi-resolution. */
1754 Mesh *me_nores = nullptr;
1755 ModifierData *md = nullptr;
1756 ModifierMode mode;
1757
1758 BKE_object_eval_reset(ob_low_eval);
1760
1761 if (md) {
1762 mode = md->mode;
1764
1765 /* Evaluate modifiers again. */
1766 me_nores = BKE_mesh_new_from_object(nullptr, ob_low_eval, false, false, true);
1767 if (check_valid_uv_map && me_nores->uv_map_names().is_empty()) {
1768 BKE_reportf(reports,
1769 RPT_ERROR,
1770 "No UV map found in the evaluated object \"%s\"",
1771 ob_low->id.name + 2);
1772 BKE_id_free(nullptr, &me_nores->id);
1773 ok = false;
1774 goto cleanup;
1775 }
1776 bake_targets_populate_pixels(bkr, &targets, ob_low, me_nores, pixel_array_low);
1777 }
1778
1779 RE_bake_normal_world_to_tangent(pixel_array_low,
1780 targets.pixels_num,
1781 targets.channels_num,
1782 targets.result,
1783 (me_nores) ? me_nores : me_low_eval,
1784 bkr->normal_swizzle,
1785 ob_low_eval->object_to_world().ptr());
1786
1787 if (md) {
1788 BKE_id_free(nullptr, &me_nores->id);
1789 md->mode = mode;
1790 }
1791 }
1792 break;
1793 }
1794 default:
1795 break;
1796 }
1797 }
1798
1799 if (!ok) {
1800 BKE_reportf(reports, RPT_ERROR, "Problem baking object \"%s\"", ob_low->id.name + 2);
1801 }
1802 else if (bake_has_been_canceled(bkr)) {
1803 ok = false;
1804 }
1805 else {
1806 /* save the results */
1808 bkr, &targets, ob_low, ob_low_eval, me_low_eval, pixel_array_low, reports);
1809 }
1810
1811 bake_targets_refresh(&targets);
1812
1813cleanup:
1814
1815 if (highpoly) {
1816 for (int i = 0; i < highpoly_num; i++) {
1817 if (highpoly[i].mesh != nullptr) {
1818 BKE_id_free(nullptr, &highpoly[i].mesh->id);
1819 }
1820 }
1821 MEM_delete(highpoly);
1822 }
1823
1824 if (mmd_low) {
1825 mmd_low->flags = mmd_flags_low;
1826 }
1827
1828 if (pixel_array_low) {
1829 MEM_delete(pixel_array_low);
1830 }
1831
1832 if (pixel_array_high) {
1833 MEM_delete(pixel_array_high);
1834 }
1835
1836 bake_targets_free(&targets);
1837
1838 if (me_low_eval != nullptr) {
1839 BKE_id_free(nullptr, &me_low_eval->id);
1840 }
1841
1842 if (me_cage_eval != nullptr) {
1843 BKE_id_free(nullptr, &me_cage_eval->id);
1844 }
1845
1846 DEG_graph_free(depsgraph);
1847
1848 return ok;
1849}
1850
1851/* Bake Operator */
1852
1854{
1855 bScreen *screen = CTX_wm_screen(C);
1856
1857 bkr->ob = context_active_object(C);
1858 bkr->main = CTX_data_main(C);
1860 bkr->scene = CTX_data_scene(C);
1861 bkr->area = screen ? BKE_screen_find_big_area(screen, SPACE_IMAGE, 10) : nullptr;
1862 bkr->stop = nullptr;
1863
1864 bkr->pass_type = eScenePassType(RNA_enum_get(op->ptr, "type"));
1865 bkr->pass_filter = RNA_enum_get(op->ptr, "pass_filter");
1866 bkr->margin = RNA_int_get(op->ptr, "margin");
1867 bkr->margin_type = eBakeMarginType(RNA_enum_get(op->ptr, "margin_type"));
1868
1869 bkr->save_mode = eBakeSaveMode(RNA_enum_get(op->ptr, "save_mode"));
1870 bkr->target = eBakeTarget(RNA_enum_get(op->ptr, "target"));
1871
1872 bkr->is_clear = RNA_boolean_get(op->ptr, "use_clear");
1875 RNA_boolean_get(op->ptr, "use_split_materials");
1876 bkr->is_automatic_name = RNA_boolean_get(op->ptr, "use_automatic_name");
1877 bkr->is_selected_to_active = RNA_boolean_get(op->ptr, "use_selected_to_active");
1878 bkr->is_cage = RNA_boolean_get(op->ptr, "use_cage");
1879 bkr->cage_extrusion = RNA_float_get(op->ptr, "cage_extrusion");
1880 bkr->max_ray_distance = RNA_float_get(op->ptr, "max_ray_distance");
1881
1882 bkr->normal_space = RNA_enum_get(op->ptr, "normal_space");
1883 bkr->normal_swizzle[0] = eBakeNormalSwizzle(RNA_enum_get(op->ptr, "normal_r"));
1884 bkr->normal_swizzle[1] = eBakeNormalSwizzle(RNA_enum_get(op->ptr, "normal_g"));
1885 bkr->normal_swizzle[2] = eBakeNormalSwizzle(RNA_enum_get(op->ptr, "normal_b"));
1886
1887 bkr->width = RNA_int_get(op->ptr, "width");
1888 bkr->height = RNA_int_get(op->ptr, "height");
1889 bkr->identifier = "";
1890
1891 bkr->uv_layer = RNA_string_get(op->ptr, "uv_layer");
1892
1893 RNA_string_get(op->ptr, "cage_object", bkr->custom_cage);
1894
1895 if (bkr->save_mode == R_BAKE_SAVE_EXTERNAL && bkr->is_automatic_name) {
1896 PropertyRNA *prop = RNA_struct_find_property(op->ptr, "type");
1897 RNA_property_enum_identifier(C, op->ptr, prop, bkr->pass_type, &bkr->identifier);
1898 }
1899
1901
1902 bkr->reports = op->reports;
1903
1904 bkr->render = RE_NewSceneRender(bkr->scene);
1905
1906 /* XXX hack to force saving to always be internal. Whether (and how) to support
1907 * external saving will be addressed later */
1908 if (bkr->save_mode == R_BAKE_SAVE_EXTERNAL) {
1910 }
1911
1912 if (((bkr->pass_type == SCE_PASS_NORMAL) && (bkr->normal_space == R_BAKE_SPACE_TANGENT)) ||
1913 bkr->pass_type == SCE_PASS_UV)
1914 {
1916 }
1917}
1918
1920{
1921 Render *re;
1923 BakeAPIRender bkr = {nullptr};
1924 Scene *scene = CTX_data_scene(C);
1925
1926 G.is_break = false;
1927 G.is_rendering = true;
1928
1929 bake_set_props(op, scene);
1930
1931 bake_init_api_data(op, C, &bkr);
1932 re = bkr.render;
1933
1934 /* setup new render */
1935 RE_test_break_cb(re, nullptr, bake_break);
1936
1938 G.is_rendering = false;
1939 return result;
1940 }
1941
1942 if (!bake_objects_check(bkr.main,
1943 bkr.scene,
1944 bkr.view_layer,
1945 bkr.ob,
1946 bkr.selected_objects,
1947 bkr.reports,
1949 bkr.target))
1950 {
1951 G.is_rendering = false;
1952 return result;
1953 }
1954
1956
1957 if (bkr.is_clear) {
1958 const bool is_tangent = ((bkr.pass_type == SCE_PASS_NORMAL) &&
1960 bake_targets_clear(bkr.main, is_tangent);
1961 }
1962
1963 RE_SetReports(re, bkr.reports);
1964
1965 if (bkr.is_selected_to_active) {
1966 if (bake(&bkr, bkr.ob, bkr.selected_objects, bkr.reports)) {
1968 }
1969 }
1970 else {
1971 bkr.is_clear = bkr.is_clear && bkr.selected_objects.size() == 1;
1972 for (const PointerRNA &ptr : bkr.selected_objects) {
1973 Object *ob_iter = static_cast<Object *>(ptr.data);
1974 if (bake(&bkr, ob_iter, {}, bkr.reports)) {
1976 }
1977 if (bake_has_been_canceled(&bkr)) {
1978 break;
1979 }
1980 }
1981 }
1982
1983 RE_SetReports(re, nullptr);
1984
1985 G.is_rendering = false;
1987 return result;
1988}
1989
1990static void bake_startjob(void *bkv, wmJobWorkerStatus *worker_status)
1991{
1992 BakeAPIRender *bkr = static_cast<BakeAPIRender *>(bkv);
1993
1994 /* setup new render */
1995 bkr->do_update = &worker_status->do_update;
1996 bkr->progress = &worker_status->progress;
1997 bkr->stop = &worker_status->stop;
1998
1999 RE_SetReports(bkr->render, bkr->reports);
2000
2001 if (!bake_pass_filter_check(bkr->pass_type, bkr->pass_filter, bkr->reports)) {
2002 return;
2003 }
2004
2005 if (!bake_objects_check(bkr->main,
2006 bkr->scene,
2007 bkr->view_layer,
2008 bkr->ob,
2009 bkr->selected_objects,
2010 bkr->reports,
2012 bkr->target))
2013 {
2014 return;
2015 }
2016
2017 if (bkr->is_clear) {
2018 const bool is_tangent = ((bkr->pass_type == SCE_PASS_NORMAL) &&
2020 bake_targets_clear(bkr->main, is_tangent);
2021 }
2022
2023 if (bkr->is_selected_to_active) {
2024 bake(bkr, bkr->ob, bkr->selected_objects, bkr->reports);
2025 }
2026 else {
2027 bkr->is_clear = bkr->is_clear && bkr->selected_objects.size() == 1;
2028 for (const PointerRNA &ptr : bkr->selected_objects) {
2029 Object *ob_iter = static_cast<Object *>(ptr.data);
2030 bake(bkr, ob_iter, {}, bkr->reports);
2031
2032 if (bake_has_been_canceled(bkr)) {
2033 break;
2034 }
2035 }
2036 }
2037
2038 RE_SetReports(bkr->render, nullptr);
2039}
2040
2041static void bake_job_complete(void *bkv)
2042{
2043 BakeAPIRender *bkr = static_cast<BakeAPIRender *>(bkv);
2045}
2046static void bake_job_canceled(void *bkv)
2047{
2048 BakeAPIRender *bkr = static_cast<BakeAPIRender *>(bkv);
2050}
2051
2052static void bake_freejob(void *bkv)
2053{
2054 BakeAPIRender *bkr = static_cast<BakeAPIRender *>(bkv);
2055
2056 MEM_delete(bkr);
2057
2058 G.is_rendering = false;
2059
2061}
2062
2063static void bake_set_props(wmOperator *op, Scene *scene)
2064{
2065 PropertyRNA *prop;
2066 BakeData *bake = &scene->r.bake;
2067
2068 prop = RNA_struct_find_property(op->ptr, "filepath");
2069 if (!RNA_property_is_set(op->ptr, prop)) {
2070 RNA_property_string_set(op->ptr, prop, bake->filepath);
2071 }
2072
2073 prop = RNA_struct_find_property(op->ptr, "width");
2074 if (!RNA_property_is_set(op->ptr, prop)) {
2075 RNA_property_int_set(op->ptr, prop, bake->width);
2076 }
2077
2078 prop = RNA_struct_find_property(op->ptr, "height");
2079 if (!RNA_property_is_set(op->ptr, prop)) {
2080 RNA_property_int_set(op->ptr, prop, bake->width);
2081 }
2082
2083 prop = RNA_struct_find_property(op->ptr, "margin");
2084 if (!RNA_property_is_set(op->ptr, prop)) {
2085 RNA_property_int_set(op->ptr, prop, bake->margin);
2086 }
2087
2088 prop = RNA_struct_find_property(op->ptr, "margin_type");
2089 if (!RNA_property_is_set(op->ptr, prop)) {
2090 RNA_property_enum_set(op->ptr, prop, bake->margin_type);
2091 }
2092
2093 prop = RNA_struct_find_property(op->ptr, "use_selected_to_active");
2094 if (!RNA_property_is_set(op->ptr, prop)) {
2095 RNA_property_boolean_set(op->ptr, prop, (bake->flag & R_BAKE_TO_ACTIVE) != 0);
2096 }
2097
2098 prop = RNA_struct_find_property(op->ptr, "max_ray_distance");
2099 if (!RNA_property_is_set(op->ptr, prop)) {
2100 RNA_property_float_set(op->ptr, prop, bake->max_ray_distance);
2101 }
2102
2103 prop = RNA_struct_find_property(op->ptr, "cage_extrusion");
2104 if (!RNA_property_is_set(op->ptr, prop)) {
2105 RNA_property_float_set(op->ptr, prop, bake->cage_extrusion);
2106 }
2107
2108 prop = RNA_struct_find_property(op->ptr, "cage_object");
2109 if (!RNA_property_is_set(op->ptr, prop)) {
2111 op->ptr, prop, (bake->cage_object) ? bake->cage_object->id.name + 2 : "");
2112 }
2113
2114 prop = RNA_struct_find_property(op->ptr, "normal_space");
2115 if (!RNA_property_is_set(op->ptr, prop)) {
2116 RNA_property_enum_set(op->ptr, prop, bake->normal_space);
2117 }
2118
2119 prop = RNA_struct_find_property(op->ptr, "normal_r");
2120 if (!RNA_property_is_set(op->ptr, prop)) {
2121 RNA_property_enum_set(op->ptr, prop, bake->normal_swizzle[0]);
2122 }
2123
2124 prop = RNA_struct_find_property(op->ptr, "normal_g");
2125 if (!RNA_property_is_set(op->ptr, prop)) {
2126 RNA_property_enum_set(op->ptr, prop, bake->normal_swizzle[1]);
2127 }
2128
2129 prop = RNA_struct_find_property(op->ptr, "normal_b");
2130 if (!RNA_property_is_set(op->ptr, prop)) {
2131 RNA_property_enum_set(op->ptr, prop, bake->normal_swizzle[2]);
2132 }
2133
2134 prop = RNA_struct_find_property(op->ptr, "target");
2135 if (!RNA_property_is_set(op->ptr, prop)) {
2136 RNA_property_enum_set(op->ptr, prop, bake->target);
2137 }
2138
2139 prop = RNA_struct_find_property(op->ptr, "save_mode");
2140 if (!RNA_property_is_set(op->ptr, prop)) {
2141 RNA_property_enum_set(op->ptr, prop, bake->save_mode);
2142 }
2143
2144 prop = RNA_struct_find_property(op->ptr, "use_clear");
2145 if (!RNA_property_is_set(op->ptr, prop)) {
2146 RNA_property_boolean_set(op->ptr, prop, (bake->flag & R_BAKE_CLEAR) != 0);
2147 }
2148
2149 prop = RNA_struct_find_property(op->ptr, "use_cage");
2150 if (!RNA_property_is_set(op->ptr, prop)) {
2151 RNA_property_boolean_set(op->ptr, prop, (bake->flag & R_BAKE_CAGE) != 0);
2152 }
2153
2154 prop = RNA_struct_find_property(op->ptr, "use_split_materials");
2155 if (!RNA_property_is_set(op->ptr, prop)) {
2156 RNA_property_boolean_set(op->ptr, prop, (bake->flag & R_BAKE_SPLIT_MAT) != 0);
2157 }
2158
2159 prop = RNA_struct_find_property(op->ptr, "use_automatic_name");
2160 if (!RNA_property_is_set(op->ptr, prop)) {
2161 RNA_property_boolean_set(op->ptr, prop, (bake->flag & R_BAKE_AUTO_NAME) != 0);
2162 }
2163
2164 prop = RNA_struct_find_property(op->ptr, "pass_filter");
2165 if (!RNA_property_is_set(op->ptr, prop)) {
2166 RNA_property_enum_set(op->ptr, prop, bake->pass_filter);
2167 }
2168}
2169
2171{
2172 wmJob *wm_job;
2173 Render *re;
2174 Scene *scene = CTX_data_scene(C);
2175
2176 bake_set_props(op, scene);
2177
2178 /* only one render job at a time */
2180 return OPERATOR_CANCELLED;
2181 }
2182
2183 BakeAPIRender *bkr = MEM_new<BakeAPIRender>(__func__);
2184
2185 /* init bake render */
2186 bake_init_api_data(op, C, bkr);
2188 re = bkr->render;
2189
2190 /* setup new render */
2191 RE_test_break_cb(re, bkr, bake_break);
2193
2194 /* setup job */
2195 wm_job = WM_jobs_get(CTX_wm_manager(C),
2197 scene,
2198 "Baking texture...",
2202 /* TODO: only draw bake image, can we enforce this. */
2204 wm_job, 0.5, (bkr->target == R_BAKE_TARGET_VERTEX_COLORS) ? NC_GEOM | ND_DATA : NC_IMAGE, 0);
2206 wm_job, bake_startjob, nullptr, nullptr, nullptr, bake_job_complete, bake_job_canceled);
2207
2208 G.is_break = false;
2209 G.is_rendering = true;
2210
2212
2213 WM_jobs_start(CTX_wm_manager(C), wm_job);
2214
2215 WM_cursor_wait(false);
2216
2217 /* add modal handler for ESC */
2219
2222}
2223
2225{
2226 PropertyRNA *prop;
2227
2228 /* identifiers */
2229 ot->name = "Bake";
2230 ot->description = "Bake image textures of selected objects";
2231 ot->idname = "OBJECT_OT_bake";
2232
2233 /* API callbacks. */
2234 ot->exec = bake_exec;
2235 ot->modal = bake_modal;
2236 ot->invoke = bake_invoke;
2238
2240 ot->srna,
2241 "type",
2244 "Type",
2245 "Type of pass to bake, some of them may not be supported by the current render engine");
2247 "pass_filter",
2250 "Pass Filter",
2251 "Filter to combined, diffuse, glossy, transmission and subsurface passes");
2253 "filepath",
2254 nullptr,
2255 FILE_MAX,
2256 "File Path",
2257 "Image filepath to use when saving externally");
2258 RNA_def_int(ot->srna,
2259 "width",
2260 512,
2261 1,
2262 INT_MAX,
2263 "Width",
2264 "Horizontal dimension of the baking map (external only)",
2265 64,
2266 4096);
2267 RNA_def_int(ot->srna,
2268 "height",
2269 512,
2270 1,
2271 INT_MAX,
2272 "Height",
2273 "Vertical dimension of the baking map (external only)",
2274 64,
2275 4096);
2276 RNA_def_int(ot->srna,
2277 "margin",
2278 16,
2279 0,
2280 INT_MAX,
2281 "Margin",
2282 "Extends the baked result as a post process filter",
2283 0,
2284 64);
2285 RNA_def_enum(ot->srna,
2286 "margin_type",
2289 "Margin Type",
2290 "Which algorithm to use to generate the margin");
2292 "use_selected_to_active",
2293 false,
2294 "Selected to Active",
2295 "Bake shading on the surface of selected objects to the active object");
2296 RNA_def_float(ot->srna,
2297 "max_ray_distance",
2298 0.0f,
2299 0.0f,
2300 FLT_MAX,
2301 "Max Ray Distance",
2302 "The maximum ray distance for matching points between the active and selected "
2303 "objects. If zero, there is no limit",
2304 0.0f,
2305 1.0f);
2306 RNA_def_float(ot->srna,
2307 "cage_extrusion",
2308 0.0f,
2309 0.0f,
2310 FLT_MAX,
2311 "Cage Extrusion",
2312 "Inflate the active object by the specified distance for baking. This helps "
2313 "matching to points nearer to the outside of the selected object meshes",
2314 0.0f,
2315 1.0f);
2316 RNA_def_string(ot->srna,
2317 "cage_object",
2318 nullptr,
2319 MAX_NAME,
2320 "Cage Object",
2321 "Object to use as cage, instead of calculating the cage from the active object "
2322 "with cage extrusion");
2323 RNA_def_enum(ot->srna,
2324 "normal_space",
2327 "Normal Space",
2328 "Choose normal space for baking");
2329 prop = RNA_def_enum(ot->srna,
2330 "normal_r",
2333 "R",
2334 "Axis to bake in red channel");
2336 prop = RNA_def_enum(ot->srna,
2337 "normal_g",
2340 "G",
2341 "Axis to bake in green channel");
2343 prop = RNA_def_enum(ot->srna,
2344 "normal_b",
2347 "B",
2348 "Axis to bake in blue channel");
2350 RNA_def_enum(ot->srna,
2351 "target",
2354 "Target",
2355 "Where to output the baked map");
2356 RNA_def_enum(ot->srna,
2357 "save_mode",
2360 "Save Mode",
2361 "Where to save baked image textures");
2363 "use_clear",
2364 false,
2365 "Clear",
2366 "Clear images before baking (only for internal saving)");
2367 RNA_def_boolean(ot->srna, "use_cage", false, "Cage", "Cast rays to active object from a cage");
2369 ot->srna,
2370 "use_split_materials",
2371 false,
2372 "Split Materials",
2373 "Split baked maps per material, using material name in output file (external only)");
2375 "use_automatic_name",
2376 false,
2377 "Automatic Name",
2378 "Automatically name the output file with the pass type");
2379 RNA_def_string(ot->srna,
2380 "uv_layer",
2381 nullptr,
2383 "UV Layer",
2384 "UV layer to override active");
2385}
2386
2387} // namespace blender::ed::object
Generic geometry attributes built on CustomData.
#define ORIGINDEX_NONE
#define G_MAIN
General operations, lookup, etc. for materials.
General operations, lookup, etc. for blender objects.
#define FILE_MAX
#define SNPRINTF_UTF8(dst, format,...)
#define BLT_I18NCONTEXT_COLOR
#define MAX_NAME
Definition DNA_defs.h:63
Object is a sort of wrapper for general info.
Read Blender memory allocation and freeing.
#define C
Definition RandGen.cpp:29
#define NC_GEOM
Definition WM_types.hh:406
#define ND_RENDER_RESULT
Definition WM_types.hh:465
#define ND_DATA
Definition WM_types.hh:552
#define NC_SCENE
Definition WM_types.hh:391
#define NC_IMAGE
Definition WM_types.hh:397
volatile int lock
#define UNUSED_VARS_NDEBUG(...)
#define ELEM(...)
#define BLI_assert_msg(a, msg)
#define BM_ELEM_CD_GET_VOID_P(ele, offset)
#define BM_ITER_ELEM(ele, iter, data, itype)
#define BM_ITER_MESH(ele, iter, bm, itype)
#define MEM_realloc_zeroed(vmemh, len)
#define MAX_CUSTOMDATA_LAYER_NAME_NO_PREFIX
bool is() const
ChannelStorageType r
const CPPType & type() const
MutableSpan< T > typed() const
constexpr int64_t size() const
constexpr int64_t start() const
constexpr bool is_empty() const
Definition BLI_span.hh:264
bool is_empty() const
std::optional< AttributeMetaData > lookup_meta_data(StringRef name) const
GSpanAttributeWriter lookup_for_write_span(StringRef name)
#define offsetof(t, d)
#define FLT_MAX
#define INT_MAX
const ccl_global KernelWorkTile * tile
#define unit_float_to_uchar_clamp_v4(v1, v2)
bool is_uv_map(const AttributeMetaData &meta_data)
void corner_tris_calc(Span< float3 > vert_positions, OffsetIndices< int > faces, Span< int > corner_verts, MutableSpan< int3 > corner_tris)
bool node_is_connected_to_output(const bNodeTree &ntree, const bNode &node)
static bool bake_objects_check(Main *bmain, const Scene *scene, ViewLayer *view_layer, Object *ob, Span< PointerRNA > selected_objects, ReportList *reports, const bool is_selected_to_active, const eBakeTarget target)
static Mesh * bake_mesh_new_from_object(Depsgraph *depsgraph, Object *object, const bool preserve_origindex)
static bool bake_targets_init(const BakeAPIRender *bkr, BakeTargets *targets, Object *ob, Object *ob_eval, ReportList *reports)
static void bake_startjob(void *bkv, wmJobWorkerStatus *worker_status)
static void bake_targets_populate_pixels_color_attributes(BakeTargets *targets, Object *ob, Mesh *mesh_eval, BakePixel *pixel_array)
static void bake_targets_refresh(BakeTargets *targets)
static bool write_external_bake_pixels(const char *filepath, BakePixel pixel_array[], float *buffer, const int width, const int height, const int margin, const int margin_type, ImageFormatData const *im_format, const bool is_noncolor, const bool is_tangent_normal, Mesh const *mesh_eval, const StringRef uv_layer, const float uv_offset[2])
static void bake_job_complete(void *bkv)
static int find_original_loop(const OffsetIndices< int > orig_faces, const Span< int > orig_corner_verts, const int *vert_origindex, const int *poly_origindex, const int poly_eval, const int vert_eval)
void OBJECT_OT_bake(wmOperatorType *ot)
static void convert_float_color_to_byte_color(const ColorGeometry4f *float_colors, const int num, const bool is_noncolor, ColorGeometry4b *byte_colors)
static void bake_job_canceled(void *bkv)
static wmOperatorStatus bake_modal(bContext *C, wmOperator *, const wmEvent *event)
static bool bake_targets_output_internal(const BakeAPIRender *bkr, BakeTargets *targets, Object *ob, BakePixel *pixel_array, ReportList *reports, Mesh *mesh_eval)
static void bake_update_image(ScrArea *area, Image *image)
static wmOperatorStatus bake_invoke(bContext *C, wmOperator *op, const wmEvent *)
static bool bake_has_been_canceled(const BakeAPIRender *bkr)
static void bake_targets_populate_pixels(const BakeAPIRender *bkr, BakeTargets *targets, Object *ob, Mesh *mesh_eval, BakePixel *pixel_array)
static bool bake_object_check(const Main &bmain, const Scene *scene, ViewLayer *view_layer, Object *ob, const eBakeTarget target, ReportList *reports)
static bool write_internal_bake_pixels(Image *image, const int image_tile_number, BakePixel pixel_array[], float *buffer, const int width, const int height, const int margin, const char margin_type, const bool is_clear, const bool is_noncolor, const bool is_tangent_normal, Mesh const *mesh_eval, const StringRef uv_layer, const float uv_offset[2])
static void bake_freejob(void *bkv)
static void bake_init_api_data(wmOperator *op, bContext *C, BakeAPIRender *bkr)
Object * context_active_object(const bContext *C)
static bool bake_targets_output_vertex_colors(BakeTargets *targets, Object *ob)
static void bias_tangent_normal_pixels(float *rect, int channels, int width, int height, int stride)
static bool bake_pass_filter_check(eScenePassType pass_type, const int pass_filter, ReportList *reports)
static bool bake_targets_init_image_textures(const BakeAPIRender *bkr, BakeTargets *targets, Object *ob, ReportList *reports)
static bool bake_targets_output(const BakeAPIRender *bkr, BakeTargets *targets, Object *ob, Object *ob_eval, Mesh *mesh_eval, BakePixel *pixel_array, ReportList *reports)
static void bake_targets_free(BakeTargets *targets)
static bool bake_targets_init_vertex_colors(Main *bmain, BakeTargets *targets, Object *ob, ReportList *reports)
static bool bake_targets_init_internal(const BakeAPIRender *bkr, BakeTargets *targets, Object *ob, ReportList *reports)
static bool bake_targets_init_external(const BakeAPIRender *bkr, BakeTargets *targets, Object *ob, ReportList *reports)
static void bake_progress_update(void *bjv, float progress)
static void bake_set_props(wmOperator *op, Scene *scene)
static void bake_targets_clear(Main *bmain, const bool is_tangent)
static bool bake_targets_output_external(const BakeAPIRender *bkr, BakeTargets *targets, Object *ob, Object *ob_eval, Mesh *mesh_eval, BakePixel *pixel_array, ReportList *reports)
bool editmode_load(Main *bmain, Object *obedit)
static bool is_noncolor_pass(eScenePassType pass_type)
static bool bake_break(void *rjv)
static void bake_result_add_to_rgba(float rgba[4], const float *result, const int channels_num)
static wmOperatorStatus bake_exec(bContext *C, wmOperator *op)
const EnumPropertyItem rna_enum_normal_space_items[]
Definition rna_scene.cc:436
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
void RE_bake_ibuf_clear(Image *image, const bool is_tangent)
Definition bake.cc:1102
PropertyRNA * RNA_def_string_file_path(StructOrFunctionRNA *cont_, const char *identifier, const char *default_value, const int maxlen, const char *ui_name, const char *ui_description)
void BKE_object_eval_reset(Object *ob_eval)
int RNA_enum_get(PointerRNA *ptr, const char *name)
void RE_SetReports(Render *re, ReportList *reports)
PropertyRNA * RNA_def_string(StructOrFunctionRNA *cont_, const char *identifier, const char *default_value, const int maxlen, const char *ui_name, const char *ui_description)
void DEG_graph_build_from_view_layer(Depsgraph *graph)
const EnumPropertyItem rna_enum_bake_pass_filter_type_items[]
Definition rna_scene.cc:573
PropertyRNA * RNA_struct_find_property(PointerRNA *ptr, const char *identifier)
BMesh * BKE_editmesh_bmesh_get_for_write(Mesh *mesh)
Definition editmesh.cc:79
unsigned int uint
Definition BKE_text.h:55
void * MEM_new_array_uninitialized(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
@ BKE_CB_EVT_OBJECT_BAKE_COMPLETE
@ BKE_CB_EVT_OBJECT_BAKE_CANCEL
@ BKE_CB_EVT_OBJECT_BAKE_PRE
void RE_test_break_cb(Render *re, void *handle, bool(*f)(void *handle))
@ R_BAKE_PASS_FILTER_TRANSM
@ R_BAKE_PASS_FILTER_SUBSURFACE
@ R_BAKE_PASS_FILTER_INDIRECT
@ R_BAKE_PASS_FILTER_DIFFUSE
@ R_BAKE_PASS_FILTER_EMIT
@ R_BAKE_PASS_FILTER_NONE
@ R_BAKE_PASS_FILTER_COLOR
@ R_BAKE_PASS_FILTER_GLOSSY
@ R_BAKE_PASS_FILTER_DIRECT
void DEG_graph_free(Depsgraph *graph)
Definition depsgraph.cc:307
ATTR_WARN_UNUSED_RESULT const BMLoop * l
bool RNA_property_enum_identifier(bContext *C, PointerRNA *ptr, PropertyRNA *prop, const int value, const char **r_identifier)
void RNA_property_float_set(PointerRNA *ptr, PropertyRNA *prop, float value)
@ BASE_ENABLED_AND_MAYBE_VISIBLE_IN_VIEWPORT
int RE_pass_depth(const eScenePassType pass_type)
Definition bake.cc:1127
Material * BKE_object_material_get(Object *ob, short act)
@ WM_JOB_TYPE_OBJECT_BAKE
Definition WM_api.hh:2018
bool RE_bake_has_engine(const Render *re)
void IMB_mark_dirty(ImBuf *ibuf)
bool bool BLI_path_suffix(char *path, size_t path_maxncpy, const char *suffix, const char *sep) ATTR_NONNULL(1
bool invert_m4_m4(float inverse[4][4], const float mat[4][4])
PropertyRNA * RNA_def_enum(StructOrFunctionRNA *cont_, const char *identifier, const EnumPropertyItem *items, const int default_value, const char *ui_name, const char *ui_description)
Depsgraph * DEG_graph_new(Main *bmain, Scene *scene, ViewLayer *view_layer, eEvaluationMode mode)
Definition depsgraph.cc:279
void * BLI_findstring(const ListBase *listbase, const char *id, int offset) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:615
int RNA_int_get(PointerRNA *ptr, const char *name)
bool is_negative_m4(const float mat[4][4])
wmWindow * CTX_wm_window(const bContext *C)
const char * IMB_colormanagement_get_byte_colorspace(const ImBuf *ibuf)
void BKE_callback_exec_id(Main *bmain, ID *id, eCbEvent evt)
Definition callbacks.cc:45
ATTR_WARN_UNUSED_RESULT const BMVert * v
void WM_jobs_customdata_set(wmJob *wm_job, void *customdata, void(*free)(void *customdata))
Definition wm_jobs.cc:377
void IMB_buffer_byte_from_float(unsigned char *dest, const float *src, int src_channels, float dither, bool predivide, int width, int height, int stride, int start_y=0)
Definition conversion.cc:50
@ WM_JOB_PRIORITY
Definition WM_api.hh:1990
@ WM_JOB_EXCL_RENDER
Definition WM_api.hh:1995
@ WM_JOB_PROGRESS
Definition WM_api.hh:1997
const EnumPropertyItem rna_enum_bake_target_items[]
Definition rna_scene.cc:462
void BKE_main_id_tag_idcode(Main *mainvar, short type, int tag, bool value)
Definition lib_id.cc:1307
ViewLayer * CTX_data_view_layer(const bContext *C)
void IMB_buffer_byte_from_float_mask(unsigned char *dest, const float *src, int src_channels, float dither, int width, int height, const char *mask)
bool RE_bake_pixels_populate_from_objects(Mesh *me_low, BakePixel pixel_array_from[], BakePixel pixel_array_to[], BakeHighPolyData highpoly[], const int highpoly_num, const size_t pixels_num, const bool is_custom_cage, const float cage_extrusion, const float max_ray_distance, const float mat_low[4][4], const float mat_cage[4][4], Mesh *me_cage)
Definition bake.cc:542
void WM_jobs_start(wmWindowManager *wm, wmJob *wm_job)
Definition wm_jobs.cc:498
Global G
Definition blender.cc:57
const char * BKE_main_blendfile_path(const Main *bmain) ATTR_NONNULL()
Definition main.cc:1058
MINLINE void zero_v4(float r[4])
void RNA_property_int_set(PointerRNA *ptr, PropertyRNA *prop, int value)
@ SCE_PASS_GLOSSY_COLOR
@ SCE_PASS_TRANSM_COLOR
@ SCE_PASS_DIFFUSE_COLOR
@ SCE_PASS_SUBSURFACE_COLOR
void RE_bake_pixels_populate(Mesh *mesh, BakePixel pixel_array[], const size_t pixels_num, const BakeTargets *targets, const StringRef uv_layer, int margin)
Definition bake.cc:766
void RE_progress_cb(Render *re, void *handle, void(*f)(void *handle, float))
void BKE_object_handle_data_update(Depsgraph *depsgraph, Scene *scene, Object *ob)
void WM_jobs_callbacks_ex(wmJob *wm_job, wm_jobs_start_callback startjob, void(*initjob)(void *), void(*update)(void *), void(*endjob)(void *), void(*completed)(void *), void(*canceled)(void *))
Definition wm_jobs.cc:416
void IMB_freeImBuf(ImBuf *ibuf)
void RE_bake_normal_world_to_tangent(const BakePixel pixel_array[], const size_t pixels_num, const int depth, float result[], Mesh *mesh, const eBakeNormalSwizzle normal_swizzle[3], const float mat[4][4])
Definition bake.cc:921
VecBase< int32_t, 3 > int3
void RNA_property_string_set(PointerRNA *ptr, PropertyRNA *prop, const char *value)
void * MEM_new_zeroed(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
void BLI_remlink(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:138
MINLINE void add_v4_v4(float r[4], const float a[4])
void DEG_id_tag_update(ID *id, unsigned int flags)
const char * IMB_colormanagement_role_colorspace_name_get(int role)
void BKE_report(ReportList *reports, eReportType type, const char *message)
Definition report.cc:162
BMesh const char void * data
const EnumPropertyItem rna_enum_bake_margin_type_items[]
Definition rna_scene.cc:452
bool WM_jobs_has_running(const wmWindowManager *window_manager, const void *owner, const eWM_JobType type, const eWM_JobFlag exclude_flags)
Definition wm_jobs.cc:809
T * DEG_get_evaluated(const Depsgraph *depsgraph, T *id)
wmWindowManager * CTX_wm_manager(const bContext *C)
void ED_mesh_split_faces(Mesh *mesh)
void RE_bake_normal_world_to_object(const BakePixel pixel_array[], const size_t pixels_num, const int depth, float result[], Object *ob, const eBakeNormalSwizzle normal_swizzle[3])
Definition bake.cc:1046
const EnumPropertyItem rna_enum_bake_pass_type_items[]
Definition rna_render.cc:31
void BKE_modifier_free(ModifierData *md)
void BKE_scene_graph_update_tagged(Depsgraph *depsgraph, Main *bmain)
Definition scene.cc:2867
bool BKE_view_layer_synced_ensure(const Main &bmain, const Scene *scene, ViewLayer *view_layer)
void IMB_partial_update_mark_full(ImBuf *ibuf)
void IMB_buffer_float_rgba_from_float(float *dest, const float *src, int src_channels, int width, int height)
BMDataLayerLookup BM_data_layer_lookup(const BMesh &bm, const StringRef name)
const void * CustomData_get_layer(const CustomData *data, eCustomDataType type)
ATTR_WARN_UNUSED_RESULT const size_t num
bool RNA_boolean_get(PointerRNA *ptr, const char *name)
void RE_bake_mask_fill(const BakePixel pixel_array[], const size_t pixels_num, char *mask)
Definition bake.cc:128
MINLINE int poly_to_tri_count(int poly_count, int corner_count)
const EnumPropertyItem rna_enum_normal_swizzle_items[]
Definition rna_scene.cc:442
void BKE_id_free(Main *bmain, void *idv)
void IMB_buffer_float_rgba_from_float_mask(float *dest, const float *src, int src_channels, int width, int height, const char *mask)
void WM_event_add_notifier(const bContext *C, uint type, void *reference)
bool RE_bake_engine(Render *re, Depsgraph *depsgraph, Object *object, const int object_id, const BakePixel pixel_array[], const BakeTargets *targets, const eScenePassType pass_type, const int pass_filter, float result[])
ImBuf * IMB_allocImBuf(unsigned int x, unsigned int y, ImBufFlags flags)
void RNA_def_property_translation_context(PropertyRNA *prop, const char *context)
void RE_bake_normal_world_to_world(const BakePixel pixel_array[], const size_t pixels_num, const int depth, float result[], const eBakeNormalSwizzle normal_swizzle[3])
Definition bake.cc:1078
ImBuf * BKE_image_acquire_ibuf(Image *ima, ImageUser *iuser, void **r_lock)
ModifierData * BKE_modifiers_findby_type(const Object *ob, ModifierType type)
MINLINE void add_v3_v3(float r[3], const float a[3])
wmEventHandler_Op * WM_event_add_modal_handler(bContext *C, wmOperator *op)
PropertyRNA * RNA_def_int(StructOrFunctionRNA *cont_, const char *identifier, const int default_value, const int hardmin, const int hardmax, const char *ui_name, const char *ui_description, const int softmin, const int softmax)
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)
wmJob * WM_jobs_get(wmWindowManager *wm, wmWindow *win, const void *owner, const char *name, const eWM_JobFlag flag, const eWM_JobType job_type)
Definition wm_jobs.cc:228
const EnumPropertyItem rna_enum_bake_save_mode_items[]
Definition rna_scene.cc:476
void RE_bake_engine_set_engine_parameters(Render *re, Main *bmain, Scene *scene)
Render * RE_NewSceneRender(const Scene *scene)
@ ID_RECALC_GEOMETRY
Definition DNA_ID.h:1074
Vector< bke::path_templates::Error > BKE_image_path_from_imtype(char *filepath, const char *base, const char *relbase, const bke::path_templates::VariableMap *template_variables, int frame, char imtype, bool use_ext, bool use_frames, const char *suffix)
ScrArea * BKE_screen_find_big_area(const bScreen *screen, int spacetype, short min)
Definition screen.cc:1189
Base * BKE_view_layer_base_find(ViewLayer *view_layer, Object *ob)
@ ID_TAG_DOIT
Definition DNA_ID.h:1029
void ED_render_view3d_auto_pause(Main *bmain, bool pause)
void RNA_property_enum_set(PointerRNA *ptr, PropertyRNA *prop, int value)
PropertyRNA * RNA_def_enum_flag(StructOrFunctionRNA *cont_, const char *identifier, const EnumPropertyItem *items, const int default_value, const char *ui_name, const char *ui_description)
void RNA_property_boolean_set(PointerRNA *ptr, PropertyRNA *prop, bool value)
Mesh * BKE_mesh_new_from_object(Depsgraph *depsgraph, Object *object, bool preserve_all_data_layers, bool preserve_origindex, bool ensure_subdivision)
Scene * CTX_data_scene(const bContext *C)
bScreen * CTX_wm_screen(const bContext *C)
ColorSceneLinear4f< eAlpha::Premultiplied > ColorGeometry4f
std::string RNA_string_get(PointerRNA *ptr, const char *name)
void copy_m4_m4(float m1[4][4], const float m2[4][4])
MINLINE void mul_v4_fl(float r[4], float f)
bool CTX_data_selected_objects(const bContext *C, Vector< PointerRNA > *list)
PropertyRNA * RNA_def_boolean(StructOrFunctionRNA *cont_, const char *identifier, const bool default_value, const char *ui_name, const char *ui_description)
void RE_bake_margin(ImBuf *ibuf, char *mask, const int margin, const char margin_type, const Mesh *mesh, const StringRef uv_layer, const float uv_offset[2])
Definition bake.cc:145
MINLINE void linearrgb_to_srgb_uchar4(unsigned char srgb[4], const float linear[4])
@ R_BAKE_TARGET_VERTEX_COLORS
@ R_BAKE_TARGET_IMAGE_TEXTURES
bool ED_operator_object_active_editable_mesh(bContext *C)
void IMB_colormanagement_transform_float(float *buffer, int width, int height, int channels, const char *from_colorspace, const char *to_colorspace, bool predivide)
void BKE_imageuser_default(ImageUser *iuser)
void WM_cursor_wait(bool val)
PropertyRNA * RNA_def_float(StructOrFunctionRNA *cont_, const char *identifier, const float default_value, const float hardmin, const float hardmax, const char *ui_name, const char *ui_description, const float softmin, const float softmax)
void WM_jobs_timer(wmJob *wm_job, double time_step, uint note, uint endnote, void(*timer_step)(void *))
Definition wm_jobs.cc:393
bool BKE_imbuf_write(ImBuf *ibuf, const char *filepath, const ImageFormatData *imf)
const char * IMB_colormanagement_get_float_colorspace(const ImBuf *ibuf)
float RNA_float_get(PointerRNA *ptr, const char *name)
bool RNA_property_is_set(PointerRNA *ptr, PropertyRNA *prop)
ColorSceneLinearByteEncoded4b< eAlpha::Premultiplied > ColorGeometry4b
void DEG_disable_visibility_optimization(Depsgraph *depsgraph)
Definition depsgraph.cc:350
void * MEM_new_array_zeroed(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
bool BKE_id_attributes_color_find(const struct ID *id, StringRef name)
void BKE_image_get_tile_uv(const Image *ima, const int tile_number, float r_uv[2])
void BKE_image_release_ibuf(Image *ima, ImBuf *ibuf, void *lock)
bool ED_object_get_active_image(Object *ob, int mat_nr, Image **r_ima, ImageUser **r_iuser, const bNode **r_node, const bNodeTree **r_ntree)
Definition uvedit_ops.cc:98
#define fabsf
struct Object * ob
Definition RE_bake.h:65
struct Object * ob_eval
Definition RE_bake.h:66
struct Mesh * mesh
Definition RE_bake.h:67
float uv_offset[2]
Definition RE_bake.h:27
struct Image * image
Definition RE_bake.h:25
struct Image ** material_to_image
Definition RE_bake.h:42
BakeImage * images
Definition RE_bake.h:38
char name[258]
Definition DNA_ID.h:433
struct ID * orig_id
Definition DNA_ID.h:502
uint8_t * byte_data_for_write()
Definition allocimbuf.cc:60
const float * float_data() const
ImColorMode color_mode
float * float_data_for_write()
Definition allocimbuf.cc:79
ListBaseT< Object > objects
Definition BKE_main.hh:388
ListBaseT< Image > images
Definition BKE_main.hh:394
struct Material ** mat
CustomData face_data
CustomData vert_data
struct ModifierData * next
ListBaseT< ModifierData > modifiers
eObject_VisibilityFlag visibility_flag
struct Material ** mat
struct RenderData r
ListBaseT< SpaceLink > spacedata
wmEventType type
Definition WM_types.hh:817
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
wmOperatorStatus(*) modal(bContext *C, wmOperator *op, const wmEvent *event) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1157
struct ReportList * reports