I’m guessing texture_path = Sketchup.find_support_file 'Corrogated_Iron.jpg', 'C:\path_to_material\Corrogated_Iron.jpg' didn’t find the file and returned nil.
In your second example you used an absolute path.
Btw, Sketchup.find_support_file is meant only to find files that ship with SketchUp.
A texture object uses an image (file.)
A material object has a texture property.
When you build a material (.skm) file it has within it (being a zip archive) a few XML files containing color, aspect ratio and scaling information, etc, and a resources subfolder that contains the texture image(s).
Then you do not need to recreate the material each time via code. Just load the SKM file into your model, and paint faces (or components.)
It’s not that they “need” to be. But there is are standard places for .skm files (as I’ve shown above,) but not standard disk locations for texture images files.
So, material (.skm) files are better because they were invented to be used by SketchUp as a neat wrapped up package containing the texture and information on how to apply it surfaces. (An image file alone doesn’t have the usage information.)
If you can avoid using jpg, use png instead. Jpg are lossy and do not scale well.
In this code, the toolbar creator has no problems with adding the *.jpg to the toolbar, but will not add it as a material.texture. The only way I can get that to work is if it is an absolute path.
Any suggestions as to why?
(The reason I need to use the *.jpg and a relative path, is because this will be shipped over to others computers, to be used by them)
It hasn’t changed, but that’s the purpose of it, find resources that ship with SketchUp.
There is no need to use it otherwise.
I’ve seen quite often that people use it to resolve a path to SU’s Plugins folder with the assumption that their extension is installed there. Which isn’t always the case if a user installs the extension at the different location either manually or using managers like SketchUcation’s Plugin Manager.
If you have the path where a file is located there is no need to use Sketchup.find_support_file since you already know where the file is.
If you want to resolve a path to a file within your extension installation path, build paths using __FILE__ or __dir__.
I beg to differ, agreeing with John. I believe you are reading too much into old, outdated and incorrect API documentation. Ie, the phrases that say “… of a file within the SketchUp installation directory.”
The way the 2 SketchUp “find_support_…” module methods worked was overhauled with the SketchUp 2014 release, but the documentation wasn’t. Even some of SketchUp’s own “distro’d” files were moved out of the installation directory.
Which is false as I showed above a valid reason.
This is not what I was really discussing above with respect to finding skm files. We were discussing getting the location of resource files because the code does not know exactly where they are, because resource file locations have changed o’r the versions, and making an incorrect assumption is poor programming practice.
So we properly ask the API to find the files. If it does great, if not nil is returned.
I do agree that using File.dirname(__FILE__) or __dir__ (for ruby 2+) is safer for an extension’s own files.
The icon setter methods (assuming a relative path from the extension’s folder path) have always been a deviation from the norm, and always cause me confusion. The documentation does not properly explain what kind of filepath argument is allowed.
The norm for most all file I/O operations is that filepaths are first checked if they resolve to a valid absolute path, and if not, then are checked to see if they can find a valid filepath relative from the current working directory. (You can get this from Dir::getwd or Dir::pwd class methods.)
Since your code cannot trust other extensions to reset the working directory, you have 2 options.
1. Temporarily change the working directory before making file I/O method calls using chdir…
texture_file = 'Corrogated_Sheet.jpg'
resources = 'Script_Resources'
texture_path = File.join( resources, texture_file )
Dir::chdir(__dir__) do
# Now in the folder path of the current running Ruby file ...
draw_face.material.texture = texture_path
draw_face.back_material.texture = texture_path
end
# Now back in the previous working directory path.
2. Use absolute paths …
texture_file = 'Corrogated_Sheet.jpg'
resources = 'Script_Resources'
texture_path = File.join( __dir__, resources, texture_file )
# It doesn't matter what the current working directory is,
# ... because absolute paths to the texture files are given:
draw_face.material.texture = texture_path
draw_face.back_material.texture = texture_path
Other people’s computers can use PNG images better than JPG images.