This is going to be a long post, but there are a lot of things to address.
Ricky:
- I’m using a class, because the push button in the gui needs to refer to an operator. And the easiest way to create a new operator is to define a new class for it and then add it to blender’s list of known operators. You can of course add meshes outside of classes as well.
2&5. The ‘select_all_toggle()’ line being there twice, is for a good reason. It works like pressing the A-key. So the first time you toggle it, it deselects all vertices and the second time it selects all vertices. To make sure that it always works like this, there needs to be at least one selected and one deselected vertex when you begin. That’s what the lines before ‘me.update()’ are for.
-
if context.active_object.type == 'MESH':
This does not do an action. Neither does it make a selection. You might want to read up a bit on python before you dive into Blender 2.5 (Byte of Python is a good read, and you can test your knowledge at Project Euler).
The active_object is a property of the context. I simply test if the type of that object is a mesh. It’s like bpy.data.scenes.active.objects.active in blender 2.4x
4.
layout.itemL(text="Change mesh:")
layout.itemO("object.monkify")
This is what shows up in the gui. You can also see this in the screenshot.
6. I chose to do it in editmode, because then the new vertices are automatically inserted into the active object. Otherwise you have to start renaming or relinking objects and meshes, which is quite some more work.
Ricky (2nd post): Your example shows two different operators (the first is an object operator, the second is a mesh operator), but I think you’re being confused by the ways of referring to operators. It’s a good question though and a pretty important one. Take a look at this (examples for how you might code a pushbutton):
layout.itemO("object.object_add")
layout.itemO("OBJECT_OT_object_add")
layout.itemO(bpy.ops.object.object_add(type = "EMPTY"))
The first two are the same and create a pushbutton. They will invoke the operator when the buttons is pressed. The third one executes the operator and doesn’t create a button (itemO takes a string as argument). So you’ll end up with hundreds of empties after a few seconds as itemO is called on each redraw.
As you migh remember of the script examples so far that involved operators, we defined the ‘invoke’ part of the operator. If you wish to ‘execute’ an operator (so run it directly from your script) you need to define an ‘execute’ part as well (or replace the ‘invoke’ part with it, if you don’t want to use it with a gui).
demohero: what exactly do you mean with creating your own toolbar? A custom window, a bar in the 3d-view, a panel inside another window? Perhaps a quick mock-up image would help to explain what you mean.
Ammiddeon: In the monkify example I got the picked object using:
ob = context.active_object
For iterating objects, Michael W already gave the answer.
Michael W: The reason context.object doesn’t work in the toolbar, is because the context is different. In the ‘hello world’ example the context is ‘object’, while in the ‘monkify’ example the context is ‘objectmode’. And the ‘objectmode’ context doesn’t have an ‘object’ property. Both contexts do have the ‘active_object’ property though. So it’s probably best to use that.
You can try this yourself: if you replace “ob = context.object” in the ‘hello world’ example with “ob = context.active_object”, it will still work correctly.