Skip to content

Instantly share code, notes, and snippets.

@timknip
Created December 19, 2022 11:15
Show Gist options
  • Save timknip/54c80742aeffec48b1196fc996a7f517 to your computer and use it in GitHub Desktop.
Save timknip/54c80742aeffec48b1196fc996a7f517 to your computer and use it in GitHub Desktop.
import bpy
from bpy_extras.io_utils import ExportHelper
import os
import sys
import shutil
import struct
import tempfile
import zipfile
bl_info = {
"name": 'USDZ export',
"category": 'Export',
"version": (1, 0, 8),
"blender": (3, 0, 0),
'location': 'File > Export > USDZ',
'description': 'Addon export USDZ models.',
'tracker_url': 'https://github.com/floorplanner/fp.blender.render/issues/', # Replace with your issue tracker
'isDraft': False,
'developer': 'Tim Knip',
'url': 'https://github.com/floorplanner/fp.blender.render',
}
def usdzip(output_filename, source_dir, verbose=False):
""" zip files with 64 byte alignment
see: https://github.com/101arrowz/fflate/issues/39#issuecomment-777263109
"""
offset = 0
with zipfile.ZipFile(output_filename, "w", zipfile.ZIP_STORED) as zip:
relroot = os.path.abspath(
os.path.join(source_dir, os.pardir, os.path.basename(source_dir)))
for root, dirs, files in os.walk(source_dir):
for file in files:
filename = os.path.join(root, file)
if os.path.isfile(filename): # regular files only
info = zipfile.ZipInfo.from_file(filename)
arcname = os.path.join(
os.path.relpath(root, relroot), file).replace('\\', '/')
info.filename = arcname
if verbose:
print('adding %s' % arcname)
header_size = 34 + len(arcname)
offset += header_size;
offset_mod64 = offset & 63
if offset_mod64 != 4:
pad_length = 64 - offset_mod64
info.extra = struct.pack(
'hh%ds' % pad_length,
12345,
pad_length,
b' ' * pad_length)
with open(filename, 'rb') as f:
data = f.read()
zip.writestr(info, data)
offset = len(data)
def export_usdz(outfile):
print('saving model as %s' % outfile)
tmp = tempfile.mkdtemp()
basename = os.path.basename(outfile)[:-5]
out_usdc = os.path.join(tmp, '%s.usdc' % basename)
print('exporting tmp file %s' % out_usdc)
try:
if not os.path.exists(tmp):
os.makedirs(tmp)
bpy.ops.wm.usd_export(
filepath=out_usdc,
generate_preview_surface=True)
usdzip(outfile, tmp, True)
except Exception as e:
print(e)
shutil.rmtree(tmp)
class OpenFileBrowserOperator(bpy.types.Operator, ExportHelper):
"""Export current scene to USDZ"""
bl_label = "Export file"
bl_idname = "usdz.filebrowser"
filter_glob: bpy.props.StringProperty(
default='*.usdz',
options={'HIDDEN'}
)
filename_ext: bpy.props.StringProperty(
default='.usdz',
options={'HIDDEN'}
)
check_existing: bpy.props.BoolProperty(
name="Check Existing",
description="Check and warn on overwriting existing files",
default=True,
options={'HIDDEN'},
)
def execute(self, context):
export_usdz(self.filepath)
return {'FINISHED'}
def menu_func(self, context):
self.layout.operator(OpenFileBrowserOperator.bl_idname, text="Universal Scene Description (.usdz)")
def register():
bpy.utils.register_class(OpenFileBrowserOperator)
bpy.types.TOPBAR_MT_file_export.append(menu_func)
def unregister():
bpy.utils.unregister_class(OpenFileBrowserOperator)
bpy.types.TOPBAR_MT_file_export.remove(menu_func)
if __name__ == "__main__":
register()
@timknip
Copy link
Author

timknip commented Dec 19, 2022

In Blender 3.0 and higher:

  1. Edit > Preferences > Add-ons
  2. Install > browse to usdz-ezport.addon.py and check the checkbox

There will be a new entry under File > Export > Universal Scene Description (.usdz)

The addon uses the existing export to USDC and zips the created directory following the specs for the zip file

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment