【问题标题】:View3d operator is not available if there is no object selected. Why?如果没有选择对象,则 View3d 运算符不可用。为什么?
【发布时间】:2020-02-23 08:44:20
【问题描述】:

我想创建一个带有一些按钮的弹出窗口。代码如下:

import bpy

class MaxFloater(bpy.types.Operator):
    """Tooltip"""
    bl_idname = "view3d.max_floater"
    bl_label = "MAX Floater"

    @classmethod
    def poll(cls, context):
        return context.active_object is not None

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

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

    def draw(self, context):        
        row = self.layout.row()
        row.operator("mesh.primitive_cube_add")
        row.operator("object.modifier_add", text="MIRROR").type='MIRROR'

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

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

if __name__ == "__main__":
    register()

一切都很好,只是它希望选择某个对象,或者最后选择的对象存在。如果该对象被删除,则操作员将无法工作。 (并且不会出现在运算符搜索窗口中)。我不明白为什么会这样?这是一个 view3d 运算符,而不是一个对象运算符,不是吗?

【问题讨论】:

  • 我尝试将 poll 函数修改为始终返回 True,它解决了问题。但在我看来,这不是最好的解决方案……

标签: python blender operator-keyword


【解决方案1】:

我想我找到了合适的解决方案。这个字符串应该在 poll 函数中:

return bpy.context.area.type == 'VIEW_3D'

它检查当前活动视图是否为 3d 视图,如果不是 - 操作员将无法工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-23
    • 1970-01-01
    • 2020-11-02
    • 2014-05-20
    相关资源
    最近更新 更多