[Bf-committers] exppython OS X

Willian Padovani Germano bf-committers@blender.org
14 Jun 2003 07:01:08 -0300


--=-USy+OpUCBUiQrvyHxVeo
Content-Type: text/plain
Content-Transfer-Encoding: 7bit

Here's the attachment.  Argh... 

Mental note: attach first, and then write about it ...

> I'm attaching a piece of script that implements a simple file selector,
> using Draw and BGL.  All this piece does is let you choose a file in a
> blue screen, then it complains about it in a red screen.  If you got
> that, it's working fine. 

--
Willian, wgermano@ig.com.br

--=-USy+OpUCBUiQrvyHxVeo
Content-Disposition: attachment; filename=simple-filesel.py
Content-Transfer-Encoding: quoted-printable
Content-Type: text/x-python; charset=ISO-8859-1

# Clumsy File Selector=0D
=0D
HAS_MODULE_OS =3D 1=0D
myfile =3D ""=0D
doffset =3D 0=0D
errmsg =3D ""=0D
=0D
try:=0D
    import os=0D
except ImportError:=0D
    print """=0D
ImportError: No module named os.=0D
   =20=0D
    You don't have Python installed or your PYTHONPATH system=0D
    variable is not correctly set: you will have to manually type the name=0D
    of the file you want to load (including full path). Note: use the NUMPA=
D=0D
    SLASH key, PAD "/", in case the normal "\\" and "/" don't work.=0D
=0D
    If you have Python installed, simply check the comments at the beginnin=
g=0D
    of this very script to find a link that explains how to set PYTHONPATH.=0D
    """=0D
    errmsg =3D "ImportError, check the Blender shell window."=0D
    HAS_MODULE_OS =3D 0=0D
=0D
from Blender.BGL import *=0D
from Blender.Draw import *=0D
=0D
class MenuStr:=0D
=0D
    def __init__(self):=0D
        self.curdir =3D ""=0D
        self.dlist =3D []=0D
        self.flist =3D []=0D
        self.update()=0D
       =20=0D
    def update(self):=0D
        global doffset, myfile=0D
        self.dlist =3D []=0D
        self.flist =3D []=0D
        self.curdir =3D os.getcwd()=0D
        self.dlist.append(os.pardir)=0D
        for f in os.listdir(self.curdir):=0D
            if os.path.isdir(f):=0D
                self.dlist.append(f)=0D
            elif os.path.isfile(f):=0D
                self.flist.append(f)=0D
        doffset =3D len(self.flist)=0D
        if self.flist: myfile =3D self.flist[0]=0D
       =20=0D
    def getMenuStr(self):=0D
        global doffset=0D
        n =3D 1=0D
        s =3D self.curdir=0D
        s +=3D "%t|"=0D
        for d in self.dlist:=0D
            if len(d) > 15: d =3D d[:12] + '...'=0D
            s +=3D d + os.sep + "%x" + str(doffset + n) + "|"=0D
            n +=3D 1=0D
        n =3D 1=0D
        for f in self.flist:=0D
            if len(f) > 15: f =3D f[:11] + "~.ac"=0D
            s +=3D f + "%x" + str(n) + "|"=0D
            n +=3D 1=0D
        return s=0D
=0D
# End of Class MenuStr=0D
=0D
if HAS_MODULE_OS: mstr =3D MenuStr()=0D
menu =3D 0=0D
default =3D 1=0D
errorgui =3D 0=0D
=0D
def gui():=0D
    global menu, default, myfile, errmsg, HAS_MODULE_OS, errorgui=0D
=0D
    if errorgui:=0D
        glClearColor(1,0,0,1)=0D
        glClear(GL_COLOR_BUFFER_BIT)=0D
        glColor3f(1,1,1)=0D
        glRasterPos2i(25, 230)=0D
        Text("Failed trying to import file %s :" % myfile)=0D
        glBegin(GL_LINES)=0D
        glVertex2i(23, 221)=0D
        glVertex2i(229, 221)=0D
        glEnd()=0D
        glRasterPos2i(30, 185)=0D
        Text("- %s" % errmsg)=0D
        glRasterPos2i(290, 110)=0D
        Text("Did you know? The FileSelector is back! That's my end.")=0D
        glRasterPos2i(25, 40)=0D
        Text("Press ESC or a mouse button to leave. "=0D
             "But they can't take this nice red tone away from me.")=0D
        Redraw(1)=0D
=0D
    else:=0D
        glClearColor(0,0,1,1)=0D
        glClear(GL_COLOR_BUFFER_BIT)=0D
        glColor3f(1,1,1)=0D
        glRasterPos2i(10, 230)=0D
        Text("AC3D Importer for Blender 2.25")=0D
        glBegin(GL_LINES)=0D
        glVertex2i(10, 220)=0D
        glVertex2i(186, 220)=0D
        glEnd()=0D
        glRasterPos2i(10, 30)=0D
        Text("Or press ESC to cancel. ")=0D
        if errmsg:=0D
            glColor3f(1, 0.4, 0.4)=0D
            glRasterPos2i(10, 12)=0D
            Text(errmsg)=0D
            glColor3f(1,1,1)=0D
        glRasterPos2i(10, 180)=0D
        if HAS_MODULE_OS:=0D
            Text("Choose an AC3D file to import:")=0D
            menu =3D Menu(mstr.getMenuStr(),2,60,120,200,40,default,=0D
                        "File Selector.")=0D
        else:=0D
            Text("Just type the AC3D filename. Then press Import.") =20=0D
            strfield =3D String(myfile, 3, 60, 120, 300, 30, "", 300,=0D
                              "Just type. No need to click here.")=0D
        if myfile =3D=3D "":=0D
            bmsg =3D "No file selected."=0D
        else: bmsg =3D "Import %s" % myfile=0D
        Button(bmsg, 1, 300, 50, len(bmsg) + 200, 50,=0D
                    "Click to import the selected AC3D file.")=0D
=0D
shiftkey =3D 0=0D
=0D
def ev(evt, val):=0D
    global myfile, shiftkey, errorgui=0D
=0D
    if not val:=0D
        if evt =3D=3D LEFTSHIFTKEY or evt =3D=3D RIGHTSHIFTKEY: shiftkey =
=3D 0=0D
        return=0D
=0D
    if errorgui:=0D
        if evt =3D=3D ESCKEY or evt =3D=3D LEFTMOUSE or evt =3D=3D MIDDLEMO=
USE \=0D
                         or evt =3D=3D RIGHTMOUSE:=0D
          Exit()=0D
        else:=0D
          Register(gui, ev, bev)=0D
          return=0D
=20=0D
    if evt =3D=3D ESCKEY:=0D
        Exit()=0D
        return=0D
    elif evt =3D=3D LEFTSHIFTKEY or evt =3D=3D RIGHTSHIFTKEY:=0D
        shiftkey =3D 1=0D
    elif AKEY <=3D evt <=3D ZKEY or ZEROKEY <=3D evt <=3D NINEKEY:=0D
        myfile +=3D chr(evt)=0D
    elif PAD0 <=3D evt <=3D PAD9:=0D
        myfile +=3D chr(ZEROKEY + evt - PAD0)=0D
    elif evt =3D=3D SPACEKEY:=0D
        myfile +=3D ' '=0D
    elif evt =3D=3D BACKSLASHKEY or evt =3D=3D SLASHKEY or evt =3D=3D PADSL=
ASHKEY:=0D
        myfile +=3D '/'=0D
    elif evt =3D=3D SEMICOLONKEY:=0D
        if shiftkey: myfile +=3D ':'=0D
        else: myfile +=3D ';'=0D
    elif evt =3D=3D MINUSKEY:=0D
        if shiftkey: myfile +=3D '_'=0D
        else: myfile +=3D '-'=0D
    elif evt =3D=3D PERIODKEY:=0D
        myfile +=3D '.'=0D
    elif evt =3D=3D BACKSPACEKEY and myfile:=0D
        myfile =3D myfile[:-1]=0D
    elif evt =3D=3D RETKEY or evt =3D=3D PADENTER:=0D
        pass=0D
    else: return=0D
    Register(gui, ev, bev)=0D
=0D
def bev(evt):=0D
    global myfile, errmsg, doffset, default, HAS_MODULE_OS, errorgui=0D
    if evt =3D=3D 2 and HAS_MODULE_OS:=0D
        default =3D menu.val=0D
        if default > doffset:=0D
            folder =3D mstr.dlist[default - doffset - 1]=0D
            default =3D 1=0D
            myfile =3D ""=0D
            os.chdir(folder)=0D
            mstr.update()=0D
        elif default >=3D 0:=0D
            myfile =3D mstr.flist[default - 1]=0D
    elif evt =3D=3D 1:=0D
        if myfile !=3D "":=0D
            errmsg =3D "Sorry, that's the wrong file, you lost."=0D
            errmsg +=3D " You just can't win. Really."=0D
            if errmsg:=0D
                errorgui =3D 1=0D
            else:=0D
                Exit()=0D
    else:=0D
        Register(gui, ev, bev)=0D
=0D
Register(gui,ev,bev)=0D

--=-USy+OpUCBUiQrvyHxVeo--