"""Dedicated request converter; does not modify the shop's existing converter.""" import bpy import sys import os import zipfile def operator_options(operator, wanted): properties = operator.get_rna_type().properties.keys() return {key: value for key, value in wanted.items() if key in properties} def main(): args = sys.argv[sys.argv.index('--') + 1:] if len(args) != 3: raise RuntimeError('Uso: request_convert.py entrada glb-ou-hifen usdz-ou-hifen') source, glb, usdz = args source = os.path.abspath(source) extension = os.path.splitext(source)[1].lower() bpy.ops.object.select_all(action='SELECT') bpy.ops.object.delete(use_global=False) if extension == '.glb': bpy.ops.import_scene.gltf(filepath=source) elif extension == '.usdz': options = operator_options(bpy.ops.wm.usd_import, { 'filepath': source, 'import_materials': True, 'import_textures_mode': 'IMPORT_PACK', 'import_cameras': False, 'import_lights': False, }) bpy.ops.wm.usd_import(**options) else: raise RuntimeError('Entrada deve ser GLB ou USDZ. O OBJ passa primeiro pelo conversor Node existente.') if not any(obj.type == 'MESH' for obj in bpy.context.scene.objects): raise RuntimeError('O modelo não contém uma malha importável.') # Detect absent texture resources instead of silently exporting untextured materials. for image in bpy.data.images: if image.source == 'FILE' and not image.packed_file and not image.has_data: if not os.path.isfile(bpy.path.abspath(image.filepath)): raise RuntimeError('Textura ausente no modelo: ' + image.name) bpy.context.scene.unit_settings.system = 'METRIC' bpy.context.scene.unit_settings.scale_length = 1.0 if glb != '-': glb = os.path.abspath(glb) os.makedirs(os.path.dirname(glb), exist_ok=True) bpy.ops.export_scene.gltf(**operator_options(bpy.ops.export_scene.gltf, { 'filepath': glb, 'export_format': 'GLB', 'export_materials': 'EXPORT', 'export_yup': True, 'export_cameras': False, 'export_lights': False, })) with open(glb, 'rb') as stream: if stream.read(4) != b'glTF': raise RuntimeError('O Blender não gerou um GLB válido.') if usdz != '-': usdz = os.path.abspath(usdz) os.makedirs(os.path.dirname(usdz), exist_ok=True) wanted = { 'filepath': usdz, 'export_materials': True, 'export_textures': True, 'export_uvmaps': True, 'generate_preview_surface': True, 'relative_paths': True, 'overwrite_textures': True, 'export_textures_mode': 'NEW', 'export_cameras': False, 'export_lights': False, } bpy.ops.wm.usd_export(**operator_options(bpy.ops.wm.usd_export, wanted)) if not zipfile.is_zipfile(usdz) or os.path.getsize(usdz) == 0: raise RuntimeError('O Blender não gerou um pacote USDZ válido. Confira o suporte USD desta instalação.') print('REQUEST_CONVERSION_OK') if __name__ == '__main__': main()