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