利用者:Raa/Addons/Pie Menu Editor/Scripting

提供: wiki
移動先: 案内検索

Scripting

Tutorials

Global Variables

menu

Name of the active menu

slot

Name of the active slot

C

bpy.context

D

bpy.data

O

bpy.ops

T

bpy.types

P

bpy.props

L

Current UILayout object

L.box().label("My Label")

E

Current Event object

E.ctrl and E.shift and message_box("Ctrl+Shift")


Global Functions

Common Functions

execute_script (path, **kwargs)

Execute external python script

Arguments:path - Path to file
Returns:Value of local variable return_value or True

Display 'Hello World!' message:

execute_script("scripts/hello_world.py", msg="Hello World!")

scripts/hello_world.py:

message_box(kwargs["msg"])

Display 'Hi!' message:

message_box(execute_script("scripts/hi.py"))

scripts/hi.py:

return_value = "Hi!"

props (name=None, value=None)

Get or set the value of the Property

Arguments:
name - Name of the property
value - New value of the property
Returns:The value of the Property by its name, if value is None, True otherwise
value = props("MyProperty")
value = props().MyProperty
props("MyProperty", value)
props().MyProperty = value

paint_settings ()

Returns:Context sensitive paint settings
ps = paint_settings(); ps and L.template_ID_preview(ps, 'brush')

find_by (collection, key, value)

Find the first item in collection by key value

Returns:Collection item if found, None otherwise
m = find_by(C.active_object.modifiers, "type", 'SUBSURF')

setattr (object, name, value)

Same as setattr(), but returns True

Returns:True


Command Tab Functions

open_menu (name, slot=None, **kwargs)

Open menu, pie menu, popup dialog or execute stack key, sticky key, modal operator or macro operator

Arguments:
name - Name of the menu
slot - Index or name of the Stack Key's slot you want to execute
kwargs - Allows to pass the arguments to Modal/Macro Operator and use them as local variables
Returns:True if the menu exists, False otherwise

Open the menu depending on the active object's type:

open_menu("Lamp Pie Menu" if C.active_object.type == 'LAMP' else "Object Pie Menu")

Call My Stack Key's slot depending on Ctrl key modifier:

open_menu("My Stack Key", "Ctrl slot" if E.ctrl else "Shift slot")

toggle_menu (name, value=None)

Enable or disable menu

Arguments:
name - Name of the menu
value - True - enable, False - disable, None - toggle
Returns:True if the menu exists, False otherwise

tag_redraw (area=None, region=None)

Redraw UI

Arguments:
area - Type of the areas to redraw. Redraw all areas if None.
region - Type of the regions to redraw. Redraw all regions if None.
Returns:True

close_popups ()

Close all popups

Returns:True

overlay (text, **kwargs)

Draw overlay message

Arguments:
text - Message to display
alignment - Alignment, enum in ['TOP', 'TOP_LEFT', 'TOP_RIGHT', 'BOTTOM', 'BOTTOM_LEFT', 'BOTTOM_RIGHT']
duration - Duration
offset_x - X Offset
offset_y - Y Offset
Returns:True

message_box (text, icon='INFO', title="Pie Menu Editor")

Show message box

Arguments:text - Message to display
Returns:True

input_box (func=None, prop=None)

Show input box

Arguments:
func - Function to call
prop - Path to property
Returns:True

Rename object:

input_box(prop="C.active_object.name")

Display input value:

input_box(func=lambda value: overlay(value))


Custom Tab Functions

draw_menu (name, frame=True, dx=0, dy=0)

Draw popup dialog inside another popup dialog or a pie menu

Arguments:
name - Name of the menu
frame - Use frame
dx - Horizontal offset
dy - Vertical offset
Returns:True if the popup dialog exists, False otherwise

operator (layout, operator, text="", icon='NONE', emboss=True, icon_value=0, **kwargs)

Same as UILayout.operator(), but allows to fill operator properties.

Arguments:
layout - Layout
operator - Identifier of the operator
Returns:OperatorProperties object
operator(L, "wm.context_set_int", "Material Slot 1", data_path="active_object.active_material_index", value=0)

Same as:

op = L.operator("wm.context_set_int", "Material Slot 1")
op.data_path = "active_object.active_material_index"
op.value = 0

custom_icon (filename)

Icon value

Arguments:
filename - Icon filename without extension
Returns:Returns value of the custom icon

Display text message with custom (pie_menu_editor/icons/p1.png) icon.

L.label("My Custom Icon", icon_value=custom_icon("p1"))

panel (id, frame=True, header=True, expand=None)

Draws panel by it's ID.

Arguments:
id - ID of the panel
frame - Use frame
header - Draw header
expand - True - expanded, False - collapsed, None - use current state
Returns:True
panel("MATERIAL_PT_context_material", True, True, True)


Auto-run Scripts

The addon allows you to write python scripts that will be executed when you start Blender. Just copy .py files, folders or symlinks to pie_menu_editor/scripts/autorun folder.


Custom Global Functions

To use your functions in PME you need to copy your scipt to pie_menu_editor/scripts/autorun folder and register the functions using pme.context.add_global():

def hello_world():
    print("Hello World")

pme.context.add_global("hello", hello_world)

Now you can use hello() function in Command, Custom tabs and external scripts.