Dev:Py/Scripts/Cookbook/Panels and Operators/Ping Pong

提供: wiki
< Dev:Py‎ | Scripts‎ | Cookbook‎ | Panels and Operators
2018年6月29日 (金) 04:40時点におけるYamyam (トーク | 投稿記録)による版 (1版 をインポートしました)
(差分) ← 古い版 | 最新版 (差分) | 新しい版 → (差分)
移動先: 案内検索

This script is a simple introduction to Panels and Operators using Blender Python scripting. The following description hopefully fulfills it's purpose (to describe).

Code

import bpy
 
class OBJECT_PT_pingpong(bpy.types.Panel):
    bl_label = "Ping Pong"
    bl_space_type = "PROPERTIES"
    bl_region_type = "WINDOW"
    bl_context = "object"
 
    is_left = True
 
    def draw_header(self, context):
        layout = self.layout
        layout.label(text="", icon="PHYSICS")
 
    def draw(self, context):
        layout = self.layout
        
        row = layout.row()
        split = row.split(percentage=0.5)
        col_left = split.column()
        col_right = split.column()
 
        if self.is_left:
            col_left.operator("object.pingpong", text="Ping")
        else:
            col_right.operator("object.pingpong", text="Pong")
 
 
class OBJECT_OT_pingpong(bpy.types.Operator):
    bl_label = "Ping Pong Operator"
    bl_idname = "object.pingpong"
    bl_description = "Move the ball"
 
    def execute(self, context):
        OBJECT_PT_pingpong.is_left = not OBJECT_PT_pingpong.is_left
        self.report({'INFO'}, "Moving the ball")
        return {'FINISHED'}
    
def register():
    bpy.utils.register_module(__name__)
    
def unregister():
    bpy.utils.unregister_module(__name__)
    
if __name__ == "__main__":
    register()

Description

OBJECT_PT_pingpong

The OBJECT_PT_pingpong class is to draw the gui (it is a Panel subclass).

bl_space_type
is the kind of space it shows up. Here it's the Properties Editor ('PROPERTIES').
bl_region_type
is the kind of region it shows up in. 'WINDOW' is the main region of every area / space type.
bl_context
is when the panel will show (so you will need to have an object selected, for the panel to show up). The combination of the space type 'PROPERTIES' and the context 'object' will show the panel in the Object tab of the Properties Editor.
bl_label
is the text displayed in the header.
is_left
is a custom class variable which we use to determine which button needs to be displayed.
draw_header()
is used to draw extra things in the header. In our case an icon. You can also use it to draw a checkbox that enables or disables all the settings in the panel (the actual action / state change it not handled here however).
draw()
defines the visual elements of the panel. There is a single row, which is split in half. Each half contains a column. Additionally we draw a button in one of these columns. The 'is_left' variable determines which one we draw. These buttons refer to an operator called object.pingpong, which is the bl_idname of the next class.

OBJECT_OT_pingpong

The OBJECT_OT_pingpong class defines our custom operator.

bl_label
is the default text that's used by buttons in the user-interface (which we overwrite by for instance: text="Ping" )
bl_idname
is what we use to refer to this operator.
bl_description
is the text that shows up when you hover over a button that calls this operator (a tooltip).
execute()
is called whenever a button of this operator is clicked.
report()
displays a message in the main header of Blender. After this line we also set the 'is_left' variable to the other value.