Python类记录属性而不指定self?

2024-04-26 22:34:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个关于我在Blender中使用的Python类的问题。基本上,我想知道类是如何工作的,因为有些属性是在我没有特别编写self.value = something的情况下记录的。代码如下:

class DialogOperator(bpy.types.Operator):

    bl_idname = "object.dialog_operator"
    bl_label = "Save/Load animation"

    saving = bpy.props.BoolProperty(name="Save ? Else load.")
    path_to_anim = bpy.props.StringProperty(name="Path to folder")
    anim_name = bpy.props.StringProperty(name="Animation name:")
    # path_to_anim += "/home/mehdi/Blender/Scripts/"

    def execute(self, context):
        # print('This is execute with: Saving: {}  Name:{}'.format(self.saving, self.path_to_anim))

        if self.saving: 
            self.launch_save()
            message = 'Animation {} saved at {}'.format(self.anim_name, self.path_to_anim)
        else: 
            self.launch_load()
            message = 'Animation {} loaded'.format(self.anim_name)

        self.report({'INFO'}, message)

        return {'FINISHED'}

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

    def launch_load(self): 
        full_path = self.path_to_anim + self.anim_name
        target_armature = Humanoid(bpy.data.objects['Armature'])
        load_all(full_path, target_armature, 'LastLoaded')

    def launch_save(self): 

        full_path = self.path_to_anim + self.anim_name
        source_armature = Humanoid(bpy.data.objects['Armature'])
        curves = source_armature.get_curves()
        save_all(curves, source_armature,full_path)

现在,为什么savingpath_to_animanim_name被认为是属性(我可以在execute()launch()中调用它们),即使我没有编写self.saving = saving

谢谢


Tags: topathnameselfexecutedefloadprops