728x90
반응형
- 개요
랜덤한 3개의 단어를 생성하는 Add-On을 만든다
1) PropertyGroup 클래스
import bpy
from bpy.types import Panel, Operator, PropertyGroup
from bpy.props import IntProperty, PointerProperty
from random import randint
class MyProperties(PropertyGroup):
list_a = ["A", "Dr", "Mr", "Mrs", "Our", "The"]
list_b = ["Adorable", "Adventurous", "Agressive", ... ]
list_c = ["Actor", "Anchor", "Antagonist", "Apple", ... ]
num_1 : IntProperty(default = 0)
num_2 : IntProperty(default = 0)
num_3 : IntProperty(default = 0)
...
PropertyGroup 클래스를 위와 같이 생성하였다
구성은 다음과 같다
- 3개의 리스트 : 랜덤으로 생성할 단어를 저장하는 역할
- 3개의 정수형 Property : 3개의 리스트에 접근하기 위한 인덱스를 지정하는 역할
2) 메인 패널 클래스
...
class AR_PT_main_panel(Panel):
bl_label = "Main Panel"
bl_idname = "AR_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.label(text = mytool.list_a[mytool.num_1])
layout.label(text = mytool.list_b[mytool.num_2])
layout.label(text = mytool.list_c[mytool.num_3])
layout.operator("ar.myop_operator")
...
메인 패널 클래스를 위와 같이 구성하였다
메인 패널을 설정하는 draw 함수의 로직은 다음과 같다
- 3개의 label을 추가하여 PropertyGroup의 리스트에 저장한 단어들을 보여준다
- operator를 지정하여 패널에서 수행하는 동작을 지정한다
3) Operator 클래스
...
class AR_OT_my_op(Operator):
bl_label = "Add Object"
bl_idname = "ar.myop_operator"
def execute(self, context):
scene = context.scene
mytool = scene.my_tool
r1 = randint(0, len(mytool.list_a) - 1)
mytool.num_1 = r1
r2 = randint(0, len(mytool.list_b) - 1)
mytool.num_2 = r2
r3 = randint(0, len(mytool.list_c) - 1)
mytool.num_3 = r3
return {'FINISHED'}
...
Operator 클래스를 위와 같이 구성하였다
동작을 정의하는 execute 함수의 로직은 다음과 같다
- PropertyGroup의 각각의 리스트의 크기만큼의 랜덤한 값을 가져와 저장한다
- 저장한 값을 PropertyGroup의 정수 Property에 설정한다
- 설정된 정수 Property를 사용하여 메인 패널 클래스에서 보여줄 단어를 설정한다
4) 클래스의 등록 / 등록해제
classes = [MyProperties, AR_PT_main_panel, AR_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()
위와 같이 register / unregister 함수를 정의하여 생성한 클래스를 등록 / 등록해제하고
PropertyGroup을 설정 / 제거한다
- 최종 실행 결과
728x90
반응형
'blender > blender python' 카테고리의 다른 글
| Redo 패널 (0) | 2025.08.20 |
|---|---|
| 타이머 (0) | 2025.08.19 |
| Info 메세지 출력 (0) | 2025.08.19 |
| 리스트 (0) | 2025.08.19 |
| 안개 효과 생성 (0) | 2025.08.13 |