blender 2.56 UI scripting

this (code snippets) used to be my go-to-reference for buttons and other gui things, now it seems to just do nothing…

#----------------------------------------------------------
# File layout.py
#----------------------------------------------------------
import bpy

class LayoutPanel(bpy.types.Panel):
    bl_label = "Panel with funny layout"
    bl_space_type = "VIEW_3D"
    bl_region_type = "TOOL_PROPS"

    def draw(self, context):
        layout = self.layout

        layout.label("First row")
        row = layout.row(align=True)
        row.alignment = 'EXPAND'
        row.operator("object.Button", text="1")
        row.operator("object.Button", text="2", icon='MESH_DATA')
        row.operator("object.Button", icon='LAMP_DATA')

        row = layout.row(align=False)
        row.alignment = 'LEFT'
        row.operator("object.Button", text="4")
        row.operator("object.Button", text="", icon='MATERIAL')
        row.operator("object.Button", text="6", icon='BLENDER')
        row.operator("object.Button", text="7", icon='WORLD')

        layout.label("Third row", icon='TEXT')
        row = layout.row()
        row.alignment = 'RIGHT'
        row.operator("object.Button", text="8")
        row.operator("object.Button", text="9", icon='SCENE')
        row.operator("object.Button", text="10", icon='BRUSH_INFLATE')
        
        layout.label("Fourth row", icon='ACTION')
        row = layout.row()
        box = row.box()
        box.operator("object.Button", text="11", emboss=False)
        box.operator("object.Button", text="12", emboss=False)
        col = row.column()
        subrow = col.row()
        subrow.operator("object.Button", text="13")
        subrow.operator("object.Button", text="14")
        subrow = col.row(align=True)
        subrow.operator("object.Button", text="15")
        subrow.operator("object.Button", text="16")
        box = row.box()
        box.operator("object.Button", text="17")
        box.separator()
        box.operator("object.Button", text="18")
        box.operator("object.Button", text="19")
        
        layout.label("Fifth row")
        row = layout.row()
        split = row.split(percentage=0.25)
        col = split.column()
        col.operator("object.Button", text="21")
        col.operator("object.Button", text="22")
        split = split.split(percentage=0.3)
        col = split.column()
        col.operator("object.Button", text="23")
        split = split.split(percentage=0.5)
        col = split.column()
        col.operator("object.Button", text="24")
        col.operator("object.Button", text="25")
        

class OBJECT_OT_Button(bpy.types.Operator):
    bl_idname = "OBJECT_OT_Button"
    bl_label = "Button"

    def execute(self, context):
        print("Hello world!")
        return{'FINISHED'}    

used to display a variety of buttons and layout details, now it just goes silent.
what’s the deal, the current way of doing it is not as obvious as it used to be.

You need to register both classes:

<b>bpy.utils.register_class</b>(cls)
Register a subclass of a blender type in (Panel, Menu, Header, Operator, KeyingSetInfo, RenderEngine).
<i>ValueError</i> exception is raised if the class is not a subclass of a registerable blender class.

i was running from the text editor (that’s what the register button is for?), but even when i add :

def register():
    bpy.utils.register_class(__name__)


def unregister():
    bpy.utils.unregister_class(__name__)


if __name__ == "__main__":
    register()

full code : http://www.pasteall.org/19668/python

there’s not a whole lot of movement. infact, that evaporates my blender session. ( r35301)
so maybe it’s unstable?

You should register OBJECT_OT_Button and LayoutPanel so:

bpy.utils.register_class(OBJECT_OT_Button)
bpy.utils.register_class(LayoutPanel)

You also need to rename operator bl_idname to smth like “object.button” (with dot-chat and no Capital chars).

thanks!

#----------------------------------------------------------
# File layout.py
#----------------------------------------------------------
import bpy

class LayoutPanel(bpy.types.Panel):
    bl_label = "Panel with funny layout"
    bl_space_type = "VIEW_3D"
    bl_region_type = "TOOL_PROPS"

    def draw(self, context):
        layout = self.layout

        layout.label("First row")
        row = layout.row(align=True)
        row.alignment = 'EXPAND'
        row.operator("object.Button", text="1")
        row.operator("object.Button", text="2", icon='MESH_DATA')
        row.operator("object.Button", icon='LAMP_DATA')

        row = layout.row(align=False)
        row.alignment = 'LEFT'
        row.operator("object.Button", text="4")
        row.operator("object.Button", text="", icon='MATERIAL')
        row.operator("object.Button", text="6", icon='BLENDER')
        row.operator("object.Button", text="7", icon='WORLD')

        layout.label("Third row", icon='TEXT')
        row = layout.row()
        row.alignment = 'RIGHT'
        row.operator("object.Button", text="8")
        row.operator("object.Button", text="9", icon='SCENE')
        row.operator("object.Button", text="10", icon='BRUSH_INFLATE')
        
        layout.label("Fourth row", icon='ACTION')
        row = layout.row()
        box = row.box()
        box.operator("object.Button", text="11", emboss=False)
        box.operator("object.Button", text="12", emboss=False)
        col = row.column()
        subrow = col.row()
        subrow.operator("object.Button", text="13")
        subrow.operator("object.Button", text="14")
        subrow = col.row(align=True)
        subrow.operator("object.Button", text="15")
        subrow.operator("object.Button", text="16")
        box = row.box()
        box.operator("object.Button", text="17")
        box.separator()
        box.operator("object.Button", text="18")
        box.operator("object.Button", text="19")
        
        layout.label("Fifth row")
        row = layout.row()
        split = row.split(percentage=0.25)
        col = split.column()
        col.operator("object.Button", text="21")
        col.operator("object.Button", text="22")
        split = split.split(percentage=0.3)
        col = split.column()
        col.operator("object.Button", text="23")
        split = split.split(percentage=0.5)
        col = split.column()
        col.operator("object.Button", text="24")
        col.operator("object.Button", text="25")
        

class OBJECT_OT_Button(bpy.types.Operator):
    bl_idname = "object.button"
    bl_label = "Button"

    def execute(self, context):
        print("Hello world!")
        return{'FINISHED'}    

def register():
    bpy.utils.register_class(__name__)


def unregister():
    bpy.utils.unregister_class(__name__)



this runs nicely now, from texteditor at least

the above script does not work in r35301

I find it a little crazy that we have to register all classes by name now i thought a name would catch all of them?

at present only this works


#----------------------------------------------------------
# File layout.py
#----------------------------------------------------------
import bpy

class LayoutPanel(bpy.types.Panel):
    bl_label = "Panel with funny layout"
    bl_space_type = "VIEW_3D"
    bl_region_type = "TOOL_PROPS"

    def draw(self, context):
        layout = self.layout

        layout.label("First row")
        row = layout.row(align=True)
        row.alignment = 'EXPAND'
        row.operator("object.button", text="1")
        row.operator("object.button", text="2", icon='MESH_DATA')
        row.operator("object.button", icon='LAMP_DATA')

        row = layout.row(align=False)
        row.alignment = 'LEFT'
        row.operator("object.button", text="4")
        row.operator("object.button", text="", icon='MATERIAL')
        row.operator("object.button", text="6", icon='BLENDER')
        row.operator("object.button", text="7", icon='WORLD')

        layout.label("Third row", icon='TEXT')
        row = layout.row()
        row.alignment = 'RIGHT'
        row.operator("object.button", text="8")
        row.operator("object.button", text="9", icon='SCENE')
        row.operator("object.button", text="10", icon='BRUSH_INFLATE')
        
        layout.label("Fourth row", icon='ACTION')
        row = layout.row()
        box = row.box()
        box.operator("object.button", text="11", emboss=False)
        box.operator("object.button", text="12", emboss=False)
        col = row.column()
        subrow = col.row()
        subrow.operator("object.button", text="13")
        subrow.operator("object.button", text="14")
        subrow = col.row(align=True)
        subrow.operator("object.button", text="15")
        subrow.operator("object.button", text="16")
        box = row.box()
        box.operator("object.button", text="17")
        box.separator()
        box.operator("object.button", text="18")
        box.operator("object.button", text="19")
        
        layout.label("Fifth row")
        row = layout.row()
        split = row.split(percentage=0.25)
        col = split.column()
        col.operator("object.button", text="21")
        col.operator("object.button", text="22")
        split = split.split(percentage=0.3)
        col = split.column()
        col.operator("object.button", text="23")
        split = split.split(percentage=0.5)
        col = split.column()
        col.operator("object.button", text="24")
        col.operator("object.button", text="25")
        

class OBJECT_OT_Button(bpy.types.Operator):
    bl_idname = "object.button"
    bl_label = "Button"

    def execute(self, context):
        print("Hello world!")
        return{'FINISHED'}    

def register():
    bpy.utils.register_class(OBJECT_OT_Button)
    bpy.utils.register_class(LayoutPanel)

def unregister():
    bpy.utils.unregister_class(OBJECT_OT_Button)
    bpy.utils.unregister_class(LayoutPanel)

if __name__ == "__main__":
    register()


This works by me (Vista W32 SVN35382)
And adding a mode parameter to the operator, to be able to do different things :yes:


#http://blenderartists.org/forum/showthread.php?t=210513&p=1799525&viewfull=1#post1799525
#----------------------------------------------------------
# File layout.py
#----------------------------------------------------------
import bpy
from bpy.props import IntProperty

class LayoutPanel(bpy.types.Panel):
    bl_label = "Panel with funny layout"
    bl_space_type = "VIEW_3D"
    bl_region_type = "TOOL_PROPS"
    
    def draw(self, context):
        layout = self.layout

        layout.label("First row")
        row = layout.row(align=True)
        row.alignment = 'EXPAND'
        row.operator("object.button", text="1").mode = 1
        row.operator("object.button", text="2", icon='MESH_DATA').mode = 2
        row.operator("object.button", icon='LAMP_DATA')

        row = layout.row(align=False)
        row.alignment = 'LEFT'
        row.operator("object.button", text="4").mode = 4
        row.operator("object.button", text="", icon='MATERIAL').mode =5
        row.operator("object.button", text="6", icon='BLENDER').mode=6
        row.operator("object.button", text="7", icon='WORLD').mode=7

        layout.label("Third row", icon='TEXT')
        row = layout.row()
        row.alignment = 'RIGHT'
        row.operator("object.button", text="8").mode=8
        row.operator("object.button", text="9", icon='SCENE').mode=9
        row.operator("object.button", text="10", icon='BRUSH_INFLATE').mode=10
        #etc for the other object.buttons calls mode = ....
        layout.label("Fourth row", icon='ACTION')
        row = layout.row()
        box = row.box()
        box.operator("object.button", text="11", emboss=False)
        box.operator("object.button", text="12", emboss=False)
        col = row.column()
        subrow = col.row()
        subrow.operator("object.button", text="13")
        subrow.operator("object.button", text="14")
        subrow = col.row(align=True)
        subrow.operator("object.button", text="15")
        subrow.operator("object.button", text="16")
        box = row.box()
        box.operator("object.button", text="17")
        box.separator()
        box.operator("object.button", text="18")
        box.operator("object.button", text="19")
        
        layout.label("Fifth row")
        row = layout.row()
        split = row.split(percentage=0.25)
        col = split.column()
        col.operator("object.button", text="21")
        col.operator("object.button", text="22")
        split = split.split(percentage=0.3)
        col = split.column()
        col.operator("object.button", text="23")
        split = split.split(percentage=0.5)
        col = split.column()
        col.operator("object.button", text="24")
        col.operator("object.button", text="25")
        

class OBJECT_OT_Button(bpy.types.Operator):
    bl_idname = "object.button"
    bl_label = "Button"
    mode = IntProperty(name="mode",min =-100, max =20,soft_max=10,default =-99) 
    def execute(self, context):
        print("Hello world!")
        print(self.mode)
        return{'FINISHED'}    

def register():
    bpy.utils.register_module(__name__)
    pass

def unregister():
    bpy.utils.unregister_module(__name__)
    pass

if __name__ == "__main__":
    register()

one question here
you addeda propr inside a class which is not the panel class

does it means it can re assign otherwise then using a button
or is it out of context ?

thanks

A rather old thread, but this is an interesting example of UI Panel coding, so it’s worth updating.

The only change I made was: object.Button became object.button (all lowercase naming convention)


#----------------------------------------------------------
# File layout.py
#----------------------------------------------------------
import bpy

class LayoutPanel(bpy.types.Panel):
    bl_label = "Panel with funny layout"
    bl_space_type = "VIEW_3D"
    bl_region_type = "UI"

    def draw(self, context):
        layout = self.layout

        layout.label("First row")
        row = layout.row(align=True)
        row.alignment = 'EXPAND'
        row.operator("object.button", text="1")
        row.operator("object.button", text="2", icon='MESH_DATA')
        row.operator("object.button", icon='LAMP_DATA')

        row = layout.row(align=False)
        row.alignment = 'LEFT'
        row.operator("object.button", text="4")
        row.operator("object.button", text="", icon='MATERIAL')
        row.operator("object.button", text="6", icon='BLENDER')
        row.operator("object.button", text="7", icon='WORLD')

        layout.label("Third row", icon='TEXT')
        row = layout.row()
        row.alignment = 'RIGHT'
        row.operator("object.button", text="8")
        row.operator("object.button", text="9", icon='SCENE')
        row.operator("object.button", text="10", icon='BRUSH_INFLATE')
        
        layout.label("Fourth row", icon='ACTION')
        row = layout.row()
        box = row.box()
        box.operator("object.button", text="11", emboss=False)
        box.operator("object.button", text="12", emboss=False)
        col = row.column()
        subrow = col.row()
        subrow.operator("object.button", text="13")
        subrow.operator("object.button", text="14")
        subrow = col.row(align=True)
        subrow.operator("object.button", text="15")
        subrow.operator("object.button", text="16")
        box = row.box()
        box.operator("object.button", text="17")
        box.separator()
        box.operator("object.button", text="18")
        box.operator("object.button", text="19")
        
        layout.label("Fifth row")
        row = layout.row()
        split = row.split(percentage=0.25)
        col = split.column()
        col.operator("object.button", text="21")
        col.operator("object.button", text="22")
        split = split.split(percentage=0.3)
        col = split.column()
        col.operator("object.button", text="23")
        split = split.split(percentage=0.5)
        col = split.column()
        col.operator("object.button", text="24")
        col.operator("object.button", text="25")
        

class OBJECT_OT_Button(bpy.types.Operator):
    bl_idname = "object.button"
    bl_label = "Button"

    def execute(self, context):
        print("Hello world!")
        return{'FINISHED'}    

def register():
    bpy.utils.register_module(__name__)

def unregister():
    bpy.utils.unregister_module(__name__)

if __name__ == "__main__":
    register()