3ds maxscript 根据名称和名称+后缀对对象进行分组

3ds maxscript group objects according to name and name + suffix

macroScript Grouper category: "MaxScript==Shit"
(
on isEnabled return
 selection.count > 0 

on execute do
    (
        createDialog (
                            rollout mf_main "LOD Grouper"
                            (


                                button savebtn "Group Proper LODs"
                                on savebtn pressed do
                                (


                                    max_count = 2   
                                    lodlist = #()




                                        for index in 1 to $.count do
                                        (


                                            if($[1].name == $[index].name + "_lod1")
                                            then(append lodlist $[1])
                                            else()

                                            if($[1].name == $[index].name + "_lod1")
                                            then(append lodlist $[index])
                                            else(reset)


                                            print lodlist

                                        )




                                        lodgroup = group lodlist 
                                        select lodgroup


                                )


                            )
                         )
    )




)

这是我的脚本,它通过检查选择中的名称并比较它们以查看基于前缀和后缀的匹配项来执行我想要的操作,但它只是 对我选择中的一个对象执行此操作,而不是循环遍历我的选择数组

我试图使脚本执行的操作示例

objects name box01, box01_lod1 / box02, box02_lod1 / box03 , box03_lod1

               group-1              group-2           group-3

非常感谢任何帮助

提前致谢

在您的示例中,同一测试连续执行两次:

if($[1].name == $[index].name + "_lod1")
then(append lodlist $[1])
else()

if($[1].name == $[index].name + "_lod1")
then(append lodlist $[index])
else(reset)

我怀疑这是一个打字错误,你想先测试一个前缀然后一个后缀...

上面的代码正在与选择数组的第一项进行比较。不知道你在选择什么,很难知道这是否确实是正确的方法。

我采用的方法是使用名称数组进行测试,以便对所选节点进行分组。每次选定的节点不在名称数组中 'matched' 时,您将其作为唯一数组添加到 lod_groups。如果匹配,则它只是附加到现有 lod_groups 数组之一。

请注意,这假设您在场景中始终有一个名为 'Box001' 的节点。例如,如果场景中没有 'Box001' 节点,'Lod1_Box001' 和 'Box001_Lod2' 将不会被分组。

local lod_groups = #() -- 2d array
local _names = #() -- list of names to test against
local selected_nodes = getCurrentSelection() -- get selected nodes
for i = 1 to selected_nodes.count do
(
    local found = False
    local index = 0
    local node_name = selected_nodes[i].name
    -- test node_name against _names
    for j = 1 to _names.count do
    (
        local n = node_name
        local pattern = "*" + _names[j] + "*"
        -- if the name in the list is greater swap the pattern test, this will cover suffix and prefix
        if _names[j].count > node_name.count then
        (
            n = _names[j]
            pattern = "*" + node_name + "*"
        )
        -- if it matches the current name, then record the j-index
        if (matchpattern n pattern:(pattern)) then
        (
            found = True
            index = j
        )
    )
    -- append to lod_groups based on results
    if found then
    (
        append lod_groups[index] selected_nodes[i]
    )
    else
    (
        -- add this name to the _names list, it most likely is unique
        append _names selected_nodes[i].name
        append lod_groups #(selected_nodes[i])
    )
)
-- do your grouping here
for lod_group in lod_groups do
(
    group lod_group
)