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