Can’t get mtl for OBJ robot to work

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

@UberGoober - that’s what I use on Dropbox.

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.

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

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

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 .

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

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

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

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.

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

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

@Bri_G Here’s my updated version.

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)

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.

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

@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 :wink: (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.

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

@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

@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