Notice
Recent Posts
Recent Comments
Link
«   2025/04   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30
Tags
more
Archives
Today
Total
관리 메뉴

Trikang

Blender 설치 후 VSCode에서 파이썬으로 프로그래밍하기 본문

개발 Tip

Blender 설치 후 VSCode에서 파이썬으로 프로그래밍하기

Trikang 2025. 3. 26. 15:16

디렉토리 구분 등은 맥 os를 기준으로 설명됨.

 

VSCode에 blender 관련 패키지, 확장 프로그램 설치

pip install fake-bpy-module

이후 Cmd + Shift + P로 명령 팔레트 연 다음, Blender 키워드로 입력하면 Start 옵션이 보임. 얘 실행하면 블렌더 켜짐(첫 실행 시 Blender 디렉토리를 지정하라는 안내를 받음)

 

mac os에서의 블렌더 디렉토리

유저 디렉토리(버전 4.3 기준)

~/Library/Application Support/Blender/4.3

 

스크립트 로딩에는 두 가지 방법이 있음(출처: https://docs.blender.org/api/current/info_overview.html)

  • 스크립트를 직접 실행하기
  • 스크립트를 모듈로 가져오기

아래는 환경설정 후, obj 파일을 불러와 6방향에서 이미지를 렌더링하여 저장하는 코드 예시임.

- Orthogonal Camera, scale 120

- 거리 64(본인의 프로젝트에서는 오브젝트의 좌표가 각 축마다 64를 넘지 않는다는 가정이 존재했음)

import bpy
import math

bpy.ops.object.select_all(action='SELECT')
bpy.ops.object.delete(use_global=False)

# 씬 렌더 엔진을 Eevee로 설정
bpy.context.scene.render.engine = 'CYCLES'

obj_file_path = 'obj/tv_on_desk.obj'
bpy.ops.wm.obj_import(filepath=obj_file_path)


# 카메라 및 조명 설정
cam_data = bpy.data.cameras.new("Camera")
cam_data.type = 'ORTHO'          # 카메라 모드를 orthographic으로 변경
cam_data.ortho_scale = 120       # orthographic scale을 120으로 설정
cam_obj = bpy.data.objects.new("Camera", cam_data)
bpy.context.scene.collection.objects.link(cam_obj)
bpy.context.scene.camera = cam_obj

# 조명 추가
light_data = bpy.data.lights.new(name="Light", type='POINT')
light_obj = bpy.data.objects.new(name="Light", object_data=light_data)
light_obj.location = (5, 5, 5)  # 필요에 따라 조정
bpy.context.scene.collection.objects.link(light_obj)

DIST = 64

# 카메라 위치 및 회전 설정
views = {
    "right": ((DIST/2, 0, DIST/2), (-math.radians(90), 0, 0)),
    "front": ((DIST, -DIST/2, DIST/2), (0, math.radians(90), 0)),
    "left": ((DIST/2, -DIST, DIST/2), (math.radians(90), 0, 0)),
    "back": ((0, -DIST/2, DIST/2), (0, -math.radians(90), 0)),
    "top": ((DIST/2, -DIST/2, DIST), (0, 0, 0)),
    "bottom": ((DIST/2, -DIST/2, 0), (math.radians(180), 0, 0)),
}

# 렌더링 결과 저장할 디렉토리 설정
target_dir_name = obj_file_path.split('/')[-1].split('.')[0]
output_dir = f"rendered/{target_dir_name}/"
print(output_dir)

# 각 각도에서 렌더링 및 이미지 저장
scene = bpy.context.scene
for view_name, (location, rotation) in views.items():
    cam_obj.location = location
    cam_obj.rotation_euler = rotation
    scene.render.filepath = f"{output_dir}{view_name}.png"
    bpy.ops.render.render(write_still=True)
Comments