Update app.py
Browse files
app.py
CHANGED
|
@@ -1932,72 +1932,92 @@ def similarity_matching(sprites_data: dict, project_folder: str, top_k: int = 1,
|
|
| 1932 |
# print(f" β No project.json in {matched_folder}")
|
| 1933 |
|
| 1934 |
# print("---")
|
| 1935 |
-
|
|
|
|
|
|
|
|
|
|
| 1936 |
from pathlib import Path
|
| 1937 |
|
| 1938 |
-
#
|
| 1939 |
sprite_base_p = Path(sprite_base_path).resolve(strict=False)
|
| 1940 |
backdrop_base_p = Path(backdrop_base_path).resolve(strict=False)
|
| 1941 |
project_folder_p = Path(project_folder)
|
| 1942 |
project_folder_p.mkdir(parents=True, exist_ok=True)
|
| 1943 |
|
| 1944 |
-
# convert copied sets to use resolved strings
|
| 1945 |
copied_sprite_folders = set()
|
| 1946 |
copied_backdrop_folders = set()
|
| 1947 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1948 |
for matched_idx in matched_indices:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1949 |
matched_image_path = paths_list[matched_idx]
|
| 1950 |
-
matched_path_p = Path(matched_image_path)
|
| 1951 |
-
matched_folder_p = matched_path_p.parent
|
| 1952 |
matched_filename = matched_path_p.name
|
| 1953 |
-
|
| 1954 |
-
|
| 1955 |
-
|
| 1956 |
-
s = str(p).replace("\\", "/")
|
| 1957 |
-
if s.startswith("/"):
|
| 1958 |
-
s = s[1:]
|
| 1959 |
-
return s.replace("/", "\\")
|
| 1960 |
-
|
| 1961 |
-
matched_folder_p = to_no_leading_and_backslashes(matched_folder_p)
|
| 1962 |
|
| 1963 |
print(f"Processing matched image: {matched_image_path}")
|
| 1964 |
-
print(f" - Folder: {
|
| 1965 |
-
print(f" - Sprite path: {sprite_base_p}")
|
| 1966 |
-
print(f" - Backdrop path: {backdrop_base_p}")
|
| 1967 |
print(f" - Filename: {matched_filename}")
|
| 1968 |
|
| 1969 |
-
#
|
| 1970 |
-
|
| 1971 |
-
|
| 1972 |
-
|
| 1973 |
-
|
| 1974 |
-
|
| 1975 |
-
|
| 1976 |
|
| 1977 |
-
# If it's a sprite (under SPRITE_DIR) -> copy sprite assets and read sprite.json
|
| 1978 |
-
if is_subpath(matched_folder_p, sprite_base_p) and str(matched_folder_p) not in copied_sprite_folders:
|
| 1979 |
-
print(f"Processing SPRITE folder: {matched_folder_p}")
|
| 1980 |
-
copied_sprite_folders.add(str(matched_folder_p))
|
| 1981 |
sprite_json_path = matched_folder_p / "sprite.json"
|
| 1982 |
print("sprite_json_path----------------------->", sprite_json_path)
|
| 1983 |
print("copied sprite folder----------------------->", copied_sprite_folders)
|
| 1984 |
-
if sprite_json_path.exists():
|
| 1985 |
try:
|
| 1986 |
with sprite_json_path.open("r", encoding="utf-8") as f:
|
| 1987 |
sprite_info = json.load(f)
|
| 1988 |
project_data.append(sprite_info)
|
| 1989 |
-
print(f" β Successfully read sprite.json from {
|
| 1990 |
except Exception as e:
|
| 1991 |
-
print(f" β Failed to read sprite.json in {
|
| 1992 |
else:
|
| 1993 |
-
print(f" β No sprite.json in {
|
| 1994 |
|
| 1995 |
-
# copy non-matching files from the sprite folder (except
|
| 1996 |
try:
|
| 1997 |
sprite_files = list(matched_folder_p.iterdir())
|
| 1998 |
except Exception as e:
|
| 1999 |
sprite_files = []
|
| 2000 |
-
print(f" β Failed to list files in {
|
| 2001 |
|
| 2002 |
print(f" Files in sprite folder: {[p.name for p in sprite_files]}")
|
| 2003 |
for p in sprite_files:
|
|
@@ -2015,27 +2035,31 @@ def similarity_matching(sprites_data: dict, project_folder: str, top_k: int = 1,
|
|
| 2015 |
else:
|
| 2016 |
print(f" Skipping {fname} (not a file)")
|
| 2017 |
|
| 2018 |
-
#
|
| 2019 |
-
if is_subpath(matched_folder_p, backdrop_base_p) and
|
| 2020 |
-
print(f"Processing BACKDROP folder: {
|
| 2021 |
-
copied_backdrop_folders.add(
|
| 2022 |
-
print("backdrop_base_path----------------------->", backdrop_base_p)
|
| 2023 |
print("copied backdrop folder----------------------->", copied_backdrop_folders)
|
|
|
|
| 2024 |
# copy matched backdrop image
|
| 2025 |
backdrop_src = matched_folder_p / matched_filename
|
| 2026 |
backdrop_dst = project_folder_p / matched_filename
|
| 2027 |
-
|
| 2028 |
-
|
| 2029 |
-
|
| 2030 |
-
|
| 2031 |
-
|
|
|
|
|
|
|
|
|
|
| 2032 |
|
| 2033 |
# copy other files from folder (skip project.json and matched image)
|
| 2034 |
try:
|
| 2035 |
backdrop_files = list(matched_folder_p.iterdir())
|
| 2036 |
except Exception as e:
|
| 2037 |
backdrop_files = []
|
| 2038 |
-
print(f" β Failed to list files in {
|
| 2039 |
|
| 2040 |
print(f" Files in backdrop folder: {[p.name for p in backdrop_files]}")
|
| 2041 |
for p in backdrop_files:
|
|
@@ -2055,7 +2079,7 @@ def similarity_matching(sprites_data: dict, project_folder: str, top_k: int = 1,
|
|
| 2055 |
|
| 2056 |
# read project.json to extract Stage/targets
|
| 2057 |
pj = matched_folder_p / "project.json"
|
| 2058 |
-
if pj.exists():
|
| 2059 |
try:
|
| 2060 |
with pj.open("r", encoding="utf-8") as f:
|
| 2061 |
bd_json = json.load(f)
|
|
@@ -2064,14 +2088,13 @@ def similarity_matching(sprites_data: dict, project_folder: str, top_k: int = 1,
|
|
| 2064 |
if tgt.get("isStage"):
|
| 2065 |
backdrop_data.append(tgt)
|
| 2066 |
stage_count += 1
|
| 2067 |
-
print(f" β Successfully read project.json from {
|
| 2068 |
except Exception as e:
|
| 2069 |
-
print(f" β Failed to read project.json in {
|
| 2070 |
else:
|
| 2071 |
-
print(f" β No project.json in {
|
| 2072 |
|
| 2073 |
print("---")
|
| 2074 |
-
|
| 2075 |
# for matched_idx in matched_indices:
|
| 2076 |
# matched_image_path = paths_list[matched_idx]
|
| 2077 |
# matched_folder = os.path.dirname(matched_image_path)
|
|
|
|
| 1932 |
# print(f" β No project.json in {matched_folder}")
|
| 1933 |
|
| 1934 |
# print("---")
|
| 1935 |
+
# add at top of file
|
| 1936 |
+
import shutil
|
| 1937 |
+
import json
|
| 1938 |
+
import os
|
| 1939 |
from pathlib import Path
|
| 1940 |
|
| 1941 |
+
# normalize base paths once before the loop
|
| 1942 |
sprite_base_p = Path(sprite_base_path).resolve(strict=False)
|
| 1943 |
backdrop_base_p = Path(backdrop_base_path).resolve(strict=False)
|
| 1944 |
project_folder_p = Path(project_folder)
|
| 1945 |
project_folder_p.mkdir(parents=True, exist_ok=True)
|
| 1946 |
|
|
|
|
| 1947 |
copied_sprite_folders = set()
|
| 1948 |
copied_backdrop_folders = set()
|
| 1949 |
|
| 1950 |
+
def display_like_windows_no_lead(p: Path) -> str:
|
| 1951 |
+
"""
|
| 1952 |
+
For human-readable logs only β convert Path to a string like:
|
| 1953 |
+
"app\\blocks\\Backdrops\\Castle 2.sb3" (no leading slash).
|
| 1954 |
+
"""
|
| 1955 |
+
s = p.as_posix() # forward-slash string, safe for Path objects
|
| 1956 |
+
if s.startswith("/"):
|
| 1957 |
+
s = s[1:]
|
| 1958 |
+
return s.replace("/", "\\")
|
| 1959 |
+
|
| 1960 |
+
def is_subpath(child: Path, parent: Path) -> bool:
|
| 1961 |
+
"""Robust membership test: is child under parent?"""
|
| 1962 |
+
try:
|
| 1963 |
+
# use non-strict resolve only if needed, but avoid exceptions
|
| 1964 |
+
child.relative_to(parent)
|
| 1965 |
+
return True
|
| 1966 |
+
except Exception:
|
| 1967 |
+
return False
|
| 1968 |
+
|
| 1969 |
+
# Flatten unique matched indices (if not already)
|
| 1970 |
+
matched_indices = sorted({idx for lst in per_sprite_matched_indices for idx in lst})
|
| 1971 |
+
print("matched_indices------------------>", matched_indices)
|
| 1972 |
+
|
| 1973 |
for matched_idx in matched_indices:
|
| 1974 |
+
# defensive check
|
| 1975 |
+
if not (0 <= matched_idx < len(paths_list)):
|
| 1976 |
+
print(f" β matched_idx {matched_idx} out of range, skipping")
|
| 1977 |
+
continue
|
| 1978 |
+
|
| 1979 |
matched_image_path = paths_list[matched_idx]
|
| 1980 |
+
matched_path_p = Path(matched_image_path).resolve(strict=False) # keep as Path
|
| 1981 |
+
matched_folder_p = matched_path_p.parent # Path object
|
| 1982 |
matched_filename = matched_path_p.name
|
| 1983 |
+
|
| 1984 |
+
# Prepare display-only string (do NOT reassign matched_folder_p)
|
| 1985 |
+
matched_folder_display = display_like_windows_no_lead(matched_folder_p)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1986 |
|
| 1987 |
print(f"Processing matched image: {matched_image_path}")
|
| 1988 |
+
print(f" - Folder: {matched_folder_display}")
|
| 1989 |
+
print(f" - Sprite path: {display_like_windows_no_lead(sprite_base_p)}")
|
| 1990 |
+
print(f" - Backdrop path: {display_like_windows_no_lead(backdrop_base_p)}")
|
| 1991 |
print(f" - Filename: {matched_filename}")
|
| 1992 |
|
| 1993 |
+
# Use a canonical string to store in the copied set (POSIX absolute-ish)
|
| 1994 |
+
folder_key = matched_folder_p.as_posix()
|
| 1995 |
+
|
| 1996 |
+
# ---------- SPRITE ----------
|
| 1997 |
+
if is_subpath(matched_folder_p, sprite_base_p) and folder_key not in copied_sprite_folders:
|
| 1998 |
+
print(f"Processing SPRITE folder: {matched_folder_display}")
|
| 1999 |
+
copied_sprite_folders.add(folder_key)
|
| 2000 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2001 |
sprite_json_path = matched_folder_p / "sprite.json"
|
| 2002 |
print("sprite_json_path----------------------->", sprite_json_path)
|
| 2003 |
print("copied sprite folder----------------------->", copied_sprite_folders)
|
| 2004 |
+
if sprite_json_path.exists() and sprite_json_path.is_file():
|
| 2005 |
try:
|
| 2006 |
with sprite_json_path.open("r", encoding="utf-8") as f:
|
| 2007 |
sprite_info = json.load(f)
|
| 2008 |
project_data.append(sprite_info)
|
| 2009 |
+
print(f" β Successfully read sprite.json from {matched_folder_display}")
|
| 2010 |
except Exception as e:
|
| 2011 |
+
print(f" β Failed to read sprite.json in {matched_folder_display}: {repr(e)}")
|
| 2012 |
else:
|
| 2013 |
+
print(f" β No sprite.json in {matched_folder_display}")
|
| 2014 |
|
| 2015 |
+
# copy non-matching files from the sprite folder (except matched image and sprite.json)
|
| 2016 |
try:
|
| 2017 |
sprite_files = list(matched_folder_p.iterdir())
|
| 2018 |
except Exception as e:
|
| 2019 |
sprite_files = []
|
| 2020 |
+
print(f" β Failed to list files in {matched_folder_display}: {repr(e)}")
|
| 2021 |
|
| 2022 |
print(f" Files in sprite folder: {[p.name for p in sprite_files]}")
|
| 2023 |
for p in sprite_files:
|
|
|
|
| 2035 |
else:
|
| 2036 |
print(f" Skipping {fname} (not a file)")
|
| 2037 |
|
| 2038 |
+
# ---------- BACKDROP ----------
|
| 2039 |
+
if is_subpath(matched_folder_p, backdrop_base_p) and folder_key not in copied_backdrop_folders:
|
| 2040 |
+
print(f"Processing BACKDROP folder: {matched_folder_display}")
|
| 2041 |
+
copied_backdrop_folders.add(folder_key)
|
| 2042 |
+
print("backdrop_base_path----------------------->", display_like_windows_no_lead(backdrop_base_p))
|
| 2043 |
print("copied backdrop folder----------------------->", copied_backdrop_folders)
|
| 2044 |
+
|
| 2045 |
# copy matched backdrop image
|
| 2046 |
backdrop_src = matched_folder_p / matched_filename
|
| 2047 |
backdrop_dst = project_folder_p / matched_filename
|
| 2048 |
+
if backdrop_src.exists() and backdrop_src.is_file():
|
| 2049 |
+
try:
|
| 2050 |
+
shutil.copy2(str(backdrop_src), str(backdrop_dst))
|
| 2051 |
+
print(f" β Copied matched backdrop image: {backdrop_src} -> {backdrop_dst}")
|
| 2052 |
+
except Exception as e:
|
| 2053 |
+
print(f" β Failed to copy matched backdrop image {backdrop_src}: {repr(e)}")
|
| 2054 |
+
else:
|
| 2055 |
+
print(f" β Matched backdrop source not found: {backdrop_src}")
|
| 2056 |
|
| 2057 |
# copy other files from folder (skip project.json and matched image)
|
| 2058 |
try:
|
| 2059 |
backdrop_files = list(matched_folder_p.iterdir())
|
| 2060 |
except Exception as e:
|
| 2061 |
backdrop_files = []
|
| 2062 |
+
print(f" β Failed to list files in {matched_folder_display}: {repr(e)}")
|
| 2063 |
|
| 2064 |
print(f" Files in backdrop folder: {[p.name for p in backdrop_files]}")
|
| 2065 |
for p in backdrop_files:
|
|
|
|
| 2079 |
|
| 2080 |
# read project.json to extract Stage/targets
|
| 2081 |
pj = matched_folder_p / "project.json"
|
| 2082 |
+
if pj.exists() and pj.is_file():
|
| 2083 |
try:
|
| 2084 |
with pj.open("r", encoding="utf-8") as f:
|
| 2085 |
bd_json = json.load(f)
|
|
|
|
| 2088 |
if tgt.get("isStage"):
|
| 2089 |
backdrop_data.append(tgt)
|
| 2090 |
stage_count += 1
|
| 2091 |
+
print(f" β Successfully read project.json from {matched_folder_display}, found {stage_count} stage(s)")
|
| 2092 |
except Exception as e:
|
| 2093 |
+
print(f" β Failed to read project.json in {matched_folder_display}: {repr(e)}")
|
| 2094 |
else:
|
| 2095 |
+
print(f" β No project.json in {matched_folder_display}")
|
| 2096 |
|
| 2097 |
print("---")
|
|
|
|
| 2098 |
# for matched_idx in matched_indices:
|
| 2099 |
# matched_image_path = paths_list[matched_idx]
|
| 2100 |
# matched_folder = os.path.dirname(matched_image_path)
|