moved shape key insert function into BKE_object.h
[blender.git] / source / blender / editors / mesh / meshtools.c
1 /**
2  * $Id: 
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2004 by Blender Foundation
21  * All rights reserved.
22  *
23  * The Original Code is: all of this file.
24  *
25  * Contributor(s): none yet.
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  */
29
30 /*
31         meshtools.c: no editmode (violated already :), tools operating on meshes
32 */
33
34 #include <stddef.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <math.h>
38 #include <float.h>
39
40 #include "MEM_guardedalloc.h"
41
42 #include "DNA_image_types.h"
43 #include "DNA_key_types.h"
44 #include "DNA_material_types.h"
45 #include "DNA_meshdata_types.h"
46 #include "DNA_mesh_types.h"
47 #include "DNA_object_types.h"
48 #include "DNA_scene_types.h"
49 #include "DNA_screen_types.h"
50 #include "DNA_space_types.h"
51 #include "DNA_view3d_types.h"
52 #include "DNA_windowmanager_types.h"
53 #include "DNA_world_types.h"
54
55 #include "BLI_math.h"
56 #include "BLI_blenlib.h"
57 #include "BLI_editVert.h"
58 #include "BLI_ghash.h"
59 #include "BLI_rand.h" /* for randome face sorting */
60 #include "BLI_threads.h"
61
62
63 #include "BKE_blender.h"
64 #include "BKE_context.h"
65 #include "BKE_depsgraph.h"
66 #include "BKE_DerivedMesh.h"
67 #include "BKE_customdata.h"
68 #include "BKE_global.h"
69 #include "BKE_image.h"
70 #include "BKE_key.h"
71 #include "BKE_library.h"
72 #include "BKE_main.h"
73 #include "BKE_mesh.h"
74 #include "BKE_material.h"
75 #include "BKE_object.h"
76 #include "BKE_utildefines.h"
77 #include "BKE_report.h"
78
79 #include "RE_pipeline.h"
80 #include "RE_shader_ext.h"
81
82 #include "PIL_time.h"
83
84 #include "IMB_imbuf_types.h"
85 #include "IMB_imbuf.h"
86
87 #include "GPU_draw.h"
88
89 #include "BLO_sys_types.h" // for intptr_t support
90
91 #include "ED_mesh.h"
92 #include "ED_object.h"
93 #include "ED_view3d.h"
94
95 #include "WM_api.h"
96 #include "WM_types.h"
97
98 /* own include */
99 #include "mesh_intern.h"
100
101 /* XXX */
102 static void waitcursor(int val) {}
103 static void error() {}
104 static int pupmenu() {return 0;}
105 /* XXX */
106
107
108 /* * ********************** no editmode!!! *********** */
109
110 /*********************** JOIN ***************************/
111
112 /* join selected meshes into the active mesh, context sensitive
113 return 0 if no join is made (error) and 1 of the join is done */
114
115 int join_mesh_exec(bContext *C, wmOperator *op)
116 {
117         Scene *scene= CTX_data_scene(C);
118         Object *ob= CTX_data_active_object(C);
119         Material **matar, *ma;
120         Mesh *me;
121         MVert *mvert, *mv, *mvertmain;
122         MEdge *medge = NULL, *medgemain;
123         MFace *mface = NULL, *mfacemain;
124         Key *key, *nkey=NULL;
125         KeyBlock *kb, *okb, *kbn;
126         float imat[4][4], cmat[4][4], *fp1, *fp2, curpos;
127         int a, b, totcol, totmat=0, totedge=0, totvert=0, totface=0, ok=0;
128         int vertofs, *matmap=NULL;
129         int     i, j, index, haskey=0, edgeofs, faceofs;
130         bDeformGroup *dg, *odg;
131         MDeformVert *dvert;
132         CustomData vdata, edata, fdata;
133
134         if(scene->obedit)
135                 return OPERATOR_CANCELLED;
136         
137         /* ob is the object we are adding geometry to */
138         if(!ob || ob->type!=OB_MESH)
139                 return OPERATOR_CANCELLED;
140         
141         /* count & check */
142         CTX_DATA_BEGIN(C, Base*, base, selected_editable_bases) {
143                 if(base->object->type==OB_MESH) {
144                         me= base->object->data;
145                         
146                         totvert+= me->totvert;
147                         totedge+= me->totedge;
148                         totface+= me->totface;
149                         totmat+= base->object->totcol;
150                         
151                         if(base->object == ob)
152                                 ok= 1;
153                         
154                         /* check for shapekeys */
155                         if(me->key)
156                                 haskey++;
157                 }
158         }
159         CTX_DATA_END;
160         
161         /* that way the active object is always selected */ 
162         if(ok==0)
163                 return OPERATOR_CANCELLED;
164         
165         /* only join meshes if there are verts to join, there aren't too many, and we only had one mesh selected */
166         me= (Mesh *)ob->data;
167         key= me->key;
168         if(totvert==0 || totvert>MESH_MAX_VERTS || totvert==me->totvert) 
169                 return OPERATOR_CANCELLED;
170         
171         /* new material indices and material array */
172         matar= MEM_callocN(sizeof(void*)*totmat, "join_mesh matar");
173         if (totmat) matmap= MEM_callocN(sizeof(int)*totmat, "join_mesh matmap");
174         totcol= ob->totcol;
175         
176         /* obact materials in new main array, is nicer start! */
177         for(a=0; a<ob->totcol; a++) {
178                 matar[a]= give_current_material(ob, a+1);
179                 id_us_plus((ID *)matar[a]);
180                 /* increase id->us : will be lowered later */
181         }
182         
183         /* - if destination mesh had shapekeys, move them somewhere safe, and set up placeholders
184          *      with arrays that are large enough to hold shapekey data for all meshes
185          * -    if destination mesh didn't have shapekeys, but we encountered some in the meshes we're 
186          *      joining, set up a new keyblock and assign to the mesh
187          */
188         if(key) {
189                 /* make a duplicate copy that will only be used here... (must remember to free it!) */
190                 nkey= copy_key(key);
191                 
192                 /* for all keys in old block, clear data-arrays */
193                 for(kb= key->block.first; kb; kb= kb->next) {
194                         if(kb->data) MEM_freeN(kb->data);
195                         kb->data= MEM_callocN(sizeof(float)*3*totvert, "join_shapekey");
196                         kb->totelem= totvert;
197                         kb->weights= NULL;
198                 }
199         }
200         else if(haskey) {
201                 /* add a new key-block and add to the mesh */
202                 key= me->key= add_key((ID *)me);
203                 key->type = KEY_RELATIVE;
204         }
205         
206         /* first pass over objects - copying materials and vertexgroups across */
207         CTX_DATA_BEGIN(C, Base*, base, selected_editable_bases) {
208                 /* only act if a mesh, and not the one we're joining to */
209                 if((ob!=base->object) && (base->object->type==OB_MESH)) {
210                         me= base->object->data;
211                         
212                         /* Join this object's vertex groups to the base one's */
213                         for(dg=base->object->defbase.first; dg; dg=dg->next) {
214                                 /* See if this group exists in the object (if it doesn't, add it to the end) */
215                                 for(odg=ob->defbase.first; odg; odg=odg->next) {
216                                         if(!strcmp(odg->name, dg->name)) {
217                                                 break;
218                                         }
219                                 }
220                                 if(!odg) {
221                                         odg = MEM_callocN(sizeof(bDeformGroup), "join deformGroup");
222                                         memcpy(odg, dg, sizeof(bDeformGroup));
223                                         BLI_addtail(&ob->defbase, odg);
224                                 }
225                         }
226                         if(ob->defbase.first && ob->actdef==0)
227                                 ob->actdef=1;
228                         
229                         
230                         if(me->totvert) {
231                                 /* Add this object's materials to the base one's if they don't exist already (but only if limits not exceeded yet) */
232                                 if(totcol < MAXMAT-1) {
233                                         for(a=1; a<=base->object->totcol; a++) {
234                                                 ma= give_current_material(base->object, a);
235
236                                                 for(b=0; b<totcol; b++) {
237                                                         if(ma == matar[b]) break;
238                                                 }
239                                                 if(b==totcol) {
240                                                         matar[b]= ma;
241                                                         if(ma)
242                                                                 ma->id.us++;
243                                                         totcol++;
244                                                 }
245                                                 if(totcol>=MAXMAT-1) 
246                                                         break;
247                                         }
248                                 }
249                                 
250                                 /* if this mesh has shapekeys, check if destination mesh already has matching entries too */
251                                 if(me->key && key) {
252                                         for(kb= me->key->block.first; kb; kb= kb->next) {
253                                                 /* if key doesn't exist in destination mesh, add it */
254                                                 if(key_get_named_keyblock(key, kb->name) == NULL) {
255                                                         /* copy this existing one over to the new shapekey block */
256                                                         kbn= MEM_dupallocN(kb);
257                                                         kbn->prev= kbn->next= NULL;
258                                                         
259                                                         /* adjust adrcode and other settings to fit (allocate a new data-array) */
260                                                         kbn->data= MEM_callocN(sizeof(float)*3*totvert, "joined_shapekey");
261                                                         kbn->totelem= totvert;
262                                                         kbn->weights= NULL;
263                                                         
264                                                         okb= key->block.last;
265                                                         curpos= (okb) ? okb->pos : -0.1f;
266                                                         if(key->type == KEY_RELATIVE)
267                                                                 kbn->pos= curpos + 0.1f;
268                                                         else
269                                                                 kbn->pos= curpos;
270                                                         
271                                                         BLI_addtail(&key->block, kbn);
272                                                         kbn->adrcode= key->totkey;
273                                                         key->totkey++;
274                                                         if(key->totkey==1) key->refkey= kbn;
275                                                         
276                                                         // XXX 2.5 Animato
277 #if 0
278                                                         /* also, copy corresponding ipo-curve to ipo-block if applicable */
279                                                         if(me->key->ipo && key->ipo) {
280                                                                 // FIXME... this is a luxury item!
281                                                                 puts("FIXME: ignoring IPO's when joining shapekeys on Meshes for now...");
282                                                         }
283 #endif
284                                                 }
285                                         }
286                                 }
287                         }
288                 }
289         }
290         CTX_DATA_END;
291         
292         /* setup new data for destination mesh */
293         memset(&vdata, 0, sizeof(vdata));
294         memset(&edata, 0, sizeof(edata));
295         memset(&fdata, 0, sizeof(fdata));
296         
297         mvert= CustomData_add_layer(&vdata, CD_MVERT, CD_CALLOC, NULL, totvert);
298         medge= CustomData_add_layer(&edata, CD_MEDGE, CD_CALLOC, NULL, totedge);
299         mface= CustomData_add_layer(&fdata, CD_MFACE, CD_CALLOC, NULL, totface);
300         
301         mvertmain= mvert;
302         medgemain= medge;
303         mfacemain= mface;
304         
305         vertofs= 0;
306         edgeofs= 0;
307         faceofs= 0;
308         
309         /* inverse transform for all selected meshes in this object */
310         invert_m4_m4(imat, ob->obmat);
311         
312         CTX_DATA_BEGIN(C, Base*, base, selected_editable_bases) {
313                 /* only join if this is a mesh */
314                 if(base->object->type==OB_MESH) {
315                         me= base->object->data;
316                         
317                         if(me->totvert) {
318                                 /* standard data */
319                                 CustomData_merge(&me->vdata, &vdata, CD_MASK_MESH, CD_DEFAULT, totvert);
320                                 CustomData_copy_data(&me->vdata, &vdata, 0, vertofs, me->totvert);
321                                 
322                                 /* vertex groups */
323                                 dvert= CustomData_get(&vdata, vertofs, CD_MDEFORMVERT);
324                                 
325                                 /* NB: vertex groups here are new version */
326                                 if(dvert) {
327                                         for(i=0; i<me->totvert; i++) {
328                                                 for(j=0; j<dvert[i].totweight; j++) {
329                                                         /*      Find the old vertex group */
330                                                         odg = BLI_findlink(&base->object->defbase, dvert[i].dw[j].def_nr);
331                                                         if(odg) {
332                                                                 /*      Search for a match in the new object, and set new index */
333                                                                 for(dg=ob->defbase.first, index=0; dg; dg=dg->next, index++) {
334                                                                         if(!strcmp(dg->name, odg->name)) {
335                                                                                 dvert[i].dw[j].def_nr = index;
336                                                                                 break;
337                                                                         }
338                                                                 }
339                                                         }
340                                                 }
341                                         }
342                                 }
343                                 
344                                 /* if this is the object we're merging into, no need to do anything */
345                                 if(base->object != ob) {
346                                         /* watch this: switch matmul order really goes wrong */
347                                         mul_m4_m4m4(cmat, base->object->obmat, imat);
348                                         
349                                         /* transform vertex coordinates into new space */
350                                         for(a=0, mv=mvert; a < me->totvert; a++, mv++) {
351                                                 mul_m4_v3(cmat, mv->co);
352                                         }
353                                         
354                                         /* for each shapekey in destination mesh:
355                                          *      - if there's a matching one, copy it across (will need to transform vertices into new space...)
356                                          *      - otherwise, just copy own coordinates of mesh (no need to transform vertex coordinates into new space)
357                                          */
358                                         if(key) {
359                                                 /* if this mesh has any shapekeys, check first, otherwise just copy coordinates */
360                                                 for(kb= key->block.first; kb; kb= kb->next) {
361                                                         /* get pointer to where to write data for this mesh in shapekey's data array */
362                                                         fp1= ((float *)kb->data) + (vertofs*3); 
363                                                         
364                                                         /* check if this mesh has such a shapekey */
365                                                         okb= key_get_named_keyblock(me->key, kb->name);
366                                                         if(okb) {
367                                                                 /* copy this mesh's shapekey to the destination shapekey (need to transform first) */
368                                                                 fp2= ((float *)(okb->data));
369                                                                 for(a=0; a < me->totvert; a++, fp1+=3, fp2+=3) {
370                                                                         VECCOPY(fp1, fp2);
371                                                                         mul_m4_v3(cmat, fp1);
372                                                                 }
373                                                         }
374                                                         else {
375                                                                 /* copy this mesh's vertex coordinates to the destination shapekey */
376                                                                 mv= mvert;
377                                                                 for(a=0; a < me->totvert; a++, fp1+=3, mv++) {
378                                                                         VECCOPY(fp1, mv->co);
379                                                                 }
380                                                         }
381                                                 }
382                                         }
383                                 }
384                                 else {
385                                         /* for each shapekey in destination mesh:
386                                          *      - if it was an 'original', copy the appropriate data from nkey
387                                          *      - otherwise, copy across plain coordinates (no need to transform coordinates)
388                                          */
389                                         if(key) {
390                                                 for(kb= key->block.first; kb; kb= kb->next) {
391                                                         /* get pointer to where to write data for this mesh in shapekey's data array */
392                                                         fp1= ((float *)kb->data) + (vertofs*3); 
393                                                         
394                                                         /* check if this was one of the original shapekeys */
395                                                         okb= key_get_named_keyblock(nkey, kb->name);
396                                                         if(okb) {
397                                                                 /* copy this mesh's shapekey to the destination shapekey */
398                                                                 fp2= ((float *)(okb->data));
399                                                                 for(a=0; a < me->totvert; a++, fp1+=3, fp2+=3) {
400                                                                         VECCOPY(fp1, fp2);
401                                                                 }
402                                                         }
403                                                         else {
404                                                                 /* copy base-coordinates to the destination shapekey */
405                                                                 mv= mvert;
406                                                                 for(a=0; a < me->totvert; a++, fp1+=3, mv++) {
407                                                                         VECCOPY(fp1, mv->co);
408                                                                 }
409                                                         }
410                                                 }
411                                         }
412                                 }
413                                 
414                                 /* advance mvert pointer to end of base mesh's data */
415                                 mvert+= me->totvert;
416                         }
417                         
418                         if(me->totface) {
419                                 /* make mapping for materials */
420                                 for(a=1; a<=base->object->totcol; a++) {
421                                         ma= give_current_material(base->object, a);
422
423                                         for(b=0; b<totcol; b++) {
424                                                 if(ma == matar[b]) {
425                                                         matmap[a-1]= b;
426                                                         break;
427                                                 }
428                                         }
429                                 }
430                                 
431                                 CustomData_merge(&me->fdata, &fdata, CD_MASK_MESH, CD_DEFAULT, totface);
432                                 CustomData_copy_data(&me->fdata, &fdata, 0, faceofs, me->totface);
433                                 
434                                 for(a=0; a<me->totface; a++, mface++) {
435                                         mface->v1+= vertofs;
436                                         mface->v2+= vertofs;
437                                         mface->v3+= vertofs;
438                                         if(mface->v4) mface->v4+= vertofs;
439                                         
440                                         if (matmap)
441                                                 mface->mat_nr= matmap[(int)mface->mat_nr];
442                                         else 
443                                                 mface->mat_nr= 0;
444                                 }
445                                 
446                                 faceofs += me->totface;
447                         }
448                         
449                         if(me->totedge) {
450                                 CustomData_merge(&me->edata, &edata, CD_MASK_MESH, CD_DEFAULT, totedge);
451                                 CustomData_copy_data(&me->edata, &edata, 0, edgeofs, me->totedge);
452                                 
453                                 for(a=0; a<me->totedge; a++, medge++) {
454                                         medge->v1+= vertofs;
455                                         medge->v2+= vertofs;
456                                 }
457                                 
458                                 edgeofs += me->totedge;
459                         }
460                         
461                         /* vertofs is used to help newly added verts be reattached to their edge/face 
462                          * (cannot be set earlier, or else reattaching goes wrong)
463                          */
464                         vertofs += me->totvert;
465                         
466                         /* free base, now that data is merged */
467                         if(base->object != ob)
468                                 ED_base_object_free_and_unlink(scene, base);
469                 }
470         }
471         CTX_DATA_END;
472         
473         /* return to mesh we're merging to */
474         me= ob->data;
475         
476         CustomData_free(&me->vdata, me->totvert);
477         CustomData_free(&me->edata, me->totedge);
478         CustomData_free(&me->fdata, me->totface);
479
480         me->totvert= totvert;
481         me->totedge= totedge;
482         me->totface= totface;
483         
484         me->vdata= vdata;
485         me->edata= edata;
486         me->fdata= fdata;
487
488         mesh_update_customdata_pointers(me);
489         
490         /* old material array */
491         for(a=1; a<=ob->totcol; a++) {
492                 ma= ob->mat[a-1];
493                 if(ma) ma->id.us--;
494         }
495         for(a=1; a<=me->totcol; a++) {
496                 ma= me->mat[a-1];
497                 if(ma) ma->id.us--;
498         }
499         if(ob->mat) MEM_freeN(ob->mat);
500         if(ob->matbits) MEM_freeN(ob->matbits);
501         if(me->mat) MEM_freeN(me->mat);
502         ob->mat= me->mat= NULL;
503         ob->matbits= NULL;
504         
505         if(totcol) {
506                 me->mat= matar;
507                 ob->mat= MEM_callocN(sizeof(void *)*totcol, "join obmatar");
508                 ob->matbits= MEM_callocN(sizeof(char)*totcol, "join obmatbits");
509         }
510         else
511                 MEM_freeN(matar);
512         
513         ob->totcol= me->totcol= totcol;
514         ob->colbits= 0;
515
516         if (matmap) MEM_freeN(matmap);
517         
518         /* other mesh users */
519         test_object_materials((ID *)me);
520         
521         /* free temp copy of destination shapekeys (if applicable) */
522         if(nkey) {
523                 // XXX 2.5 Animato
524 #if 0
525                 /* free it's ipo too - both are not actually freed from memory yet as ID-blocks */
526                 if(nkey->ipo) {
527                         free_ipo(nkey->ipo);
528                         BLI_remlink(&G.main->ipo, nkey->ipo);
529                         MEM_freeN(nkey->ipo);
530                 }
531 #endif
532                 
533                 free_key(nkey);
534                 BLI_remlink(&G.main->key, nkey);
535                 MEM_freeN(nkey);
536         }
537         
538         DAG_scene_sort(scene);  // removed objects, need to rebuild dag before editmode call
539         
540         ED_object_enter_editmode(C, EM_WAITCURSOR);
541         ED_object_exit_editmode(C, EM_FREEDATA|EM_WAITCURSOR|EM_DO_UNDO);
542
543         WM_event_add_notifier(C, NC_SCENE|ND_OB_ACTIVE, scene);
544
545         return OPERATOR_FINISHED;
546 }
547
548 /*********************** JOIN AS SHAPES ***************************/
549
550 /* Append selected meshes vertex locations as shapes of the active mesh, 
551   return 0 if no join is made (error) and 1 of the join is done */
552
553 int join_mesh_shapes_exec(bContext *C, wmOperator *op)
554 {
555         Scene *scene= CTX_data_scene(C);
556         Object *ob= CTX_data_active_object(C);
557         Mesh *me= (Mesh *)ob->data;
558         Mesh *selme=NULL;
559         DerivedMesh *dm=NULL;
560         Key *key=me->key;
561         KeyBlock *kb;
562         int ok=0, nonequal_verts=0;
563         
564         CTX_DATA_BEGIN(C, Base*, base, selected_editable_bases) {
565                 if (base->object == ob) continue;
566                 
567                 if (base->object->type==OB_MESH) {
568                         selme = (Mesh *)base->object->data;
569                         
570                         if (selme->totvert==me->totvert)
571                                 ok++;
572                         else
573                                 nonequal_verts=1;
574                 }
575         }
576         CTX_DATA_END;
577         
578         if (!ok) {
579                 if (nonequal_verts)
580                         BKE_report(op->reports, RPT_ERROR, "Selected meshes must have equal numbers of vertices.");
581                 else
582                         BKE_report(op->reports, RPT_ERROR, "No additional selected meshes with equal vertex count to join.");
583                 return OPERATOR_CANCELLED;
584         }
585         
586         if(key == NULL) {
587                 key= me->key= add_key((ID *)me);
588                 key->type= KEY_RELATIVE;
589
590                 /* first key added, so it was the basis. initialise it with the existing mesh */
591                 kb= add_keyblock(key);
592                 mesh_to_key(me, kb);
593         }
594         
595         /* now ready to add new keys from selected meshes */
596         CTX_DATA_BEGIN(C, Base*, base, selected_editable_bases) {
597                 if (base->object == ob) continue;
598                 
599                 if(base->object->type==OB_MESH) {
600                         selme = (Mesh *)base->object->data;
601                         
602                         if (selme->totvert==me->totvert) {
603                                 dm = mesh_get_derived_deform(scene, base->object, CD_MASK_BAREMESH);
604                                 
605                                 if (!dm) continue;
606                                         
607                                 kb= add_keyblock(key);
608                                 strcpy(kb->name, base->object->id.name+2);
609                                 BLI_uniquename(&key->block, kb, "Key", '.', offsetof(KeyBlock, name), 32);
610                                 
611                                 DM_to_meshkey(dm, me, kb);
612                                 
613                                 dm->release(dm);
614                         }
615                 }
616         }
617         CTX_DATA_END;
618         
619         WM_event_add_notifier(C, NC_SCENE|ND_OB_ACTIVE, scene);
620         
621         return OPERATOR_FINISHED;
622 }
623
624 /* ********************** SORT FACES ******************* */
625
626 static void permutate(void *list, int num, int size, int *index)
627 {
628         void *buf;
629         int len;
630         int i;
631
632         len = num * size;
633
634         buf = MEM_mallocN(len, "permutate");
635         memcpy(buf, list, len);
636         
637         for (i = 0; i < num; i++) {
638                 memcpy((char *)list + (i * size), (char *)buf + (index[i] * size), size);
639         }
640         MEM_freeN(buf);
641 }
642
643 /* sort faces on view axis */
644 static float *face_sort_floats;
645 static int float_sort(const void *v1, const void *v2)
646 {
647         float x1, x2;
648         
649         x1 = face_sort_floats[((int *) v1)[0]];
650         x2 = face_sort_floats[((int *) v2)[0]];
651         
652         if( x1 > x2 ) return 1;
653         else if( x1 < x2 ) return -1;
654         return 0;
655 }
656
657
658 void sort_faces(Scene *scene, View3D *v3d)
659 {
660         RegionView3D *rv3d= NULL; // get from context 
661         Object *ob= OBACT;
662         Mesh *me;
663         CustomDataLayer *layer;
664         int i, *index;
665         short event;
666         float reverse = 1;
667         int ctrl= 0;    // XXX
668         
669         if(!ob) return;
670         if(scene->obedit) return;
671         if(ob->type!=OB_MESH) return;
672         if (!v3d) return;
673         
674         me= ob->data;
675         if(me->totface==0) return;
676         
677         event = pupmenu(
678         "Sort Faces (Ctrl to reverse)%t|"
679         "View Axis%x1|"
680         "Cursor Distance%x2|"
681         "Material%x3|"
682         "Selection%x4|"
683         "Randomize%x5");
684         
685         if (event==-1) return;
686         
687         if(ctrl)
688                 reverse = -1;
689         
690 /*      create index list */
691         index = (int *) MEM_mallocN(sizeof(int) * me->totface, "sort faces");
692         for (i = 0; i < me->totface; i++) {
693                 index[i] = i;
694         }
695         
696         face_sort_floats = (float *) MEM_mallocN(sizeof(float) * me->totface, "sort faces float");
697         
698 /* sort index list instead of faces itself 
699    and apply this permutation to all face layers */
700    
701         if (event == 5) {
702                 /* Random */
703                 for(i=0; i<me->totface; i++) {
704                         face_sort_floats[i] = BLI_frand();
705                 }
706                 qsort(index, me->totface, sizeof(int), float_sort);             
707         } else {
708                 MFace *mf;
709                 float vec[3];
710                 float mat[4][4];
711                 float cur[3];
712                 
713                 if (event == 1)
714                         mul_m4_m4m4(mat, OBACT->obmat, rv3d->viewmat); /* apply the view matrix to the object matrix */
715                 else if (event == 2) { /* sort from cursor */
716                         if( v3d && v3d->localvd ) {
717                                 VECCOPY(cur, v3d->cursor);
718                         } else {
719                                 VECCOPY(cur, scene->cursor);
720                         }
721                         invert_m4_m4(mat, OBACT->obmat);
722                         mul_m4_v3(mat, cur);
723                 }
724                 
725                 mf= me->mface;
726                 for(i=0; i<me->totface; i++, mf++) {
727                         
728                         if (event==3) {
729                                 face_sort_floats[i] = ((float)mf->mat_nr)*reverse;
730                         } else if (event==4) {
731                                 /*selected first*/
732                                 if (mf->flag & ME_FACE_SEL)     face_sort_floats[i] = 0.0;
733                                 else                                            face_sort_floats[i] = reverse;
734                         } else {
735                                 /* find the faces center */
736                                 add_v3_v3v3(vec, (me->mvert+mf->v1)->co, (me->mvert+mf->v2)->co);
737                                 if (mf->v4) {
738                                         add_v3_v3v3(vec, vec, (me->mvert+mf->v3)->co);
739                                         add_v3_v3v3(vec, vec, (me->mvert+mf->v4)->co);
740                                         mul_v3_fl(vec, 0.25f);
741                                 } else {
742                                         add_v3_v3v3(vec, vec, (me->mvert+mf->v3)->co);
743                                         mul_v3_fl(vec, 1.0f/3.0f);
744                                 } /* done */
745                                 
746                                 if (event == 1) { /* sort on view axis */
747                                         mul_m4_v3(mat, vec);
748                                         face_sort_floats[i] = vec[2] * reverse;
749                                 } else { /* distance from cursor*/
750                                         face_sort_floats[i] = len_v3v3(cur, vec) * reverse; /* back to front */
751                                 }
752                         }
753                 }
754                 qsort(index, me->totface, sizeof(int), float_sort);
755         }
756         
757         MEM_freeN(face_sort_floats);
758         
759         for(i = 0; i < me->fdata.totlayer; i++) {
760                 layer = &me->fdata.layers[i];
761                 permutate(layer->data, me->totface, CustomData_sizeof(layer->type), index);
762         }
763
764         MEM_freeN(index);
765
766         DAG_id_flush_update(ob->data, OB_RECALC_DATA);
767 }
768
769
770
771 /* ********************* MESH VERTEX OCTREE LOOKUP ************* */
772
773 /* important note; this is unfinished, needs better API for editmode, and custom threshold */
774
775 #define MOC_RES                 8
776 #define MOC_NODE_RES    8
777 #define MOC_THRESH              0.0002f
778
779 typedef struct MocNode {
780         struct MocNode *next;
781         intptr_t index[MOC_NODE_RES];
782 } MocNode;
783
784 static int mesh_octree_get_base_offs(float *co, float *offs, float *div)
785 {
786         int vx, vy, vz;
787         
788         vx= floor( (co[0]-offs[0])/div[0] );
789         vy= floor( (co[1]-offs[1])/div[1] );
790         vz= floor( (co[2]-offs[2])/div[2] );
791         
792         CLAMP(vx, 0, MOC_RES-1);
793         CLAMP(vy, 0, MOC_RES-1);
794         CLAMP(vz, 0, MOC_RES-1);
795
796         return (vx*MOC_RES*MOC_RES) + vy*MOC_RES + vz;
797 }
798
799 static void mesh_octree_add_node(MocNode **bt, intptr_t index)
800 {
801         if(*bt==NULL) {
802                 *bt= MEM_callocN(sizeof(MocNode), "MocNode");
803                 (*bt)->index[0]= index;
804         }
805         else {
806                 int a;
807                 for(a=0; a<MOC_NODE_RES; a++) {
808                         if((*bt)->index[a]==index)
809                                 return;
810                         else if((*bt)->index[a]==0) {
811                                 (*bt)->index[a]= index;
812                                 return;
813                         }
814                 }
815                 mesh_octree_add_node(&(*bt)->next, index);
816         }
817 }
818
819 static void mesh_octree_free_node(MocNode **bt)
820 {
821         if( (*bt)->next ) {
822                 mesh_octree_free_node(&(*bt)->next);
823         }
824         MEM_freeN(*bt);
825 }
826
827
828 /* temporal define, just to make nicer code below */
829 #define MOC_ADDNODE(vx, vy, vz) mesh_octree_add_node(basetable + ((vx)*MOC_RES*MOC_RES) + (vy)*MOC_RES + (vz), index)
830
831 static void mesh_octree_add_nodes(MocNode **basetable, float *co, float *offs, float *div, intptr_t index)
832 {
833         float fx, fy, fz;
834         int vx, vy, vz;
835         
836         if (!finite(co[0]) ||
837                 !finite(co[1]) ||
838                 !finite(co[2])
839         ) {
840                 return;
841         }
842         
843         fx= (co[0]-offs[0])/div[0];
844         fy= (co[1]-offs[1])/div[1];
845         fz= (co[2]-offs[2])/div[2];
846         CLAMP(fx, 0.0f, MOC_RES-MOC_THRESH);
847         CLAMP(fy, 0.0f, MOC_RES-MOC_THRESH);
848         CLAMP(fz, 0.0f, MOC_RES-MOC_THRESH);
849         
850         vx= floor(fx);
851         vy= floor(fy);
852         vz= floor(fz);
853         
854         MOC_ADDNODE(vx, vy, vz);
855         
856         if( vx>0 )
857                 if( fx-((float)vx)-MOC_THRESH < 0.0f)
858                         MOC_ADDNODE(vx-1, vy, vz);
859         if( vx<MOC_RES-2 )
860                 if( fx-((float)vx)+MOC_THRESH > 1.0f)
861                         MOC_ADDNODE(vx+1, vy, vz);
862
863         if( vy>0 )
864                 if( fy-((float)vy)-MOC_THRESH < 0.0f) 
865                         MOC_ADDNODE(vx, vy-1, vz);
866         if( vy<MOC_RES-2 )
867                 if( fy-((float)vy)+MOC_THRESH > 1.0f) 
868                         MOC_ADDNODE(vx, vy+1, vz);
869
870         if( vz>0 )
871                 if( fz-((float)vz)-MOC_THRESH < 0.0f) 
872                         MOC_ADDNODE(vx, vy, vz-1);
873         if( vz<MOC_RES-2 )
874                 if( fz-((float)vz)+MOC_THRESH > 1.0f) 
875                         MOC_ADDNODE(vx, vy, vz+1);
876         
877 }
878
879 static intptr_t mesh_octree_find_index(MocNode **bt, MVert *mvert, float *co)
880 {
881         float *vec;
882         int a;
883         
884         if(*bt==NULL)
885                 return -1;
886         
887         for(a=0; a<MOC_NODE_RES; a++) {
888                 if((*bt)->index[a]) {
889                         /* does mesh verts and editmode, code looks potential dangerous, octree should really be filled OK! */
890                         if(mvert) {
891                                 vec= (mvert+(*bt)->index[a]-1)->co;
892                                 if(compare_v3v3(vec, co, MOC_THRESH))
893                                         return (*bt)->index[a]-1;
894                         }
895                         else {
896                                 EditVert *eve= (EditVert *)((*bt)->index[a]);
897                                 if(compare_v3v3(eve->co, co, MOC_THRESH))
898                                         return (*bt)->index[a];
899                         }
900                 }
901                 else return -1;
902         }
903         if( (*bt)->next)
904                 return mesh_octree_find_index(&(*bt)->next, mvert, co);
905         
906         return -1;
907 }
908
909 static struct {
910         MocNode **table;
911         float offs[3], div[3];
912 } MeshOctree = {NULL, {0, 0, 0}, {0, 0, 0}};
913
914 /* mode is 's' start, or 'e' end, or 'u' use */
915 /* if end, ob can be NULL */
916 intptr_t mesh_octree_table(Object *ob, EditMesh *em, float *co, char mode)
917 {
918         MocNode **bt;
919         
920         if(mode=='u') {         /* use table */
921                 if(MeshOctree.table==NULL)
922                         mesh_octree_table(ob, em, NULL, 's');
923            
924                 if(MeshOctree.table) {
925                         Mesh *me= ob->data;
926                         bt= MeshOctree.table + mesh_octree_get_base_offs(co, MeshOctree.offs, MeshOctree.div);
927                         if(em)
928                                 return mesh_octree_find_index(bt, NULL, co);
929                         else
930                                 return mesh_octree_find_index(bt, me->mvert, co);
931                 }
932                 return -1;
933         }
934         else if(mode=='s') {    /* start table */
935                 Mesh *me= ob->data;
936                 float min[3], max[3];
937
938                 /* we compute own bounding box and don't reuse ob->bb because
939                  * we are using the undeformed coordinates*/
940                 INIT_MINMAX(min, max);
941
942                 if(em && me->edit_mesh==em) {
943                         EditVert *eve;
944                         
945                         for(eve= em->verts.first; eve; eve= eve->next)
946                                 DO_MINMAX(eve->co, min, max)
947                 }
948                 else {          
949                         MVert *mvert;
950                         int a;
951                         
952                         for(a=0, mvert= me->mvert; a<me->totvert; a++, mvert++)
953                                 DO_MINMAX(mvert->co, min, max);
954                 }
955                 
956                 /* for quick unit coordinate calculus */
957                 VECCOPY(MeshOctree.offs, min);
958                 MeshOctree.offs[0]-= MOC_THRESH;                /* we offset it 1 threshold unit extra */
959                 MeshOctree.offs[1]-= MOC_THRESH;
960                 MeshOctree.offs[2]-= MOC_THRESH;
961                 
962                 sub_v3_v3v3(MeshOctree.div, max, min);
963                 MeshOctree.div[0]+= 2*MOC_THRESH;       /* and divide with 2 threshold unit more extra (try 8x8 unit grid on paint) */
964                 MeshOctree.div[1]+= 2*MOC_THRESH;
965                 MeshOctree.div[2]+= 2*MOC_THRESH;
966                 
967                 mul_v3_fl(MeshOctree.div, 1.0f/MOC_RES);
968                 if(MeshOctree.div[0]==0.0f) MeshOctree.div[0]= 1.0f;
969                 if(MeshOctree.div[1]==0.0f) MeshOctree.div[1]= 1.0f;
970                 if(MeshOctree.div[2]==0.0f) MeshOctree.div[2]= 1.0f;
971                         
972                 if(MeshOctree.table) /* happens when entering this call without ending it */
973                         mesh_octree_table(ob, em, co, 'e');
974                 
975                 MeshOctree.table= MEM_callocN(MOC_RES*MOC_RES*MOC_RES*sizeof(void *), "sym table");
976                 
977                 if(em && me->edit_mesh==em) {
978                         EditVert *eve;
979
980                         for(eve= em->verts.first; eve; eve= eve->next) {
981                                 mesh_octree_add_nodes(MeshOctree.table, eve->co, MeshOctree.offs, MeshOctree.div, (intptr_t)(eve));
982                         }
983                 }
984                 else {          
985                         MVert *mvert;
986                         int a;
987                         
988                         for(a=0, mvert= me->mvert; a<me->totvert; a++, mvert++)
989                                 mesh_octree_add_nodes(MeshOctree.table, mvert->co, MeshOctree.offs, MeshOctree.div, a+1);
990                 }
991         }
992         else if(mode=='e') { /* end table */
993                 if(MeshOctree.table) {
994                         int a;
995                         
996                         for(a=0, bt=MeshOctree.table; a<MOC_RES*MOC_RES*MOC_RES; a++, bt++) {
997                                 if(*bt) mesh_octree_free_node(bt);
998                         }
999                         MEM_freeN(MeshOctree.table);
1000                         MeshOctree.table= NULL;
1001                 }
1002         }
1003         return 0;
1004 }
1005
1006 int mesh_get_x_mirror_vert(Object *ob, int index)
1007 {
1008         Mesh *me= ob->data;
1009         MVert *mvert;
1010         float vec[3];
1011         
1012         mvert= me->mvert+index;
1013         vec[0]= -mvert->co[0];
1014         vec[1]= mvert->co[1];
1015         vec[2]= mvert->co[2];
1016         
1017         return mesh_octree_table(ob, NULL, vec, 'u');
1018 }
1019
1020 EditVert *editmesh_get_x_mirror_vert(Object *ob, EditMesh *em, float *co)
1021 {
1022         float vec[3];
1023         intptr_t poinval;
1024         
1025         /* ignore nan verts */
1026         if (!finite(co[0]) ||
1027                 !finite(co[1]) ||
1028                 !finite(co[2])
1029            )
1030                 return NULL;
1031         
1032         vec[0]= -co[0];
1033         vec[1]= co[1];
1034         vec[2]= co[2];
1035         
1036         poinval= mesh_octree_table(ob, em, vec, 'u');
1037         if(poinval != -1)
1038                 return (EditVert *)(poinval);
1039         return NULL;
1040 }
1041
1042 static unsigned int mirror_facehash(void *ptr)
1043 {
1044         MFace *mf= ptr;
1045         int v0, v1;
1046
1047         if(mf->v4) {
1048                 v0= MIN4(mf->v1, mf->v2, mf->v3, mf->v4);
1049                 v1= MAX4(mf->v1, mf->v2, mf->v3, mf->v4);
1050         }
1051         else {
1052                 v0= MIN3(mf->v1, mf->v2, mf->v3);
1053                 v1= MAX3(mf->v1, mf->v2, mf->v3);
1054         }
1055
1056         return ((v0*39)^(v1*31));
1057 }
1058
1059 static int mirror_facerotation(MFace *a, MFace *b)
1060 {
1061         if(b->v4) {
1062                 if(a->v1==b->v1 && a->v2==b->v2 && a->v3==b->v3 && a->v4==b->v4)
1063                         return 0;
1064                 else if(a->v4==b->v1 && a->v1==b->v2 && a->v2==b->v3 && a->v3==b->v4)
1065                         return 1;
1066                 else if(a->v3==b->v1 && a->v4==b->v2 && a->v1==b->v3 && a->v2==b->v4)
1067                         return 2;
1068                 else if(a->v2==b->v1 && a->v3==b->v2 && a->v4==b->v3 && a->v1==b->v4)
1069                         return 3;
1070         }
1071         else {
1072                 if(a->v1==b->v1 && a->v2==b->v2 && a->v3==b->v3)
1073                         return 0;
1074                 else if(a->v3==b->v1 && a->v1==b->v2 && a->v2==b->v3)
1075                         return 1;
1076                 else if(a->v2==b->v1 && a->v3==b->v2 && a->v1==b->v3)
1077                         return 2;
1078         }
1079         
1080         return -1;
1081 }
1082
1083 static int mirror_facecmp(void *a, void *b)
1084 {
1085         return (mirror_facerotation((MFace*)a, (MFace*)b) == -1);
1086 }
1087
1088 int *mesh_get_x_mirror_faces(Object *ob, EditMesh *em)
1089 {
1090         Mesh *me= ob->data;
1091         MVert *mv, *mvert= me->mvert;
1092         MFace mirrormf, *mf, *hashmf, *mface= me->mface;
1093         GHash *fhash;
1094         int *mirrorverts, *mirrorfaces;
1095         int a;
1096
1097         mirrorverts= MEM_callocN(sizeof(int)*me->totvert, "MirrorVerts");
1098         mirrorfaces= MEM_callocN(sizeof(int)*2*me->totface, "MirrorFaces");
1099
1100         mesh_octree_table(ob, em, NULL, 's');
1101
1102         for(a=0, mv=mvert; a<me->totvert; a++, mv++)
1103                 mirrorverts[a]= mesh_get_x_mirror_vert(ob, a);
1104
1105         mesh_octree_table(ob, em, NULL, 'e');
1106
1107         fhash= BLI_ghash_new(mirror_facehash, mirror_facecmp);
1108         for(a=0, mf=mface; a<me->totface; a++, mf++)
1109                 BLI_ghash_insert(fhash, mf, mf);
1110
1111         for(a=0, mf=mface; a<me->totface; a++, mf++) {
1112                 mirrormf.v1= mirrorverts[mf->v3];
1113                 mirrormf.v2= mirrorverts[mf->v2];
1114                 mirrormf.v3= mirrorverts[mf->v1];
1115                 mirrormf.v4= (mf->v4)? mirrorverts[mf->v4]: 0;
1116
1117                 /* make sure v4 is not 0 if a quad */
1118                 if(mf->v4 && mirrormf.v4==0) {
1119                         SWAP(int, mirrormf.v1, mirrormf.v3);
1120                         SWAP(int, mirrormf.v2, mirrormf.v4);
1121                 }
1122
1123                 hashmf= BLI_ghash_lookup(fhash, &mirrormf);
1124                 if(hashmf) {
1125                         mirrorfaces[a*2]= hashmf - mface;
1126                         mirrorfaces[a*2+1]= mirror_facerotation(&mirrormf, hashmf);
1127                 }
1128                 else
1129                         mirrorfaces[a*2]= -1;
1130         }
1131
1132         BLI_ghash_free(fhash, NULL, NULL);
1133         MEM_freeN(mirrorverts);
1134         
1135         return mirrorfaces;
1136 }
1137
1138 /* ****************** render BAKING ********************** */
1139
1140 /* threaded break test */
1141 static int thread_break(void *unused)
1142 {
1143         return G.afbreek;
1144 }
1145
1146 static ScrArea *biggest_image_area(bScreen *screen)
1147 {
1148         ScrArea *sa, *big= NULL;
1149         int size, maxsize= 0;
1150         
1151         for(sa= screen->areabase.first; sa; sa= sa->next) {
1152                 if(sa->spacetype==SPACE_IMAGE) {
1153                         size= sa->winx*sa->winy;
1154                         if(sa->winx > 10 && sa->winy > 10 && size > maxsize) {
1155                                 maxsize= size;
1156                                 big= sa;
1157                         }
1158                 }
1159         }
1160         return big;
1161 }
1162
1163
1164 typedef struct BakeRender {
1165         Render *re;
1166         struct Object *actob;
1167         int event, tot, ready;
1168 } BakeRender;
1169
1170 static void *do_bake_render(void *bake_v)
1171 {
1172         BakeRender *bkr= bake_v;
1173         
1174         bkr->tot= RE_bake_shade_all_selected(bkr->re, bkr->event, bkr->actob);
1175         bkr->ready= 1;
1176         
1177         return NULL;
1178 }
1179
1180
1181 void objects_bake_render(Scene *scene, short event, char **error_msg)
1182 {
1183         Object *actob= OBACT;
1184         int active= scene->r.bake_flag & R_BAKE_TO_ACTIVE;
1185         short prev_r_raytrace= 0, prev_wo_amb_occ= 0;
1186         
1187         if(event==0) event= scene->r.bake_mode;
1188         
1189         if(scene->r.renderer!=R_INTERN) {        
1190                 *error_msg = "Bake only supported for Internal Renderer";
1191                 return;
1192         }        
1193         
1194         if(active && !actob) {
1195                 *error_msg = "No active object";
1196                 return;
1197         }
1198         
1199         if(event>0) {
1200                 bScreen *screen= NULL; // XXX CTX
1201                 Render *re= RE_NewRender("_Bake View_");
1202                 ScrArea *area= biggest_image_area(screen);
1203                 ListBase threads;
1204                 BakeRender bkr;
1205                 int timer=0, tot; // XXX, sculptmode= G.f & G_SCULPTMODE;
1206
1207 // XXX          if(sculptmode) set_sculptmode();
1208                 
1209                 if(event==1) event= RE_BAKE_ALL;
1210                 else if(event==2) event= RE_BAKE_AO;
1211                 else if(event==3) event= RE_BAKE_NORMALS;
1212                 else if(event==4) event= RE_BAKE_TEXTURE;
1213                 else if(event==5) event= RE_BAKE_DISPLACEMENT;
1214                 else event= RE_BAKE_SHADOW;
1215
1216                 if(event==RE_BAKE_AO) {
1217                         if(scene->world==NULL) {
1218                                 *error_msg = "No world set up";
1219                                 return;
1220                         }
1221
1222                         /* If raytracing or AO is disabled, switch it on temporarily for baking. */
1223                         prev_wo_amb_occ = (scene->world->mode & WO_AMB_OCC) != 0;
1224                         scene->world->mode |= WO_AMB_OCC;
1225                 }
1226                 if(event==RE_BAKE_AO || active) {
1227                         prev_r_raytrace = (scene->r.mode & R_RAYTRACE) != 0;
1228                         scene->r.mode |= R_RAYTRACE;
1229                 }
1230                 
1231                 waitcursor(1);
1232                 RE_test_break_cb(re, NULL, thread_break);
1233                 G.afbreek= 0;   /* blender_test_break uses this global */
1234                 
1235                 RE_Database_Baking(re, scene, event, (active)? actob: NULL);
1236                 
1237                 /* baking itself is threaded, cannot use test_break in threads. we also update optional imagewindow */
1238         
1239                 BLI_init_threads(&threads, do_bake_render, 1);
1240                 bkr.re= re;
1241                 bkr.event= event;
1242                 bkr.ready= 0;
1243                 bkr.actob= (active)? actob: NULL;
1244                 BLI_insert_thread(&threads, &bkr);
1245                 
1246                 while(bkr.ready==0) {
1247                         PIL_sleep_ms(50);
1248                         if(bkr.ready)
1249                                 break;
1250                         
1251                         if (!G.background) {
1252                                 blender_test_break();
1253                                 
1254                                 timer++;
1255                                 if(area && timer==20) {
1256                                         Image *ima= RE_bake_shade_get_image();
1257                                         if(ima) ((SpaceImage *)area->spacedata.first)->image= ima;
1258 // XX                                   scrarea_do_windraw(area);
1259 //                                      myswapbuffers();        
1260                                         timer= 0;
1261                                 }
1262                         }
1263                 }
1264                 BLI_end_threads(&threads);
1265                 tot= bkr.tot;
1266                 
1267                 RE_Database_Free(re);
1268                 waitcursor(0);
1269                 
1270                 if(tot==0) *error_msg = "No Images found to bake to";
1271                 else {
1272                         Image *ima;
1273                         /* force OpenGL reload and mipmap recalc */
1274                         for(ima= G.main->image.first; ima; ima= ima->id.next) {
1275                                 if(ima->ok==IMA_OK_LOADED) {
1276                                         ImBuf *ibuf= BKE_image_get_ibuf(ima, NULL);
1277                                         if(ibuf && (ibuf->userflags & IB_BITMAPDIRTY)) {
1278                                                 GPU_free_image(ima); 
1279                                                 imb_freemipmapImBuf(ibuf);
1280                                         }
1281                                 }
1282                         }
1283                 }
1284                 
1285                 /* restore raytrace and AO */
1286                 if(event==RE_BAKE_AO)
1287                         if(prev_wo_amb_occ == 0)
1288                                 scene->world->mode &= ~WO_AMB_OCC;
1289
1290                 if(event==RE_BAKE_AO || active)
1291                         if(prev_r_raytrace == 0)
1292                                 scene->r.mode &= ~R_RAYTRACE;
1293                 
1294 // XXX          if(sculptmode) set_sculptmode();
1295                 
1296         }
1297 }
1298
1299 /* all selected meshes with UV maps are rendered for current scene visibility */
1300 static void objects_bake_render_ui(Scene *scene, short event)
1301 {
1302         char *error_msg = NULL;
1303 //      int is_editmode = (obedit!=NULL);
1304         
1305         /* Deal with editmode, this is a bit clunky but since UV's are in editmode, users are likely to bake from their */
1306 // XXX  if (is_editmode) exit_editmode(0);
1307         
1308         objects_bake_render(scene, event, &error_msg);
1309         
1310 // XXX  if (is_editmode) enter_editmode(0);
1311         
1312         if (error_msg)
1313                 error(error_msg);
1314 }
1315
1316 void objects_bake_render_menu(Scene *scene)
1317 {
1318         short event;
1319         
1320         event= pupmenu("Bake Selected Meshes %t|Full Render %x1|Ambient Occlusion %x2|Normals %x3|Texture Only %x4|Displacement %x5|Shadow %x6");
1321         if (event < 1) return;
1322         objects_bake_render_ui(scene, event);
1323 }
1324