하나의 기능으로 속성 업데이트를 중앙 집중화합니까? [복제]

Dec 14 2020

업데이트 기능에서 소품에 대한 더 많은 정보를 얻을 수 있습니까?

아래 예제에서 불행히도 self 는 prop 자체가 아닌 bpy.types.Scene을 가리 킵니다. 그래서 언뜻보기에 우리는 업데이트 기능에서 소품에 대한 정보를 얻을 수없는 것처럼 보이며, 소품 마다 작동 할 수있는 단일 중앙 업데이트 기능 대신 소품 당 하나의 업데이트 기능을 가져야 합니다.

( set 에서 더 많은 정보를 얻을 수 있지만 물론 prop 값을 변경하면 피드백 루프가 생성됩니다.)

내가 틀렸다는 것을 증명 해주세요

def update(self,context):
    print("what is this prop value?")
    print("what is this prop api?")
    print("what is this prop name?")
    return None 

bpy.types.Scene._prop          : bpy.props.FloatProperty(default=1.0 , name="Prop", update=update)
bpy.types.Scene._prop_this     : bpy.props.FloatProperty(default=1.1 , name="Prop This", update=update)
bpy.types.Scene._prop_that     : bpy.props.FloatProperty(default=1.2 , name="Prop That", update=update)
bpy.types.Scene._prop_another  : bpy.props.FloatProperty(default=1.3 , name="Prop Another", update=update)

답변

5 batFINGER Dec 14 2020 at 20:22

업데이트 메서드를 생성하는 일반 함수를 만듭니다.

@pinkvertex가 찾을 수 없었던 이것과 매우 유사한 또 다른 답변이 있습니다.

하나의 함수를 업데이트 메서드로 사용하는 대신 속성을 전달할 메서드 팩토리를 만듭니다.

예제 코드 및 예제 실행 (아주 자명하다),

import bpy
from bpy.props import FloatProperty
from bpy.types import Scene


def update(prop):
    def update(self, context):
        print(f"Update {prop} of {self}")
        return None
    return update


for prop in ("prop_this", "prop_that", "prop_another"):
    setattr(
        Scene, 
        prop,
        FloatProperty(
            default=1.0,
            name="Prop", 
            update=update(prop)
            )
        )

샘플 실행.

>>> C.scene.prop_this
1.0

>>> C.scene.prop_this = 4
Update prop_this of <bpy_struct, Scene("Scene") at 0x7fc97bff8008>

>>> 

두 가지 질문 코드,

  • 속성은 주석이 아닌 유형에 할당됩니다. 클래스 정의에 주석을 사용하십시오.

    밑줄로 시작하는 개인용 변수를 만들 수 없습니다.

     >>> bpy.types.Scene._prop = bpy.props.FloatProperty()
     Traceback (most recent call last):
     File "<blender_console>", line 1, in <module>
     ValueError: bpy_struct "Scene" registration error: _prop could not register because the property starts with an '_'