设置弹出菜单的默认值

时间:2015-05-13 08:13:06

标签: python popup blender

假设我有一个带有一个变量的脚本弹出式菜单(我的更复杂但是为了清晰起见而简化)

import bpy
from bpy.props import *

class DialogOperator(bpy.types.Operator):
    bl_idname = "object.dialog_operator"
    bl_label = "Test"

    defValue = bpy.context.active_object.scale[1]

    propertyScale = IntProperty(
                    name = "Scale",
                    default = defValue)

    def execute(self, context):
        return {'FINISHED'}

    def invoke(self, context, event):
        wm = context.window_manager
        return wm.invoke_props_dialog(self)

bpy.utils.register_class(DialogOperator)
bpy.ops.object.dialog_operator('INVOKE_DEFAULT')

让我们说我有两个具有不同比例值的立方体,每次调用此弹出菜单时,我想将当前所选立方体的值缩放为我的默认值。

上面的这个脚本不起作用,当你通过“运行脚本”按钮运行它时它会起作用,但之后如果按“空格”并在那里找到脚本,它将不会根据当前活动对象调整默认值。

有什么方法可以做到这一点吗?我认为应该很容易,但我错过了一些东西。

1 个答案:

答案 0 :(得分:0)

我搜索了网页并找到了关于调用函数(from here

的信息

Operator.invoke用于在调用运算符时从上下文初始化运算符。 invoke()通常用于分配execute()使用的属性。某些运算符没有execute()函数,删除了从脚本或宏重复的功能。 此示例显示如何定义获取鼠标输入以执行函数的运算符,以及可以从Python API调用或执行此运算符。 另请注意,此运算符定义了自己的属性,这些属性与典型的类属性不同,因为blender将其与运算符一起注册,在调用时用作参数,保存用于操作员撤消/重做并自动添加到用户界面中。

代码:

import bpy
class SimpleMouseOperator(bpy.types.Operator):
    """ This operator shows the mouse location,
        this string is used for the tooltip and API docs
    """
    bl_idname = "wm.mouse_position"
    bl_label = "Invoke Mouse Operator"

    x = bpy.props.IntProperty()
    y = bpy.props.IntProperty()

    def execute(self, context):
        # rather then printing, use the report function,
        # this way the messag appiers in the header,
        self.report({'INFO'}, "Mouse coords are %d %d" % (self.x, self.y))
        return {'FINISHED'}

    def invoke(self, context, event):
        self.x = event.mouse_x
        self.y = event.mouse_y
        return self.execute(context)

bpy.utils.register_class(SimpleMouseOperator)

# Test call to the newly defined operator.
# Here we call the operator and invoke it, meaning that the settings are taken
# from the mouse.
bpy.ops.wm.mouse_position('INVOKE_DEFAULT')

# Another test call, this time call execute() directly with pre-defined settings. 
bpy.ops.wm.mouse_position('EXEC_DEFAULT', x=20, y=66)

这应该为每个有类似问题的人解释一切。