【问题标题】:Create a file in maxscript在 maxscript 中创建文件
【发布时间】:2015-11-10 13:29:06
【问题描述】:

我正在尝试从 maxscript 中选择的材料中收集一些数据并将它们保存到 xml 文件中。起初我尝试使用 txt 文件,但我似乎被卡住了。

这是我所拥有的:

try(DestroyDialog Test)catch()

Rollout Test "TestMt"  width: 200 height: 120
(
    button btn_name "Connection" height: 50 width:80
    button btn_export "Export Mt Data" height:50 width:90

    fn Createtxt = 
        for o in selection do
                (
                    out_name = ((GetDir #export)+"/testmesh3.txt")
                    out_file = createfile out_name

                )   


            on btn_export pressed do
                (
                    Createtxt()
                )
)

Createdialog Test

【问题讨论】:

  • 到底有什么问题?你能说得更具体点吗?
  • 我在运行脚本时遇到 I/O 错误,在保存文件时遇到或未命中。我可以运行一次并保存,但我再次运行它并失败。
  • 很有趣.......我在一个干净的最大场景中尝试它并且它工作正常,(在工作中尝试它可能是它的管道)所以我现在的问题是,这是正确的行写入文件? for o in selection do ( out_name = ((GetDir #export) ="/testmesh.txt) out_file = createFile out_name format "%,%\n" o.name o.material to:out_file ) 感谢您的帮助!跨度>

标签: 3dsmax maxscript


【解决方案1】:

我认为问题在于您在 Createtxt 函数中有 Createtxt() 函数。

试试这个:

try(DestroyDialog Test)catch()

Rollout Test "TestMt"  width: 200 height: 120
(
    button btn_name "Connection" height: 50 width:80
    button btn_export "Export Mt Data" height:50 width:90


                on btn_export pressed do
                (
                    Createtxt()
                )

    fn Createtxt = 
        for o in selection do
                (
                    out_name = ((GetDir #export)+"/testmesh3.txt")
                    out_file = createfile out_name

                )   



)

Createdialog Test

【讨论】:

  • 我尝试了那个版本,得到“调用需要函数或类,得到:未定义”是不是与函数顶部的按钮相同的示例?你能解释一下吗?感谢您的帮助!
  • 我会尝试写一篇文章。你能告诉我你想从选定的对象中得到什么样的材料数据吗? (假设您想将该数据保存到文件中)
  • 感谢大家的帮助,我正在测试是否可以获取对象名称和着色器名称并将其保存到文本文件中,进一步了解一些信息人们说最好使用 xml,因为它有说明,以后可以用来阅读。你怎么看?再次感谢您的帮助!
  • 我已经完成了一个类似的功能,只会删除/翻译其中的一些特定的荷兰名字:)。
【解决方案2】:

Problem nr 1 - 下面你可以看到我写的一段代码,它可以帮助你将材料分配给选定的对象。 (甚至基于外部 .mat 库文件)它完全可以工作。

rollout matTests "MatTester_v01" (


global renderwidth = renderWidth
global renderHeight = renderHeight 

    local outputFolderURL = ""
    local bestandsNaam = ""
    local materiaalNaam = #() 
    local saveFolder = "testen" 


    label usage0 "Step 1 - Give your images a filename"     
    edittext filenaam "" fieldWidth:220 labelOnTop:false    
    label usage1 "Step 2 - Choose a save folder"    

    button save_folder "Save folder" width:250
    label usage "Step 3"
    pickbutton kiesGeo "Select your geometry" width:250
    label usage2 "Step 4"
    button btn_loadmat "Open a .mat library" width:250

    on save_folder pressed do 
    (
        outputFolderURL = getSavepath()
        saveFolder = outputFolderURL
    )

    global obj
    --camlist = #()

    on kiesGeo picked obj do
    (
    select obj 
    )

    on filenaam entered txt do
    (

        bestandsNaam = txt
    )   


    on kies_camera selected i do
        format "You selected '%'!\n" scale_dd.items[i]

    on btn_loadmat pressed do 
    (
        local sel = getCurrentSelection()

        if (sel.count == 0) then ( messagebox "no objects are selected" title:"Error 001 - no objects selected" )
        else 
        (
            local libFile = getOpenFileName caption:"select a material library file" types:"Material Libraries (*.mat)|*.mat"
            if (libFile != undefined) then 
            (
                local tempLib = loadTempMaterialLibrary libFile
                if (tempLib == undefined) then ( messagebox "loading a library file failed" title:"Error 002 - loading a library file failed" )
                else 
                (
                    -- save materials in a array

                    for m in tempLib do (
                        global materiaalNaam = #(m) 
                        print materiaalNaam
                    )

                    local aantalmaterials = templib.count 
                    for iterations = 1 to aantalmaterials do -- begin iterations loop
                    (

                        global welkeImage = iterations
                        function snapMats label size:[renderWidth,renderHeight] folder:"testMats" =
                        (
                            for c in cameras do
                            (
                            local fname = folder + label + "-" + c.name 

                            materiaalNaamString = materiaalNaam as string
                            versie = welkeImage as string

                            render camera:c outputFile:(outputFolderURL + "\\" + bestandsNaam + "-" + versie + ".jpg") outputSize:[renderWidth,renderHeight]    

                            )
                        )

                        snapMats "label"

                    local mat = tempLib[welkeImage]

                    sel.material = mat -- assign geindexerd material to my selected geometry
                    meditmaterials[activeMeditSlot] = mat

                    ) -- end for iterations loop


                )
            )
        )
    )
)
createDialog matTests width:250

一般情况下 - 为了将任何内容(如材料数据)保存到 .txt 文件中,您可以为用户提供先选择文件夹的选项。

您可以通过在用户界面中创建一个简单的按钮来做到这一点。 然后执行以下操作:

on yourbuttonname_btn pressed do 
(
        inputFolderURL = getSavepath() 
-- this will store the folder URL into the inputFolderURL variable.

)

problem nr 2 - 要将一些数据实际保存到 .txt 文件中,您可以执行以下操作: 在下面的示例中,您可以看到如何将 vertes.data 存储到 .txt 文件中(但也可以很容易地成为材质数据或其他任何内容)

tmesh = snapshotAsMesh selection[1]
out_name = ((GetDir #export)+"/testmesh.txt")
out_file = createfile out_name
num_verts = tmesh.numverts
num_faces = tmesh.numfaces
format "%,%\n" num_verts num_faces to:out_file
for v = 1 to num_verts do
(
 vert = getVert tmesh v
 format "%," vert to:out_file
)
format "\n" to:out_file
for f = 1 to num_faces do
(
 face = getFace tmesh f
 format "%," face to:out_file
)
close out_file
delete tmesh
edit out_name

【讨论】:

  • 非常感谢,我会剖析这个,看看我能得到什么!真的很感激!
  • 不,谢谢!你需要做一些樱桃采摘,但如果我是正确的,那么你需要的一切都在那里。如果这是您的答案,请考虑将您的问题标记为已回答。
猜你喜欢
  • 2020-04-04
  • 2020-10-22
  • 1970-01-01
  • 2020-06-21
  • 2021-01-13
  • 2015-11-07
  • 2014-02-17
  • 2014-06-18
  • 2023-04-01
相关资源
最近更新 更多