blender/blender python

프로퍼티 서브타입

monstro 2025. 8. 6. 17:17
728x90
반응형

- 개요

블렌더 파이썬에서 사용하는 bpy.props의 Property들에는 subtype이라는 속성이 존재한다

subtype 속성을 사용하면 Property에 의해 설정되는 특정 데이터의 값을 더 직관적으로 표현하여

사용자가 값을 변경하거나 수정할 때 더 명확하게 이해할 수 있다

 

블렌더 파이썬에 존재하는 subtype의 종류는 다음의 링크에서 찾아볼 수 있다

https://docs.blender.org/api/current/bpy_types_enum_items/property_subtype_items.html

 

Property Subtype Items - Blender Python API

Previous Property Type Items

docs.blender.org

 

1) 프로퍼티 그룹 클래스

import bpy
 
 
class MyProperties(bpy.types.PropertyGroup):
    
    my_string : bpy.props.StringProperty(
        name="Enter Password",
        subtype='PASSWORD'
    )
    
    my_float : bpy.props.FloatProperty(
        name="Enter Temperature",
        subtype='TEMPERATURE'
    )
    
...

 

프로퍼티 그룹으로 사용하는 MyProperties 클래스를 생성한다

프로퍼티는 총 2개로 구성하여 문자열실수값을 받도록 설정한다

이때 2개의 프로퍼티에 subtype 속성을 사용하여 다음과 같이 설정하였다

  •  PASSWORD 서브타입 : 입력하는 문자를 *로 마스킹
  • TEMPERATURE 서브타입 : K(켈빈) 값으로 입력받도록 표현

 

2) 메인 라벨 클래스

...

class ps_PT_main_panel(bpy.types.Panel):
    bl_label = "Main Panel"
    bl_idname = "ps_PT_main_panel"
    bl_space_type = 'VIEW_3D'
    bl_region_type = 'UI'
    bl_category = "New Tab"
 
    def draw(self, context):
        layout = self.layout
        scene = context.scene
        mytool = scene.my_tool
        
        layout.prop(mytool, "my_string")
        layout.prop(mytool, "my_float")
        
 
        layout.operator("ps.myop_operator")
    
...

 

메인 라벨로 사용하는 ps_PT_main_panel 클래스를 생성하였다

라벨을 설정하는 draw 함수를 위와 같이 구성하여 프로퍼티 그룹의 값입력받아 설정하고

설정한 값을 바탕으로 Operator 클래스를 동작시킨다

 

3) Operator 클래스

...

class ps_OT_my_op(bpy.types.Operator):
    bl_label = "Confirm"
    bl_idname = "ps.myop_operator"
    
    def execute(self, context):
        scene = context.scene
        mytool = scene.my_tool
        
        if mytool.my_string == "password":
            bpy.ops.mesh.primitive_cube_add()
            bpy.context.object.name = str(mytool.my_float)
            
        mytool.my_string = ""
                
        return {'FINISHED'}
        
...

 

라벨에서 수행하는 동작을 정의하는 ps_OT_my_op 클래스를 위와 같이 생성하였다

동작을 정의하는 execute 함수에서는 프로퍼티 그룹인 my_tool을 가져와 설정한 프로퍼티의 값을 가져온다

StringProperty적합한 문자열을 입력하면 큐브를 생성하고 FloatProperty입력한 값이름으로 설정한다

 

4) 클래스 등록 / 등록해제

...

classes = [
        MyProperties, 
        ps_PT_main_panel, 
        ps_OT_my_op
    ]
    
    
def register():
    for cls in classes:
        bpy.utils.register_class(cls)
        bpy.types.Scene.my_tool = bpy.props.PointerProperty(type= MyProperties)
 
 
def unregister():
    for cls in classes:
        bpy.utils.unregister_class(cls)
        del bpy.types.Scene.my_tool


if __name__ == "__main__":
    register()

 

생성한 클래스들의 등록 및 등록해제, 프로퍼티 그룹의 설정과 제거를 수행한다

 

- 최종 실행 결과

 

728x90
반응형

'blender > blender python' 카테고리의 다른 글

랜덤값  (0) 2025.08.13
모듈 임포트  (0) 2025.08.06
프로퍼티 그룹  (0) 2025.08.06
머티리얼 생성과 할당  (0) 2025.07.30
클래스 네이밍 컨벤션  (0) 2025.07.30