Python에서 3D 장면을 저장하는 방법
Aspose.3D FOSS for Python은 당신이 저장할 수 있습니다. Scene 모든 지원되는 출력 형식에 하나를 사용하여 Scene.save() 형식 탐지 파일 경로를 통과 할 때 자동; 바이너리 출력 또는 구조 통합과 같은 고급 옵션을 위해, 당신은 형태 특정 저장 택시 개체를 제공합니다.
단계별 가이드
단계 1 : 패키지를 설치합니다.
PyPI에서 Aspose.3D FOSS를 설치합니다.이 도서관은 필요하지 않습니다.
asposefoss/3d is not yet published — build from source until it ships. See the project README for build instructions.
지원되는 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().도서관은 파일 확장에서 자동으로 원본 형식을 감지합니다.이 대신 스크래치에서 장면을 만들려면, 참조 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() A와 함께 .stl 기본적으로 출력은 ASCII STL입니다.이중 ST L (작은 파일, 인간 읽을 수있는 헤드가 없음) 사용을 작성하기 위해 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 파일을 사용하여 수출할 수 있습니다. GltfExporter 그리고 GltfSaveOptions.세트 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에 저장하기
실행되지 않은:
FbxExporter.save()그리고FbxExporter.save_to_stream()상승NotImplementedError현재 출시 중 FBX 수출은 Aspose.3D FOSS에서 Python에 사용할 수 없습니다.대신 GLB, OBJ, STL 또는 Collada (DAE) 형식을 사용하십시오. 작업 수출 옵션을 위한 단계 4, 5 및 7 참조.
# The following code raises NotImplementedError in the current release:
# from aspose.threed.formats.fbx import FbxExporter, FbxSaveOptions
# exporter = FbxExporter()
# exporter.save(scene, "output.fbx", options) # raises NotImplementedError
# Use a supported format instead:
scene.save("output.glb") # binary GLB — recommended
scene.save("output.obj") # Wavefront OBJ
scene.save("output.stl") # STL단계 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() 이것은 일반적으로 장면의 뿌리 노드가 지질을 가진 어린이 노드를 가지고 있지 않다는 것을 의미합니다. scene.root_node 전화를 하기 전에 save.확인하기 len(scene.root_node.child_nodes) 장면을 만들고 나서.
AttributeError 메쉬 지질을 구축하는 경우 그들의 Mesh 클래스 스토어는 내부 컨트롤 포인트 목록으로 좌석을 제공합니다. 자세한 메시지 건설 패턴은 다음과 같습니다. Python에서 메시지를 만드는 방법 폴리곤 창조, 척추 요소 및 UV 데이터를 다루는 기사를 포함합니다.
GLB 생산량은 예상보다 더 크다. 바이너리 GLB는 모든 지질 및 구조 데이터를 포함합니다. GltfSaveOptions.flip_tex_coord_v 설정되어 있는 것에 대하여 True, 추가 코디네이터-플립 패스가 포함되어 있습니다. False V-axis 텍스처가 필요하지 않은 경우.
FBX 수출은 현재 출시에서 사용할 수 없습니다. FbxExporter.save() 상승 NotImplementedError.FBX 수입 (로드) .fbx 파일) 정상적으로 작동하지만 FBX로 수출이 실행되지 않습니다. 대신 GLB, OBJ, STL 또는 Collada로 변환합니다.
Collada DAE는 재료를 포함하지 않습니다. 세트 ColladaSaveOptions.enable_materials = True (이것은 False 수출하기 전에 (예를 들어, 수입하기 전).
자주 묻는 질문들
어떤 형식으로 Aspose.3D FOSS를 Python 수출 할 수 있습니까?
도서관은 다음으로 수출을 지원합니다 : STL, glTF 2.0 (텍스트 및 바이너리 GLB), OBJ, Collada (DAE). 형식 탐지 자동으로 파일 경로 스트립을 통과 할 때 Scene.save();도서관은 확장을 읽고 올바른 수출자를 선택합니다.
노트: FBX 수출은 실행되지 않음 현재의 발행 -
FbxExporter.save()상승NotImplementedError.대신 GLB, OBJ, STL 또는 Collada를 사용하십시오.
디스크에 글을 쓰지 않는 스트리밍 수출 API가 있습니까?
예요 ᄒᄒ. GltfExporter.export(scene, stream, options) 누구에게나 글을 쓰는 것 io.BytesIO 메모리 뷔퍼를 웹 응답 또는 파일 시스템에 접촉하지 않고도 추가 처리로 직접 전송할 수 있습니다.
어떻게 하나의 형식에서 다른 형태로 장면을 변환합니까?
장면을 켜라 Scene.from_file("input.fbx") 저장하고 함께 하여 scene.save("output.gltf").도서관은 메모리 변환을 관리합니다; 중간 파일이 필요하지 않습니다.
여러 개의 하위 스크립트를 별도의 파일로 저장할 수 있습니까?
접근 scene.sub_scenes 각 하위 장면을 이테라이트, 새로운 창조 Scene 개체, 관련 노드를 첨부하고 전화 save() 각각에 대하여.
는 하는 Scene.save() 기존 파일을 조용히 작성하십시오.?
예. 도서관은 이미 대상 파일이 존재하는 경우 오류를 발생하지 않습니다; 그것은 과장합니다.당신의 코드에 파일-존재 확인을 추가하면 우연한 과장을 방지해야합니다.