Hi, I am creating a series of materials, each assign to a specific texture, but can’t understand why the texture is not assigned to the material. This is an extract of the code. ts is a list of Time objects from which I generate the file names for the tectures;
timeseries.keys.each do |time|
maps[time] = File.join('irradiation_' + time.strftime("%Y%m%d%H") + '.png')
if materials[maps[time]] == nil
puts 'Adding material' + maps[time]
materials.add(maps[time])
end
end
At this point I have all my materials. Now I want to assign the textures to the materials
maps.each do |time, texture_filename|
mat = materials[texture_filename]
puts mat.to_s
puts 'Full path: ' + File.join(path, texture_filename)
mat.texture = File.join(path, texture_filename)
mat.texture.size = [width, height]
end
If I run the script I get the error:
Error: #<NoMethodError: undefined method `size=' for nil:NilClass>
So the texture has not been assigned. If I do the same manually, it works.
your code is a vary hard to follow…
can you provide an overview of your intent…
are you time stamping existing files to the use as textures?
use unique names for your vars to make it a little clearer, i,e, my_tex instead of texture [which is assigned to the method name]
john
You are making a hash based on the time code as key and the matching PNG file as a string value.
You then create a new material [if needed] named after the hash string value.
I assume if you stop your code there are of the expected materials are in the model’s collection ?
If you try this does it work any better ?
maps.each{|a|
time, texture_filename = a
mat = materials[texture_filename]
puts mat.to_s
puts 'Full path: ' + File.join(path, texture_filename)
mat.texture = File.join(path, texture_filename)
puts mat.texture
p 999
mat.texture.size = [width, height]
}
I assume ‘width’ and ‘height’ are defined earlier ?
I assume the material ‘mat’ is listed properly.
What about the ‘texture’ reference before ‘999’
maps.each{|a|
time, texture_filename = a
p mat = materials[texture_filename]
p ipath = File.join(path, texture_filename)
p File.exist?(ipath)
p 666
mat.texture= ipath
puts mat.texture
p 999
mat.texture.size = [width, height]
}
This will tell you the image’s file path AND if it exists…
Since we don’t know the path there might be an issue with encoding ?
What is ‘path’ ?
Can you try this earlier on [inserted just after you have set up ‘path’]:
if defined?(Encoding)
begin
opath = path
path = path.force_encoding("UTF-8") ### >= v2014 lashup
rescue
path = opath
end
end
This should resolve accented characters in the path, wherever possible, but it needs the newer Ruby available with >= v2014…