Update app.py
Browse files
app.py
CHANGED
|
@@ -1829,49 +1829,18 @@ def similarity_matching(sprites_data: dict, project_folder: str, top_k: int = 1,
|
|
| 1829 |
# Flatten unique matched indices to process copying once per folder
|
| 1830 |
matched_indices = sorted({idx for lst in per_sprite_matched_indices for idx in lst})
|
| 1831 |
print("matched_indices------------------>",matched_indices)
|
| 1832 |
-
|
| 1833 |
for matched_idx in matched_indices:
|
| 1834 |
matched_image_path = paths_list[matched_idx]
|
| 1835 |
-
|
| 1836 |
-
# --- Change 1: Convert path to absolute path for reliable comparison
|
| 1837 |
-
matched_image_path = os.path.abspath(matched_image_path)
|
| 1838 |
-
|
| 1839 |
matched_folder = os.path.dirname(matched_image_path)
|
| 1840 |
matched_filename = os.path.basename(matched_image_path)
|
| 1841 |
-
|
| 1842 |
-
|
| 1843 |
-
|
| 1844 |
-
|
| 1845 |
-
|
| 1846 |
-
mf_repr = repr(matched_folder)
|
| 1847 |
-
mf_abspath_repr = repr(mf_abs)
|
| 1848 |
-
|
| 1849 |
-
# backdrop / sprite base (may be pathlib.Path in prod)
|
| 1850 |
-
bb = os.path.normpath(os.path.abspath(str(backdrop_base_path)))
|
| 1851 |
-
sb = os.path.normpath(os.path.abspath(str(sprite_base_path)))
|
| 1852 |
-
|
| 1853 |
-
# quick membership tests
|
| 1854 |
-
# --- Change 3: Use os.path.commonpath for reliable cross-platform checks
|
| 1855 |
-
starts_with_backdrop = (os.path.commonpath([mf_abs, bb]) == bb)
|
| 1856 |
-
starts_with_sprite = (os.path.commonpath([mf_abs, sb]) == sb)
|
| 1857 |
-
|
| 1858 |
-
print("DEBUG matched_idx:", matched_idx)
|
| 1859 |
-
print("DEBUG matched_image_path:", matched_image_path)
|
| 1860 |
-
print("DEBUG matched_folder (raw):", mf_repr)
|
| 1861 |
-
print("DEBUG matched_folder (abs):", mf_abspath_repr)
|
| 1862 |
-
print("DEBUG backdrop_base_path (raw):", repr(backdrop_base_path))
|
| 1863 |
-
print("DEBUG backdrop_base_path (abs):", repr(bb))
|
| 1864 |
-
print("DEBUG sprite_base_path (raw):", repr(sprite_base_path))
|
| 1865 |
-
print("DEBUG sprite_base_path (abs):", repr(sb))
|
| 1866 |
-
print("DEBUG starts_with_backdrop:", starts_with_backdrop, " starts_with_sprite:", starts_with_sprite)
|
| 1867 |
-
print("DEBUG common_with_backdrop:", common_with_backdrop, " common_with_sprite:", common_with_sprite)
|
| 1868 |
-
# Show a short sample of the reference path for matched index
|
| 1869 |
-
print("DEBUG sample paths_list entry:", repr(paths_list[matched_idx])[:400])
|
| 1870 |
-
except Exception as _e:
|
| 1871 |
-
print("DEBUG diagnostics failed:", _e)
|
| 1872 |
# If it's a sprite (under SPRITE_DIR) -> copy sprite assets and read sprite.json
|
| 1873 |
-
|
| 1874 |
-
|
| 1875 |
copied_sprite_folders.add(matched_folder)
|
| 1876 |
sprite_json_path = os.path.join(matched_folder, "sprite.json")
|
| 1877 |
if os.path.exists(sprite_json_path):
|
|
@@ -1879,44 +1848,60 @@ def similarity_matching(sprites_data: dict, project_folder: str, top_k: int = 1,
|
|
| 1879 |
with open(sprite_json_path, "r", encoding="utf-8") as f:
|
| 1880 |
sprite_info = json.load(f)
|
| 1881 |
project_data.append(sprite_info)
|
|
|
|
| 1882 |
except Exception as e:
|
| 1883 |
-
print("Failed to read sprite.json in
|
| 1884 |
else:
|
| 1885 |
-
print("No sprite.json in
|
|
|
|
| 1886 |
# copy non-matching files from the sprite folder (except the matched image and sprite.json)
|
| 1887 |
-
|
|
|
|
|
|
|
| 1888 |
if fname in (matched_filename, "sprite.json"):
|
|
|
|
| 1889 |
continue
|
| 1890 |
src = os.path.join(matched_folder, fname)
|
| 1891 |
dst = os.path.join(project_folder, fname)
|
| 1892 |
if os.path.isfile(src):
|
| 1893 |
try:
|
| 1894 |
shutil.copy2(src, dst)
|
|
|
|
| 1895 |
except Exception as e:
|
| 1896 |
-
print("Failed to copy sprite asset
|
|
|
|
|
|
|
| 1897 |
|
| 1898 |
# If it's a backdrop (under BACKDROP_DIR) -> copy backdrop assets and read project.json for stage
|
| 1899 |
-
|
| 1900 |
-
|
| 1901 |
copied_backdrop_folders.add(matched_folder)
|
|
|
|
| 1902 |
# copy matched backdrop image
|
|
|
|
| 1903 |
try:
|
| 1904 |
-
shutil.copy2(matched_image_path,
|
| 1905 |
-
print("Copied matched backdrop image
|
| 1906 |
except Exception as e:
|
| 1907 |
-
print("Failed to copy matched backdrop image
|
| 1908 |
|
| 1909 |
# copy other files from folder (skip project.json and matched image)
|
| 1910 |
-
|
|
|
|
|
|
|
| 1911 |
if fname in (matched_filename, "project.json"):
|
|
|
|
| 1912 |
continue
|
| 1913 |
src = os.path.join(matched_folder, fname)
|
| 1914 |
dst = os.path.join(project_folder, fname)
|
| 1915 |
if os.path.isfile(src):
|
| 1916 |
try:
|
| 1917 |
shutil.copy2(src, dst)
|
|
|
|
| 1918 |
except Exception as e:
|
| 1919 |
-
print("Failed to copy backdrop asset
|
|
|
|
|
|
|
| 1920 |
|
| 1921 |
# read project.json to extract Stage/targets
|
| 1922 |
pj = os.path.join(matched_folder, "project.json")
|
|
@@ -1924,13 +1909,18 @@ def similarity_matching(sprites_data: dict, project_folder: str, top_k: int = 1,
|
|
| 1924 |
try:
|
| 1925 |
with open(pj, "r", encoding="utf-8") as f:
|
| 1926 |
bd_json = json.load(f)
|
|
|
|
| 1927 |
for tgt in bd_json.get("targets", []):
|
| 1928 |
if tgt.get("isStage"):
|
| 1929 |
backdrop_data.append(tgt)
|
|
|
|
|
|
|
| 1930 |
except Exception as e:
|
| 1931 |
-
print("Failed to read project.json in
|
| 1932 |
else:
|
| 1933 |
-
print("No project.json in
|
|
|
|
|
|
|
| 1934 |
|
| 1935 |
# --- Merge into final Scratch project.json (identical logic to before)
|
| 1936 |
final_project = {
|
|
|
|
| 1829 |
# Flatten unique matched indices to process copying once per folder
|
| 1830 |
matched_indices = sorted({idx for lst in per_sprite_matched_indices for idx in lst})
|
| 1831 |
print("matched_indices------------------>",matched_indices)
|
|
|
|
| 1832 |
for matched_idx in matched_indices:
|
| 1833 |
matched_image_path = paths_list[matched_idx]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1834 |
matched_folder = os.path.dirname(matched_image_path)
|
| 1835 |
matched_filename = os.path.basename(matched_image_path)
|
| 1836 |
+
|
| 1837 |
+
print(f"Processing matched image: {matched_image_path}")
|
| 1838 |
+
print(f" - Folder: {matched_folder}")
|
| 1839 |
+
print(f" - Filename: {matched_filename}")
|
| 1840 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1841 |
# If it's a sprite (under SPRITE_DIR) -> copy sprite assets and read sprite.json
|
| 1842 |
+
if matched_folder.startswith(sprite_base_path) and matched_folder not in copied_sprite_folders:
|
| 1843 |
+
print(f"Processing SPRITE folder: {matched_folder}")
|
| 1844 |
copied_sprite_folders.add(matched_folder)
|
| 1845 |
sprite_json_path = os.path.join(matched_folder, "sprite.json")
|
| 1846 |
if os.path.exists(sprite_json_path):
|
|
|
|
| 1848 |
with open(sprite_json_path, "r", encoding="utf-8") as f:
|
| 1849 |
sprite_info = json.load(f)
|
| 1850 |
project_data.append(sprite_info)
|
| 1851 |
+
print(f" ✓ Successfully read sprite.json from {matched_folder}")
|
| 1852 |
except Exception as e:
|
| 1853 |
+
print(f" ✗ Failed to read sprite.json in {matched_folder}: {e}")
|
| 1854 |
else:
|
| 1855 |
+
print(f" ⚠ No sprite.json in {matched_folder}")
|
| 1856 |
+
|
| 1857 |
# copy non-matching files from the sprite folder (except the matched image and sprite.json)
|
| 1858 |
+
sprite_files = os.listdir(matched_folder)
|
| 1859 |
+
print(f" Files in sprite folder: {sprite_files}")
|
| 1860 |
+
for fname in sprite_files:
|
| 1861 |
if fname in (matched_filename, "sprite.json"):
|
| 1862 |
+
print(f" Skipping {fname} (matched image or sprite.json)")
|
| 1863 |
continue
|
| 1864 |
src = os.path.join(matched_folder, fname)
|
| 1865 |
dst = os.path.join(project_folder, fname)
|
| 1866 |
if os.path.isfile(src):
|
| 1867 |
try:
|
| 1868 |
shutil.copy2(src, dst)
|
| 1869 |
+
print(f" ✓ Copied sprite asset: {src} -> {dst}")
|
| 1870 |
except Exception as e:
|
| 1871 |
+
print(f" ✗ Failed to copy sprite asset {src}: {e}")
|
| 1872 |
+
else:
|
| 1873 |
+
print(f" Skipping {fname} (not a file)")
|
| 1874 |
|
| 1875 |
# If it's a backdrop (under BACKDROP_DIR) -> copy backdrop assets and read project.json for stage
|
| 1876 |
+
if matched_folder.startswith(backdrop_base_path) and matched_folder not in copied_backdrop_folders:
|
| 1877 |
+
print(f"Processing BACKDROP folder: {matched_folder}")
|
| 1878 |
copied_backdrop_folders.add(matched_folder)
|
| 1879 |
+
|
| 1880 |
# copy matched backdrop image
|
| 1881 |
+
backdrop_dst = os.path.join(project_folder, matched_filename)
|
| 1882 |
try:
|
| 1883 |
+
shutil.copy2(matched_image_path, backdrop_dst)
|
| 1884 |
+
print(f" ✓ Copied matched backdrop image: {matched_image_path} -> {backdrop_dst}")
|
| 1885 |
except Exception as e:
|
| 1886 |
+
print(f" ✗ Failed to copy matched backdrop image {matched_image_path}: {e}")
|
| 1887 |
|
| 1888 |
# copy other files from folder (skip project.json and matched image)
|
| 1889 |
+
backdrop_files = os.listdir(matched_folder)
|
| 1890 |
+
print(f" Files in backdrop folder: {backdrop_files}")
|
| 1891 |
+
for fname in backdrop_files:
|
| 1892 |
if fname in (matched_filename, "project.json"):
|
| 1893 |
+
print(f" Skipping {fname} (matched image or project.json)")
|
| 1894 |
continue
|
| 1895 |
src = os.path.join(matched_folder, fname)
|
| 1896 |
dst = os.path.join(project_folder, fname)
|
| 1897 |
if os.path.isfile(src):
|
| 1898 |
try:
|
| 1899 |
shutil.copy2(src, dst)
|
| 1900 |
+
print(f" ✓ Copied backdrop asset: {src} -> {dst}")
|
| 1901 |
except Exception as e:
|
| 1902 |
+
print(f" ✗ Failed to copy backdrop asset {src}: {e}")
|
| 1903 |
+
else:
|
| 1904 |
+
print(f" Skipping {fname} (not a file)")
|
| 1905 |
|
| 1906 |
# read project.json to extract Stage/targets
|
| 1907 |
pj = os.path.join(matched_folder, "project.json")
|
|
|
|
| 1909 |
try:
|
| 1910 |
with open(pj, "r", encoding="utf-8") as f:
|
| 1911 |
bd_json = json.load(f)
|
| 1912 |
+
stage_count = 0
|
| 1913 |
for tgt in bd_json.get("targets", []):
|
| 1914 |
if tgt.get("isStage"):
|
| 1915 |
backdrop_data.append(tgt)
|
| 1916 |
+
stage_count += 1
|
| 1917 |
+
print(f" ✓ Successfully read project.json from {matched_folder}, found {stage_count} stage(s)")
|
| 1918 |
except Exception as e:
|
| 1919 |
+
print(f" ✗ Failed to read project.json in {matched_folder}: {e}")
|
| 1920 |
else:
|
| 1921 |
+
print(f" ⚠ No project.json in {matched_folder}")
|
| 1922 |
+
|
| 1923 |
+
print("---")
|
| 1924 |
|
| 1925 |
# --- Merge into final Scratch project.json (identical logic to before)
|
| 1926 |
final_project = {
|