New asset API

Hi,
The new asset API do not allow to access assets from a depend project, right?
It will be nice to be able to do it.

@moechofe2 - have tried putting the dependency assets in a function call in the dependency?

@moechofe2 you could potentially do this by referring to the project directly?

asset.documents.ProjectName.someFile

That’s not want I want to do, I will try to explain better, also while experimenting, I found another bug.

What I want is: a project loaded as dependency can load it’s own assets.

project A, in tab A:

num=123
img1=asset.myAsset
-- img2=readImage(asset.myAsset) -- uncomment this to make it fail

function img3()
  readImage(asset.myAsset)
end

project B, with project A as dependency, in tab Main:

function setup()
  print(num) -- ok, print 123
  print(img1) -- not-ok, print nil
  print(img2) -- I don't know, it fail without error
  print(img3()) -- expect userdata, got nil
  print(asset.documents.A.myAsset) -- it works, but ...

The last print() is working but project B MUST known the name of the asset of project A, but it is not obvious in my scenario.

you could potentially do this by referring to the project directly.
Yes, it works but I thoughout this syntax was to access a folder in the documents.

– Number of files in a folder in documents
print(#asset.documents.SomeFolder)
_Sauce: https://codea.io/reference/Graphics.html#assetsOverview_

By uncommenting the line in tab A of project A: the screen remain black, no output, no error, no nothing.

@moechofe2 - I’ve used asset lists in dependencies to enable loading files easily into multiple projects - by setting up the asset paths etc in tables within a function in the dependency file.

Simple demo attached with a dependency file and a project file. Try this out and play with commenting out to see it does work.

It’s a workaround but I find it reliable.

@moechofe2
What Simeon was saying is you can do this by making the local project call itself, it’s counterintuitive but it works

project A, in tab A:


num=123
img1=asset.documents.A.myAsset
-- img2=readImage(asset.documents.A.myAsset)

function img3()
  readImage(asset. documents.A. myAsset)
end

@skar I got it, it works

@moechofe2 that is a good find though, and while it may be tricky to support, the behaviour you suggest is the ideal way to handle this (the project, even when loaded as dependent, should still be looking up its “own” assets). For now, the workaround where you explicitly reference the project is necessary to ensure the files are found when it’s used as a dependency

A better way to do this, however, might be to tell the dependent project where to find its assets. This will allow you to control the location from the calling project, e.g.

Project A

local assetPath = asset

ProjectA = {}

function ProjectA.setAssetPath(key)
    assetPath = key
end

function img3()
    readImage(assetPath.myAsset)
end

Project B

function setup()
    ProjectA.setAssetPath(asset.documents.A)

    img3()
end

Doing it like this allows Project A to exist anywhere, and you only really need to know where it exists from Project B. It makes the library a little less brittle

Thanx @Simeon

Not sure if I’m following what the problem is. I don’t use dependencies because I don’t write anything that complex.

All,
Anybody tried using dependency libraries of assets in a dependency tab function. Seems a bit more logical to me and you can place them in a function with a ‘reserved’ name that you can use from the dependency and any master project you call it from.

I have dependencies with several tabs containing differently named functions which you can call to initialize them then refer to the resource name/path and, if you have tables of resources) you can call addressing the table.path to load your assets.

@dave1707 the issue as I understand it is that when Project A is a dependency of Project B, its root asset path is the same as Project B (that is, any asset key asset.someFile will look for someFile inside Project B ).

When running Project A standalone asset.someFile == asset.documents.A.someFile

When running Project B which imports Project A, the line asset.someFile == asset.documents.B.someFile instead, even though it exists in Project A

@Simeon Thanks for the explanation. Like I mentioned, I don’t use dependencies so I didn’t fully understand what was really happening. I’ll play around with your above code to understand it better.

@Simeon,

The new asset API can store 3D Models .obj files but doesn’t seem to be able to store .mtl files— is it supposed to? Is there another way that we are supposed to load in .mtl files? @UberGoober had asked me if I could pre-load up some of my 3D models into the 3D FPS demo projects I posted so he and others could try them out without having to download and integrate the 3D models into the projects themselves. Without being able to get the .mtl files loaded into the project’s assets, I don’t see how I’ll be able to preload the 3D models in my project to share with others on the forum.

Appreciate any suggestions you have.

Thanks!

Cc: @dave1707

@SugarRay What problems are you having. I have some projects that were reading and writing mtl files.

function readFile()
    fileName=asset.builtin.Watercraft.watercraftPack_003_mtl
    local inFile=io.open(fileName.path,"rb")
    data=nil
    if inFile then
        data=inFile:read("*all") 
        inFile:close()
        print(data)
    else
        print("file not found")
    end
end

function saveFile(data)
    fileName=asset.."watercraftPack_999_mtl"
    if data~=nil then
        io.output(io.open(fileName.path,"w"))
        io.write(data)
        io.close()
        print("file "..fileName.name.." created")
    else
        print("data nil")
    end
end

Thanks, @dave1707.

It looks like your your example above writes to the general(?) Codea asset folder.

I’m trying to put .mtl files into a specific project’s asset folder so that I can export the project as a standalone .zip file that someone else can unzip on their device in Codea and use the packaged assets (including the .mtl file) in that imported project.

Perhaps then by using a modified version of your saveFile function above, one can write .mtl files from one’s asset.documents folder to a specific project’s asset folder? If so, that would work, although I was hoping that I could just “pick” the .mtl file from my assets.document folder and use the asset menu to just import that .mtl file into the specific project’s assets (like I did to import .obj files from my assets.document folder to my specific project’s assets folder). :slight_smile:

@SugarRay Doesn’t look like there’s an easy way. Where are the mtl files at that you’re trying to move. I take it that the mtl files are in the Documents folder and you want to move them to a certain projects folder.

Yes, exactly, @dave1707.

@SugarRay This might work. Create a new project and put this code in it. Use it to copy the mtl files to the project folder. Then delete this code and copy the regular code you want to use into the project. I think this might work. Change the input and output file names for each mtl file. Let me know if this works or not and I’ll try something else if it doesn’t.

viewer.mode=STANDARD

function setup()  
    readFile() 
    saveFile()    
end

function readFile()
    fileName=asset.builtin.Watercraft.watercraftPack_003_mtl
    local inFile=io.open(fileName.path,"rb")
    data=nil
    if inFile then
        data=inFile:read("*all") 
        inFile:close()
        print(data)
    else
        print("file not found")
    end
end

function saveFile()
    fileName=asset.."wat_003_mtl.mtl"
    if data~=nil then
        io.output(io.open(fileName.path,"w"))
        io.write(data)
        io.close()
        print("file "..fileName.name.." created")
    else
        print("data nil")
    end
end

Thanks so much, @dave1707, I’ll try it out! :slight_smile: