Fix DCI Image node returning None due to function

definition order issue

The DCI Image node was returning None because the
_translate_to_original function was being called before it was
defined, causing a NameError that prevented the node from
executing properly.

Root cause:
- _translate_to_original function was called on line 119 for
  palette type conversion
- Function definition was located on line 158, after the call
- This caused a NameError during execution, resulting in None output

Fix:
1. Moved _translate_to_original function definition to before
   its first usage (before palette type conversion)
2. Removed duplicate function definition that appeared later
   in the code
3. Maintained all existing functionality and translation logic

Testing:
- Created comprehensive test script to verify fix
- Tested with various parameter combinations
- Confirmed proper DCI image data generation
- Verified all required fields are present in output
- Validated image content generation and metadata

Result:
- DCI Image node now properly returns DCI_IMAGE_DATA tuple
- All translation functionality works correctly
- Generated proper DCI paths and image content
- Node executes without errors in all test scenarios

修复DCI图像节点因函数定义顺序问题返回None

DCI图像节点返回None是因为_translate_to_original函数在定义
之前被调用,导致NameError阻止节点正常执行。

问题根源:
- _translate_to_original函数在第119行被调用用于调色板类型转换
- 函数定义位于第158行,在调用之后
- 这导致执行时出现NameError,结果返回None

修复方案:
1. 将_translate_to_original函数定义移到首次使用之前
2. 删除代码中后面出现的重复函数定义
3. 保持所有现有功能和翻译逻辑

测试验证:
- 创建全面的测试脚本验证修复
- 测试各种参数组合
- 确认正确的DCI图像数据生成
- 验证输出中包含所有必需字段
- 验证图像内容生成和元数据

修复结果:
- DCI图像节点现在正确返回DCI_IMAGE_DATA元组
- 所有翻译功能正常工作
- 生成正确的DCI路径和图像内容
- 节点在所有测试场景中无错误执行
This commit is contained in:
JiDe Zhang
2025-06-10 20:13:12 +08:00
parent 3e829cf38f
commit f9cc1332e5
+29 -29
View File
@@ -124,6 +124,35 @@ class DCIImage(BaseNode):
# Resize image to target size
resized_image = pil_image.resize((actual_size, actual_size), Image.Resampling.LANCZOS)
# Convert translated values back to original English for DCI path
# DCI file structure must use original English values, not translations
def _translate_to_original(value, value_type):
"""Convert translated UI values back to original English for DCI file structure"""
if value_type == "state":
state_map = {
t("normal"): "normal",
t("disabled"): "disabled",
t("hover"): "hover",
t("pressed"): "pressed"
}
return state_map.get(value, value) # fallback to original if not found
elif value_type == "tone":
tone_map = {
t("light"): "light",
t("dark"): "dark"
}
return tone_map.get(value, value) # fallback to original if not found
elif value_type == "palette":
palette_map = {
t("none"): "none",
t("foreground"): "foreground",
t("background"): "background",
t("highlight_foreground"): "highlight_foreground",
t("highlight"): "highlight"
}
return palette_map.get(value, value) # fallback to original if not found
return value
# Convert palette type to numeric value according to DCI specification
# Use original English values for palette mapping
original_palette_type = _translate_to_original(palette_type, "palette")
@@ -164,35 +193,6 @@ class DCIImage(BaseNode):
img_content = img_bytes.getvalue()
# Convert translated values back to original English for DCI path
# DCI file structure must use original English values, not translations
def _translate_to_original(value, value_type):
"""Convert translated UI values back to original English for DCI file structure"""
if value_type == "state":
state_map = {
t("normal"): "normal",
t("disabled"): "disabled",
t("hover"): "hover",
t("pressed"): "pressed"
}
return state_map.get(value, value) # fallback to original if not found
elif value_type == "tone":
tone_map = {
t("light"): "light",
t("dark"): "dark"
}
return tone_map.get(value, value) # fallback to original if not found
elif value_type == "palette":
palette_map = {
t("none"): "none",
t("foreground"): "foreground",
t("background"): "background",
t("highlight_foreground"): "highlight_foreground",
t("highlight"): "highlight"
}
return palette_map.get(value, value) # fallback to original if not found
return value
# Convert values to original English for DCI path
original_icon_state = _translate_to_original(icon_state, "state")
original_tone_type = _translate_to_original(tone_type, "tone")