不记录self的Python类记录属性?

时间:2018-06-28 10:22:56

标签: python class attributes blender

我对在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

谢谢!

1 个答案:

答案 0 :(得分:0)

这是因为savingpath_to_animanim_name是类属性。它们是为该类而不是特定实例定义的。它们在实例之间共享。这是进一步解释的链接class-instance-attributes-python