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