blender/blender python

리스트

monstro 2025. 8. 19. 20:10
728x90
반응형

- 개요

패널의 입력을 통해 리스트를 설정하는 애드온을 만든다

 

1) PropertyGroup 클래스

import bpy
from bpy.types import Panel, Operator, PropertyGroup
from bpy.props import EnumProperty, PointerProperty, StringProperty
 
 
class MyProperties(PropertyGroup):
    my_enum : EnumProperty(
        name = "Enumerator / Dropdown",
        description = "sample text",
        items = [
            ('OP1', "Append", ""),
            ('OP2', "Remove", ""),
        ]
    )
    
    new_item : StringProperty()
    my_list = []
    
...

 

PropertyGroup으로 사용하는 MyProperties 클래스를 위와 같이 생성한다

해당 클래스는 EnumProperty를 사용하여 여러개의 값을 저장한다

  • OP1 : list에 대한 Append 함수를 호출
  • OP2 : list에 대한 Clear 함수를 호출

 

문자열로 사용할 new_item빈 배열인 my_list도 추가한다

 

2) 메인 패널 클래스

...

class AL_PT_main_panel(Panel):
    bl_label = "Main Panel"
    bl_idname = "AL_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_enum", expand = True)
        layout.prop(mytool, "new_item")
        
        layout.operator("al.myop_operator")
        
...

 

메인 패널로 사용할 AL_PT_main_panel 클래스를 추가한다

패널을 설정하는 draw 함수에서는 입력받아 설정PropertyGroup의 프로퍼티를 지정하고,

operator를 설정하여 패널의 동작을 정의한다

 

3) Operator 클래스

...

class AL_OT_my_op(Operator):
    bl_label = "Submit"
    bl_idname = "al.myop_operator"
    
    def execute(self, context):
        scene = context.scene
        mytool = scene.my_tool
        list = mytool.my_list
        enum = mytool.my_enum
        new_item = mytool.new_item
        
        a = "alpha"
        b = 'beta'
        
        if enum == 'OP1':
            if a and b not in list:
                list.extend((a, b))
            if new_item != "":
                list.append(new_item)
        else:
            list.clear()
            
        print(list)
        
        return {'FINISHED'}
        
...

 

AL_OT_my_op 클래스는 Operator 클래스로서 사용한다

수행할 동작을 정의하는 execute 함수의 로직은 다음과 같다

  • PropertyGroup의 값이 'OP1'인 경우 
    • "alpha"와 "beta" 문자열이 PropertyGroup의 리스트에 없다면 extend 함수를 호출하여 두 문자열을 추가한다
    • PropertyGroup의 문자열이 비어있지 않다면 Append 함수를 호출하여 해당 문자열을 리스트에 추가한다
  • PropertyGroup의 값이 'OP2'인 경우
    • PropertyGroup의 리스트로부터 clear 함수를 호출하고 리스트를 비워준다

이후 실행 결과가 반영된 PropertyGroup의 리스트콘솔창에 출력한다

 

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

...

classes = [MyProperties, AL_PT_main_panel, AL_OT_my_op]
 
 
def register():
    for cls in classes:
        bpy.utils.register_class(cls)
    bpy.types.Scene.my_tool = PointerProperty(type = MyProperties)
 
 
def unregister():
    for cls in classes:
        bpy.utils.unregister_class(cls)
    del bpy.types.Scene.my_tool
 
 
 
if __name__ == "__main__":
    register()

 

생성한 클래스들을 등록하고, 또 PropertyGroup도 설정 / 제거한다

 

- 최종 실행 결과

 

문자열을 입력하지 않고 Append - Submit한 결과는 위와 같다

 

 

문자열 "new_text"를 입력하고 Append - Submit한 결과는 위와 같다

 

 

Remove - Submit한 결과는 위와 같다

728x90
반응형

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

랜덤한 글자 생성  (0) 2025.08.19
Info 메세지 출력  (0) 2025.08.19
안개 효과 생성  (0) 2025.08.13
랜덤값  (0) 2025.08.13
모듈 임포트  (0) 2025.08.06