在Blender 2.92中使用IntProperties时,会导致插件崩溃。解决方法是使用FloatProperties或EnumProperties代替IntProperties。
以下是使用FloatProperties的示例代码:
import bpy
class MyProperties(bpy.types.PropertyGroup):
my_int: bpy.props.FloatProperty(
name="My Int",
default=0
)
class MyOperator(bpy.types.Operator):
bl_idname = "object.my_operator"
bl_label = "My Operator"
def execute(self, context):
props = context.scene.my_properties
print(props.my_int)
return {'FINISHED'}
class MyPanel(bpy.types.Panel):
bl_idname = "OBJECT_PT_my_panel"
bl_label = "My Panel"
bl_space_type = "VIEW_3D"
bl_region_type = "UI"
bl_category = "My Addon"
def draw(self, context):
props = context.scene.my_properties
self.layout.prop(props, "my_int")
self.layout.operator("object.my_operator", text="Print Int")
classes = (
MyProperties,
MyOperator,
MyPanel
)
def register():
for cls in classes:
bpy.utils.register_class(cls)
bpy.types.Scene.my_properties = bpy.props.PointerProperty(type=MyProperties)
def unregister():
del bpy.types.Scene.my_properties
for cls in classes:
bpy.utils.unregister_class(cls)
if __name__ == "__main__":
register()
这是使用EnumProperties的示例代码:
import bpy
class MyEnumChoices:
VALUES = (
("A", "Option A", ""),
("B", "Option B", ""),
("C", "Option C", "")
)
class MyProperties(bpy.types.PropertyGroup):
my_int: bpy.props.EnumProperty(
name="My Int",
items=MyEnumChoices.VALUES,
default="A"
)
class MyOperator(bpy.types.Operator):
bl_idname = "object.my_operator"
bl_label = "My Operator"
def execute(self, context):
props = context.scene.my_properties
print(props.my_int)
return {'FINISHED'}
class MyPanel(bpy.types.Panel):
bl_idname = "OBJECT_PT_my_panel"
bl_label = "My Panel"
bl_space_type = "VIEW_3D"
bl_region_type = "UI"
bl_category = "My Addon"
def draw(self, context):
props = context.scene.my_properties
self.layout.prop(props, "my_int")
self.layout.operator("object.my_operator", text="Print Int")
classes = (
MyProperties,
MyOperator,
My