Python에서 3D 씬 저장하는 방법

Python에서 3D 씬 저장하는 방법

Aspose.3D FOSS for Python은 단일 Scene.save() 호출을 사용하여 Scene을(를) 지원되는 모든 출력 형식으로 저장할 수 있게 합니다. 파일 경로를 전달하면 형식 감지가 자동으로 이루어지며, 이진 출력이나 텍스처 포함과 같은 고급 옵션을 위해서는 형식별 저장 옵션 객체를 제공해야 합니다.

단계별 가이드

1단계: 패키지 설치

PyPI에서 Aspose.3D FOSS를 설치하십시오. 네이티브 라이브러리는 필요하지 않습니다.

pip install aspose-3d-foss

지원되는 Python 버전: 3.7, 3.8, 3.9, 3.10, 3.11, 3.12.


2단계: 필요한 클래스 가져오기

최소한 Scene이 필요합니다. 기본 동작이 아닌 경우에만 형식별 익스포터 또는 저장 옵션 클래스를 가져오세요.

from aspose.threed import Scene

형식별 옵션:

from aspose.threed.formats.gltf import GltfSaveOptions, GltfExporter
from aspose.threed.formats.stl import StlFormat, StlSaveOptions
from aspose.threed.formats.fbx import FbxExporter, FbxSaveOptions
from aspose.threed.formats.collada import ColladaExporter, ColladaSaveOptions

3단계: 장면 로드

Scene.from_file()를 사용하여 디스크에서 기존 씬을 로드합니다. 라이브러리는 파일 확장자를 통해 소스 형식을 자동으로 감지합니다. 대신 처음부터 씬을 구축하려면 How to Build a Mesh in Python을 참조하십시오.

# Load from an existing file — format auto-detected from extension
scene = Scene.from_file("input.obj")

또는 Scene.open()를 통해 명시적 옵션으로 장면을 엽니다:

from aspose.threed import Scene

scene = Scene()
scene.open("input.fbx")

4단계: STL로 저장

Scene.save().stl 경로와 함께 호출하십시오. 기본적으로 출력은 ASCII STL입니다. 이진 STL(파일이 더 작고 사람이 읽을 수 있는 헤더가 없음)을 쓰려면 StlSaveOptions을 사용하십시오.

# ASCII STL — format detected from the .stl extension
scene.save("output.stl")

# Binary STL — smaller file size
from aspose.threed.formats.stl import StlFormat, StlSaveOptions

stl_format = StlFormat()
options = stl_format.create_save_options()
options.binary_mode = True
scene.save("output_binary.stl", options)

5단계: glTF 또는 GLB로 저장

GLTF 2.0 파일은 GltfExporterGltfSaveOptions을 사용하여 내보낼 수 있습니다. binary_mode = True을 설정하면 자체 포함된 .glb 바이너리 번들을 생성하고, binary_mode = False을 설정하면 JSON 기반 .gltf 형식을 사용합니다.

import io
from aspose.threed.formats.gltf import GltfExporter, GltfSaveOptions

# Text glTF
options = GltfSaveOptions()
options.binary_mode = False
options.file_name = "output.gltf"

exporter = GltfExporter()
with open("output.gltf", "wb") as f:
    stream = io.BytesIO()
    exporter.export(scene, stream, options)
    f.write(stream.getvalue())

# Binary GLB
options_glb = GltfSaveOptions()
options_glb.binary_mode = True
options_glb.file_name = "output.glb"

stream_glb = io.BytesIO()
exporter.export(scene, stream_glb, options_glb)
with open("output.glb", "wb") as f:
    f.write(stream_glb.getvalue())

6단계: FBX로 저장

FBX 씬은 FbxExporter를 통해 내보냅니다. FbxSaveOptions를 사용하여 압축을 활성화하거나 텍스처를 출력 파일에 포함시킵니다.

from aspose.threed.formats.fbx import FbxExporter, FbxSaveOptions

options = FbxSaveOptions()
options.enable_compression = True
options.embed_textures = False  # keep textures as separate files

exporter = FbxExporter()
exporter.save(scene, "output.fbx", options)

7단계: OBJ 또는 Collada (DAE)로 저장

OBJ와 Collada의 경우, 파일 경로를 Scene.save()에 직접 전달하십시오. 라이브러리는 확장자를 통해 형식을 감지합니다.

# OBJ — format auto-detected from .obj extension
scene.save("output.obj")

# Collada DAE — with material and coordinate-system options
from aspose.threed.formats.collada import ColladaExporter, ColladaSaveOptions

options = ColladaSaveOptions()
options.enable_materials = True
options.flip_coordinate_system = False
options.indented = True

exporter = ColladaExporter()
exporter.export(scene, open("output.dae", "wb"), options)

일반적인 문제 및 해결책

scene.save() 이후 빈 출력 파일
이는 일반적으로 씬의 루트 노드에 기하학을 가진 자식 노드가 없음을 의미합니다. save을 호출하기 전에 각 메쉬 노드가 scene.root_node에 연결되었는지 확인하십시오. 씬을 구축한 후 len(scene.root_node.child_nodes)을 확인하십시오.

AttributeError 메시 지오메트리 구축 시
Mesh 클래스는 정점을 내부 제어점 목록으로 저장합니다. 자세한 메시 구성 패턴은 Python에서 메쉬 구축 방법 기사에서 폴리곤 생성, 정점 요소 및 UV 데이터를 다룹니다.

GLB 출력이 예상보다 큽니다
Binary GLB는 모든 기하와 텍스처 데이터를 인라인합니다. GltfSaveOptions.flip_tex_coord_vTrue로 설정되면 추가 좌표 플립 패스가 포함됩니다. V축 텍스처 플립이 필요하지 않다면 False로 설정하십시오.

FBX 가져오기/내보내기 라운드트립에서 재질이 손실됩니다
FBX 재질 내보내기는 FbxSaveOptions.export_legacy_material_properties에 의해 제어됩니다. 이를 True로 설정하면 타사 도구가 읽을 수 있는 표준 FBX 재질 블록을 작성합니다.

Collada DAE는 재질을 포함하지 않습니다
내보내기 전에 ColladaSaveOptions.enable_materials = True를 설정하십시오(기본값은 False입니다).

자주 묻는 질문

Aspose.3D FOSS for Python이 내보낼 수 있는 형식은 무엇입니까?

이 라이브러리는 다음으로 내보내기를 지원합니다: STL, glTF 2.0 (텍스트 및 바이너리 GLB), FBX, OBJ, Collada (DAE), 및 3MF. 파일 경로 문자열을 Scene.save()에 전달하면 형식 감지가 자동으로 이루어지며, 라이브러리는 확장자를 읽어 올바른 내보내기 도구를 선택합니다.

디스크에 쓰는 것을 피하는 스트리밍 내보내기 API가 있나요?

예. GltfExporter.export(scene, stream, options)는 모든 io.BytesIO 또는 파일과 유사한 객체에 씁니다. 파일 시스템에 접근하지 않고 메모리 버퍼를 웹 응답이나 추가 처리에 직접 전달할 수 있습니다.

하나의 형식에서 다른 형식으로 씬을 변환하려면 어떻게 해야 하나요?

Scene.from_file("input.fbx")를 사용하여 씬을 로드하고 scene.save("output.gltf")로 저장합니다. 라이브러리는 메모리 내 변환을 처리하므로 중간 파일이 필요하지 않습니다.

여러 개의 서브 씬을 별도의 파일에 저장할 수 있나요?

scene.sub_scenes에 접근하여 각 서브 씬을 반복하고, 새로운 Scene 객체를 생성하고, 관련 노드를 연결한 뒤, 각에 대해 save()을 호출합니다.

Scene.save()가 기존 파일을 조용히 덮어쓰나요?

예. 라이브러리는 대상 파일이 이미 존재해도 오류를 발생시키지 않으며, 덮어씁니다. 실수로 덮어쓰는 것을 방지하려면 코드에 파일 존재 여부 확인을 추가하십시오.

참고

 한국어