Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

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.

Tagged:

Comments

  • Posts: 2,689
    @moechofe2 - have tried putting the dependency assets in a function call in the dependency?
  • SimeonSimeon Admin Mod
    Posts: 5,778

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

    asset.documents.ProjectName.someFile

  • Posts: 35

    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.

  • Posts: 2,689

    @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.

  • edited April 15 Posts: 308

    @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
  • Posts: 35

    @skar I got it, it works

  • SimeonSimeon Admin Mod
    edited April 15 Posts: 5,778

    @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

  • Posts: 35

    Thanx @Simeon

  • dave1707dave1707 Mod
    edited April 15 Posts: 9,977

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

  • Posts: 2,689
    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.
  • SimeonSimeon Admin Mod
    edited April 15 Posts: 5,778

    @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

  • dave1707dave1707 Mod
    Posts: 9,977

    @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.

  • Posts: 128

    @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

  • dave1707dave1707 Mod
    Posts: 9,977

    @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
    
  • Posts: 128

    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). :-)

  • dave1707dave1707 Mod
    Posts: 9,977

    @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.

  • Posts: 128

    Yes, exactly, @dave1707.

  • dave1707dave1707 Mod
    Posts: 9,977

    @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
    
  • Posts: 128

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

  • Posts: 128

    @Bri_G,

    I’d like to cc: you on this topic. I just found some posts from you from 2018 where you were also trying to get some of your 3D models more conveniently into a project’s assets. It looks like you settled on sharing assets from Dropbox but the ending step you said was to share Dropbox asset with a folder called “Assets” in the Codea file menu of the iPad file system. However, I don’t see such a folder labeled Simple’s “Assets” in my Codea file men using the iPad File system. (I only have folders labeled “DocumentAssets” and “DropBoxAssets”. DocumentAssets doesn’t currently have anything in it, and I don’t seem to be able to choose that folder from within the project editor. I can pick assets from Dropbox, but the Dropbox asset picker seems even more limited than the “Documents” asset picker which currently stores my multiple Codea assets (e.g. the Documents asset picker lets me choose Models from my Codea Documents folder but the Dropbox picker doesn’t let me choose Models that I place there; what I really need as mentioned in my recent additional post is a way to easily transfer .mtl files to my project’s assets).

    I’m going to try Dave’s solution, but it would be easier to be able to just “pick” .mtl files in my Codea documents folder to just transfer the .mtl assets to my project.

    Another workaround I guess I could try would be just use primarily .fbx 3D models which don’t require a separate .mtl file. The problem I worry about is that if not a text file like .mtl and somehow .fbx texture file name does not match what is in .fbx that I won’t be able to get textures on model (whereas with .obj/.mtl text files I can just rename the file/libraries in those files to correspond with changes in the .mtl or texture files as needed).

    Appreciate any additional experience you might have in getting .mtl files into your project assets.

    Thanks!

  • Posts: 2,689
    @SugarRay - That's over three years ago and I can't remember offhand what options I used. Will dig into it and come back to you later.
  • Posts: 412
    @SugarRay If you're willing to pay for another app, Textastic is a fantastic editor and is what I've been using to write directly to assets within my Codea projects. It just sees the .Codea bundles as plain folders so you can look inside and copy things into them.
  • Posts: 128

    Thanks, Everyone (@dave1707, @Bri_G and @Steppers).

    Dave1707, your code/solution for importing .mtl files into a project’s assets from my Codea documents folder worked! The only thing that I changed is then when I saved the file I made sure to remove the _mtl on the name (i.e. imported filename_mtl from Codea documents folder becomes exported filename.mtl to the file folder). I suspected that would be important since the .obj file would be looking for a mtllib file in the format of filename.mtl rather than filename_mtl. Interestingly, the imported filename.mtl shows up in the project’s asset library as an “other” file.

    As mentioned earlier, one could probably avoid needing to do this manual reading and writing of .mtl files if one coverts one’s models all to the .fbx 3D file format which Codea also imports but doesn’t require a separate .mtl file. However, since the .obj file is so prevalent, I think it would be helpful in future versions of Codea if Codea could simply recognize .mtl files in one’s Codea document folder to then be able to transfer to one’s project’s asset folder (cc: @Simeon).

    Bri_G, please let me know if you end up remembering another solution that worked well for you at transferring in .mtl files to your projects’ asset folders.

    And Steppers, I value your recommendation, so purchased Textastic. I have some additional apps that I would also like to see and manipulate their file folders better so would like to see if Textastic could help me view and manipulate them better than iOS’s file system. I read Textastic’s instructions but initially am having some trouble understanding how to use Textastic to view Codea and other app folders. At your convenience, would you mind replying with a brief list of steps to do so (off topic from this post so okay to also direct message me in Code).

    Have a great rest of your weekend and upcoming week, Everyone! :-)

  • dave1707dave1707 Mod
    Posts: 9,977

    @SugarRay Great demo. I loaded it into my Dropbox and when I was exporting it to Codea, it was going really slow so I stopped it. Maybe I’ll try loading it some other time. Kinda big. I noticed in the demo you were still using the old movement controls where you have to stop one direction before moveing in another. I created an example back whenever where you can vary the speed and you can change direction without stopping what else is happening. In other words, you can move and change direction smoothly to make it seem like your actually walking around and turning.

    As for the mtl files, since I haven’t messed with them for a long time, I wasn’t sure of the file format. I didn’t remember if .mtl or _mtl was the correct way. I also noticed the mtl files would show up in other.

  • Posts: 128

    Thanks, @dave1707. I had a feeling that with large 3D models that might be a slow import. Will stay open to suggestions from others in the future as to other ways to export large 3D model project to the Codea community.

    I don’t think the YouTube videos capture the actual user player experience so well. Let me know what you think about the movement if you are able to get it downloaded and running in the future. Will try out your newer movement controls in these demos as well and compare the experience.

    One thing I forgot to mention (which I’ll add to the other post) is that sphereCast method of collision detect still needs a lot of fine tuning. You may need to therefore do a bit of maneuvering more than you’d expect to move through doors or around some objects.

  • dave1707dave1707 Mod
    Posts: 9,977

    @SugarRay Sorry, I posted the demo comment in the wrong discussion. But since you already responded, I’ll just leave the comment here.

  • edited April 18 Posts: 412
    @SugarRay You need to add Codea's documents folder using 'Add External Folder'. It should be as simple as that.
  • Posts: 128

    Thanks, @Steppers. I just figured it out—the app boots up with an intro file, and I initially couldn’t find the ‘Add External Folder’ option from there. I realized I just needed to navigate in the app out of that file and then saw the option to Add External Folders which worked well :-)

  • edited April 18 Posts: 128

    PS, @Steppers, Textastic is the best iPad so far at opening up to view and edit large .obj files! I just used it to open and view in place a large 98MB .obj in-place and although it printed a warning that such a large file might make the app unstable, it opened and displayed the .obj file perfectly :-)

  • Posts: 412
    @SugarRay Glad to hear it's working for you! It's been great for me so far. Great ssh terminal too.
Sign In or Register to comment.