Howdy, Stranger!

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

Can’t get mtl for OBJ robot to work

in Questions Posts: 1,278

I downloaded the obj & mtl files for the cool robot in @yojimbo2000’s old OBJ loader.

The obj loaded just fine into craft but I can’t figure out how to get the mtl loaded.

Attached is the project (includes assets) and a screenshot.

Tagged:
«1

Comments

  • Posts: 2,356
    @UberGoober - not sure if the files you mention are fully valid obj and mtl files as Craft wasn't available and there were a few home made obj loaders. Have you tried them with an external model loader?

    Will try to pull the files down and check them if my iPad & Codea behave.
  • Posts: 1,278

    @Bri_G I think the OBJ and MTL files are downloaded directly from the net somewhere, not generated by the OBJ loader, so they should be legit. I could be wrong though.

  • Posts: 2,356

    @UberGoober - the robot obj and mtl are present in your documents folder ie Codea root. I’ll check in my system.

  • Posts: 1,278

    @Bri_G not sure what you mean, I included those in the assets of the zipped up project, at least I thought I did.

  • edited June 3 Posts: 2,356

    @UberGoober - yeah your right, it is in the Project.codea folder. But if you check it out it does what I have previously mentioned about obj and mtl files. The file names are robot_obj.txt and robot_mtl.txt. Also I couldn;t see them as resources in the Project resources viewer. @Simeon - this is the same format naming that I have seen on Dropbox. I got round it by using name_obj.obj and name_mtl.mtl for naming. This is confusing.

  • Posts: 644

    @UberGoober as @Bri_G stated the model does not show up in the asset picker?
    If the filenames have the extension .txt it will not work. They have to be .obj and .mtl.

    I have never succeeded to get obj/mtl files into the project assets-how did you manage to do that? I have complained to @Simeon in the past that it is not possible to move the obj/mtl from dropbox to project assets.

  • Posts: 1,278

    @piinthesky readText(...) is your friend. That’s how I moved them. I just typed it in on a blank line, used it to move the files, then deleted the line again.

  • edited June 2 Posts: 2,356
    @UberGoober - the colour data is in the mtl file but the file may not be read as the filename has been changed to robot_mtl.txt. if you can read you project file on a PC or Mac you could rename it to a .zip file, unzip it and change the names of the two files then rezip the file and import it into Codea. That may or may not work depending on where the Codea edits the file names. I'll play around with this but it won't be soon. Busy, busy, busy.
  • Posts: 1,278

    @Bri_G you can actually edit a file extension from Dropbox, so it was pretty easy to do. So now It appears as a model in the model browser, but it still doesn’t have the color information.

  • Posts: 644

    @UberGoober are you sure the .mtl file is in the project assets? If instead you have the .obj/.mtl in dropbox does it work?

  • edited June 3 Posts: 2,356

    @UberGoober @piinthesky - you can get obj/mtl files visible in the viewer if they are in Codea root. Copy the properly named files there then use asset.documents or look them up on the assets viewer from the project list page.

  • Posts: 1,278

    @piinthesky I get the same (non) results whether the files are in the Codea root, Dropbox, or the project assets.

  • Posts: 2,356
    @UberGoober - how are the files named? Can you see them in the asset viewer?
  • Posts: 1,278

    The files are robot_obj.obj and robot_mtl.mtl. The obj file shows up as a model asset in the asset viewer. It’s the same in all three locations. The mtl file only shows up inside Codea as an auto completion option when I type asset-dot-this-or-that.

  • Posts: 2,356
    @UberGoober - rename them to robot.obj and robot.mtl and change that in you project then run it.
  • Posts: 1,278

    @Bri_G yes! Thank you! Victory over everything!

    ...the only thing is, it won’t work from inside the project’s own assets.

    Using the following code I can verify that “robot.mtl” actually is in the project assets:


    foo = readText(asset.documents .. "robot.mtl") print(foo) saveText(asset.."robot.mtl", foo) bar = readText(asset .. "robot.mtl") print(bar)

    So the exact same files are in my project assets and in my documents folder, but in the documents folder they load properly and in the project assets they don’t.

    Also the little obj preview in the documents folder looks colored-in, and the one in the project assets is blank.

  • Posts: 2,356
    @UberGoober - are you sure the assets in your project file are named the same. They may have been changed when the project was built then exported.

    @simeon @John could you please explain this. I know why you did it but it would be useful to understand the reasons and the details.
  • Posts: 1,278

    @Bri_G pretty sure; the obj file should be the same because I copied it into the project assets directly using Codea’s asset browser, and the code I posted above confirms that the mtl file is the same.

  • edited June 3 Posts: 2,356
    @UberGoober - ah, aha. If you copied it using the internal asset browser it may have changed the naming and renamed as described above. The asset browser may also be designed to accomodate that.

    I know when I have say a robot.obj file in Dropbox the new asset lister displays robot_obj. I know the _ is used for non-alphanumerics in paths and thought that was why it is displayed in this way. Hope @Simeon or @John can clear this up.
  • Posts: 1,278

    @Bri_G wait scratch that. My code isn’t working, I read the output wrong. The old “Codea loops through the code twice before starting” misunderstanding. So actually, no, I’m not certain I’m successfully copying the mtl file. From what you just said it seems I should change the readText command to look for “robot_mtl.mtl”.

  • Posts: 2,356
    @UberGoober - that's what I use on Dropbox.
  • Posts: 1,278

    I don’t know if I mentioned this before, but the OBJ and the MTL work properly inside the dropbox folder also.

    So they work correctly everywhere but inside project assets.

  • dave1707dave1707 Mod
    Posts: 9,423

    When the two of you get this figured out, post a detailed description of what needs to be done and how to do it.

  • edited June 3 Posts: 2,356

    @dave1707 - first point (others to follow)

    Loading model related files (possibly other types) from Dropbox server.

    say files robot.obj and robot.mtl in directory models

    1. by new asset lister
      asset.documents.Dropbox.models.robot_obj
      the asset lister will show up the files as robot_obj and robot_mtl
      but you don't need to load the mtl file it will be loaded with the model
      but should be stored in the same folder.
    2. by text in tables you could use
      path = asset.documents.Dropbox
      with
      dir = {"models"}
      data = {"robot"}
      finally say
      e = scene:entity()
      e.model = craft.model(path[dir[mod]][data[mod]])
      where mod is your model index if you have more than 1 in the dir and
      data arrays.

    Hope that's easily understood. Any problems post a query. Will look at other options, than Dropbox, next.

    p.s. Always remember to re-synch Dropbox when you change anything on your Dropbox server.

    note: just downloading latest iPadOS 14.6 now so will check this out and confirm when installed.

  • dave1707dave1707 Mod
    Posts: 9,423

    I think I’m getting the hang of the obj and mtl files. I was able to do this. It doesn’t matter if you use name_obj or name.obj as long as the original file is name.obj .

  • Posts: 2,356
    @dave1707 - interesting, the obj file specified the material filename, which, for ease of use, should be stored in the same folder as the obj file. The mtl file specified the materials used and their characteristics - in the simplest case the colours for each material. That's the case with this model - but you have s different colour scheme. Been playing with the materials properties?

    Alternatively the mtl file usually specifies the texture that is wrapped around the model to produce the best effects.

    We're your files held in Dropbox folders?
  • edited June 4 Posts: 2,356
    @John - whilst we are discussing models could you give a demo/information on the use of multicomponent models lighting and other feature textures - either by example or pointing to a few informative references (preferably written for beginners).

    P.s. I know there are examples in the Craft demos but they are quite complex.
  • dave1707dave1707 Mod
    Posts: 9,423

    @Bri_G I changed the colors in the mtl file. I would rename it to .txt in the Dropbox app, edit it there, rename it back to .mtl and sync it with the Codea Dropbox folder.

  • Posts: 2,356

    All,

    Placed the robot.obj and robot.mtl files in Codea root (ie documents) and lashed together a project to display it. Attached, needs a little more attention to get it to operate smoothly and some tidying up.

    RobutBG.zip 57.4K
  • dave1707dave1707 Mod
    Posts: 9,423

    @Bri_G Getting an error on this line. Not sure if the file isn’t included in your zip.

    e.model = craft.model(asset.documents.robot_obj)
    
  • Posts: 2,356

    @dave1707 - yeah, half expected. Looks like exporting doesn't incorporate files unless they are in the project directory. My problem is I find it difficult using the Codea assets system to transfer assets to the project folder. I think I managed it once by transferring images to photos then back. But I rarely see Codea project folders in the Codea asset viewer.

    Can someone walk me through it.

  • dave1707dave1707 Mod
    Posts: 9,423

    @Bri_G Here's my updated version.

    Rpbot.zip 66.5K
  • Posts: 644

    As far as i know, it is not easily possible to get mtl to the project assets. @Simeon knows this, but i guess doesn't want to waste time fixing it, as the handling of assets will be revisited in Codea4.

    @Bri_G what do you want to know about lighting?

    It is possible to modify each material of a multi-material model.
    In the example below, mysHi is a model with 3 materials

                local mat1=craft.material(asset.builtin.Materials.Standard)
                mat1.diffuse=color(152, 152, 165)
                mat1.roughness=0.6
                mat1.metalness=1.0
                mysHi:get(craft.renderer):setMaterial(mat1, 1)
    
                local mat2=craft.material(asset.builtin.Materials.Standard)
                mat2.diffuse=color(241, 91, 7)
                mat2.roughness=0.5
                mat2.metalness=1.0
                mysHi:get(craft.renderer):setMaterial(mat2, 2)
    
                local mat3 = craft.material(asset.builtin.Materials.Specular) 
                mat3.diffuse = black
                mat3.roughness=0.7
                mat3.metalness=0.5 
                mysHi:get(craft.renderer):setMaterial(mat3, 3)
    
  • edited June 4 Posts: 1,278
    I deleted this post because I originally made it on the iPhone and the formatting was all flooey and it wouldn't even show half of it. The correct post is below, but this is the file that goes with it.
  • dave1707dave1707 Mod
    Posts: 9,423

    @UberGoober See my Rpbot.zip above. Both the obj and mtl are in the project folder.

  • edited June 4 Posts: 1,278

    @dave1707 they are? I don't see you mentioning that in your comments, from what you wrote it seemed like you had the assets in the Dropbox folder. How did you get the .mtl into assets? Did you explain it in this thread and I'm missing it?

    Here's my original post, with apparently unwarranted excitement ;) (the file it references is attached in my previous post):

    I did it! I got the mtl file into the project assets and it works! Check attached project for proof—at least, I hope. It’s working for me, I’d love some verification.

    Here’s how you do it:

    • first get the mtl.txt file into the project
    • the file should now be visible in the autocomplete bar when you type “asset.” plus the first few letters of the file name. At this point it’s just showing you the .txt file, so it won’t work, but right now you just need to get the dot-notation reference to the file.
    • using that, get the file's full path and save it to a tab named “savedPathTab” (or whatever you want)
    • here’s the command that does that: saveProjectTab("savedPathTab", "--"..tostring(asset.robot_mtl)) <--very important to use the asset-dot-notation to reference the .txt file
    • now you should have a tab that contains the full string path of the .txt file. Copy that path, and you’re almost there
    • use that path with the os.rename() command to change the file’s extension from .txt to .mtl
    • the format is is os.rename(oldNameWithPath, newNameWithPath)
    • here’s an example:

    local result, errorMessage = os.rename ( "/private/var/mobile/Containers/Data/Application/815B2CB2-BA05-406C-A08C-5F532E4D4E73/Documents/Robot OBJ to Model.codea/robot_mtl.txt", "/private/var/mobile/Containers/Data/Application/815B2CB2-BA05-406C-A08C-5F532E4D4E73/Documents/Robot OBJ to Model.codea/robot.mtl" ) print ( result, " ", errorMessage )
    • if it worked, the print statement will print “true nil” to the console
    • as long as you have the right .mtl file name, it should work!

    It worked for me, so please try it out on your own and report back, so we can verify that this is an actual reliable solution.

  • Posts: 2,356
    @dave1707 - I commented in the Ignatz dogfight thread to @UberGoober that I managed to remove all the formatting errors but had to use the asset..string format for it. Which is counterintuitive, mixing old and new file addressing. And - here it is again. Have I missed something, is the .. linking retained in the new asset path spec?
  • dave1707dave1707 Mod
    Posts: 9,423

    @UberGoober Just above your post, I gave a comment to the zip file and said that the obj and mtl files were in the project folder. The obj file can be copied from the Codea dropbox folder to the project folder when you’re in the project using the Add To selection. That doesn’t work for the mtl file though. I use the below code to move the mtl file. Copy the below code and put it in your project and run moveFile(name,ext). It moves the mtl file from the Dropbox folder to the project folder. The just remove the code. This assumes you have the obj and mtl files in the Dropbox folder.

    function setup()
        moveFile("robot","mtl")
    end
    
    function moveFile(name,ext)
        local inName=asset.documents.Dropbox..name.."."..ext
        local outName=asset..name.."."..ext   
        local inFile=io.open(inName.path,"r")
        local data=nil
        if inFile then
            data=inFile:read("*all") 
            inFile:close()
            print(name..".mtl  read OK")        
            local outFile=io.open(outName.path,"w")
            if outFile then
                outFile:write(data)
                outFile:close()
                print(name..".mtl  write OK")
            else
                print("out file error")
            end
        else
            print("in file error")
        end
    end
    
  • dave1707dave1707 Mod
    Posts: 9,423

    @piinthesky I tried using your above code to modify the mtl values in my Rpbot.zip example above. I can’t get it to work, can you try and show the code. Here’s what the mtl file looks like.

    # Blender MTL File: 'robot.blend'
    # Material Count: 4
    
    newmtl metal
    Ns 417.647059
    Ka  0.1985  0.0000  0.0000
    Kd  0.5921  0.0167  0.0000
    Ks  0.5973  0.2083  0.2083
    Ni 1.000000
    d 1.000000
    illum 2
    
    newmtl metalblack
    Ns 0.000000
    Ka 0.000000 0.900000 0.000000
    Kd 0.000000 0.900000 0.000000
    Ks 0.0 0.9 0.0
    Ni 1.000000
    d 1.000000
    illum 2
    
    newmtl metalcol
    Ns 96.078431
    Ka 1.000000 1.000000 0.00000
    Kd 1.000000 1.0 0.0000
    Ks 0.000000 0.000000 0.00000
    Ni 1.000000
    d 1.000000
    illum 1
    
    newmtl ojitos
    Ns 96.078431
    Ka 0.000000 0.00000 1.00000
    Kd 0.0 0.0 1.0
    Ks 0.00000 0.00000 0.00000
    Ni 1.000000
    d 1.000000
    illum 2
    
  • Posts: 1,278

    @dave1707 thanks for sharing that, it’s great.

    We basically went the same route conceptually—using lua’s os commands to get around codea’s limitations—but you’ve done it way better.

  • Posts: 1,278

    .......and the next question is, now that we’ve got the .mtl working, is it possible to get it creating nicely rounded surfaces like it does in @yojimbo2000’s OBJ loader?

  • Posts: 644

    @dave1707 here it is. The various properties can be changed, not just the colour.

    --viewer.mode=FULLSCREEN
    function setup()
        assert(OrbitViewer, "Please include Cameras (not Camera) as a dependency")
        scene = craft.scene()
        scene.sun.rotation=quat.eulerAngles(30,60,0)
        skyMaterial = scene.sky.material
        skyMaterial.horizon = color(255)
        scene.ambientColor = color(255)   
        m=scene:entity()    
        m.model = craft.model(asset.."robot.obj")
        m.position=vec3(0,0,0)
        v = scene.camera:add(OrbitViewer, vec3(0,3,0), 10, 0, 2000)
        v.rx=15
        v.ry=210
    
        nMesh=m.model.submeshCount
        print("number of meshes ", nMesh)
    
        local mat1=craft.material(asset.builtin.Materials.Standard)
        mat1.diffuse=color(13, 235, 220)
        mat1.roughness=0.6
        mat1.metalness=1.0
        m:get(craft.renderer):setMaterial(mat1, 1)
    
        local mat2=craft.material(asset.builtin.Materials.Standard)
        mat2.diffuse=color(108, 241, 7)
        mat2.roughness=0.5
        mat2.metalness=1.0
        m:get(craft.renderer):setMaterial(mat2, 2)
    
        local mat3 = craft.material(asset.builtin.Materials.Specular) 
        mat3.diffuse = color(239, 23, 70)
        mat3.roughness=0.7
        mat3.metalness=0.5 
        m:get(craft.renderer):setMaterial(mat3, 3)
    
        local mat4 = craft.material(asset.builtin.Materials.Specular) 
        mat4.diffuse = color(128, 152, 106)
        mat4.roughness=0.7
        mat4.metalness=0.5 
        m:get(craft.renderer):setMaterial(mat3, 4)
    end
    
    function update(dt)
        scene:update(dt)
    end
    
    function draw()
        update(DeltaTime)
        scene:draw()    
    end
    
  • dave1707dave1707 Mod
    Posts: 9,423

    @piinthesky Thanks for the update. That’s kind of what I was able to do with just one of the mats, but since I couldn’t get the color to change I thought I was doing something wrong. The color values (Ka) would change in the .mtl file, but it’s like I didn’t have control of it and I couldn’t figure why it changed. Is it final or is there something that isn’t being done right to change the color. I’ll play around with this and see what I can or can’t come up with.

  • Posts: 644

    @dave1707 the colour does change, via the 'diffuse', so i don't understand what is the problem?

  • Posts: 644

    @UberGoober if you apply the CalculateAverageNormals function in @yojimbo2000 obj loader code, you can get a sort of smoothed surface:

        vertices=m.model.positions
        m.model.normals=CalculateAverageNormals(vertices)
    
  • Posts: 2,356

    All,

    My contribution to playing around with the robot is attached (hope it runs OK).

    RobotPlay.zip 147.3K
  • edited June 5 Posts: 1,278

    Not to be a dweeb but:

    This is awesome how everybody’s contributing to making this one thing cooler!

    @Bri_G here’s my contribution to your version. I added the code like @piinthesky suggested, so it can use @yojimbo2000’s approach to averaging the normals, and I renamed the variables to be descriptive (which I think is especially helpful in knowing which colors you’re adjusting with which control), and I made it so the color controls look like they’re the same as the colors being used on the model right from the start, and I added a verrry slight color tint to the horizon that shouldn’t bother anyone, and I made a little color tweak to the robot’s palms so they don’t just look like they’re the black of the eternal void.

    And of course, again, much thanks to @dave1707 for figuring out the best way to get .mtl files into project assets.

    [[also attached, just some colors I played around with—it’s really fun to play with them]]

  • Posts: 2,356

    OK All,,

    Bit off the wall and would probably take me weeks fiddling, but - this demo could develop into a cool tool. Take a basic non-coloured model, analyse to get the component parts (triangles) then list them. Select one and add glow to it, then for that component select and define the colour. When the model is fully painted write out an mtl file for it.

    Major work there.

    @UberGoober - neat update, think the smoothing works in some areas and not in others but thanks for including it, nice to see it working.

  • Posts: 1,278

    @Bri_G yeah the smoothing’s a bit funky, maybe someone who understands that stuff better can fix it... I assume it’s down to how meshes treat normals vs how craft models treat normals... you don’t know the difference by any chance?

  • edited June 5 Posts: 2,356
    @UberGoober - I think it's simpler than that. Each models built up of different sections. Some sections are significantly different to others, so if you do a global average the overlaps between sections get distorted.
Sign In or Register to comment.