在maxscript中创建一个文件

时间:2015-08-17 19:40:15

标签: 3dsmax maxscript

我正在尝试从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

2 个答案:

答案 0 :(得分:0)

我认为问题是你在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

答案 1 :(得分:0)

问题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.

)

问题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