So you are a Vb coder? If so, I would think that it wouldn’t be that much different coding in python than in Vb. There are some differences in python that would make coding like a Vb programmer difficult.
To my experience, Python’s object scope would be one hard point that I would have diffulty in. Usually, python’s program scope variables require a “global varname” when used within its functions. Python functions are also different. When defining functions in python, you don’t need something like a “end sub” or “end function” at the end of the function. Function declaration in Python goes like this…
The body of the function follows the line after the declaration and must be tabbed correctly.To end the function, simply leave a blank line.
Here’s an example…
#This is a comment python alternative to '
#This function defines the txtName_Change event
print "The new value for txtName is " + txtName.val
# ^^^ blank line ends the function.
About the gui… creating an event driven graphical user interface was easier than differentiating between Vb code and Python code. To achieve the structure neccesary for an event driven gui like Vb, you need the following…
Program init function - this is much like the form_load of vb6.
Program uninit function - this is much like the form_unload of vb6
Blender event handler - Has arguments (evt). Responds to user actions happening in gui.
keyboard and mouse event handler - Has argumants (evt,val). Responds to keyboard and mouse events.
gui function - used to render your guiUse your init function to start your program. Within the init function, use the Draw.Register(Gui,event,bevent) function. That function starts up your gui.
In your gui function, all gui elements such as text boxes and buttons are variables. define them as global first, then give them a value. Each gui variable would have an event number which would be passed to the blender event handler.
In your blender event handler, check to see which event happened and call your functions accordingly. Since python has no switch statement equivilent, use the if…elif…else statement.
As for the Keyboard and mouse event handler, the schematic is the same as the blender event handler.
Here’s the gui setup as a reference…
if evt == 1:
if evt == Draw.ESCKEY:
#These variables belong to the GUI of the program.
txtObjName = Draw.String("Object Name:",1,10,29,258,17,strObjName,20)
cmdExit = Draw.PushButton("Exit",10,94,192,81,33)
chkEnabled = Draw.Toggle("Enabled",11,10,200,89,17,blnEnabled)
#terminate gui interface
strObjName = txtObjName.val
blnEnabled = chkEnabled.val
#When all functions are defined, initialize the program...
I hope this post wasn’t too long. I also hope this would make your Vb -> Python experience much more enjoyable. Good luck with your endevor.