Атрибуты записи класса Python без указания себя? - PullRequest
0 голосов
/ 28 июня 2018

У меня есть вопрос относительно класса Python, который я использую в Blender. По сути, мне интересно, как работает этот класс, потому что некоторые атрибуты записываются без моего специального написания 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)

Теперь, почему saving, path_to_anim и anim_name считаются атрибутами (я могу назвать их в execute() и launch()), хотя я не писал self.saving = saving

Спасибо!

1 Ответ

0 голосов
/ 28 июня 2018

Это потому, что saving, path_to_anim и anim_name являются атрибутами класса. Они определены для класса, а не для конкретного экземпляра. Они делятся между экземплярами. Вот ссылка для дальнейшего объяснения class-instance-attribute-python

...