1- """Build the installable extension zips with Blender's own builder.
1+ """Build the installable extension zip with Blender's own builder: one zip for every platform .
22
3- python scripts/build.py # every platform -> dist/
4- python scripts/build.py --platform windows-x64 # one platform, for local testing
3+ python scripts/build.py # -> dist/blender_ai-<version>.zip
4+ python scripts/build.py --vendor # download the wheels first
55 python scripts/build.py --blender "C:/Program Files/Blender Foundation/Blender 5.2/blender.exe"
66
7- Steps: stage src/ into build/, write the `wheels` list for the bundled wheels into the
8- staged manifest, then run `blender --command extension build --split-platforms` and validate.
7+ Steps: stage src/ into build/, list every bundled wheel in the staged manifest (with the platforms those
8+ wheels cover), then run `blender --command extension build` and validate. On install, Blender extracts
9+ only the wheels that match the user's platform and Python version.
910"""
1011
1112from __future__ import annotations
1920import zipfile
2021from pathlib import Path
2122
23+ from vendor_wheels import PLATFORM_TAGS
24+
2225ROOT = Path (__file__ ).resolve ().parent .parent
2326SOURCE = ROOT / "src"
2427STAGE = ROOT / "build" / "blender_ai"
2528DIST = ROOT / "dist"
2629
27- sys .path .insert (0 , str (ROOT / "scripts" ))
28- from vendor_wheels import PLATFORM_TAGS
29-
3030WINDOWS_SITE_PACKAGES_LEN = 110
3131WINDOWS_MAX_PATH = 259
3232
33- WHEEL_PLATFORM_MARKERS = {
34- "windows-x64" : ("win_amd64" ,),
35- "linux-x64" : ("manylinux" , "linux_x86_64" ),
36- "macos-arm64" : ("arm64" , "universal2" ),
37- "macos-x64" : ("x86_64" , "universal2" ),
38- }
39-
4033
4134def find_blender (explicit : str | None ) -> str :
4235 candidates = [explicit , os .environ .get ("BLENDER" )]
@@ -52,79 +45,56 @@ def find_blender(explicit: str | None) -> str:
5245 raise SystemExit ("Blender not found; pass --blender or set BLENDER." )
5346
5447
55- def wheel_matches (name : str , platforms : list [str ]) -> bool :
56- tag = name [:- 4 ].split ("-" )[- 1 ]
57- if tag == "any" :
58- return True
59- for platform in platforms :
60- markers = WHEEL_PLATFORM_MARKERS [platform ]
61- if platform == "macos-x64" and "macosx" not in tag :
62- continue
63- if platform == "macos-arm64" and "macosx" not in tag :
64- continue
65- if any (m in tag for m in markers ):
66- return True
67- return False
48+ def covered_platforms (wheels : list [str ]) -> list [str ]:
49+ tags = [name [:- 4 ].split ("-" )[- 1 ] for name in wheels if name .startswith ("pydantic_core-" )]
50+ return [platform for platform , markers in PLATFORM_TAGS .items () if any (m in tag for tag in tags for m in markers )]
6851
6952
70- def stage (platforms : list [str ]) -> list [str ]:
53+ def stage () -> tuple [ list [str ], list [str ] ]:
7154 if STAGE .exists ():
7255 shutil .rmtree (STAGE )
73- shutil .copytree (SOURCE , STAGE , ignore = shutil .ignore_patterns ("__pycache__" , "*.pyc" , "*.whl" ))
74- wheels = sorted (p for p in (SOURCE / "wheels" ).glob ("*.whl" ) if wheel_matches (p .name , platforms ))
75- if not wheels :
76- raise SystemExit ("No wheels vendored; run scripts/vendor_wheels.py first." )
77- (STAGE / "wheels" ).mkdir (exist_ok = True )
78- for whl in wheels :
79- shutil .copy2 (whl , STAGE / "wheels" / whl .name )
80-
56+ shutil .copytree (SOURCE , STAGE , ignore = shutil .ignore_patterns ("__pycache__" , "*.pyc" ))
57+ wheels = sorted (p .name for p in (STAGE / "wheels" ).glob ("*.whl" ))
58+ platforms = covered_platforms (wheels )
59+ if not platforms :
60+ raise SystemExit ("No wheels vendored for a supported platform; run scripts/vendor_wheels.py first." )
61+ listing = "" .join (f' "./wheels/{ name } ",\n ' for name in wheels )
62+ block = "platforms = [" + ", " .join (f'"{ p } "' for p in platforms ) + "]\n wheels = [\n " + listing + "]\n "
8163 manifest = (STAGE / "blender_manifest.toml" ).read_text (encoding = "utf-8" )
82- listing = "wheels = [\n " + "" .join (f' "./wheels/{ w .name } ",\n ' for w in wheels ) + "]\n "
83- manifest = re .sub (r"(?m)^# The `wheels` list is written by scripts/build\.py.*\n" , listing , manifest )
84- manifest = re .sub (r"(?m)^platforms = \[.*\]$" , "platforms = [" + ", " .join (f'"{ p } "' for p in platforms ) + "]" , manifest )
64+ manifest = re .sub (r"(?m)^platforms = \[.*\]\n" , lambda _match : block , manifest , count = 1 )
8565 (STAGE / "blender_manifest.toml" ).write_text (manifest , encoding = "utf-8" )
86- return [ w . name for w in wheels ]
66+ return wheels , platforms
8767
8868
8969def warn_long_paths (wheels : list [str ]) -> None :
9070 longest , member = 0 , ""
9171 for name in wheels :
92- with zipfile .ZipFile (SOURCE / "wheels" / name ) as zf :
72+ with zipfile .ZipFile (STAGE / "wheels" / name ) as zf :
9373 for item in zf .namelist ():
9474 if len (item ) > longest :
9575 longest , member = len (item ), item
96- room = WINDOWS_MAX_PATH - WINDOWS_SITE_PACKAGES_LEN - longest
97- if room < 25 :
76+ if WINDOWS_MAX_PATH - WINDOWS_SITE_PACKAGES_LEN - longest < 25 :
9877 print (f"WARNING: { member } is { longest } characters; Windows users with long profile paths may fail to install." )
9978
10079
10180def main () -> int :
10281 parser = argparse .ArgumentParser (description = __doc__ , formatter_class = argparse .RawDescriptionHelpFormatter )
103- parser .add_argument ("--platform" , choices = sorted (PLATFORM_TAGS ), action = "append" )
10482 parser .add_argument ("--blender" )
10583 parser .add_argument ("--vendor" , action = "store_true" , help = "download wheels first" )
10684 args = parser .parse_args ()
107-
108- platforms = args .platform or sorted (PLATFORM_TAGS )
10985 if args .vendor :
110- cmd = [sys .executable , str (ROOT / "scripts" / "vendor_wheels.py" )]
111- for p in platforms :
112- cmd += ["--platform" , p ]
113- subprocess .run (cmd , check = True )
114-
86+ subprocess .run ([sys .executable , str (ROOT / "scripts" / "vendor_wheels.py" )], check = True )
11587 blender = find_blender (args .blender )
116- wheels = stage (platforms )
88+ wheels , platforms = stage ()
11789 print (f"Staged { len (wheels )} wheels for { ', ' .join (platforms )} " )
118- if "windows-x64" in platforms :
119- warn_long_paths (wheels )
90+ warn_long_paths (wheels )
12091 DIST .mkdir (exist_ok = True )
121- build = [blender , "--factory-startup" , "--command" , "extension" , "build" , "--source-dir" , str (STAGE ), "--output-dir" , str (DIST )]
122- if len (platforms ) > 1 or args .platform :
123- build .append ("--split-platforms" )
124- subprocess .run (build , check = True )
125- for zip_path in sorted (DIST .glob ("blender_ai-*.zip" )):
126- subprocess .run ([blender , "--factory-startup" , "--command" , "extension" , "validate" , str (zip_path )], check = True )
127- print (f"Built { zip_path .relative_to (ROOT )} ({ zip_path .stat ().st_size / 1e6 :.1f} MB)" )
92+ for old in DIST .glob ("blender_ai-*.zip" ):
93+ old .unlink ()
94+ subprocess .run ([blender , "--factory-startup" , "--command" , "extension" , "build" , "--source-dir" , str (STAGE ), "--output-dir" , str (DIST )], check = True )
95+ zip_path = next (DIST .glob ("blender_ai-*.zip" ))
96+ subprocess .run ([blender , "--factory-startup" , "--command" , "extension" , "validate" , str (zip_path )], check = True )
97+ print (f"Built { zip_path .relative_to (ROOT )} ({ zip_path .stat ().st_size / 1e6 :.1f} MB)" )
12898 return 0
12999
130100
0 commit comments