Script GUI writing tutorial?

Hi

Is there tutorial for the python script GUI writing?

I’m not sure if this possible to do at (composed picture):
http://www.warezhouze.1g.fi/kuvat/Blender_python/Menu.png

I would like to create a GUI, which is listing the available Blender text objects and allow user to select/unselect the objects.
According to the selection, the script will create the list of the names. Example: tobj = [‘Text’, ‘Text.001’]

http://www.warezhouze.1g.fi/kuvat/Blender_python/Menu.png

hi Xjazz
for my current projects (my signature) I deal intensive with GUI, but I don’t know any GUI tutorial.
Try to analyze existing scripts and read API docu: http://www.zoo-logique.org/3D.Blender/scripts_python/API/
GUI stuff: http://www.zoo-logique.org/3D.Blender/scripts_python/API/toc-Draw-module.html

migius

Thanks for the advices, migius :slight_smile:

So far I managed to modify the sample codes a bit:

#! BPY
import Blender
from Blender import Draw, BGL, Text

toggle = tgb = tcnt = 0
tobj_lst=[]

## Create a list of the Text objects
text_obj = Text.Get()
for i in text_obj:
    print i
    i = str(i)
    tobj = i[(i.find('"')+1):i.rfind('"')]
    if tobj.endswith('.py'):
        tobj_lst.append(tobj)
        tcnt = tcnt +1
## Create a list of the Text objects

def event(evt, val):        # the function to handle input events
    if evt == Draw.ESCKEY:
        Draw.Exit()         # exit when user presses ESC
        return
    else: return # no need to redraw if nothing changed
    Draw.Redraw(1)

def button_event(evt):    # the function to handle Draw Button events
    global mymsg, toggle
    if evt == 1:
        mymsg = "You pressed the toggle button."
        toggle = 1 - toggle
        Draw.Redraw(1)

def gui():        # the function to draw the screen
    global toggle, tobj_lst, tgb, tcnt
    BGL.glClearColor(0,0,1,1)
    BGL.glClear(BGL.GL_COLOR_BUFFER_BIT)
    BGL.glColor3f(1,1,1)
    BGL.glRasterPos2i(10, 230)
    c = 10
    r = 230
    
    for t in tobj_lst:    # Draw buttons and Text object names
        cnt = len(t)
        if tgb < tcnt:
            tgb = tgb +1
            r = r + 20
            BGL.glRasterPos2i(80, r)
            Draw.Toggle("Select", tgb, 10, r-2, 55, 15, toggle,"A select button")
            Draw.Text(t, 'large')

Draw.Register(gui, event, button_event)

Sample .blend file:
http://www.warezhouze.1g.fi/Here/Blender3d/Samples/MENU_02.blend
The menü still missing some buttons, like ‘Select/Unselect All’ toggle and ‘Proceed’.

For some reason the whole selection list disappear if I click the last button. Why so?

How I can write the selected names to the parameter? This is planned to do with ‘Proceed’ button.

Any help?

Hello,

you have a very simple problem.
In the if-statement in the for loop you are checking for tgb < tcnt and
if it is true you add 1 to tgb, but you never reset the tgb value.
So everytime the gui is redrawn the value is incremented.
(you can see this if you simply print the tgb value)

To solve add:
tgb = 0
above the for-statement.
tgb = 0for t in tobj_lst: # Draw buttons and Text object names

Thank your for your advices

My part time coding path really is a ‘Learn one and forget two’ way :o

I managed to get buttons to the places and working as I want. Anyhow It seems like I need to create a ‘case’ to the Button_Event function for every and each possible file (up to the designed limit). I wonder if ‘object oriented’ coding could solve this in more efficient manner.

#! BPY
import Blender
from Blender import Draw, BGL, Text

toggle = tgb = tcnt = 0
tobj_lst=[]

## Create a list of the Text objects, which are  ending with' .py'
text_obj = Text.Get()
for i in text_obj:
    i = str(i)
    tobj = i[(i.find('"')+1):i.rfind('"')]
    if tobj.endswith('.py'):
        tobj_lst.append(tobj)
        tcnt = tcnt +1
## Create a list of the Text objects, which are  ending with' .py' / END

def event(evt, val):        # the function to handle input events
    if evt == Draw.ESCKEY:
        Draw.Exit()         # exit when user presses ESC
        return
    else: return # no need to redraw if nothing changed
    Draw.Redraw(1)

def button_event(evt):    # the function to handle Draw Button events
    # code can only handle a seven .py files...
    if evt == 7:
        print evt, F7
    if evt == 6:
        print evt, F6    
    if evt == 5:
        print evt, F5
    if evt == 4:
        print evt, F4
    if evt == 3:
        print evt, F3
    if evt == 2:
        print evt, F2
    if evt == 1:
        print evt, F1
    if evt == 0:
        print 'Just a test'

def gui():        # the function to draw the screen
    global toggle, tobj_lst, tgb, tcnt, F1, F2, F3, F4, F5, F6, F7
    BGL.glClearColor(0,0,1,1)
    BGL.glClear(BGL.GL_COLOR_BUFFER_BIT)
    BGL.glColor3f(1,1,1)
    
    Draw.PushButton("Print", 0, 10,450, 55, 15)
    BGL.glRasterPos2i(80, 452)
    Draw.Text('Print "Just a test" text', 'large')
    BGL.glRasterPos2i(10, 230)
    
    r = 230
    tgb=1
    for t in tobj_lst:    # Draw buttons and Text object names
        # Variable generating
        exec '%s = ("%s")' % ("f"+str(tgb), t)
        # Passing to the global variables
        try:
            F1 = f1
            F2 = f2
            F3 = f3
            F4 = f4
            F5 = f5
            F6 = f6
            F7 = f7
        except NameError:
            pass
        
        if tgb &lt;= tcnt:
            r = r + 20
            exec '%s = ("%s")' % ("f"+str(tgb), t)
            BGL.glRasterPos2i(80, r)
            Draw.Toggle("Select", tgb, 10, r-2, 55, 15, toggle,"A select button")
            Draw.Text(t, 'large')
            tgb = tgb +1

print '
'*2
Draw.Register(gui, event, button_event)

A sample .blend file:
www.warezhouze.1g.fi/Here/Blender3d/Samples/MENU_03.blend

Okey…

One major problem rise-up. I can’t get the toggle (toggle named parameter) button down/up detection working and I a bit lost with this now.

My target is to use this menu for the moded AC3D batch export file selection.

Look at this python script from Campbell: http://wiki.blender.org/index.php/Sc…#Image_Browser.
There is the same thing with multiple toggle buttons.
I use it in Image Browser too.

edit: i’ve checked your MENU_03.blend.
follow the Blender script convention: for indenting use tabulators(=4spaces) instead of spaces(some of test scripts in your blendfile have this problem)