Graphics Reference
In-Depth Information
Drawing the Interface
The next part of the class definition is the
draw()
method, which tells Blender how to draw the interactive
interface for the operator. This is something new. In previous examples, the
draw()
method was not defined.
So how did Blender know to draw the interactive interface? The answer is that a
draw()
method is built into
the
AddObjectHelper
class. If you do not define a method by the same name to override the default meth-
od, Blender automatically draws an interactive interface for setting object location, rotation, and scale values.
In this example, we want to include other interactive properties besides just location, rotation, and scale, so it is
necessary to override the
draw()
method. This definition begins as follows:
def draw(self, context):
The first argument,
self
, passes the
Bouncing_Cone
object's own data to the method. The second argu-
ment,
context
, passes the context information about Blender's internal state to the method.
Operator objects have a property called
layout
. We assign the
layout
property to a local variable called
layout
like this:
layout = self.layout
The next few lines of code arrange the properties in the UI. You can arrange properties in columns or rows.
The specific widget type used to access the properties depends on the property type. Float properties like the
ones used here are displayed as numerical slider bars. Enum properties are displayed as drop-down menus, and
booleanpropertiesaredisplayedascheckboxes.Thisisautomatic,anditmaintainsconsistencyintheinterface.
To begin a column, call
layout.column()
and assign the result to a variable like this:
col = layout.column()
You can now call the
prop()
method on the column to put a property widget into the column, as in the
following two lines, which access the
height
and
radius
properties:
col.prop(self, 'height')
col.prop(self, 'radius')
The column class has other methods that can add other kinds of widgets besides properties. To place a text
label on a column, use the
label()
method, like this:
col.label(text='Material color: ')
Youcanarrangewidgetshorizontallybyusingarowobject.Rowobjectsarecreatedjustlikecolumnobjects
but using the
row()
method, like this:
row = layout.row()
Here is how RGB color values can be arranged horizontally on the row:
row.prop(self, 'r')
row.prop(self, 'g')
row.prop(self, 'b')
After this, we return to column layout by calling
layout.column()
again and attaching the remaining
labels and properties to the column as follows:
col = layout.column()
col.label(text='Bounce properties:')
col.prop(self, 'speed')
col.prop(self, 'bounce_height')
col.prop(self, 'location')