Fix some inconsistencies in object visibility/selectability tests.
[blender.git] / source / blender / editors / object / object_bake_api.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2004 by Blender Foundation
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s):
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file blender/editors/object/object_bake_api.c
29  *  \ingroup edobj
30  */
31
32
33 #include "MEM_guardedalloc.h"
34
35 #include "DNA_object_types.h"
36 #include "DNA_mesh_types.h"
37 #include "DNA_material_types.h"
38
39 #include "RNA_access.h"
40 #include "RNA_define.h"
41 #include "RNA_enum_types.h"
42
43 #include "BLI_listbase.h"
44 #include "BLI_string.h"
45 #include "BLI_fileops.h"
46 #include "BLI_math_geom.h"
47 #include "BLI_path_util.h"
48
49 #include "BKE_context.h"
50 #include "BKE_global.h"
51 #include "BKE_image.h"
52 #include "BKE_layer.h"
53 #include "BKE_library.h"
54 #include "BKE_main.h"
55 #include "BKE_material.h"
56 #include "BKE_mesh.h"
57 #include "BKE_modifier.h"
58 #include "BKE_node.h"
59 #include "BKE_object.h"
60 #include "BKE_report.h"
61 #include "BKE_scene.h"
62 #include "BKE_screen.h"
63
64 #include "DEG_depsgraph.h"
65 #include "DEG_depsgraph_build.h"
66 #include "DEG_depsgraph_query.h"
67
68 #include "RE_engine.h"
69 #include "RE_pipeline.h"
70
71 #include "IMB_imbuf_types.h"
72 #include "IMB_imbuf.h"
73 #include "IMB_colormanagement.h"
74
75 #include "WM_api.h"
76 #include "WM_types.h"
77
78 #include "ED_object.h"
79 #include "ED_screen.h"
80 #include "ED_uvedit.h"
81
82 #include "GPU_draw.h"
83
84 #include "object_intern.h"
85
86 /* prototypes */
87 static void bake_set_props(wmOperator *op, Scene *scene);
88
89 typedef struct BakeAPIRender {
90         Object *ob;
91         Main *main;
92         Scene *scene;
93         ViewLayer *view_layer;
94         ReportList *reports;
95         ListBase selected_objects;
96
97         eScenePassType pass_type;
98         int pass_filter;
99         int margin;
100
101         int save_mode;
102
103         bool is_clear;
104         bool is_split_materials;
105         bool is_automatic_name;
106         bool is_selected_to_active;
107         bool is_cage;
108
109         float cage_extrusion;
110         int normal_space;
111         eBakeNormalSwizzle normal_swizzle[3];
112
113         char uv_layer[MAX_CUSTOMDATA_LAYER_NAME];
114         char custom_cage[MAX_NAME];
115         char filepath[FILE_MAX];
116
117         int width;
118         int height;
119         const char *identifier;
120
121         int result;
122         bool ready;
123
124         /* callbacks */
125         Render *render;
126         float *progress;
127         short *do_update;
128
129         /* for redrawing */
130         ScrArea *sa;
131 } BakeAPIRender;
132
133 /* callbacks */
134
135 static void bake_progress_update(void *bjv, float progress)
136 {
137         BakeAPIRender *bj = bjv;
138
139         if (bj->progress && *bj->progress != progress) {
140                 *bj->progress = progress;
141
142                 /* make jobs timer to send notifier */
143                 *(bj->do_update) = true;
144         }
145 }
146
147 /* catch esc */
148 static int bake_modal(bContext *C, wmOperator *UNUSED(op), const wmEvent *event)
149 {
150         /* no running blender, remove handler and pass through */
151         if (0 == WM_jobs_test(CTX_wm_manager(C), CTX_data_scene(C), WM_JOB_TYPE_OBJECT_BAKE))
152                 return OPERATOR_FINISHED | OPERATOR_PASS_THROUGH;
153
154         /* running render */
155         switch (event->type) {
156                 case ESCKEY:
157                 {
158                         G.is_break = true;
159                         return OPERATOR_RUNNING_MODAL;
160                 }
161         }
162         return OPERATOR_PASS_THROUGH;
163 }
164
165 /* for exec() when there is no render job
166  * note: this wont check for the escape key being pressed, but doing so isnt threadsafe */
167 static int bake_break(void *UNUSED(rjv))
168 {
169         if (G.is_break)
170                 return 1;
171         return 0;
172 }
173
174
175 static void bake_update_image(ScrArea *sa, Image *image)
176 {
177         if (sa && sa->spacetype == SPACE_IMAGE) { /* in case the user changed while baking */
178                 SpaceImage *sima = sa->spacedata.first;
179                 if (sima)
180                         sima->image = image;
181         }
182 }
183
184 static bool write_internal_bake_pixels(
185         Image *image, BakePixel pixel_array[], float *buffer,
186         const int width, const int height, const int margin,
187         const bool is_clear, const bool is_noncolor)
188 {
189         ImBuf *ibuf;
190         void *lock;
191         bool is_float;
192         char *mask_buffer = NULL;
193         const size_t num_pixels = (size_t)width * (size_t)height;
194
195         ibuf = BKE_image_acquire_ibuf(image, NULL, &lock);
196
197         if (!ibuf)
198                 return false;
199
200         if (margin > 0 || !is_clear) {
201                 mask_buffer = MEM_callocN(sizeof(char) * num_pixels, "Bake Mask");
202                 RE_bake_mask_fill(pixel_array, num_pixels, mask_buffer);
203         }
204
205         is_float = (ibuf->flags & IB_rectfloat);
206
207         /* colormanagement conversions */
208         if (!is_noncolor) {
209                 const char *from_colorspace;
210                 const char *to_colorspace;
211
212                 from_colorspace = IMB_colormanagement_role_colorspace_name_get(COLOR_ROLE_SCENE_LINEAR);
213
214                 if (is_float)
215                         to_colorspace = IMB_colormanagement_get_float_colorspace(ibuf);
216                 else
217                         to_colorspace = IMB_colormanagement_get_rect_colorspace(ibuf);
218
219                 if (from_colorspace != to_colorspace)
220                         IMB_colormanagement_transform(buffer, ibuf->x, ibuf->y, ibuf->channels, from_colorspace, to_colorspace, false);
221         }
222
223         /* populates the ImBuf */
224         if (is_clear) {
225                 if (is_float) {
226                         IMB_buffer_float_from_float(
227                                 ibuf->rect_float, buffer, ibuf->channels,
228                                 IB_PROFILE_LINEAR_RGB, IB_PROFILE_LINEAR_RGB, false,
229                                 ibuf->x, ibuf->y, ibuf->x, ibuf->x);
230                 }
231                 else {
232                         IMB_buffer_byte_from_float(
233                                 (unsigned char *) ibuf->rect, buffer, ibuf->channels, ibuf->dither,
234                                 IB_PROFILE_SRGB, IB_PROFILE_SRGB,
235                                 false, ibuf->x, ibuf->y, ibuf->x, ibuf->x);
236                 }
237         }
238         else {
239                 if (is_float) {
240                         IMB_buffer_float_from_float_mask(
241                                 ibuf->rect_float, buffer, ibuf->channels,
242                                 ibuf->x, ibuf->y, ibuf->x, ibuf->x, mask_buffer);
243                 }
244                 else {
245                         IMB_buffer_byte_from_float_mask(
246                                 (unsigned char *) ibuf->rect, buffer, ibuf->channels, ibuf->dither,
247                                 false, ibuf->x, ibuf->y, ibuf->x, ibuf->x, mask_buffer);
248                 }
249         }
250
251         /* margins */
252         if (margin > 0)
253                 RE_bake_margin(ibuf, mask_buffer, margin);
254
255         ibuf->userflags |= IB_DISPLAY_BUFFER_INVALID | IB_BITMAPDIRTY;
256
257         if (ibuf->rect_float)
258                 ibuf->userflags |= IB_RECT_INVALID;
259
260         /* force mipmap recalc */
261         if (ibuf->mipmap[0]) {
262                 ibuf->userflags |= IB_MIPMAP_INVALID;
263                 imb_freemipmapImBuf(ibuf);
264         }
265
266         BKE_image_release_ibuf(image, ibuf, NULL);
267
268         if (mask_buffer)
269                 MEM_freeN(mask_buffer);
270
271         return true;
272 }
273
274 /* force OpenGL reload */
275 static void refresh_images(BakeImages *bake_images)
276 {
277         int i;
278         for (i = 0; i < bake_images->size; i++) {
279                 Image *ima = bake_images->data[i].image;
280                 if (ima->ok == IMA_OK_LOADED) {
281                         GPU_free_image(ima);
282                         DEG_id_tag_update(&ima->id, 0);
283                 }
284         }
285 }
286
287 static bool write_external_bake_pixels(
288         const char *filepath, BakePixel pixel_array[], float *buffer,
289         const int width, const int height, const int margin,
290         ImageFormatData *im_format, const bool is_noncolor)
291 {
292         ImBuf *ibuf = NULL;
293         bool ok = false;
294         bool is_float;
295
296         is_float = im_format->depth > 8;
297
298         /* create a new ImBuf */
299         ibuf = IMB_allocImBuf(width, height, im_format->planes, (is_float ? IB_rectfloat : IB_rect));
300
301         if (!ibuf)
302                 return false;
303
304         /* populates the ImBuf */
305         if (is_float) {
306                 IMB_buffer_float_from_float(
307                         ibuf->rect_float, buffer, ibuf->channels,
308                         IB_PROFILE_LINEAR_RGB, IB_PROFILE_LINEAR_RGB, false,
309                         ibuf->x, ibuf->y, ibuf->x, ibuf->x);
310         }
311         else {
312                 if (!is_noncolor) {
313                         const char *from_colorspace = IMB_colormanagement_role_colorspace_name_get(COLOR_ROLE_SCENE_LINEAR);
314                         const char *to_colorspace = IMB_colormanagement_get_rect_colorspace(ibuf);
315                         IMB_colormanagement_transform(buffer, ibuf->x, ibuf->y, ibuf->channels, from_colorspace, to_colorspace, false);
316                 }
317
318                 IMB_buffer_byte_from_float(
319                         (unsigned char *) ibuf->rect, buffer, ibuf->channels, ibuf->dither,
320                         IB_PROFILE_SRGB, IB_PROFILE_SRGB,
321                         false, ibuf->x, ibuf->y, ibuf->x, ibuf->x);
322         }
323
324         /* margins */
325         if (margin > 0) {
326                 char *mask_buffer = NULL;
327                 const size_t num_pixels = (size_t)width * (size_t)height;
328
329                 mask_buffer = MEM_callocN(sizeof(char) * num_pixels, "Bake Mask");
330                 RE_bake_mask_fill(pixel_array, num_pixels, mask_buffer);
331                 RE_bake_margin(ibuf, mask_buffer, margin);
332
333                 if (mask_buffer)
334                         MEM_freeN(mask_buffer);
335         }
336
337         if ((ok = BKE_imbuf_write(ibuf, filepath, im_format))) {
338 #ifndef WIN32
339                 chmod(filepath, S_IRUSR | S_IWUSR);
340 #endif
341                 //printf("%s saving bake map: '%s'\n", __func__, filepath);
342         }
343
344         /* garbage collection */
345         IMB_freeImBuf(ibuf);
346
347         return ok;
348 }
349
350 static bool is_noncolor_pass(eScenePassType pass_type)
351 {
352         return ELEM(pass_type,
353                     SCE_PASS_Z,
354                     SCE_PASS_NORMAL,
355                     SCE_PASS_VECTOR,
356                     SCE_PASS_INDEXOB,
357                     SCE_PASS_UV,
358                     SCE_PASS_RAYHITS,
359                     SCE_PASS_INDEXMA);
360 }
361
362 /* if all is good tag image and return true */
363 static bool bake_object_check(ViewLayer *view_layer, Object *ob, ReportList *reports)
364 {
365         Image *image;
366         Base *base = BKE_view_layer_base_find(view_layer, ob);
367         void *lock;
368         int i;
369
370         if (base == NULL) {
371                 BKE_reportf(reports, RPT_ERROR, "Object \"%s\" is not in view layer", ob->id.name + 2);
372                 return false;
373         }
374
375         if (!(base->flag & BASE_ENABLED_RENDER)) {
376                 BKE_reportf(reports, RPT_ERROR, "Object \"%s\" is not enabled for rendering", ob->id.name + 2);
377                 return false;
378         }
379
380
381         if (ob->type != OB_MESH) {
382                 BKE_reportf(reports, RPT_ERROR, "Object \"%s\" is not a mesh", ob->id.name + 2);
383                 return false;
384         }
385         else {
386                 Mesh *me = (Mesh *)ob->data;
387
388                 if (CustomData_get_active_layer_index(&me->ldata, CD_MLOOPUV) == -1) {
389                         BKE_reportf(reports, RPT_ERROR,
390                                     "No active UV layer found in the object \"%s\"", ob->id.name + 2);
391                         return false;
392                 }
393         }
394
395         for (i = 0; i < ob->totcol; i++) {
396                 bNodeTree *ntree = NULL;
397                 bNode *node = NULL;
398                 ED_object_get_active_image(ob, i + 1, &image, NULL, &node, &ntree);
399
400                 if (image) {
401                         ImBuf *ibuf;
402
403                         if (node) {
404                                 if (BKE_node_is_connected_to_output(ntree, node)) {
405                                         /* we don't return false since this may be a false positive
406                                          * this can't be RPT_ERROR though, otherwise it prevents
407                                          * multiple highpoly objects to be baked at once */
408                                         BKE_reportf(reports, RPT_INFO,
409                                                     "Circular dependency for image \"%s\" from object \"%s\"",
410                                                     image->id.name + 2, ob->id.name + 2);
411                                 }
412                         }
413
414                         ibuf = BKE_image_acquire_ibuf(image, NULL, &lock);
415
416                         if (ibuf) {
417                                 BKE_image_release_ibuf(image, ibuf, lock);
418                         }
419                         else {
420                                 BKE_reportf(reports, RPT_ERROR,
421                                             "Uninitialized image \"%s\" from object \"%s\"",
422                                             image->id.name + 2, ob->id.name + 2);
423
424                                 BKE_image_release_ibuf(image, ibuf, lock);
425                                 return false;
426                         }
427                 }
428                 else {
429                         Material *mat = give_current_material(ob, i);
430                         if (mat != NULL) {
431                                 BKE_reportf(reports, RPT_INFO,
432                                             "No active image found in material \"%s\" (%d) for object \"%s\"",
433                                             mat->id.name + 2, i, ob->id.name + 2);
434                         }
435                         else {
436                                 BKE_reportf(reports, RPT_INFO,
437                                             "No active image found in material slot (%d) for object \"%s\"",
438                                             i, ob->id.name + 2);
439                         }
440                         continue;
441                 }
442
443                 image->id.tag |= LIB_TAG_DOIT;
444         }
445         return true;
446 }
447
448 static bool bake_pass_filter_check(eScenePassType pass_type, const int pass_filter, ReportList *reports)
449 {
450         switch (pass_type) {
451                 case SCE_PASS_COMBINED:
452                         if ((pass_filter & R_BAKE_PASS_FILTER_EMIT) != 0) {
453                                 return true;
454                         }
455
456                         if (((pass_filter & R_BAKE_PASS_FILTER_DIRECT) != 0) ||
457                             ((pass_filter & R_BAKE_PASS_FILTER_INDIRECT) != 0))
458                         {
459                                 if (((pass_filter & R_BAKE_PASS_FILTER_DIFFUSE) != 0) ||
460                                     ((pass_filter & R_BAKE_PASS_FILTER_GLOSSY) != 0) ||
461                                     ((pass_filter & R_BAKE_PASS_FILTER_TRANSM) != 0) ||
462                                     ((pass_filter & R_BAKE_PASS_FILTER_SUBSURFACE) != 0))
463                                 {
464                                         return true;
465                                 }
466
467                                 if ((pass_filter & R_BAKE_PASS_FILTER_AO) != 0) {
468                                         BKE_report(reports, RPT_ERROR,
469                                                    "Combined bake pass Ambient Occlusion contribution requires an enabled light pass "
470                                                    "(bake the Ambient Occlusion pass type instead)");
471                                 }
472                                 else {
473                                         BKE_report(reports, RPT_ERROR,
474                                                    "Combined bake pass requires Emit, or a light pass with "
475                                                    "Direct or Indirect contributions enabled");
476                                 }
477
478                                 return false;
479                         }
480                         else {
481                                 BKE_report(reports, RPT_ERROR,
482                                            "Combined bake pass requires Emit, or a light pass with "
483                                            "Direct or Indirect contributions enabled");
484                                 return false;
485                         }
486                         break;
487                 case SCE_PASS_DIFFUSE_COLOR:
488                 case SCE_PASS_GLOSSY_COLOR:
489                 case SCE_PASS_TRANSM_COLOR:
490                 case SCE_PASS_SUBSURFACE_COLOR:
491                         if (((pass_filter & R_BAKE_PASS_FILTER_COLOR) != 0) ||
492                             ((pass_filter & R_BAKE_PASS_FILTER_DIRECT) != 0) ||
493                             ((pass_filter & R_BAKE_PASS_FILTER_INDIRECT) != 0))
494                         {
495                                 return true;
496                         }
497                         else {
498                                 BKE_report(reports, RPT_ERROR,
499                                            "Bake pass requires Direct, Indirect, or Color contributions to be enabled");
500                                 return false;
501                         }
502                         break;
503                 default:
504                         return true;
505                         break;
506         }
507 }
508
509 /* before even getting in the bake function we check for some basic errors */
510 static bool bake_objects_check(Main *bmain, ViewLayer *view_layer, Object *ob, ListBase *selected_objects,
511                                ReportList *reports, const bool is_selected_to_active)
512 {
513         CollectionPointerLink *link;
514
515         /* error handling and tag (in case multiple materials share the same image) */
516         BKE_main_id_tag_idcode(bmain, ID_IM, LIB_TAG_DOIT, false);
517
518         if (is_selected_to_active) {
519                 int tot_objects = 0;
520
521                 if (!bake_object_check(view_layer, ob, reports))
522                         return false;
523
524                 for (link = selected_objects->first; link; link = link->next) {
525                         Object *ob_iter = (Object *)link->ptr.data;
526
527                         if (ob_iter == ob)
528                                 continue;
529
530                         if (ELEM(ob_iter->type, OB_MESH, OB_FONT, OB_CURVE, OB_SURF, OB_MBALL) == false) {
531                                 BKE_reportf(reports, RPT_ERROR, "Object \"%s\" is not a mesh or can't be converted to a mesh (Curve, Text, Surface or Metaball)", ob_iter->id.name + 2);
532                                 return false;
533                         }
534                         tot_objects += 1;
535                 }
536
537                 if (tot_objects == 0) {
538                         BKE_report(reports, RPT_ERROR, "No valid selected objects");
539                         return false;
540                 }
541         }
542         else {
543                 if (BLI_listbase_is_empty(selected_objects)) {
544                         BKE_report(reports, RPT_ERROR, "No valid selected objects");
545                         return false;
546                 }
547
548                 for (link = selected_objects->first; link; link = link->next) {
549                         if (!bake_object_check(view_layer, link->ptr.data, reports))
550                                 return false;
551                 }
552         }
553         return true;
554 }
555
556 /* it needs to be called after bake_objects_check since the image tagging happens there */
557 static void bake_images_clear(Main *bmain, const bool is_tangent)
558 {
559         Image *image;
560         for (image = bmain->image.first; image; image = image->id.next) {
561                 if ((image->id.tag & LIB_TAG_DOIT) != 0) {
562                         RE_bake_ibuf_clear(image, is_tangent);
563                 }
564         }
565 }
566
567 static void build_image_lookup(Main *bmain, Object *ob, BakeImages *bake_images)
568 {
569         const int tot_mat = ob->totcol;
570         int i, j;
571         int tot_images = 0;
572
573         /* error handling and tag (in case multiple materials share the same image) */
574         BKE_main_id_tag_idcode(bmain, ID_IM, LIB_TAG_DOIT, false);
575
576         for (i = 0; i < tot_mat; i++) {
577                 Image *image;
578                 ED_object_get_active_image(ob, i + 1, &image, NULL, NULL, NULL);
579
580                 /* Some materials have no image, we just ignore those cases. */
581                 if (image == NULL) {
582                         bake_images->lookup[i] = -1;
583                 }
584                 else if (image->id.tag & LIB_TAG_DOIT) {
585                         for (j = 0; j < i; j++) {
586                                 if (bake_images->data[j].image == image) {
587                                         bake_images->lookup[i] = j;
588                                         break;
589                                 }
590                         }
591                 }
592                 else {
593                         bake_images->lookup[i] = tot_images;
594                         bake_images->data[tot_images].image = image;
595                         image->id.tag |= LIB_TAG_DOIT;
596                         tot_images++;
597                 }
598         }
599
600         bake_images->size = tot_images;
601 }
602
603 /*
604  * returns the total number of pixels
605  */
606 static size_t initialize_internal_images(BakeImages *bake_images, ReportList *reports)
607 {
608         int i;
609         size_t tot_size = 0;
610
611         for (i = 0; i < bake_images->size; i++) {
612                 ImBuf *ibuf;
613                 void *lock;
614
615                 BakeImage *bk_image = &bake_images->data[i];
616                 ibuf = BKE_image_acquire_ibuf(bk_image->image, NULL, &lock);
617
618                 if (ibuf) {
619                         bk_image->width = ibuf->x;
620                         bk_image->height = ibuf->y;
621                         bk_image->offset = tot_size;
622
623                         tot_size += (size_t)ibuf->x * (size_t)ibuf->y;
624                 }
625                 else {
626                         BKE_image_release_ibuf(bk_image->image, ibuf, lock);
627                         BKE_reportf(reports, RPT_ERROR, "Uninitialized image %s", bk_image->image->id.name + 2);
628                         return 0;
629                 }
630                 BKE_image_release_ibuf(bk_image->image, ibuf, lock);
631         }
632         return tot_size;
633 }
634
635 /* create new mesh with edit mode changes and modifiers applied */
636 static Mesh *bake_mesh_new_from_object(Depsgraph *depsgraph, Main *bmain, Scene *scene, Object *ob)
637 {
638         ED_object_editmode_load(bmain, ob);
639
640         Mesh *me = BKE_mesh_new_from_object(depsgraph, bmain, scene, ob, 1, 0);
641         if (me->flag & ME_AUTOSMOOTH) {
642                 BKE_mesh_split_faces(me, true);
643         }
644
645         return me;
646 }
647
648 static int bake(
649         Render *re, Main *bmain, Scene *scene, ViewLayer *view_layer, Object *ob_low, ListBase *selected_objects,
650         ReportList *reports,
651         const eScenePassType pass_type, const int pass_filter, const int margin,
652         const eBakeSaveMode save_mode, const bool is_clear, const bool is_split_materials,
653         const bool is_automatic_name, const bool is_selected_to_active, const bool is_cage,
654         const float cage_extrusion, const int normal_space, const eBakeNormalSwizzle normal_swizzle[],
655         const char *custom_cage, const char *filepath, const int width, const int height,
656         const char *identifier, ScrArea *sa, const char *uv_layer)
657 {
658         /* We build a depsgraph for the baking, so we don't need to change the original data to adjust visibility and modifiers. */
659         Depsgraph *depsgraph = DEG_graph_new(scene, view_layer, DAG_EVAL_RENDER);
660         DEG_graph_build_from_view_layer(depsgraph, bmain, scene, view_layer);
661
662         int op_result = OPERATOR_CANCELLED;
663         bool ok = false;
664
665         Object *ob_cage = NULL;
666         Object *ob_cage_eval = NULL;
667         Object *ob_low_eval = NULL;
668
669         BakeHighPolyData *highpoly = NULL;
670         int tot_highpoly = 0;
671
672         Mesh *me_low = NULL;
673         Mesh *me_cage = NULL;
674
675         MultiresModifierData *mmd_low = NULL;
676         int mmd_flags_low = 0;
677
678         float *result = NULL;
679
680         BakePixel *pixel_array_low = NULL;
681         BakePixel *pixel_array_high = NULL;
682
683         const bool is_save_internal = (save_mode == R_BAKE_SAVE_INTERNAL);
684         const bool is_noncolor = is_noncolor_pass(pass_type);
685         const int depth = RE_pass_depth(pass_type);
686
687         BakeImages bake_images = {NULL};
688
689         size_t num_pixels;
690         int tot_materials;
691
692         RE_bake_engine_set_engine_parameters(re, bmain, scene);
693
694         if (!RE_bake_has_engine(re)) {
695                 BKE_report(reports, RPT_ERROR, "Current render engine does not support baking");
696                 goto cleanup;
697         }
698
699         tot_materials = ob_low->totcol;
700
701         if (uv_layer && uv_layer[0] != '\0') {
702                 Mesh *me = (Mesh *)ob_low->data;
703                 if (CustomData_get_named_layer(&me->ldata, CD_MLOOPUV, uv_layer) == -1) {
704                         BKE_reportf(reports, RPT_ERROR,
705                                     "No UV layer named \"%s\" found in the object \"%s\"", uv_layer, ob_low->id.name + 2);
706                         goto cleanup;
707                 }
708         }
709
710         if (tot_materials == 0) {
711                 if (is_save_internal) {
712                         BKE_report(reports, RPT_ERROR,
713                                    "No active image found, add a material or bake to an external file");
714
715                         goto cleanup;
716                 }
717                 else if (is_split_materials) {
718                         BKE_report(reports, RPT_ERROR,
719                                    "No active image found, add a material or bake without the Split Materials option");
720
721                         goto cleanup;
722                 }
723                 else {
724                         /* baking externally without splitting materials */
725                         tot_materials = 1;
726                 }
727         }
728
729         /* we overallocate in case there is more materials than images */
730         bake_images.data = MEM_mallocN(sizeof(BakeImage) * tot_materials, "bake images dimensions (width, height, offset)");
731         bake_images.lookup = MEM_mallocN(sizeof(int) * tot_materials, "bake images lookup (from material to BakeImage)");
732
733         build_image_lookup(bmain, ob_low, &bake_images);
734
735         if (is_save_internal) {
736                 num_pixels = initialize_internal_images(&bake_images, reports);
737
738                 if (num_pixels == 0) {
739                         goto cleanup;
740                 }
741         }
742         else {
743                 /* when saving externally always use the size specified in the UI */
744
745                 num_pixels = (size_t)width * (size_t)height * bake_images.size;
746
747                 for (int i = 0; i < bake_images.size; i++) {
748                         bake_images.data[i].width = width;
749                         bake_images.data[i].height = height;
750                         bake_images.data[i].offset = (is_split_materials ? num_pixels : 0);
751                         bake_images.data[i].image = NULL;
752                 }
753
754                 if (!is_split_materials) {
755                         /* saving a single image */
756                         for (int i = 0; i < tot_materials; i++) {
757                                 bake_images.lookup[i] = 0;
758                         }
759                 }
760         }
761
762         if (is_selected_to_active) {
763                 CollectionPointerLink *link;
764                 tot_highpoly = 0;
765
766                 for (link = selected_objects->first; link; link = link->next) {
767                         Object *ob_iter = link->ptr.data;
768
769                         if (ob_iter == ob_low)
770                                 continue;
771
772                         tot_highpoly ++;
773                 }
774
775                 if (is_cage && custom_cage[0] != '\0') {
776                         ob_cage = BLI_findstring(&bmain->object, custom_cage, offsetof(ID, name) + 2);
777
778                         if (ob_cage == NULL || ob_cage->type != OB_MESH) {
779                                 BKE_report(reports, RPT_ERROR, "No valid cage object");
780                                 goto cleanup;
781                         }
782                         else {
783                                 ob_cage_eval = DEG_get_evaluated_object(depsgraph, ob_cage);
784                                 ob_cage_eval->restrictflag |= OB_RESTRICT_RENDER;
785                                 ob_cage_eval->base_flag &= ~(BASE_VISIBLE | BASE_ENABLED_RENDER);
786                         }
787                 }
788         }
789
790         pixel_array_low = MEM_mallocN(sizeof(BakePixel) * num_pixels, "bake pixels low poly");
791         pixel_array_high = MEM_mallocN(sizeof(BakePixel) * num_pixels, "bake pixels high poly");
792         result = MEM_callocN(sizeof(float) * depth * num_pixels, "bake return pixels");
793
794         /* for multires bake, use linear UV subdivision to match low res UVs */
795         if (pass_type == SCE_PASS_NORMAL && normal_space == R_BAKE_SPACE_TANGENT && !is_selected_to_active) {
796                 mmd_low = (MultiresModifierData *) modifiers_findByType(ob_low, eModifierType_Multires);
797                 if (mmd_low) {
798                         mmd_flags_low = mmd_low->flags;
799                         mmd_low->uv_smooth = SUBSURF_UV_SMOOTH_NONE;
800                 }
801         }
802
803         /* Make sure depsgraph is up to date. */
804         BKE_scene_graph_update_tagged(depsgraph, bmain);
805         ob_low_eval = DEG_get_evaluated_object(depsgraph, ob_low);
806
807         /* get the mesh as it arrives in the renderer */
808         me_low = bake_mesh_new_from_object(depsgraph, bmain, scene, ob_low);
809
810         /* populate the pixel array with the face data */
811         if ((is_selected_to_active && (ob_cage == NULL) && is_cage) == false)
812                 RE_bake_pixels_populate(me_low, pixel_array_low, num_pixels, &bake_images, uv_layer);
813         /* else populate the pixel array with the 'cage' mesh (the smooth version of the mesh)  */
814
815         if (is_selected_to_active) {
816                 CollectionPointerLink *link;
817                 int i = 0;
818
819                 /* prepare cage mesh */
820                 if (ob_cage) {
821                         me_cage = bake_mesh_new_from_object(depsgraph, bmain, scene, ob_cage);
822                         if ((me_low->totpoly != me_cage->totpoly) || (me_low->totloop != me_cage->totloop)) {
823                                 BKE_report(reports, RPT_ERROR,
824                                            "Invalid cage object, the cage mesh must have the same number "
825                                            "of faces as the active object");
826                                 goto cleanup;
827                         }
828                 }
829                 else if (is_cage) {
830                         ModifierData *md = ob_low_eval->modifiers.first;
831                         while (md) {
832                                 ModifierData *md_next = md->next;
833
834                                 /* Edge Split cannot be applied in the cage,
835                                  * the cage is supposed to have interpolated normals
836                                  * between the faces unless the geometry is physically
837                                  * split. So we create a copy of the low poly mesh without
838                                  * the eventual edge split.*/
839
840                                 if (md->type == eModifierType_EdgeSplit) {
841                                         BLI_remlink(&ob_low_eval->modifiers, md);
842                                         modifier_free(md);
843                                 }
844                                 md = md_next;
845                         }
846
847                         BKE_object_eval_reset(ob_low_eval);
848                         me_cage = bake_mesh_new_from_object(depsgraph, bmain, scene, ob_low_eval);
849                         RE_bake_pixels_populate(me_cage, pixel_array_low, num_pixels, &bake_images, uv_layer);
850                 }
851
852                 highpoly = MEM_callocN(sizeof(BakeHighPolyData) * tot_highpoly, "bake high poly objects");
853
854                 /* populate highpoly array */
855                 for (link = selected_objects->first; link; link = link->next) {
856                         Object *ob_iter = link->ptr.data;
857
858                         if (ob_iter == ob_low)
859                                 continue;
860
861                         /* initialize highpoly_data */
862                         highpoly[i].ob = ob_iter;
863                         highpoly[i].me = bake_mesh_new_from_object(depsgraph, bmain, scene, highpoly[i].ob);
864                         highpoly[i].ob_eval = DEG_get_evaluated_object(depsgraph, ob_iter);
865                         highpoly[i].ob_eval->restrictflag &= ~OB_RESTRICT_RENDER;
866                         highpoly[i].ob_eval->base_flag |= (BASE_VISIBLE | BASE_ENABLED_RENDER);
867
868                         /* lowpoly to highpoly transformation matrix */
869                         copy_m4_m4(highpoly[i].obmat, highpoly[i].ob->obmat);
870                         invert_m4_m4(highpoly[i].imat, highpoly[i].obmat);
871
872                         highpoly[i].is_flip_object = is_negative_m4(highpoly[i].ob->obmat);
873
874                         i++;
875                 }
876
877                 BLI_assert(i == tot_highpoly);
878
879
880                 if (ob_cage != NULL) {
881                         ob_cage_eval->restrictflag |= OB_RESTRICT_RENDER;
882                         ob_cage_eval->base_flag &= ~(BASE_VISIBLE | BASE_ENABLED_RENDER);
883                 }
884                 ob_low_eval->restrictflag |= OB_RESTRICT_RENDER;
885                 ob_low_eval->base_flag &= ~(BASE_VISIBLE | BASE_ENABLED_RENDER);
886
887                 /* populate the pixel arrays with the corresponding face data for each high poly object */
888                 if (!RE_bake_pixels_populate_from_objects(
889                             me_low, pixel_array_low, pixel_array_high, highpoly, tot_highpoly, num_pixels, ob_cage != NULL,
890                             cage_extrusion, ob_low->obmat, (ob_cage ? ob_cage->obmat : ob_low->obmat), me_cage))
891                 {
892                         BKE_report(reports, RPT_ERROR, "Error handling selected objects");
893                         goto cleanup;
894                 }
895
896                 /* the baking itself */
897                 for (i = 0; i < tot_highpoly; i++) {
898                         ok = RE_bake_engine(re, depsgraph, highpoly[i].ob, i, pixel_array_high,
899                                             num_pixels, depth, pass_type, pass_filter, result);
900                         if (!ok) {
901                                 BKE_reportf(reports, RPT_ERROR, "Error baking from object \"%s\"", highpoly[i].ob->id.name + 2);
902                                 goto cleanup;
903                         }
904                 }
905         }
906         else {
907                 /* If low poly is not renderable it should have failed long ago. */
908                 BLI_assert((ob_low->restrictflag & OB_RESTRICT_RENDER) == 0);
909
910                 if (RE_bake_has_engine(re)) {
911                         ok = RE_bake_engine(re, depsgraph, ob_low, 0, pixel_array_low, num_pixels, depth, pass_type, pass_filter, result);
912                 }
913                 else {
914                         BKE_report(reports, RPT_ERROR, "Current render engine does not support baking");
915                         goto cleanup;
916                 }
917         }
918
919         /* normal space conversion
920          * the normals are expected to be in world space, +X +Y +Z */
921         if (ok && pass_type == SCE_PASS_NORMAL) {
922                 switch (normal_space) {
923                         case R_BAKE_SPACE_WORLD:
924                         {
925                                 /* Cycles internal format */
926                                 if ((normal_swizzle[0] == R_BAKE_POSX) &&
927                                     (normal_swizzle[1] == R_BAKE_POSY) &&
928                                     (normal_swizzle[2] == R_BAKE_POSZ))
929                                 {
930                                         break;
931                                 }
932                                 else {
933                                         RE_bake_normal_world_to_world(pixel_array_low, num_pixels,  depth, result, normal_swizzle);
934                                 }
935                                 break;
936                         }
937                         case R_BAKE_SPACE_OBJECT:
938                         {
939                                 RE_bake_normal_world_to_object(pixel_array_low, num_pixels, depth, result, ob_low, normal_swizzle);
940                                 break;
941                         }
942                         case R_BAKE_SPACE_TANGENT:
943                         {
944                                 if (is_selected_to_active) {
945                                         RE_bake_normal_world_to_tangent(pixel_array_low, num_pixels, depth, result, me_low, normal_swizzle, ob_low->obmat);
946                                 }
947                                 else {
948                                         /* from multiresolution */
949                                         Mesh *me_nores = NULL;
950                                         ModifierData *md = NULL;
951                                         int mode;
952
953                                         md = modifiers_findByType(ob_low, eModifierType_Multires);
954
955                                         if (md) {
956                                                 mode = md->mode;
957                                                 md->mode &= ~eModifierMode_Render;
958                                         }
959
960                                         me_nores = bake_mesh_new_from_object(depsgraph, bmain, scene, ob_low);
961                                         RE_bake_pixels_populate(me_nores, pixel_array_low, num_pixels, &bake_images, uv_layer);
962
963                                         RE_bake_normal_world_to_tangent(pixel_array_low, num_pixels, depth, result, me_nores, normal_swizzle, ob_low->obmat);
964                                         BKE_libblock_free(bmain, me_nores);
965
966                                         if (md)
967                                                 md->mode = mode;
968                                 }
969                                 break;
970                         }
971                         default:
972                                 break;
973                 }
974         }
975
976         if (!ok) {
977                 BKE_reportf(reports, RPT_ERROR, "Problem baking object \"%s\"", ob_low->id.name + 2);
978                 op_result = OPERATOR_CANCELLED;
979         }
980         else {
981                 /* save the results */
982                 for (int i = 0; i < bake_images.size; i++) {
983                         BakeImage *bk_image = &bake_images.data[i];
984
985                         if (is_save_internal) {
986                                 ok = write_internal_bake_pixels(
987                                          bk_image->image,
988                                          pixel_array_low + bk_image->offset,
989                                          result + bk_image->offset * depth,
990                                          bk_image->width, bk_image->height,
991                                          margin, is_clear, is_noncolor);
992
993                                 /* might be read by UI to set active image for display */
994                                 bake_update_image(sa, bk_image->image);
995
996                                 if (!ok) {
997                                         BKE_reportf(reports, RPT_ERROR,
998                                                    "Problem saving the bake map internally for object \"%s\"", ob_low->id.name + 2);
999                                         op_result = OPERATOR_CANCELLED;
1000                                 }
1001                                 else {
1002                                         BKE_report(reports, RPT_INFO,
1003                                                    "Baking map saved to internal image, save it externally or pack it");
1004                                         op_result = OPERATOR_FINISHED;
1005                                 }
1006                         }
1007                         /* save externally */
1008                         else {
1009                                 BakeData *bake = &scene->r.bake;
1010                                 char name[FILE_MAX];
1011
1012                                 BKE_image_path_from_imtype(name, filepath, BKE_main_blendfile_path(bmain),
1013                                                            0, bake->im_format.imtype, true, false, NULL);
1014
1015                                 if (is_automatic_name) {
1016                                         BLI_path_suffix(name, FILE_MAX, ob_low->id.name + 2, "_");
1017                                         BLI_path_suffix(name, FILE_MAX, identifier, "_");
1018                                 }
1019
1020                                 if (is_split_materials) {
1021                                         if (bk_image->image) {
1022                                                 BLI_path_suffix(name, FILE_MAX, bk_image->image->id.name + 2, "_");
1023                                         }
1024                                         else {
1025                                                 if (ob_low->mat[i]) {
1026                                                         BLI_path_suffix(name, FILE_MAX, ob_low->mat[i]->id.name + 2, "_");
1027                                                 }
1028                                                 else if (me_low->mat[i]) {
1029                                                         BLI_path_suffix(name, FILE_MAX, me_low->mat[i]->id.name + 2, "_");
1030                                                 }
1031                                                 else {
1032                                                         /* if everything else fails, use the material index */
1033                                                         char tmp[5];
1034                                                         sprintf(tmp, "%d", i % 1000);
1035                                                         BLI_path_suffix(name, FILE_MAX, tmp, "_");
1036                                                 }
1037                                         }
1038                                 }
1039
1040                                 /* save it externally */
1041                                 ok = write_external_bake_pixels(
1042                                         name,
1043                                         pixel_array_low + bk_image->offset,
1044                                         result + bk_image->offset * depth,
1045                                         bk_image->width, bk_image->height,
1046                                         margin, &bake->im_format, is_noncolor);
1047
1048                                 if (!ok) {
1049                                         BKE_reportf(reports, RPT_ERROR, "Problem saving baked map in \"%s\"", name);
1050                                         op_result = OPERATOR_CANCELLED;
1051                                 }
1052                                 else {
1053                                         BKE_reportf(reports, RPT_INFO, "Baking map written to \"%s\"", name);
1054                                         op_result = OPERATOR_FINISHED;
1055                                 }
1056
1057                                 if (!is_split_materials) {
1058                                         break;
1059                                 }
1060                         }
1061                 }
1062         }
1063
1064         if (is_save_internal)
1065                 refresh_images(&bake_images);
1066
1067 cleanup:
1068
1069         if (highpoly) {
1070                 int i;
1071                 for (i = 0; i < tot_highpoly; i++) {
1072                         if (highpoly[i].me)
1073                                 BKE_libblock_free(bmain, highpoly[i].me);
1074                 }
1075                 MEM_freeN(highpoly);
1076         }
1077
1078         if (mmd_low)
1079                 mmd_low->flags = mmd_flags_low;
1080
1081         if (pixel_array_low)
1082                 MEM_freeN(pixel_array_low);
1083
1084         if (pixel_array_high)
1085                 MEM_freeN(pixel_array_high);
1086
1087         if (bake_images.data)
1088                 MEM_freeN(bake_images.data);
1089
1090         if (bake_images.lookup)
1091                 MEM_freeN(bake_images.lookup);
1092
1093         if (result)
1094                 MEM_freeN(result);
1095
1096         if (me_low)
1097                 BKE_libblock_free(bmain, me_low);
1098
1099         if (me_cage)
1100                 BKE_libblock_free(bmain, me_cage);
1101
1102         DEG_graph_free(depsgraph);
1103
1104         return op_result;
1105 }
1106
1107 static void bake_init_api_data(wmOperator *op, bContext *C, BakeAPIRender *bkr)
1108 {
1109         bool is_save_internal;
1110         bScreen *sc = CTX_wm_screen(C);
1111
1112         bkr->ob = CTX_data_active_object(C);
1113         bkr->main = CTX_data_main(C);
1114         bkr->view_layer = CTX_data_view_layer(C);
1115         bkr->scene = CTX_data_scene(C);
1116         bkr->sa = sc ? BKE_screen_find_big_area(sc, SPACE_IMAGE, 10) : NULL;
1117
1118         bkr->pass_type = RNA_enum_get(op->ptr, "type");
1119         bkr->pass_filter = RNA_enum_get(op->ptr, "pass_filter");
1120         bkr->margin = RNA_int_get(op->ptr, "margin");
1121
1122         bkr->save_mode = RNA_enum_get(op->ptr, "save_mode");
1123         is_save_internal = (bkr->save_mode == R_BAKE_SAVE_INTERNAL);
1124
1125         bkr->is_clear = RNA_boolean_get(op->ptr, "use_clear");
1126         bkr->is_split_materials = (!is_save_internal) && RNA_boolean_get(op->ptr, "use_split_materials");
1127         bkr->is_automatic_name = RNA_boolean_get(op->ptr, "use_automatic_name");
1128         bkr->is_selected_to_active = RNA_boolean_get(op->ptr, "use_selected_to_active");
1129         bkr->is_cage = RNA_boolean_get(op->ptr, "use_cage");
1130         bkr->cage_extrusion = RNA_float_get(op->ptr, "cage_extrusion");
1131
1132         bkr->normal_space = RNA_enum_get(op->ptr, "normal_space");
1133         bkr->normal_swizzle[0] = RNA_enum_get(op->ptr, "normal_r");
1134         bkr->normal_swizzle[1] = RNA_enum_get(op->ptr, "normal_g");
1135         bkr->normal_swizzle[2] = RNA_enum_get(op->ptr, "normal_b");
1136
1137         bkr->width = RNA_int_get(op->ptr, "width");
1138         bkr->height = RNA_int_get(op->ptr, "height");
1139         bkr->identifier = "";
1140
1141         RNA_string_get(op->ptr, "uv_layer", bkr->uv_layer);
1142
1143         RNA_string_get(op->ptr, "cage_object", bkr->custom_cage);
1144
1145         if ((!is_save_internal) && bkr->is_automatic_name) {
1146                 PropertyRNA *prop = RNA_struct_find_property(op->ptr, "type");
1147                 RNA_property_enum_identifier(C, op->ptr, prop, bkr->pass_type, &bkr->identifier);
1148         }
1149
1150         CTX_data_selected_objects(C, &bkr->selected_objects);
1151
1152         bkr->reports = op->reports;
1153
1154         bkr->result = OPERATOR_CANCELLED;
1155
1156         bkr->render = RE_NewSceneRender(bkr->scene);
1157
1158         /* XXX hack to force saving to always be internal. Whether (and how) to support
1159          * external saving will be addressed later */
1160         bkr->save_mode = R_BAKE_SAVE_INTERNAL;
1161 }
1162
1163 static int bake_exec(bContext *C, wmOperator *op)
1164 {
1165         Render *re;
1166         int result = OPERATOR_CANCELLED;
1167         BakeAPIRender bkr = {NULL};
1168         Scene *scene = CTX_data_scene(C);
1169
1170         G.is_break = false;
1171         G.is_rendering = true;
1172
1173         bake_set_props(op, scene);
1174
1175         bake_init_api_data(op, C, &bkr);
1176         re = bkr.render;
1177
1178         /* setup new render */
1179         RE_test_break_cb(re, NULL, bake_break);
1180
1181         if (!bake_pass_filter_check(bkr.pass_type, bkr.pass_filter, bkr.reports)) {
1182                 goto finally;
1183         }
1184
1185         if (!bake_objects_check(bkr.main, bkr.view_layer, bkr.ob, &bkr.selected_objects, bkr.reports, bkr.is_selected_to_active)) {
1186                 goto finally;
1187         }
1188
1189         if (bkr.is_clear) {
1190                 const bool is_tangent = ((bkr.pass_type == SCE_PASS_NORMAL) && (bkr.normal_space == R_BAKE_SPACE_TANGENT));
1191                 bake_images_clear(bkr.main, is_tangent);
1192         }
1193
1194         RE_SetReports(re, bkr.reports);
1195
1196         if (bkr.is_selected_to_active) {
1197                 result = bake(
1198                         bkr.render, bkr.main, bkr.scene, bkr.view_layer, bkr.ob, &bkr.selected_objects, bkr.reports,
1199                         bkr.pass_type, bkr.pass_filter, bkr.margin, bkr.save_mode,
1200                         bkr.is_clear, bkr.is_split_materials, bkr.is_automatic_name, true, bkr.is_cage,
1201                         bkr.cage_extrusion, bkr.normal_space, bkr.normal_swizzle,
1202                         bkr.custom_cage, bkr.filepath, bkr.width, bkr.height, bkr.identifier, bkr.sa,
1203                         bkr.uv_layer);
1204         }
1205         else {
1206                 CollectionPointerLink *link;
1207                 const bool is_clear = bkr.is_clear && BLI_listbase_is_single(&bkr.selected_objects);
1208                 for (link = bkr.selected_objects.first; link; link = link->next) {
1209                         Object *ob_iter = link->ptr.data;
1210                         result = bake(
1211                                 bkr.render, bkr.main, bkr.scene, bkr.view_layer, ob_iter, NULL, bkr.reports,
1212                                 bkr.pass_type, bkr.pass_filter, bkr.margin, bkr.save_mode,
1213                                 is_clear, bkr.is_split_materials, bkr.is_automatic_name, false, bkr.is_cage,
1214                                 bkr.cage_extrusion, bkr.normal_space, bkr.normal_swizzle,
1215                                 bkr.custom_cage, bkr.filepath, bkr.width, bkr.height, bkr.identifier, bkr.sa,
1216                                 bkr.uv_layer);
1217                 }
1218         }
1219
1220         RE_SetReports(re, NULL);
1221
1222
1223 finally:
1224         G.is_rendering = false;
1225         BLI_freelistN(&bkr.selected_objects);
1226         return result;
1227 }
1228
1229 static void bake_startjob(void *bkv, short *UNUSED(stop), short *do_update, float *progress)
1230 {
1231         BakeAPIRender *bkr = (BakeAPIRender *)bkv;
1232
1233         /* setup new render */
1234         bkr->do_update = do_update;
1235         bkr->progress = progress;
1236
1237         RE_SetReports(bkr->render, bkr->reports);
1238
1239         if (!bake_pass_filter_check(bkr->pass_type, bkr->pass_filter, bkr->reports)) {
1240                 bkr->result = OPERATOR_CANCELLED;
1241                 return;
1242         }
1243
1244         if (!bake_objects_check(bkr->main, bkr->view_layer, bkr->ob, &bkr->selected_objects, bkr->reports, bkr->is_selected_to_active)) {
1245                 bkr->result = OPERATOR_CANCELLED;
1246                 return;
1247         }
1248
1249         if (bkr->is_clear) {
1250                 const bool is_tangent = ((bkr->pass_type == SCE_PASS_NORMAL) && (bkr->normal_space == R_BAKE_SPACE_TANGENT));
1251                 bake_images_clear(bkr->main, is_tangent);
1252         }
1253
1254         if (bkr->is_selected_to_active) {
1255                 bkr->result = bake(
1256                         bkr->render, bkr->main, bkr->scene, bkr->view_layer, bkr->ob, &bkr->selected_objects, bkr->reports,
1257                         bkr->pass_type, bkr->pass_filter, bkr->margin, bkr->save_mode,
1258                         bkr->is_clear, bkr->is_split_materials, bkr->is_automatic_name, true, bkr->is_cage,
1259                         bkr->cage_extrusion, bkr->normal_space, bkr->normal_swizzle,
1260                         bkr->custom_cage, bkr->filepath, bkr->width, bkr->height, bkr->identifier, bkr->sa,
1261                         bkr->uv_layer);
1262         }
1263         else {
1264                 CollectionPointerLink *link;
1265                 const bool is_clear = bkr->is_clear && BLI_listbase_is_single(&bkr->selected_objects);
1266                 for (link = bkr->selected_objects.first; link; link = link->next) {
1267                         Object *ob_iter = link->ptr.data;
1268                         bkr->result = bake(
1269                                 bkr->render, bkr->main, bkr->scene, bkr->view_layer, ob_iter, NULL, bkr->reports,
1270                                 bkr->pass_type, bkr->pass_filter, bkr->margin, bkr->save_mode,
1271                                 is_clear, bkr->is_split_materials, bkr->is_automatic_name, false, bkr->is_cage,
1272                                 bkr->cage_extrusion, bkr->normal_space, bkr->normal_swizzle,
1273                                 bkr->custom_cage, bkr->filepath, bkr->width, bkr->height, bkr->identifier, bkr->sa,
1274                                 bkr->uv_layer);
1275
1276                         if (bkr->result == OPERATOR_CANCELLED)
1277                                 return;
1278                 }
1279         }
1280
1281         RE_SetReports(bkr->render, NULL);
1282 }
1283
1284 static void bake_freejob(void *bkv)
1285 {
1286         BakeAPIRender *bkr = (BakeAPIRender *)bkv;
1287
1288         BLI_freelistN(&bkr->selected_objects);
1289         MEM_freeN(bkr);
1290
1291         G.is_rendering = false;
1292 }
1293
1294 static void bake_set_props(wmOperator *op, Scene *scene)
1295 {
1296         PropertyRNA *prop;
1297         BakeData *bake = &scene->r.bake;
1298
1299         prop = RNA_struct_find_property(op->ptr, "filepath");
1300         if (!RNA_property_is_set(op->ptr, prop)) {
1301                 RNA_property_string_set(op->ptr, prop, bake->filepath);
1302         }
1303
1304         prop =  RNA_struct_find_property(op->ptr, "width");
1305         if (!RNA_property_is_set(op->ptr, prop)) {
1306                 RNA_property_int_set(op->ptr, prop, bake->width);
1307         }
1308
1309         prop =  RNA_struct_find_property(op->ptr, "height");
1310         if (!RNA_property_is_set(op->ptr, prop)) {
1311                 RNA_property_int_set(op->ptr, prop, bake->width);
1312         }
1313
1314         prop = RNA_struct_find_property(op->ptr, "margin");
1315         if (!RNA_property_is_set(op->ptr, prop)) {
1316                 RNA_property_int_set(op->ptr, prop, bake->margin);
1317         }
1318
1319         prop = RNA_struct_find_property(op->ptr, "use_selected_to_active");
1320         if (!RNA_property_is_set(op->ptr, prop)) {
1321                 RNA_property_boolean_set(op->ptr, prop, (bake->flag & R_BAKE_TO_ACTIVE) != 0);
1322         }
1323
1324         prop = RNA_struct_find_property(op->ptr, "cage_extrusion");
1325         if (!RNA_property_is_set(op->ptr, prop)) {
1326                 RNA_property_float_set(op->ptr, prop, bake->cage_extrusion);
1327         }
1328
1329         prop = RNA_struct_find_property(op->ptr, "cage_object");
1330         if (!RNA_property_is_set(op->ptr, prop)) {
1331                 if (bake->cage_object) {
1332                         RNA_property_string_set(op->ptr, prop, bake->cage_object->id.name + 2);
1333                 }
1334         }
1335
1336         prop = RNA_struct_find_property(op->ptr, "normal_space");
1337         if (!RNA_property_is_set(op->ptr, prop)) {
1338                 RNA_property_enum_set(op->ptr, prop, bake->normal_space);
1339         }
1340
1341         prop = RNA_struct_find_property(op->ptr, "normal_r");
1342         if (!RNA_property_is_set(op->ptr, prop)) {
1343                 RNA_property_enum_set(op->ptr, prop, bake->normal_swizzle[0]);
1344         }
1345
1346         prop = RNA_struct_find_property(op->ptr, "normal_g");
1347         if (!RNA_property_is_set(op->ptr, prop)) {
1348                 RNA_property_enum_set(op->ptr, prop, bake->normal_swizzle[1]);
1349         }
1350
1351         prop = RNA_struct_find_property(op->ptr, "normal_b");
1352         if (!RNA_property_is_set(op->ptr, prop)) {
1353                 RNA_property_enum_set(op->ptr, prop, bake->normal_swizzle[2]);
1354         }
1355
1356         prop = RNA_struct_find_property(op->ptr, "save_mode");
1357         if (!RNA_property_is_set(op->ptr, prop)) {
1358                 RNA_property_enum_set(op->ptr, prop, bake->save_mode);
1359         }
1360
1361         prop = RNA_struct_find_property(op->ptr, "use_clear");
1362         if (!RNA_property_is_set(op->ptr, prop)) {
1363                 RNA_property_boolean_set(op->ptr, prop, (bake->flag & R_BAKE_CLEAR) != 0);
1364         }
1365
1366         prop = RNA_struct_find_property(op->ptr, "use_cage");
1367         if (!RNA_property_is_set(op->ptr, prop)) {
1368                 RNA_property_boolean_set(op->ptr, prop, (bake->flag & R_BAKE_CAGE) != 0);
1369         }
1370
1371         prop = RNA_struct_find_property(op->ptr, "use_split_materials");
1372         if (!RNA_property_is_set(op->ptr, prop)) {
1373                 RNA_property_boolean_set(op->ptr, prop, (bake->flag & R_BAKE_SPLIT_MAT) != 0);
1374         }
1375
1376         prop = RNA_struct_find_property(op->ptr, "use_automatic_name");
1377         if (!RNA_property_is_set(op->ptr, prop)) {
1378                 RNA_property_boolean_set(op->ptr, prop, (bake->flag & R_BAKE_AUTO_NAME) != 0);
1379         }
1380
1381         prop = RNA_struct_find_property(op->ptr, "pass_filter");
1382         if (!RNA_property_is_set(op->ptr, prop)) {
1383                 RNA_property_enum_set(op->ptr, prop, bake->pass_filter);
1384         }
1385 }
1386
1387 static int bake_invoke(bContext *C, wmOperator *op, const wmEvent *UNUSED(event))
1388 {
1389         wmJob *wm_job;
1390         BakeAPIRender *bkr;
1391         Render *re;
1392         Scene *scene = CTX_data_scene(C);
1393
1394         bake_set_props(op, scene);
1395
1396         /* only one render job at a time */
1397         if (WM_jobs_test(CTX_wm_manager(C), scene, WM_JOB_TYPE_OBJECT_BAKE))
1398                 return OPERATOR_CANCELLED;
1399
1400         bkr = MEM_mallocN(sizeof(BakeAPIRender), "render bake");
1401
1402         /* init bake render */
1403         bake_init_api_data(op, C, bkr);
1404         re = bkr->render;
1405
1406         /* setup new render */
1407         RE_test_break_cb(re, NULL, bake_break);
1408         RE_progress_cb(re, bkr, bake_progress_update);
1409
1410         /* setup job */
1411         wm_job = WM_jobs_get(CTX_wm_manager(C), CTX_wm_window(C), scene, "Texture Bake",
1412                              WM_JOB_EXCL_RENDER | WM_JOB_PRIORITY | WM_JOB_PROGRESS, WM_JOB_TYPE_OBJECT_BAKE);
1413         WM_jobs_customdata_set(wm_job, bkr, bake_freejob);
1414         WM_jobs_timer(wm_job, 0.5, NC_IMAGE, 0); /* TODO - only draw bake image, can we enforce this */
1415         WM_jobs_callbacks(wm_job, bake_startjob, NULL, NULL, NULL);
1416
1417         G.is_break = false;
1418         G.is_rendering = true;
1419
1420         WM_jobs_start(CTX_wm_manager(C), wm_job);
1421
1422         WM_cursor_wait(0);
1423
1424         /* add modal handler for ESC */
1425         WM_event_add_modal_handler(C, op);
1426
1427         WM_event_add_notifier(C, NC_SCENE | ND_RENDER_RESULT, scene);
1428         return OPERATOR_RUNNING_MODAL;
1429 }
1430
1431 void OBJECT_OT_bake(wmOperatorType *ot)
1432 {
1433         PropertyRNA *prop;
1434
1435         /* identifiers */
1436         ot->name = "Bake";
1437         ot->description = "Bake image textures of selected objects";
1438         ot->idname = "OBJECT_OT_bake";
1439
1440         /* api callbacks */
1441         ot->exec = bake_exec;
1442         ot->modal = bake_modal;
1443         ot->invoke = bake_invoke;
1444         ot->poll = ED_operator_object_active_editable_mesh;
1445
1446         RNA_def_enum(ot->srna, "type", rna_enum_bake_pass_type_items, SCE_PASS_COMBINED, "Type",
1447                      "Type of pass to bake, some of them may not be supported by the current render engine");
1448         prop = RNA_def_enum(ot->srna, "pass_filter", rna_enum_bake_pass_filter_type_items, R_BAKE_PASS_FILTER_NONE, "Pass Filter",
1449                      "Filter to combined, diffuse, glossy, transmission and subsurface passes");
1450         RNA_def_property_flag(prop, PROP_ENUM_FLAG);
1451         RNA_def_string_file_path(ot->srna, "filepath", NULL, FILE_MAX, "File Path",
1452                                  "Image filepath to use when saving externally");
1453         RNA_def_int(ot->srna, "width", 512, 1, INT_MAX, "Width",
1454                     "Horizontal dimension of the baking map (external only)", 64, 4096);
1455         RNA_def_int(ot->srna, "height", 512, 1, INT_MAX, "Height",
1456                     "Vertical dimension of the baking map (external only)", 64, 4096);
1457         RNA_def_int(ot->srna, "margin", 16, 0, INT_MAX, "Margin",
1458                     "Extends the baked result as a post process filter", 0, 64);
1459         RNA_def_boolean(ot->srna, "use_selected_to_active", false, "Selected to Active",
1460                         "Bake shading on the surface of selected objects to the active object");
1461         RNA_def_float(ot->srna, "cage_extrusion", 0.0f, 0.0f, FLT_MAX, "Cage Extrusion",
1462                       "Distance to use for the inward ray cast when using selected to active", 0.0f, 1.0f);
1463         RNA_def_string(ot->srna, "cage_object", NULL, MAX_NAME, "Cage Object",
1464                        "Object to use as cage, instead of calculating the cage from the active object with cage extrusion");
1465         RNA_def_enum(ot->srna, "normal_space", rna_enum_normal_space_items, R_BAKE_SPACE_TANGENT, "Normal Space",
1466                      "Choose normal space for baking");
1467         RNA_def_enum(ot->srna, "normal_r", rna_enum_normal_swizzle_items, R_BAKE_POSX, "R", "Axis to bake in red channel");
1468         RNA_def_enum(ot->srna, "normal_g", rna_enum_normal_swizzle_items, R_BAKE_POSY, "G", "Axis to bake in green channel");
1469         RNA_def_enum(ot->srna, "normal_b", rna_enum_normal_swizzle_items, R_BAKE_POSZ, "B", "Axis to bake in blue channel");
1470         RNA_def_enum(ot->srna, "save_mode", rna_enum_bake_save_mode_items, R_BAKE_SAVE_INTERNAL, "Save Mode",
1471                      "Choose how to save the baking map");
1472         RNA_def_boolean(ot->srna, "use_clear", false, "Clear",
1473                         "Clear Images before baking (only for internal saving)");
1474         RNA_def_boolean(ot->srna, "use_cage", false, "Cage",
1475                         "Cast rays to active object from a cage");
1476         RNA_def_boolean(ot->srna, "use_split_materials", false, "Split Materials",
1477                         "Split baked maps per material, using material name in output file (external only)");
1478         RNA_def_boolean(ot->srna, "use_automatic_name", false, "Automatic Name",
1479                         "Automatically name the output file with the pass type");
1480         RNA_def_string(ot->srna, "uv_layer", NULL, MAX_CUSTOMDATA_LAYER_NAME, "UV Layer", "UV layer to override active");
1481 }