[Blender] 解决不出现对话框/框中看不到自定义的 bpy.types.Operator 中的 bpy.props.XXXProperty 的问题

383 阅读1分钟

Blender 版本:3.4

答案发现自:blender.stackexchange.com/questions/2…


最新版的文档:docs.blender.org/api/current…

其中有一个入门示例:

import bpy


class OBJECT_OT_property_example(bpy.types.Operator):
    bl_idname = "object.property_example"
    bl_label = "Property Example"
    bl_options = {'REGISTER', 'UNDO'}

    my_float: bpy.props.FloatProperty(name="Some Floating Point")
    my_bool: bpy.props.BoolProperty(name="Toggle Option")
    my_string: bpy.props.StringProperty(name="String Value")

    def execute(self, context):
        self.report(
            {'INFO'}, 'F: %.2f  B: %s  S: %r' %
            (self.my_float, self.my_bool, self.my_string)
        )
        print('My float:', self.my_float)
        print('My bool:', self.my_bool)
        print('My string:', self.my_string)
        return {'FINISHED'}


class OBJECT_PT_property_example(bpy.types.Panel):
    bl_idname = "object_PT_property_example"
    bl_label = "Property Example"
    bl_space_type = 'VIEW_3D'
    bl_region_type = 'UI'
    bl_category = "Tool"

    def draw(self, context):
        # You can set the property values that should be used when the user
        # presses the button in the UI.
        props = self.layout.operator('object.property_example')
        props.my_bool = True
        props.my_string = "Shouldn't that be 47?"

        # You can set properties dynamically:
        if context.object:
            props.my_float = context.object.location.x
        else:
            props.my_float = 327


bpy.utils.register_class(OBJECT_OT_property_example)
bpy.utils.register_class(OBJECT_PT_property_example)

# Demo call. Be sure to also test in the 3D Viewport.
bpy.ops.object.property_example(
    my_float=47,
    my_bool=True,
    my_string="Shouldn't that be 327?",
)

可以看到,点击操作符之后,可以弹出一个对话框,里面显示了一些在操作符类里面定义的属性

44be044a478c559beb128c124099493.png

但是如果当你自己写的时候没有出现这个对话框,或者对话框里面没有你要的属性,那可能是因为你的属性语句是用的等号,比如

my_float = bpy.props.FloatProperty(name="Some Floating Point")

实际上新版 Blender 里面,不会显示使用等号定义的属性,而是只会显示用 : 定义的属性,例如

my_float : bpy.props.FloatProperty(name="Some Floating Point")

如果你看到一些教程里面用等号也可以,那就是那些教程用的是老版的 Blender

我不太理解为什么 blender 要这么设置……难道等号和冒号有区别吗……