uvcalc_smart_project.py - fixed error in rare cases.
authorCampbell Barton <ideasman42@gmail.com>
Tue, 1 May 2007 09:40:10 +0000 (09:40 +0000)
committerCampbell Barton <ideasman42@gmail.com>
Tue, 1 May 2007 09:40:10 +0000 (09:40 +0000)
xfig_export.py - NMesh to Mesh, minor improvements/cleanup.
unweld.py - fixed error when a vert had no faces, also some small speedups and changes.

release/scripts/unweld.py
release/scripts/uvcalc_smart_project.py
release/scripts/xfig_export.py

index 89cf495181cd202895b1d14dc4a64fda55abf318..34877837bb733c9ffc2c998ec79343a1ecdfe72c 100644 (file)
@@ -100,7 +100,7 @@ SUBSURF=0
 DIM=Create(1.0)
 
 def  Buffer(v,t):
-       print dir(v)
+       if DEBUG : print dir(v)
        for n in range(len(v)): t[n]=t[n]+v[n]
        return t
 
@@ -138,7 +138,7 @@ def connectedFacesList(me,thegood):
        for f in me.faces:
                for v in f.v:
                        if v==thegood:
-                               if v.index not in listf2v.keys():
+                               if v.index not in listf2v: # .keys()
                                        listf2v[me.verts.index(v)]=[f]
                                elif f not in listf2v[me.verts.index(v)]:
                                        listf2v[me.verts.index(v)].append(f)
@@ -189,7 +189,7 @@ def collecte_edge(listf2v,me,thegood):
                        vlist = [0,1,2,0]
                else:
                        vlist = [0,1]
-               for i in range(len(vlist)-1):              
+               for i in xrange(len(vlist)-1):              
                        vert0 = min(face.v[vlist[i]].index,face.v[vlist[i+1]].index)
                        vert1 = max(face.v[vlist[i]].index,face.v[vlist[i+1]].index)              
                        edgeinlist = 0
@@ -204,16 +204,17 @@ def collecte_edge(listf2v,me,thegood):
                                        edge = [vert0,vert1,1,me.faces.index(face)]
                                        edgelist.append(edge)
                                        
-       for  edge in edgelist:
+       for i, edge in enumerate(edgelist):
                #print edge
                if len(edge)==4:
-                               del edgelist[edgelist.index(edge)]
+                       del edgelist[i]
                                
        edges=len(edgelist)
        if DEBUG : print 'number of edges : ',edges," Edge list : " ,edgelist    
        return edges, edgelist     
 
-OBJECT=Blender.Scene.GetCurrent().getActiveObject()
+import bpy
+OBJECT= bpy.data.scenes.active.objects.active
 
 if OBJECT and OBJECT.type=='Mesh':
        if OBJECT.getData(mesh=1).multires:
@@ -227,22 +228,20 @@ if OBJECT and OBJECT.type=='Mesh':
                result = Blender.Draw.PupMenu(name)
                if result:
                        me=OBJECT.getData()
-                       sole=0
-                       vSelection=[]
+                       
                        for v in me.verts:
-                               if v.sel==1:
-                                       vSelection.append(v)
-                       for v in  vSelection:
+                               if v.sel:
                                        thegood=v    
                                        if DEBUG : print thegood
                                        listf2v=connectedFacesList(me,thegood)
-                                       me=createAdditionalFace(me,thegood,listf2v)
-                                       #OBJECT.link(me)
-                                       me.update()
-                                       OBJECT.makeDisplayList()
+                                       if listf2v:
+                                               me=createAdditionalFace(me,thegood,listf2v)
+                                               #OBJECT.link(me)
+                                               me.update()
+                       
+                       OBJECT.makeDisplayList()
                        
                Blender.Window.EditMode(EDITMODE)
        
 else:
-       name = "Nothing to do! Did you select at least one vertex?"
-       result = Blender.Draw.PupMenu(name)
+       BPyMessages.Error_NoMeshActive()
index 7ec3afd77691fda07a5b8fa2657b8428fffd54fa..b10b69285e0db4b4de7b6fe9cdb610d42d00e62f 100644 (file)
@@ -644,6 +644,9 @@ def getUvIslands(faceGroups, me):
                faceGroupIdx-=1
                faces = faceGroups[faceGroupIdx]
                
+               if not faces:
+                       continue
+               
                # Build edge dict
                edge_users = {}
                
index ca6f571e24464c61bb6f2e3c95fa3865797d5370..10fdb1396fecfabeb19026592bb8f190df5c4c21 100644 (file)
@@ -1,8 +1,7 @@
 #!BPY
-
 """
 Name: 'xfig export (.fig)'
-Blender: 237
+Blender: 244
 Group: 'Export'
 Tooltip: 'Export selected mesh to xfig Format (.fig)'
 """
@@ -29,7 +28,7 @@ __bpydoc__ = """\
 # 'Raw triangle export' (Anthony D'Agostino, http://www.redrival.com/scorpius)|
 
 import Blender
-from Blender import NMesh, Draw, BGL
+from Blender import Draw, BGL
 from Blender.Window import DrawProgressBar
 #, meshtools
 import sys
@@ -40,8 +39,8 @@ import sys
 # =================================
 
 #globals definition and init
-mystring = ""
-mymsg = ""
+mystring = ''
+mymsg = ''
 toggle=0
 sel3files=0
 maxX=-1000000000
@@ -64,107 +63,57 @@ SpacePopup=0
 #end of globals definition
 
 
-def getmaxmin():
+def getmaxmin(ob, mesh):
        """Gets the max-min coordinates of the mesh"""
        
        global maxX,maxY,maxZ,minX,minY,minZ
        """Getting the extremes of the mesh to be exported"""
-       objects = Blender.Object.GetSelected()
-       objname = objects[0].name
-       meshname = objects[0].getData(name_only=1)
-       mesh = Blender.NMesh.GetRaw(meshname)
-       obj = Blender.Object.Get(objname)
-       #initializing max-min find.
-       # ...is there a standard python function to find those values?
-       face =mesh.faces[1]
-       if len(face.v)==3:
-               v1,v2,v3=face.v
-       if len(face.v)==4:
-               v1,v2,v3,v4=face.v
-       if len(face.v)==2: 
-               v1,v2=face.v
-       #is the next condition a nonsense for  a face? ...Anyway, to be sure....
-       if len(face.v)==1:
-               v1=face.v
        
-       maxX,maxY,maxZ  = v1
-       minX, minY, minZ = v1
+       maxX=maxY=maxZ = -1000000000
+       minX=minY=minZ =  1000000000
        
        for face in mesh.faces:
-           #if (face.flag & Blender.NMesh.FaceFlags['ACTIVE']):        
-                               if len(face.v) == 3:            # triangle
-                                       v1, v2, v3 = face.v
-                                       x1,y1,z1 = v1.co
-                                       x2,y2,z2 = v2.co
-                                       x3,y3,z3 = v3.co
-                                       maxX = max (maxX, x1, x2, x3)
-                                       maxY = max (maxY, y1, y2, y3)
-                                       maxZ = max (maxZ, z1, z2, z3)
-                                       minX = min (minX, x1, x2, x3)
-                                       minY = min (minY, y1, y2, y3)
-                                       minZ = min (minZ, z1, z2, z3)
-                               elif len(face.v)==2:
-                                       v1,v2=face.v
-                                       x1,y1,z1 = v1.co
-                                       x2,y2,z2 = v2.co
-                                       maxX = max (maxX, x1, x2)
-                                       maxY = max (maxY, y1, y2)
-                                       maxZ = max (maxZ, z1, z2)
-                                       minX = min (minX, x1, x2)
-                                       minY = min (minY, y1, y2)
-                                       minZ = min (minZ, z1, z2)
-                               elif len(face.v)==1:
-                                       v1=face.v
-                                       x1,y1,z1 = v1.co
-                                       maxX = max (maxX, x1)
-                                       maxY = max (maxY, y1)
-                                       maxZ = max (maxZ, z1)
-                                       minX = min (minX, x1)
-                                       minY = min (minY, y1)
-                                       minZ = min (minZ, z1)
-                               elif len(face.v)==4: 
-                                       v1,v2,v3,v4=face.v
-                                       x1,y1,z1 = v1.co
-                                       x2,y2,z2 = v2.co
-                                       x3,y3,z3 = v3.co
-                                       x4,y4,z4 = v4.co
-                                       maxX = max (maxX, x1, x2, x3, x4)
-                                       maxY = max (maxY, y1, y2, y3, y4)
-                                       maxZ = max (maxZ, z1, z2, z3, z4)
-                                       minX = min (minX, x1, x2, x3, x4)
-                                       minY = min (minY, y1, y2, y3, y4)
-                                       minZ = min (minZ, z1, z2, z3, z4)
+               for v in face:
+                       x,y,z = v.co
+                       maxX = max(maxX, x)
+                       maxY = max(maxY, y)
+                       maxZ = max(maxZ, z)
+                       minX = min(minX, x)
+                       minY = min(minY, y)
+                       minZ = min(minZ, z)
+
 def xfigheader():
        global export_type
-       print "#FIG 3.2  Produced by xfig version 3.2.5-alpha5"
-       print "Landscape"
-       print"Center"
+       print '#FIG 3.2  Produced by xfig version 3.2.5-alpha5'
+       print 'Landscape'
+       print 'Center'
        if boolmode==0:
-               print"Inches"
+               print 'Inches'
        else:
-               print"Metric"
+               print 'Metric'
        #print export_type
-       print"Letter"
-       print"100.00"
-       print"Single"
-       print "-2"
-       print "1200 2"
+       print 'Letter'
+       print '100.00'
+       print 'Single'
+       print '-2'
+       print '1200 2'
 
-def xytransform (face):
+def xytransform(face):
        """gives the face vertexes coordinates in the xfig format/translation (view xy)"""
        v4=None
        x4=y4=z4=None
-       if len(face.v)==3:
-         v1,v2,v3=face.v
-       else:
-         v1,v2,v3,v4=face.v    
+       if len(face)==3:
+               v1,v2,v3=face.v
+       else:
+               v1,v2,v3,v4=face.v      
+       
        x1,y1,z1 = v1.co
        x2,y2,z2 = v2.co
        x3,y3,z3 = v3.co
        y1=-y1
        y2=-y2
        y3=-y3
-       if v4 !=None:
+       if v4:
                x4,y4,z4 = v4.co
                y4=-y4
        return x1,y1,z1,x2,y2,z2,x3,y3,z3,x4,y4,z4 
@@ -173,10 +122,10 @@ def xztransform(face):
        """gives the face vertexes coordinates in the xfig format/translation (view xz)"""
        v4=None
        x4=y4=z4=None
-       if len(face.v)==3:
-         v1,v2,v3=face.v
-       else:
-         v1,v2,v3,v4=face.v    
+       if len(face)==3:
+               v1,v2,v3=face.v
+       else:
+               v1,v2,v3,v4=face.v      
 
        #Order vertexes
        x1,y1,z1 = v1.co
@@ -190,7 +139,7 @@ def xztransform(face):
        z2=-z2+maxZ-minY +space
        z3=-z3+maxZ-minY +space
 
-       if v4 !=None:
+       if v4:
                x4,y4,z4 = v4.co
                y4=-y4
                z4=-z4+maxZ-minY +space 
@@ -200,219 +149,214 @@ def yztransform(face):
        """gives the face vertexes coordinates in the xfig format/translation (view xz)"""
        v4=None
        x4=y4=z4=None
-       if len(face.v)==3:
-         v1,v2,v3=face.v
-       else:
-         v1,v2,v3,v4=face.v    
+       if len(face)==3:
+               v1,v2,v3=face.v
+       else:
+               v1,v2,v3,v4=face.v      
 
        #Order vertexes
        x1,y1,z1 = v1.co
        x2,y2,z2 = v2.co
        x3,y3,z3 = v3.co
-       y1=-y1
-       y2=-y2
-       y3=-y3
+       y1=-y1; y2=-y2; y3=-y3
        z1=-(z1-maxZ-maxX-space)
        z2=-(z2-maxZ-maxX-space)
        z3=-(z3-maxZ-maxX-space)
-
-       if v4 !=None:
+       
+       if v4:
                x4,y4,z4 = v4.co
                y4=-y4
                z4=-(z4-maxZ-maxX-space)
        return x1,y1,z1,x2,y2,z2,x3,y3,z3,x4,y4,z4 
 
-def figdata(expview):
+def figdata(ob, expview):
        """Prints all the xfig data (no header)"""
-       objects = Blender.Object.GetSelected()
-       objname = objects[0].name
-       meshname = objects[0].getData(name_only=1)
-       mesh = Blender.NMesh.GetRaw(meshname)
-       obj = Blender.Object.Get(objname)
+       mesh = ob.getData(mesh=1)
        facenumber = len(mesh.faces)
        for face in mesh.faces:
-                       if len(face.v) == 3:            # triangle
-                               print "2 3 0 1 0 7 50 -1 -1 0.000 0 0 -1 0 0 4"
-                               if expview=="xy":
-                                       x1,y1,z1,x2,y2,z2,x3,y3,z3,x4,y4,z4=xytransform(face)
+                       if len(face) == 3:              # triangle
+                               print '2 3 0 1 0 7 50 -1 -1 0.000 0 0 -1 0 0 4'
+                               if expview=='xy':
+                                       x1,y1,z1,x2,y2,z2,x3,y3,z3,x4,y4,z4=xytransform(face)
                                        faceverts = int(x1*scale),int(y1*scale),int(x2*scale),int(y2*scale),int(x3*scale),int(y3*scale), int(x1*scale),int(y1*scale)
-                               elif expview=="xz":
+                               elif expview=='xz':
                                        x1,y1,z1,x2,y2,z2,x3,y3,z3,x4,y4,z4=xztransform(face)
                                        faceverts = int(x1*scale),int(z1*scale),int(x2*scale),int(z2*scale),int(x3*scale),int(z3*scale), int(x1*scale),int(z1*scale)
-                               elif expview=="yz":
+                               elif expview=='yz':
                                        x1,y1,z1,x2,y2,z2,x3,y3,z3,x4,y4,z4=yztransform(face)
                                        faceverts = int(z1*scale),int(y1*scale),int(z2*scale),int(y2*scale),int(z3*scale),int(y3*scale),int(z1*scale),int(y1*scale)
-                               print "\t% i % i % i % i % i % i % i % i" % faceverts
-                       else:
-                               if len(face.v) == 4: #quadrilateral     
-                                       print "2 3 0 1 0 7 50 -1 -1 0.000 0 0 -1 0 0 5"
-                                       if expview=="xy":
-                                         x1,y1,z1,x2,y2,z2,x3,y3,z3,x4,y4,z4=xytransform(face)
-                                         faceverts = int(x1*scale),int(y1*scale),int(x2*scale),int(y2*scale),int(x3*scale),int(y3*scale),int(x4*scale),int(y4*scale), int(x1*scale),int(y1*scale)
-
-                                       elif expview=="xz":
-                                         x1,y1,z1,x2,y2,z2,x3,y3,z3,x4,y4,z4=xztransform(face)
-                                         faceverts = int(x1*scale),int(z1*scale),int(x2*scale),int(z2*scale),int(x3*scale),int(z3*scale),int(x4*scale),int(z4*scale), int(x1*scale),int(z1*scale)
-
-                                       elif expview=="yz":
-                                         x1,y1,z1,x2,y2,z2,x3,y3,z3,x4,y4,z4=yztransform(face)
-                                         faceverts = int(z1*scale),int(y1*scale),int(z2*scale),int(y2*scale),int(z3*scale),int(y3*scale),int(z4*scale),int(y4*scale), int(z1*scale),int(y1*scale)
-                                       print "\t% i % i % i % i % i % i % i % i % i % i" % faceverts
-                               else: 
-                                       pass #it should not occour, but....
-
-
-def writexy(filename):
+                               print '\t% i % i % i % i % i % i % i % i' % faceverts
+                       else: # Quad
+                               print '2 3 0 1 0 7 50 -1 -1 0.000 0 0 -1 0 0 5'
+                               if expview=='xy':
+                                       x1,y1,z1,x2,y2,z2,x3,y3,z3,x4,y4,z4=xytransform(face)
+                                       faceverts = int(x1*scale),int(y1*scale),int(x2*scale),int(y2*scale),int(x3*scale),int(y3*scale),int(x4*scale),int(y4*scale), int(x1*scale),int(y1*scale)
+
+                               elif expview=='xz':
+                                       x1,y1,z1,x2,y2,z2,x3,y3,z3,x4,y4,z4=xztransform(face)
+                                       faceverts = int(x1*scale),int(z1*scale),int(x2*scale),int(z2*scale),int(x3*scale),int(z3*scale),int(x4*scale),int(z4*scale), int(x1*scale),int(z1*scale)
+
+                               elif expview=='yz':
+                                       x1,y1,z1,x2,y2,z2,x3,y3,z3,x4,y4,z4=yztransform(face)
+                                       faceverts = int(z1*scale),int(y1*scale),int(z2*scale),int(y2*scale),int(z3*scale),int(y3*scale),int(z4*scale),int(y4*scale), int(z1*scale),int(y1*scale)
+                               print '\t% i % i % i % i % i % i % i % i % i % i' % faceverts
+
+
+
+def writexy(ob, filename):
        """writes the x-y view file exported"""
        global maxX, maxY, maxZ
        global minX, minY, minZ
        global space
        global scale
        #start = time.clock()
-       file = open(filename, "wb")
+       file = open(filename, 'wb')
        std=sys.stdout
        sys.stdout=file
        xfigheader()
-       figdata("xy")# xydata()
+       figdata(ob, 'xy')# xydata()
        sys.stdout=std
        Blender.Window.DrawProgressBar(1.0, '')  # clear progressbar
        file.close()
        #end = time.clock()
        #seconds = " in %.2f %s" % (end-start, "seconds")
-       message = "Successfully exported " + Blender.sys.basename(filename)# + seconds
-       print message 
+       print 'Successfully exported ', Blender.sys.basename(filename)# + seconds
 
 def writexz(filename):
        """writes the x-z view file exported"""
        global space,maxX,maxY,maxZ, scale
        #start = time.clock()
-       file = open(filename, "wb")
+       file = open(filename, 'wb')
        std=sys.stdout
        sys.stdout=file
        xfigheader()
-       figdata("xz")#xzdata()
+       figdata(ob, 'xz')#xzdata()
        sys.stdout=std
        Blender.Window.DrawProgressBar(1.0, '')  # clear progressbar
        file.close()
        #end = time.clock()
        #seconds = " in %.2f %s" % (end-start, "seconds")
-       message = "Successfully exported " + Blender.sys.basename(filename)# + seconds
-       print message 
+       print 'Successfully exported ', Blender.sys.basename(filename)# + seconds
 
 def writeyz(filename):
        """writes the y-z view file exported"""
        global maxX, maxY, maxZ, minX, minY, minZ,scale
        #start = time.clock()
-       file = open(filename, "wb")
+       file = open(filename, 'wb')
 
        std=sys.stdout
        sys.stdout=file
 
        xfigheader()
-       figdata("yz")#yzdata()  
+       figdata(ob, 'yz')#yzdata()      
        sys.stdout=std
        Blender.Window.DrawProgressBar(1.0, '')  # clear progressbar
        file.close()
        #end = time.clock()
        #seconds = " in %.2f %s" % (end-start, "seconds")
-       message = "Successfully exported " + Blender.sys.basename(filename)# + seconds
-       print message 
+       print 'Successfully exported ', Blender.sys.basename(filename)# + seconds
 
-def writeall(filename):
+def writeall(ob, filename):
        """writes all 3 views
        
        Every view is a combined object in the resulting xfig. file."""
        global maxX, maxY, maxZ, minX, minY, minZ,scale
        #start = time.clock()
-       file = open(filename, "wb")
+       file = open(filename, 'wb')
 
        std=sys.stdout
        sys.stdout=file
 
        xfigheader()
-       print "#upper view (7)"
-       print "6 % i % i % i % i ",  minX, minY, maxX, maxY
-       figdata("xy") #xydata()
-       print "-6"
-       print "#bottom view (1)"
-       print "6 %i %i %i %i", minX, -minZ+maxZ-minY +space, maxX,-maxZ+maxZ-minY +space
-       figdata ("xz") #xzdata()
-       print "-6"
+       print '#upper view (7)'
+       print '6 % i % i % i % i ',  minX, minY, maxX, maxY
+       figdata(ob, 'xy') #xydata()
+       print '-6'
+       print '#bottom view (1)'
+       print '6 %i %i %i %i', minX, -minZ+maxZ-minY +space, maxX,-maxZ+maxZ-minY +space
+       figdata(ob, 'xz') #xzdata()
+       print '-6'
        
-       print "#right view (3)"
-       print "6 %i %i %i %i", minX, minZ-maxZ-maxX-space, maxX,maxZ-maxZ-maxX-space
-       figdata ("yz") #yzdata()        
-       print "-6"
+       print '#right view (3)'
+       print '6 %i %i %i %i', minX, minZ-maxZ-maxX-space, maxX,maxZ-maxZ-maxX-space
+       figdata(ob, 'yz') #yzdata()     
+       print '-6'
        
        sys.stdout=std
        Blender.Window.DrawProgressBar(1.0, '')  # clear progressbar
        file.close()
        #end = time.clock()
        #seconds = " in %.2f %s" % (end-start, "seconds")
-       message = "Successfully exported " + Blender.sys.basename(filename)# + seconds
+       print 'Successfully exported ', Blender.sys.basename(filename)# + seconds
 
 
 #********************************************************USER INTERFACE*****************************************************
 #********************************************************USER INTERFACE*****************************************************
 #********************************************************USER INTERFACE*****************************************************
 def gui():              # the function to draw the screen
-  global mystring, mymsg, toggle, sel3files, scale 
-  global guiret1, guiret2, guiret3, guiret4, guiret5, guiret6, guiret7
-  global ScalePopup, SpacePopup, boolmode, guiscale,hidden_flag
-  if len(mystring) > 90: mystring = ""
-  BGL.glClearColor(0,0,1,1)
-  BGL.glClear(BGL.GL_COLOR_BUFFER_BIT)
-  BGL.glColor3f(1,1,1)
-  guiret2=Draw.PushButton("Cancel", 2, 10, 10, 55, 20,"Cancel")
-  guiret3=Draw.Toggle("1 file per view",    3, 10, 40, 110,20, sel3files, "3 files")
-  guiret4=Draw.PushButton("Export", 4, 70, 10, 70, 20, "Select filename and export") 
-
-  ScalePopup=Draw.Number("Scale", 5, 10,70, 110,20, guiscale, 0.0001, 1000.1, "Scaling factor")
-  SpacePopup=Draw.Number("Space", 6, 10,90, 110,20, space, 0, 10000, "Space between projections")
-  
-  guiret5=Draw.Toggle("cm", 7, 120,70, 40,20, boolmode, "set scale to 1 blender unit = 1 cm in xfig")
-  guiret6=Draw.Toggle("in", 8, 162,70, 40,20, not boolmode, "set scale to 1 blender unit = 1 in in xfig")
-#  guiret7 = guiret6=Draw.Toggle("only visible", 9, 120,90, 82,20, hidden_flag, "hidden faces")
-
-
-  BGL.glRasterPos2i(72, 16)
-  if toggle: toggle_state = "down"
-  else: toggle_state = "up"
-  #Draw.Text("The toggle button is %s." % toggle_state, "small")
-  BGL.glRasterPos2i(10, 230)
-  #Draw.Text("Type letters from a to z, ESC to leave.")
-  BGL.glRasterPos2i(20, 200)
-  Draw.Text(mystring)
-  BGL.glColor3f(1,0.4,0.3)
-  BGL.glRasterPos2i(340, 70)
-  Draw.Text(mymsg, "tiny")
+       global mystring, mymsg, toggle, sel3files, scale 
+       global guiret1, guiret2, guiret3, guiret4, guiret5, guiret6, guiret7
+       global ScalePopup, SpacePopup, boolmode, guiscale,hidden_flag
+       if len(mystring) > 90: mystring = ''
+       # BGL.glClearColor(0,0,1,1)
+       # BGL.glClear(BGL.GL_COLOR_BUFFER_BIT)
+       BGL.glColor3f(1,1,1)
+       guiret2=Draw.PushButton('Cancel', 2, 10, 10, 55, 20,'Cancel')
+       guiret3=Draw.Toggle('1 file per view',    3, 10, 40, 110,20, sel3files, 'Export a file for each view')
+       guiret4=Draw.PushButton('Export', 4, 70, 10, 70, 20, 'Select filename and export') 
+
+       ScalePopup=Draw.Number('Scale', 5, 10,70, 110,20, guiscale, 0.0001, 1000.1, 'Scaling factor')
+       SpacePopup=Draw.Number('Space', 6, 10,90, 110,20, space, 0, 10000, 'Space between projections')
+
+       guiret5=Draw.Toggle('cm', 7, 120,70, 40,20, boolmode, 'set scale to 1 blender unit = 1 cm in xfig')
+       guiret6=Draw.Toggle('in', 8, 162,70, 40,20, not boolmode, 'set scale to 1 blender unit = 1 in in xfig')
+
+       BGL.glRasterPos2i(72, 16)
+       if toggle: toggle_state = 'down'
+       else: toggle_state = 'up'
+       #Draw.Text('The toggle button is %s.' % toggle_state, 'small')
+       BGL.glRasterPos2i(10, 230)
+       #Draw.Text('Type letters from a to z, ESC to leave.')
+       BGL.glRasterPos2i(20, 200)
+       Draw.Text(mystring)
+       BGL.glColor3f(1,0.4,0.3)
+       BGL.glRasterPos2i(340, 70)
+       Draw.Text(mymsg, 'tiny')
 
 
 def event(evt, val):    # the function to handle input events
-  Draw.Redraw(1)
+       Draw.Redraw(1)
+
+import bpy
 
 def button_event(evt):  # the function to handle Draw Button events
        global toggle, guiret5,scale, space, SpacePopup, boolmode, dimscale, guiscale
        global hidden_flag, sel3files
        if evt==1:
-         toggle = 1 - toggle
-         Draw.Redraw(1)
+               toggle = 1 - toggle
+               Draw.Redraw(1)
        if evt==2:
-         Draw.Exit()
-         return
+               Draw.Exit()
+               return
        if evt==3:
-         sel3files = 1-sel3files
-         Draw.Redraw(1)        
-       if evt==4:      
-         Blender.Window.FileSelector(fs_callback, "Export fig")
-         Draw.Exit()
-         return 
+               sel3files = 1-sel3files
+               Draw.Redraw(1)  
+       if evt==4:
+               try:    ob = bpy.data.scenes.active.objects.active
+               except: ob = None
+               if not ob or ob.type != 'Mesh':
+                       BPyMessages.Error_NoMeshActive()
+                       return
+               
+               Blender.Window.FileSelector(fs_callback, 'Export fig', Blender.sys.makename(ext='.fig'))
+               Draw.Exit()
+               return 
        if evt==5:
-         guiscale = ScalePopup.val
-         scale=dimscale*guiscale
-         Draw.Redraw(1)
+               guiscale = ScalePopup.val
+               scale=dimscale*guiscale
+               Draw.Redraw(1)
        if evt==6:
-         space =SpacePopup.val
+               space =SpacePopup.val
        if evt==7: 
                boolmode=1
                dimscale=450 #converting to cm
@@ -429,13 +373,24 @@ def button_event(evt):  # the function to handle Draw Button events
 
 Draw.Register(gui, event, button_event)  # registering the 3 callbacks
        
+import BPyMessages
 def fs_callback(filename):
-       if filename.find('.fig', -4) > 0: filename = filename[:-4]
-       getmaxmin()
+       if filename.lower().endswith('.fig'): filename = filename[:-4]
+       
+       try:    ob = bpy.data.scenes.active.objects.active
+       except: ob = None
+       if not ob or ob.type != 'Mesh':
+               BPyMessages.Error_NoMeshActive()
+               return
+       
+       mesh = ob.getData(mesh=1)
+       getmaxmin(ob, mesh)
+       
        if sel3files:
-               writexy(filename+"_XY.fig")
-               writexz(filename+"_XZ.fig")
-               writeyz(filename+"_YZ.fig")
-       writeall(filename+"_ALL.fig")
+               
+               writexy(ob, filename + '_XY.fig')
+               writexz(ob, filename + '_XZ.fig')
+               writeyz(ob, filename + '_YZ.fig')
+       writeall(ob, filename + '.fig')
        print  scale
        Draw.Exit()