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         ScenePassType 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         BakeNormalSwizzle 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(ScenePassType 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(ScenePassType 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         if (ob->mode & OB_MODE_EDIT)
627                 ED_object_editmode_load(ob);
628
629         Mesh *me = BKE_mesh_new_from_object(eval_ctx, bmain, scene, ob, 1, 2, 0, 0);
630         if (me->flag & ME_AUTOSMOOTH) {
631                 BKE_mesh_split_faces(me, true);
632         }
633
634         return me;
635 }
636
637 static int bake(
638         Render *re, Main *bmain, Depsgraph *graph, Scene *scene, Object *ob_low, ListBase *selected_objects, ReportList *reports,
639         const ScenePassType pass_type, const int pass_filter, const int margin,
640         const BakeSaveMode save_mode, const bool is_clear, const bool is_split_materials,
641         const bool is_automatic_name, const bool is_selected_to_active, const bool is_cage,
642         const float cage_extrusion, const int normal_space, const BakeNormalSwizzle normal_swizzle[],
643         const char *custom_cage, const char *filepath, const int width, const int height,
644         const char *identifier, ScrArea *sa, const char *uv_layer)
645 {
646         int op_result = OPERATOR_CANCELLED;
647         bool ok = false;
648
649         Object *ob_cage = NULL;
650
651         BakeHighPolyData *highpoly = NULL;
652         int tot_highpoly = 0;
653
654         char restrict_flag_low = ob_low->restrictflag;
655         char restrict_flag_cage = 0;
656
657         Mesh *me_low = NULL;
658         Mesh *me_cage = NULL;
659
660         MultiresModifierData *mmd_low = NULL;
661         int mmd_flags_low = 0;
662
663         float *result = NULL;
664
665         BakePixel *pixel_array_low = NULL;
666         BakePixel *pixel_array_high = NULL;
667
668         const bool is_save_internal = (save_mode == R_BAKE_SAVE_INTERNAL);
669         const bool is_noncolor = is_noncolor_pass(pass_type);
670         const int depth = RE_pass_depth(pass_type);
671
672         BakeImages bake_images = {NULL};
673
674         size_t num_pixels;
675         int tot_materials;
676
677         RE_bake_engine_set_engine_parameters(re, bmain, graph, scene);
678
679         if (!RE_bake_has_engine(re)) {
680                 BKE_report(reports, RPT_ERROR, "Current render engine does not support baking");
681                 goto cleanup;
682         }
683
684         tot_materials = ob_low->totcol;
685
686         if (uv_layer && uv_layer[0] != '\0') {
687                 Mesh *me = (Mesh *)ob_low->data;
688                 if (CustomData_get_named_layer(&me->ldata, CD_MLOOPUV, uv_layer) == -1) {
689                         BKE_reportf(reports, RPT_ERROR,
690                                     "No UV layer named \"%s\" found in the object \"%s\"", uv_layer, ob_low->id.name + 2);
691                         goto cleanup;
692                 }
693         }
694
695         if (tot_materials == 0) {
696                 if (is_save_internal) {
697                         BKE_report(reports, RPT_ERROR,
698                                    "No active image found, add a material or bake to an external file");
699
700                         goto cleanup;
701                 }
702                 else if (is_split_materials) {
703                         BKE_report(reports, RPT_ERROR,
704                                    "No active image found, add a material or bake without the Split Materials option");
705
706                         goto cleanup;
707                 }
708                 else {
709                         /* baking externally without splitting materials */
710                         tot_materials = 1;
711                 }
712         }
713
714         /* we overallocate in case there is more materials than images */
715         bake_images.data = MEM_mallocN(sizeof(BakeImage) * tot_materials, "bake images dimensions (width, height, offset)");
716         bake_images.lookup = MEM_mallocN(sizeof(int) * tot_materials, "bake images lookup (from material to BakeImage)");
717
718         build_image_lookup(bmain, ob_low, &bake_images);
719
720         if (is_save_internal) {
721                 num_pixels = initialize_internal_images(&bake_images, reports);
722
723                 if (num_pixels == 0) {
724                         goto cleanup;
725                 }
726         }
727         else {
728                 /* when saving extenally always use the size specified in the UI */
729
730                 num_pixels = (size_t)width * (size_t)height * bake_images.size;
731
732                 for (int i = 0; i < bake_images.size; i++) {
733                         bake_images.data[i].width = width;
734                         bake_images.data[i].height = height;
735                         bake_images.data[i].offset = (is_split_materials ? num_pixels : 0);
736                         bake_images.data[i].image = NULL;
737                 }
738
739                 if (!is_split_materials) {
740                         /* saving a single image */
741                         for (int i = 0; i < tot_materials; i++) {
742                                 bake_images.lookup[i] = 0;
743                         }
744                 }
745         }
746
747         if (is_selected_to_active) {
748                 CollectionPointerLink *link;
749                 tot_highpoly = 0;
750
751                 for (link = selected_objects->first; link; link = link->next) {
752                         Object *ob_iter = link->ptr.data;
753
754                         if (ob_iter == ob_low)
755                                 continue;
756
757                         tot_highpoly ++;
758                 }
759
760                 if (is_cage && custom_cage[0] != '\0') {
761                         ob_cage = BLI_findstring(&bmain->object, custom_cage, offsetof(ID, name) + 2);
762
763                         if (ob_cage == NULL || ob_cage->type != OB_MESH) {
764                                 BKE_report(reports, RPT_ERROR, "No valid cage object");
765                                 goto cleanup;
766                         }
767                         else {
768                                 restrict_flag_cage = ob_cage->restrictflag;
769                                 ob_cage->restrictflag |= OB_RESTRICT_RENDER;
770                         }
771                 }
772         }
773
774         pixel_array_low = MEM_mallocN(sizeof(BakePixel) * num_pixels, "bake pixels low poly");
775         pixel_array_high = MEM_mallocN(sizeof(BakePixel) * num_pixels, "bake pixels high poly");
776         result = MEM_callocN(sizeof(float) * depth * num_pixels, "bake return pixels");
777
778         /* for multires bake, use linear UV subdivision to match low res UVs */
779         if (pass_type == SCE_PASS_NORMAL && normal_space == R_BAKE_SPACE_TANGENT && !is_selected_to_active) {
780                 mmd_low = (MultiresModifierData *) modifiers_findByType(ob_low, eModifierType_Multires);
781                 if (mmd_low) {
782                         mmd_flags_low = mmd_low->flags;
783                         mmd_low->flags |= eMultiresModifierFlag_PlainUv;
784                 }
785         }
786
787         /* get the mesh as it arrives in the renderer */
788         me_low = bake_mesh_new_from_object(RE_GetEvalCtx(re), bmain, scene, ob_low);
789
790         /* populate the pixel array with the face data */
791         if ((is_selected_to_active && (ob_cage == NULL) && is_cage) == false)
792                 RE_bake_pixels_populate(me_low, pixel_array_low, num_pixels, &bake_images, uv_layer);
793         /* else populate the pixel array with the 'cage' mesh (the smooth version of the mesh)  */
794
795         if (is_selected_to_active) {
796                 CollectionPointerLink *link;
797                 ModifierData *md, *nmd;
798                 ListBase modifiers_tmp, modifiers_original;
799                 int i = 0;
800
801                 /* prepare cage mesh */
802                 if (ob_cage) {
803                         me_cage = bake_mesh_new_from_object(RE_GetEvalCtx(re), bmain, scene, ob_cage);
804                         if ((me_low->totpoly != me_cage->totpoly) || (me_low->totloop != me_cage->totloop)) {
805                                 BKE_report(reports, RPT_ERROR,
806                                            "Invalid cage object, the cage mesh must have the same number "
807                                            "of faces as the active object");
808                                 goto cleanup;
809                         }
810                 }
811                 else if (is_cage) {
812                         modifiers_original = ob_low->modifiers;
813                         BLI_listbase_clear(&modifiers_tmp);
814
815                         for (md = ob_low->modifiers.first; md; md = md->next) {
816                                 /* Edge Split cannot be applied in the cage,
817                                  * the cage is supposed to have interpolated normals
818                                  * between the faces unless the geometry is physically
819                                  * split. So we create a copy of the low poly mesh without
820                                  * the eventual edge split.*/
821
822                                 if (md->type == eModifierType_EdgeSplit)
823                                         continue;
824
825                                 nmd = modifier_new(md->type);
826                                 BLI_strncpy(nmd->name, md->name, sizeof(nmd->name));
827                                 modifier_copyData(md, nmd);
828                                 BLI_addtail(&modifiers_tmp, nmd);
829                         }
830
831                         /* temporarily replace the modifiers */
832                         ob_low->modifiers = modifiers_tmp;
833
834                         /* get the cage mesh as it arrives in the renderer */
835                         me_cage = bake_mesh_new_from_object(RE_GetEvalCtx(re), bmain, scene, ob_low);
836                         RE_bake_pixels_populate(me_cage, pixel_array_low, num_pixels, &bake_images, uv_layer);
837                 }
838
839                 highpoly = MEM_callocN(sizeof(BakeHighPolyData) * tot_highpoly, "bake high poly objects");
840
841                 /* populate highpoly array */
842                 for (link = selected_objects->first; link; link = link->next) {
843                         TriangulateModifierData *tmd;
844                         Object *ob_iter = link->ptr.data;
845
846                         if (ob_iter == ob_low)
847                                 continue;
848
849                         /* initialize highpoly_data */
850                         highpoly[i].ob = ob_iter;
851                         highpoly[i].restrict_flag = ob_iter->restrictflag;
852
853                         /* triangulating so BVH returns the primitive_id that will be used for rendering */
854                         highpoly[i].tri_mod = ED_object_modifier_add(
855                                 reports, bmain, scene, highpoly[i].ob,
856                                 "TmpTriangulate", eModifierType_Triangulate);
857                         tmd = (TriangulateModifierData *)highpoly[i].tri_mod;
858                         tmd->quad_method = MOD_TRIANGULATE_QUAD_FIXED;
859                         tmd->ngon_method = MOD_TRIANGULATE_NGON_EARCLIP;
860
861                         highpoly[i].me = bake_mesh_new_from_object(RE_GetEvalCtx(re), bmain, scene, highpoly[i].ob);
862                         highpoly[i].ob->restrictflag &= ~OB_RESTRICT_RENDER;
863
864                         /* lowpoly to highpoly transformation matrix */
865                         copy_m4_m4(highpoly[i].obmat, highpoly[i].ob->obmat);
866                         invert_m4_m4(highpoly[i].imat, highpoly[i].obmat);
867
868                         highpoly[i].is_flip_object = is_negative_m4(highpoly[i].ob->obmat);
869
870                         i++;
871                 }
872
873                 BLI_assert(i == tot_highpoly);
874
875                 ob_low->restrictflag |= OB_RESTRICT_RENDER;
876
877                 /* populate the pixel arrays with the corresponding face data for each high poly object */
878                 if (!RE_bake_pixels_populate_from_objects(
879                             me_low, pixel_array_low, pixel_array_high, highpoly, tot_highpoly, num_pixels, ob_cage != NULL,
880                             cage_extrusion, ob_low->obmat, (ob_cage ? ob_cage->obmat : ob_low->obmat), me_cage))
881                 {
882                         BKE_report(reports, RPT_ERROR, "Error handling selected objects");
883                         goto cage_cleanup;
884                 }
885
886                 /* the baking itself */
887                 for (i = 0; i < tot_highpoly; i++) {
888                         ok = RE_bake_engine(re, highpoly[i].ob, i, pixel_array_high,
889                                             num_pixels, depth, pass_type, pass_filter, result);
890                         if (!ok) {
891                                 BKE_reportf(reports, RPT_ERROR, "Error baking from object \"%s\"", highpoly[i].ob->id.name + 2);
892                                 goto cage_cleanup;
893                         }
894                 }
895
896 cage_cleanup:
897                 /* reverting data back */
898                 if ((ob_cage == NULL) && is_cage) {
899                         ob_low->modifiers = modifiers_original;
900
901                         while ((md = BLI_pophead(&modifiers_tmp))) {
902                                 modifier_free(md);
903                         }
904                 }
905
906                 if (!ok) {
907                         goto cleanup;
908                 }
909         }
910         else {
911                 /* make sure low poly renders */
912                 ob_low->restrictflag &= ~OB_RESTRICT_RENDER;
913
914                 if (RE_bake_has_engine(re)) {
915                         ok = RE_bake_engine(re, ob_low, 0, pixel_array_low, num_pixels, depth, pass_type, pass_filter, result);
916                 }
917                 else {
918                         BKE_report(reports, RPT_ERROR, "Current render engine does not support baking");
919                         goto cleanup;
920                 }
921         }
922
923         /* normal space conversion
924          * the normals are expected to be in world space, +X +Y +Z */
925         if (ok && pass_type == SCE_PASS_NORMAL) {
926                 switch (normal_space) {
927                         case R_BAKE_SPACE_WORLD:
928                         {
929                                 /* Cycles internal format */
930                                 if ((normal_swizzle[0] == R_BAKE_POSX) &&
931                                     (normal_swizzle[1] == R_BAKE_POSY) &&
932                                     (normal_swizzle[2] == R_BAKE_POSZ))
933                                 {
934                                         break;
935                                 }
936                                 else {
937                                         RE_bake_normal_world_to_world(pixel_array_low, num_pixels,  depth, result, normal_swizzle);
938                                 }
939                                 break;
940                         }
941                         case R_BAKE_SPACE_OBJECT:
942                         {
943                                 RE_bake_normal_world_to_object(pixel_array_low, num_pixels, depth, result, ob_low, normal_swizzle);
944                                 break;
945                         }
946                         case R_BAKE_SPACE_TANGENT:
947                         {
948                                 if (is_selected_to_active) {
949                                         RE_bake_normal_world_to_tangent(pixel_array_low, num_pixels, depth, result, me_low, normal_swizzle, ob_low->obmat);
950                                 }
951                                 else {
952                                         /* from multiresolution */
953                                         Mesh *me_nores = NULL;
954                                         ModifierData *md = NULL;
955                                         int mode;
956
957                                         md = modifiers_findByType(ob_low, eModifierType_Multires);
958
959                                         if (md) {
960                                                 mode = md->mode;
961                                                 md->mode &= ~eModifierMode_Render;
962                                         }
963
964                                         me_nores = bake_mesh_new_from_object(RE_GetEvalCtx(re), bmain, scene, ob_low);
965                                         RE_bake_pixels_populate(me_nores, pixel_array_low, num_pixels, &bake_images, uv_layer);
966
967                                         RE_bake_normal_world_to_tangent(pixel_array_low, num_pixels, depth, result, me_nores, normal_swizzle, ob_low->obmat);
968                                         BKE_libblock_free(bmain, me_nores);
969
970                                         if (md)
971                                                 md->mode = mode;
972                                 }
973                                 break;
974                         }
975                         default:
976                                 break;
977                 }
978         }
979
980         if (!ok) {
981                 BKE_reportf(reports, RPT_ERROR, "Problem baking object \"%s\"", ob_low->id.name + 2);
982                 op_result = OPERATOR_CANCELLED;
983         }
984         else {
985                 /* save the results */
986                 for (int i = 0; i < bake_images.size; i++) {
987                         BakeImage *bk_image = &bake_images.data[i];
988
989                         if (is_save_internal) {
990                                 ok = write_internal_bake_pixels(
991                                          bk_image->image,
992                                          pixel_array_low + bk_image->offset,
993                                          result + bk_image->offset * depth,
994                                          bk_image->width, bk_image->height,
995                                          margin, is_clear, is_noncolor);
996
997                                 /* might be read by UI to set active image for display */
998                                 bake_update_image(sa, bk_image->image);
999
1000                                 if (!ok) {
1001                                         BKE_reportf(reports, RPT_ERROR,
1002                                                    "Problem saving the bake map internally for object \"%s\"", ob_low->id.name + 2);
1003                                         op_result = OPERATOR_CANCELLED;
1004                                 }
1005                                 else {
1006                                         BKE_report(reports, RPT_INFO,
1007                                                    "Baking map saved to internal image, save it externally or pack it");
1008                                         op_result = OPERATOR_FINISHED;
1009                                 }
1010                         }
1011                         /* save externally */
1012                         else {
1013                                 BakeData *bake = &scene->r.bake;
1014                                 char name[FILE_MAX];
1015
1016                                 BKE_image_path_from_imtype(name, filepath, bmain->name, 0, bake->im_format.imtype, true, false, NULL);
1017
1018                                 if (is_automatic_name) {
1019                                         BLI_path_suffix(name, FILE_MAX, ob_low->id.name + 2, "_");
1020                                         BLI_path_suffix(name, FILE_MAX, identifier, "_");
1021                                 }
1022
1023                                 if (is_split_materials) {
1024                                         if (bk_image->image) {
1025                                                 BLI_path_suffix(name, FILE_MAX, bk_image->image->id.name + 2, "_");
1026                                         }
1027                                         else {
1028                                                 if (ob_low->mat[i]) {
1029                                                         BLI_path_suffix(name, FILE_MAX, ob_low->mat[i]->id.name + 2, "_");
1030                                                 }
1031                                                 else if (me_low->mat[i]) {
1032                                                         BLI_path_suffix(name, FILE_MAX, me_low->mat[i]->id.name + 2, "_");
1033                                                 }
1034                                                 else {
1035                                                         /* if everything else fails, use the material index */
1036                                                         char tmp[5];
1037                                                         sprintf(tmp, "%d", i % 1000);
1038                                                         BLI_path_suffix(name, FILE_MAX, tmp, "_");
1039                                                 }
1040                                         }
1041                                 }
1042
1043                                 /* save it externally */
1044                                 ok = write_external_bake_pixels(
1045                                         name,
1046                                         pixel_array_low + bk_image->offset,
1047                                         result + bk_image->offset * depth,
1048                                         bk_image->width, bk_image->height,
1049                                         margin, &bake->im_format, is_noncolor);
1050
1051                                 if (!ok) {
1052                                         BKE_reportf(reports, RPT_ERROR, "Problem saving baked map in \"%s\"", name);
1053                                         op_result = OPERATOR_CANCELLED;
1054                                 }
1055                                 else {
1056                                         BKE_reportf(reports, RPT_INFO, "Baking map written to \"%s\"", name);
1057                                         op_result = OPERATOR_FINISHED;
1058                                 }
1059
1060                                 if (!is_split_materials) {
1061                                         break;
1062                                 }
1063                         }
1064                 }
1065         }
1066
1067         if (is_save_internal)
1068                 refresh_images(&bake_images);
1069
1070 cleanup:
1071
1072         if (highpoly) {
1073                 int i;
1074                 for (i = 0; i < tot_highpoly; i++) {
1075                         highpoly[i].ob->restrictflag = highpoly[i].restrict_flag;
1076
1077                         if (highpoly[i].tri_mod)
1078                                 ED_object_modifier_remove(reports, bmain, highpoly[i].ob, highpoly[i].tri_mod);
1079
1080                         if (highpoly[i].me)
1081                                 BKE_libblock_free(bmain, highpoly[i].me);
1082                 }
1083                 MEM_freeN(highpoly);
1084         }
1085
1086         ob_low->restrictflag = restrict_flag_low;
1087
1088         if (mmd_low)
1089                 mmd_low->flags = mmd_flags_low;
1090
1091         if (ob_cage)
1092                 ob_cage->restrictflag = restrict_flag_cage;
1093
1094         if (pixel_array_low)
1095                 MEM_freeN(pixel_array_low);
1096
1097         if (pixel_array_high)
1098                 MEM_freeN(pixel_array_high);
1099
1100         if (bake_images.data)
1101                 MEM_freeN(bake_images.data);
1102
1103         if (bake_images.lookup)
1104                 MEM_freeN(bake_images.lookup);
1105
1106         if (result)
1107                 MEM_freeN(result);
1108
1109         if (me_low)
1110                 BKE_libblock_free(bmain, me_low);
1111
1112         if (me_cage)
1113                 BKE_libblock_free(bmain, me_cage);
1114
1115         return op_result;
1116 }
1117
1118 static void bake_init_api_data(wmOperator *op, bContext *C, BakeAPIRender *bkr)
1119 {
1120         bool is_save_internal;
1121         bScreen *sc = CTX_wm_screen(C);
1122
1123         bkr->ob = CTX_data_active_object(C);
1124         bkr->main = CTX_data_main(C);
1125         bkr->depsgraph = CTX_data_depsgraph(C);
1126         bkr->scene = CTX_data_scene(C);
1127         bkr->sa = sc ? BKE_screen_find_big_area(sc, SPACE_IMAGE, 10) : NULL;
1128
1129         bkr->pass_type = RNA_enum_get(op->ptr, "type");
1130         bkr->pass_filter = RNA_enum_get(op->ptr, "pass_filter");
1131         bkr->margin = RNA_int_get(op->ptr, "margin");
1132
1133         bkr->save_mode = RNA_enum_get(op->ptr, "save_mode");
1134         is_save_internal = (bkr->save_mode == R_BAKE_SAVE_INTERNAL);
1135
1136         bkr->is_clear = RNA_boolean_get(op->ptr, "use_clear");
1137         bkr->is_split_materials = (!is_save_internal) && RNA_boolean_get(op->ptr, "use_split_materials");
1138         bkr->is_automatic_name = RNA_boolean_get(op->ptr, "use_automatic_name");
1139         bkr->is_selected_to_active = RNA_boolean_get(op->ptr, "use_selected_to_active");
1140         bkr->is_cage = RNA_boolean_get(op->ptr, "use_cage");
1141         bkr->cage_extrusion = RNA_float_get(op->ptr, "cage_extrusion");
1142
1143         bkr->normal_space = RNA_enum_get(op->ptr, "normal_space");
1144         bkr->normal_swizzle[0] = RNA_enum_get(op->ptr, "normal_r");
1145         bkr->normal_swizzle[1] = RNA_enum_get(op->ptr, "normal_g");
1146         bkr->normal_swizzle[2] = RNA_enum_get(op->ptr, "normal_b");
1147
1148         bkr->width = RNA_int_get(op->ptr, "width");
1149         bkr->height = RNA_int_get(op->ptr, "height");
1150         bkr->identifier = "";
1151
1152         RNA_string_get(op->ptr, "uv_layer", bkr->uv_layer);
1153
1154         RNA_string_get(op->ptr, "cage_object", bkr->custom_cage);
1155
1156         if ((!is_save_internal) && bkr->is_automatic_name) {
1157                 PropertyRNA *prop = RNA_struct_find_property(op->ptr, "type");
1158                 RNA_property_enum_identifier(C, op->ptr, prop, bkr->pass_type, &bkr->identifier);
1159         }
1160
1161         CTX_data_selected_objects(C, &bkr->selected_objects);
1162
1163         bkr->reports = op->reports;
1164
1165         bkr->result = OPERATOR_CANCELLED;
1166
1167         bkr->render = RE_NewRender(bkr->scene->id.name);
1168
1169         /* XXX hack to force saving to always be internal. Whether (and how) to support
1170          * external saving will be addressed later */
1171         bkr->save_mode = R_BAKE_SAVE_INTERNAL;
1172 }
1173
1174 static int bake_exec(bContext *C, wmOperator *op)
1175 {
1176         Render *re;
1177         int result = OPERATOR_CANCELLED;
1178         BakeAPIRender bkr = {NULL};
1179         Scene *scene = CTX_data_scene(C);
1180
1181         G.is_break = false;
1182         G.is_rendering = true;
1183
1184         bake_set_props(op, scene);
1185
1186         bake_init_api_data(op, C, &bkr);
1187         re = bkr.render;
1188
1189         /* setup new render */
1190         RE_test_break_cb(re, NULL, bake_break);
1191
1192         if (!bake_pass_filter_check(bkr.pass_type, bkr.pass_filter, bkr.reports)) {
1193                 goto finally;
1194         }
1195
1196         if (!bake_objects_check(bkr.main, bkr.scene, bkr.ob, &bkr.selected_objects, bkr.reports, bkr.is_selected_to_active)) {
1197                 goto finally;
1198         }
1199
1200         if (bkr.is_clear) {
1201                 const bool is_tangent = ((bkr.pass_type == SCE_PASS_NORMAL) && (bkr.normal_space == R_BAKE_SPACE_TANGENT));
1202                 bake_images_clear(bkr.main, is_tangent);
1203         }
1204
1205         RE_SetReports(re, bkr.reports);
1206
1207         if (bkr.is_selected_to_active) {
1208                 result = bake(
1209                         bkr.render, bkr.main, bkr.depsgraph, bkr.scene, bkr.ob, &bkr.selected_objects, bkr.reports,
1210                         bkr.pass_type, bkr.pass_filter, bkr.margin, bkr.save_mode,
1211                         bkr.is_clear, bkr.is_split_materials, bkr.is_automatic_name, true, bkr.is_cage,
1212                         bkr.cage_extrusion, bkr.normal_space, bkr.normal_swizzle,
1213                         bkr.custom_cage, bkr.filepath, bkr.width, bkr.height, bkr.identifier, bkr.sa,
1214                         bkr.uv_layer);
1215         }
1216         else {
1217                 CollectionPointerLink *link;
1218                 const bool is_clear = bkr.is_clear && BLI_listbase_is_single(&bkr.selected_objects);
1219                 for (link = bkr.selected_objects.first; link; link = link->next) {
1220                         Object *ob_iter = link->ptr.data;
1221                         result = bake(
1222                                 bkr.render, bkr.main, bkr.depsgraph, bkr.scene, ob_iter, NULL, bkr.reports,
1223                                 bkr.pass_type, bkr.pass_filter, bkr.margin, bkr.save_mode,
1224                                 is_clear, bkr.is_split_materials, bkr.is_automatic_name, false, bkr.is_cage,
1225                                 bkr.cage_extrusion, bkr.normal_space, bkr.normal_swizzle,
1226                                 bkr.custom_cage, bkr.filepath, bkr.width, bkr.height, bkr.identifier, bkr.sa,
1227                                 bkr.uv_layer);
1228                 }
1229         }
1230
1231         RE_SetReports(re, NULL);
1232
1233
1234 finally:
1235         G.is_rendering = false;
1236         BLI_freelistN(&bkr.selected_objects);
1237         return result;
1238 }
1239
1240 static void bake_startjob(void *bkv, short *UNUSED(stop), short *do_update, float *progress)
1241 {
1242         BakeAPIRender *bkr = (BakeAPIRender *)bkv;
1243
1244         /* setup new render */
1245         bkr->do_update = do_update;
1246         bkr->progress = progress;
1247
1248         RE_SetReports(bkr->render, bkr->reports);
1249
1250         if (!bake_pass_filter_check(bkr->pass_type, bkr->pass_filter, bkr->reports)) {
1251                 bkr->result = OPERATOR_CANCELLED;
1252                 return;
1253         }
1254
1255         if (!bake_objects_check(bkr->main, bkr->scene, bkr->ob, &bkr->selected_objects, bkr->reports, bkr->is_selected_to_active)) {
1256                 bkr->result = OPERATOR_CANCELLED;
1257                 return;
1258         }
1259
1260         if (bkr->is_clear) {
1261                 const bool is_tangent = ((bkr->pass_type == SCE_PASS_NORMAL) && (bkr->normal_space == R_BAKE_SPACE_TANGENT));
1262                 bake_images_clear(bkr->main, is_tangent);
1263         }
1264
1265         if (bkr->is_selected_to_active) {
1266                 bkr->result = bake(
1267                         bkr->render, bkr->main, bkr->depsgraph, bkr->scene, bkr->ob, &bkr->selected_objects, bkr->reports,
1268                         bkr->pass_type, bkr->pass_filter, bkr->margin, bkr->save_mode,
1269                         bkr->is_clear, bkr->is_split_materials, bkr->is_automatic_name, true, bkr->is_cage,
1270                         bkr->cage_extrusion, bkr->normal_space, bkr->normal_swizzle,
1271                         bkr->custom_cage, bkr->filepath, bkr->width, bkr->height, bkr->identifier, bkr->sa,
1272                         bkr->uv_layer);
1273         }
1274         else {
1275                 CollectionPointerLink *link;
1276                 const bool is_clear = bkr->is_clear && BLI_listbase_is_single(&bkr->selected_objects);
1277                 for (link = bkr->selected_objects.first; link; link = link->next) {
1278                         Object *ob_iter = link->ptr.data;
1279                         bkr->result = bake(
1280                                 bkr->render, bkr->main, bkr->depsgraph, bkr->scene, ob_iter, NULL, bkr->reports,
1281                                 bkr->pass_type, bkr->pass_filter, bkr->margin, bkr->save_mode,
1282                                 is_clear, bkr->is_split_materials, bkr->is_automatic_name, false, bkr->is_cage,
1283                                 bkr->cage_extrusion, bkr->normal_space, bkr->normal_swizzle,
1284                                 bkr->custom_cage, bkr->filepath, bkr->width, bkr->height, bkr->identifier, bkr->sa,
1285                                 bkr->uv_layer);
1286
1287                         if (bkr->result == OPERATOR_CANCELLED)
1288                                 return;
1289                 }
1290         }
1291
1292         RE_SetReports(bkr->render, NULL);
1293 }
1294
1295 static void bake_freejob(void *bkv)
1296 {
1297         BakeAPIRender *bkr = (BakeAPIRender *)bkv;
1298
1299         BLI_freelistN(&bkr->selected_objects);
1300         MEM_freeN(bkr);
1301
1302         G.is_rendering = false;
1303 }
1304
1305 static void bake_set_props(wmOperator *op, Scene *scene)
1306 {
1307         PropertyRNA *prop;
1308         BakeData *bake = &scene->r.bake;
1309
1310         prop = RNA_struct_find_property(op->ptr, "filepath");
1311         if (!RNA_property_is_set(op->ptr, prop)) {
1312                 RNA_property_string_set(op->ptr, prop, bake->filepath);
1313         }
1314
1315         prop =  RNA_struct_find_property(op->ptr, "width");
1316         if (!RNA_property_is_set(op->ptr, prop)) {
1317                 RNA_property_int_set(op->ptr, prop, bake->width);
1318         }
1319
1320         prop =  RNA_struct_find_property(op->ptr, "height");
1321         if (!RNA_property_is_set(op->ptr, prop)) {
1322                 RNA_property_int_set(op->ptr, prop, bake->width);
1323         }
1324
1325         prop = RNA_struct_find_property(op->ptr, "margin");
1326         if (!RNA_property_is_set(op->ptr, prop)) {
1327                 RNA_property_int_set(op->ptr, prop, bake->margin);
1328         }
1329
1330         prop = RNA_struct_find_property(op->ptr, "use_selected_to_active");
1331         if (!RNA_property_is_set(op->ptr, prop)) {
1332                 RNA_property_boolean_set(op->ptr, prop, (bake->flag & R_BAKE_TO_ACTIVE) != 0);
1333         }
1334
1335         prop = RNA_struct_find_property(op->ptr, "cage_extrusion");
1336         if (!RNA_property_is_set(op->ptr, prop)) {
1337                 RNA_property_float_set(op->ptr, prop, bake->cage_extrusion);
1338         }
1339
1340         prop = RNA_struct_find_property(op->ptr, "cage_object");
1341         if (!RNA_property_is_set(op->ptr, prop)) {
1342                 RNA_property_string_set(op->ptr, prop, bake->cage);
1343         }
1344
1345         prop = RNA_struct_find_property(op->ptr, "normal_space");
1346         if (!RNA_property_is_set(op->ptr, prop)) {
1347                 RNA_property_enum_set(op->ptr, prop, bake->normal_space);
1348         }
1349
1350         prop = RNA_struct_find_property(op->ptr, "normal_r");
1351         if (!RNA_property_is_set(op->ptr, prop)) {
1352                 RNA_property_enum_set(op->ptr, prop, bake->normal_swizzle[0]);
1353         }
1354
1355         prop = RNA_struct_find_property(op->ptr, "normal_g");
1356         if (!RNA_property_is_set(op->ptr, prop)) {
1357                 RNA_property_enum_set(op->ptr, prop, bake->normal_swizzle[1]);
1358         }
1359
1360         prop = RNA_struct_find_property(op->ptr, "normal_b");
1361         if (!RNA_property_is_set(op->ptr, prop)) {
1362                 RNA_property_enum_set(op->ptr, prop, bake->normal_swizzle[2]);
1363         }
1364
1365         prop = RNA_struct_find_property(op->ptr, "save_mode");
1366         if (!RNA_property_is_set(op->ptr, prop)) {
1367                 RNA_property_enum_set(op->ptr, prop, bake->save_mode);
1368         }
1369
1370         prop = RNA_struct_find_property(op->ptr, "use_clear");
1371         if (!RNA_property_is_set(op->ptr, prop)) {
1372                 RNA_property_boolean_set(op->ptr, prop, (bake->flag & R_BAKE_CLEAR) != 0);
1373         }
1374
1375         prop = RNA_struct_find_property(op->ptr, "use_cage");
1376         if (!RNA_property_is_set(op->ptr, prop)) {
1377                 RNA_property_boolean_set(op->ptr, prop, (bake->flag & R_BAKE_CAGE) != 0);
1378         }
1379
1380         prop = RNA_struct_find_property(op->ptr, "use_split_materials");
1381         if (!RNA_property_is_set(op->ptr, prop)) {
1382                 RNA_property_boolean_set(op->ptr, prop, (bake->flag & R_BAKE_SPLIT_MAT) != 0);
1383         }
1384
1385         prop = RNA_struct_find_property(op->ptr, "use_automatic_name");
1386         if (!RNA_property_is_set(op->ptr, prop)) {
1387                 RNA_property_boolean_set(op->ptr, prop, (bake->flag & R_BAKE_AUTO_NAME) != 0);
1388         }
1389
1390         prop = RNA_struct_find_property(op->ptr, "pass_filter");
1391         if (!RNA_property_is_set(op->ptr, prop)) {
1392                 RNA_property_enum_set(op->ptr, prop, bake->pass_filter);
1393         }
1394 }
1395
1396 static int bake_invoke(bContext *C, wmOperator *op, const wmEvent *UNUSED(event))
1397 {
1398         wmJob *wm_job;
1399         BakeAPIRender *bkr;
1400         Render *re;
1401         Scene *scene = CTX_data_scene(C);
1402
1403         bake_set_props(op, scene);
1404
1405         /* only one render job at a time */
1406         if (WM_jobs_test(CTX_wm_manager(C), scene, WM_JOB_TYPE_OBJECT_BAKE))
1407                 return OPERATOR_CANCELLED;
1408
1409         bkr = MEM_mallocN(sizeof(BakeAPIRender), "render bake");
1410
1411         /* init bake render */
1412         bake_init_api_data(op, C, bkr);
1413         re = bkr->render;
1414
1415         /* setup new render */
1416         RE_test_break_cb(re, NULL, bake_break);
1417         RE_progress_cb(re, bkr, bake_progress_update);
1418
1419         /* setup job */
1420         wm_job = WM_jobs_get(CTX_wm_manager(C), CTX_wm_window(C), scene, "Texture Bake",
1421                              WM_JOB_EXCL_RENDER | WM_JOB_PRIORITY | WM_JOB_PROGRESS, WM_JOB_TYPE_OBJECT_BAKE);
1422         WM_jobs_customdata_set(wm_job, bkr, bake_freejob);
1423         WM_jobs_timer(wm_job, 0.5, NC_IMAGE, 0); /* TODO - only draw bake image, can we enforce this */
1424         WM_jobs_callbacks(wm_job, bake_startjob, NULL, NULL, NULL);
1425
1426         G.is_break = false;
1427         G.is_rendering = true;
1428
1429         WM_jobs_start(CTX_wm_manager(C), wm_job);
1430
1431         WM_cursor_wait(0);
1432
1433         /* add modal handler for ESC */
1434         WM_event_add_modal_handler(C, op);
1435
1436         WM_event_add_notifier(C, NC_SCENE | ND_RENDER_RESULT, scene);
1437         return OPERATOR_RUNNING_MODAL;
1438 }
1439
1440 void OBJECT_OT_bake(wmOperatorType *ot)
1441 {
1442         PropertyRNA *prop;
1443
1444         /* identifiers */
1445         ot->name = "Bake";
1446         ot->description = "Bake image textures of selected objects";
1447         ot->idname = "OBJECT_OT_bake";
1448
1449         /* api callbacks */
1450         ot->exec = bake_exec;
1451         ot->modal = bake_modal;
1452         ot->invoke = bake_invoke;
1453         ot->poll = ED_operator_object_active_editable_mesh;
1454
1455         RNA_def_enum(ot->srna, "type", rna_enum_bake_pass_type_items, SCE_PASS_COMBINED, "Type",
1456                      "Type of pass to bake, some of them may not be supported by the current render engine");
1457         prop = RNA_def_enum(ot->srna, "pass_filter", rna_enum_bake_pass_filter_type_items, R_BAKE_PASS_FILTER_NONE, "Pass Filter",
1458                      "Filter to combined, diffuse, glossy, transmission and subsurface passes");
1459         RNA_def_property_flag(prop, PROP_ENUM_FLAG);
1460         RNA_def_string_file_path(ot->srna, "filepath", NULL, FILE_MAX, "File Path",
1461                                  "Image filepath to use when saving externally");
1462         RNA_def_int(ot->srna, "width", 512, 1, INT_MAX, "Width",
1463                     "Horizontal dimension of the baking map (external only)", 64, 4096);
1464         RNA_def_int(ot->srna, "height", 512, 1, INT_MAX, "Height",
1465                     "Vertical dimension of the baking map (external only)", 64, 4096);
1466         RNA_def_int(ot->srna, "margin", 16, 0, INT_MAX, "Margin",
1467                     "Extends the baked result as a post process filter", 0, 64);
1468         RNA_def_boolean(ot->srna, "use_selected_to_active", false, "Selected to Active",
1469                         "Bake shading on the surface of selected objects to the active object");
1470         RNA_def_float(ot->srna, "cage_extrusion", 0.0f, 0.0f, FLT_MAX, "Cage Extrusion",
1471                       "Distance to use for the inward ray cast when using selected to active", 0.0f, 1.0f);
1472         RNA_def_string(ot->srna, "cage_object", NULL, MAX_NAME, "Cage Object",
1473                        "Object to use as cage, instead of calculating the cage from the active object with cage extrusion");
1474         RNA_def_enum(ot->srna, "normal_space", rna_enum_normal_space_items, R_BAKE_SPACE_TANGENT, "Normal Space",
1475                      "Choose normal space for baking");
1476         RNA_def_enum(ot->srna, "normal_r", rna_enum_normal_swizzle_items, R_BAKE_POSX, "R", "Axis to bake in red channel");
1477         RNA_def_enum(ot->srna, "normal_g", rna_enum_normal_swizzle_items, R_BAKE_POSY, "G", "Axis to bake in green channel");
1478         RNA_def_enum(ot->srna, "normal_b", rna_enum_normal_swizzle_items, R_BAKE_POSZ, "B", "Axis to bake in blue channel");
1479         RNA_def_enum(ot->srna, "save_mode", rna_enum_bake_save_mode_items, R_BAKE_SAVE_INTERNAL, "Save Mode",
1480                      "Choose how to save the baking map");
1481         RNA_def_boolean(ot->srna, "use_clear", false, "Clear",
1482                         "Clear Images before baking (only for internal saving)");
1483         RNA_def_boolean(ot->srna, "use_cage", false, "Cage",
1484                         "Cast rays to active object from a cage");
1485         RNA_def_boolean(ot->srna, "use_split_materials", false, "Split Materials",
1486                         "Split baked maps per material, using material name in output file (external only)");
1487         RNA_def_boolean(ot->srna, "use_automatic_name", false, "Automatic Name",
1488                         "Automatically name the output file with the pass type");
1489         RNA_def_string(ot->srna, "uv_layer", NULL, MAX_CUSTOMDATA_LAYER_NAME, "UV Layer", "UV layer to override active");
1490 }