Hello. I’ve been using Python for some time now, I’ve also used Blender 2.4 API and I must say the GUI part of the new Blender API is nothing I’ve ever seen. I read the docs and I still don’t get the deal.
Can anyone explain me what gives? I’ve also used other libraries like wxPython and this is honestly the first API I see that requires you to create a class for GUI elements.
In Blender 2.4, you could just do “filename = Blender.Window.FileSelector” to open a file browser. In wxPython GUI library it’s similar: "filename = wx.FileSelector(“Description”).
How can you open a file browser in the new API? I’ve seen some codes making subclasses of Panel which none works with 2.6, btw.
Can anyone post a small snippet which opens a file browser please?
I’ve been searching for info the last hour and honestly I am still clueless.
this class includes a execute function to execute the operator you’ve made and a draw class to draw buttons.
class operotor(bpy.types.Operator):
bl_idname = "object.operator" # skip the bpy.ops part, blender does that for you
bl_label = "execute an operator
def execute(self,context):
#python commands here
return {'FINISHED}
def draw(self,context):
row = self.layout.row
row.prop("object.shade_smooth", shade smooth)
after that you need to register your class, look at an addon,scroll to the bottom and copy everything from the register function till the bottom, changing the class names to the ones in your file.
The API references simply briefly says what Operators or Panels can be used for and then lists all of it’s attributes. I’ve seen them and I still don’t get why they are needed (that is, why there are separate and not part of other things), or how they can be used to create a file browser. I wouldn’t complain it should, it’s an API reference (list of everything), not a manual. Even though I’m pretty frustrated that they made the API more complex from 2.4 , previously it was just a function call and I had no difficulties using it. But I know very little, so I’ll assume it’s for the better. Still, I’m not getting the whole deal.
I’ve had a look at the example codes, they are poorly documented to be used as tutorials (though enough for real code).
I’ve had a look at the API reference like I said, not much help…
And I’ve also have seen the cookbook before (the pdf version from the blenderartists topic).
the basics is that you have a class for example…
OK, thanks for telling what is basically needed.
But I still don’t get what is exactly needed to put in the execute() and draw() functions.
I don’t even understand the difference between Panel inherited classes and Operator inherited classes.
(I would also assume that as file browsers are used so often, there would be a helper function or template class somewhere so people wouldn’t reinvent the wheel, but what do I know)
So yeah, feel like a total noob even though I’ve used many other APIs before. Am I the only one getting confused with the new API?
I added a section on Python scripting to the “Blender 3D: Noob to Pro” WikiBook back in the 2.5x days. Some of the details need updating, but it does take you step-by-step through defining a custom operator and then creating a simple UI for it.
the execution of a couple of functions like invoke, execute, poll , draw, modal, init are done automatically by either python or blender.
a ‘panel’ class creates a pannel in the blender gui, for instance in the tool bar. an ‘operator’ class creates an operator. everything you do in blender is done with operators. if you click the shade smooth button, the operator bpy.ops.object.shade_smooth() is called.
Nice tutorial. I’m afraid to finally say that I got it all, but I’ll say what I got.
Operator is basically a class which is registered and can be used by any part of Blender like the default Blender stuff, which are also implemented as operators.
And Panels are the building block of GUI widgets. You specify where your panel will appear inside the class, by passing the appropriate strings to some bl_* variables.
However, I’m still a bit confused on how properties become GUI elements. Last time I checked properties were used in scripts to export custom stuff to 3d files and used by the game engine for the game logic.
A little more explanation on these properties would be nice.
But yeah thanks a lot. I finally don’t feel like a total noob.
before I’ll read them, your file browser code returns this:
Traceback (most recent call last):
File "<blender_console>", line 1, in <module>
File "C:\Program Files (x86)\Blender Foundation\blender-2.60a-release-windows32\2.60\scripts\modules\bpy\ops.py", line 180, in __call__
ret = op_call(self.idname_py(), None, kw)
RuntimeError: Error: Can't read file: "", Unable to open the file.
Seems like “” doesnt mean default dir and I can’t supply windows paths.
EDIT: Nope, those arent my issues. I don’t get the point behind Operators/ Panels/ Properties used by Panels. I think I got the first two now, but I still dont get why/how Properties work in your Panel class.
Also, I can’t get any bpy.ops to work. I want to do bpy.ops.mesh.remove_doubles(), but it doesnt work as my mouse cursor is not in 3d view when I run the script. Simply terrible impression of the new API…