You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Maya MEL脚本无报错但Script Editor无图案输出问题求助

Maya MEL脚本无报错但Script Editor不显示星号图案问题排查

问题描述

编写了一段生成空心三角形/矩形星号图案的MEL脚本,运行无报错,但Script Editor中看不到图案输出,怀疑是软件Bug?源代码如下:

global proc print_pattern(string $patternType, int $rows, int $cols)
{
    if ($patternType == "triangle")
    {
        int $i = 0;
        while ($i < $rows)
        {
            int $j = 0;
            while ($j <= $i)
            {
                if ($i == 0 || $i == $rows - 1 || $j == 0 || $j == $i)
                    print("* ");
                else
                    print("  ");
                $j++;
            }
            print("\n");
            $i++;
        }
    }
    else if ($patternType == "rectangle")
    {
        int $i = 0;
        while ($i < $rows)
        {
            int $j = 0;
            while ($j < $cols)
            {
                if ($i == 0 || $i == $rows - 1 || $j == 0 || $j == $cols - 1)
                    print("* ");
                else
                    print("  ");
                $j++;
            }
            print("\n");
            $i++;
        }
    }
    else
    {
        print("Invalid pattern type. Please choose 'triangle' or 'rectangle'.\n");
    }
}

// Get pattern choice
string $patternChoice = `promptDialog -title "Pattern Type" -message "Enter 'triangle' or 'rectangle' for the pattern type:" -button "OK" -defaultButton "OK" -cancelButton "Cancel" -dismissString "Cancel"`;
if ($patternChoice == "OK")
{
    $patternChoice = `promptDialog -query -text`;
    
    // Print the selected pattern
    if ($patternChoice == "triangle" || $patternChoice == "rectangle")
    {
        int $patternRows = `promptDialog -title "Pattern Rows" -message "Enter the number of rows for the pattern:" -button "OK" -defaultButton "OK" -cancelButton "Cancel" -dismissString "Cancel"`;
        int $patternCols = 0;
        if ($patternRows == "OK")
        {
            $patternRows = `promptDialog -query -text`;
            if ($patternChoice == "rectangle")
            {
                $patternCols = `promptDialog -title "Pattern Columns" -message "Enter the number of columns for the pattern:" -button "OK" -defaultButton "OK" -cancelButton "Cancel" -dismissString "Cancel"`;
                if ($patternCols == "OK")
                {
                    $patternCols = `promptDialog -query -text`;
                }
            }
            print("Star Pattern (", $patternChoice, "):\n");
            print_pattern($patternChoice, $patternRows, $patternCols);
        }
    }
    else
    {
        print("Invalid pattern type. Please choose 'triangle' or 'rectangle'.\n");
    }
}

问题原因(非软件Bug,为代码逻辑错误)

  • 变量类型不匹配:promptDialog返回的按钮点击结果是字符串("OK"/"Cancel"),但你将其赋值给了int类型的$patternRows和$patternCols。MEL自动类型转换会把字符串转为0,导致调用print_pattern时传入的行数/列数为0,自然没有图案输出。
  • 取消按钮分支未处理:当用户点击Cancel关闭弹窗时,代码未终止后续逻辑,会继续获取空值作为参数,进一步导致无效调用。

修复后的代码

global proc print_pattern(string $patternType, int $rows, int $cols)
{
    if ($patternType == "triangle")
    {
        int $i = 0;
        while ($i < $rows)
        {
            int $j = 0;
            while ($j <= $i)
            {
                if ($i == 0 || $i == $rows - 1 || $j == 0 || $j == $i)
                    print("* ");
                else
                    print("  ");
                $j++;
            }
            print("\n");
            $i++;
        }
    }
    else if ($patternType == "rectangle")
    {
        int $i = 0;
        while ($i < $rows)
        {
            int $j = 0;
            while ($j < $cols)
            {
                if ($i == 0 || $i == $rows - 1 || $j == 0 || $j == $cols - 1)
                    print("* ");
                else
                    print("  ");
                $j++;
            }
            print("\n");
            $i++;
        }
    }
    else
    {
        print("Invalid pattern type. Please choose 'triangle' or 'rectangle'.\n");
    }
}

// Get pattern choice
string $patternChoiceBtn = `promptDialog -title "Pattern Type" -message "Enter 'triangle' or 'rectangle' for the pattern type:" -button "OK" -defaultButton "OK" -cancelButton "Cancel" -dismissString "Cancel"`;
if ($patternChoiceBtn == "OK")
{
    string $patternChoice = `promptDialog -query -text`;
    
    // Print the selected pattern
    if ($patternChoice == "triangle" || $patternChoice == "rectangle")
    {
        string $rowsBtn = `promptDialog -title "Pattern Rows" -message "Enter the number of rows for the pattern:" -button "OK" -defaultButton "OK" -cancelButton "Cancel" -dismissString "Cancel"`;
        int $patternRows = 0;
        int $patternCols = 0;
        if ($rowsBtn == "OK")
        {
            $patternRows = `promptDialog -query -text`;
            // 验证行数是否为正整数
            if ($patternRows <= 0) {
                print("Rows must be a positive integer.\n");
                return;
            }
            if ($patternChoice == "rectangle")
            {
                string $colsBtn = `promptDialog -title "Pattern Columns" -message "Enter the number of columns for the pattern:" -button "OK" -defaultButton "OK" -cancelButton "Cancel" -dismissString "Cancel"`;
                if ($colsBtn == "OK")
                {
                    $patternCols = `promptDialog -query -text`;
                    // 验证列数是否为正整数
                    if ($patternCols <= 0) {
                        print("Columns must be a positive integer.\n");
                        return;
                    }
                } else {
                    // 用户点击取消,终止逻辑
                    return;
                }
            }
            print("Star Pattern (", $patternChoice, "):\n");
            print_pattern($patternChoice, $patternRows, $patternCols);
        } else {
            // 用户点击取消,终止逻辑
            return;
        }
    }
    else
    {
        print("Invalid pattern type. Please choose 'triangle' or 'rectangle'.\n");
    }
}

修复说明

  • 将接收按钮点击结果的变量改为string类型,避免类型转换错误。
  • 增加用户点击Cancel时的终止逻辑,防止无效参数传递。
  • 添加行数/列数的正整数验证,避免输入非正数导致无输出。

内容的提问来源于stack exchange,提问作者Pakapol Premprasopchok

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.16 01:12:23