Loading PNG Textures in Ruby plugin and SketchUp freezes
-
I have a Ruby plugin that attempts to load textures from a directory. The code looks like this:
#jpgs is an array of filenames (with path included). They are actually pngs mats = [] jpgs.each do |j| name = File.split(j)[1] mat = Sketchup.active_model.materials.add(name) mat.texture = j mat.texture.size = size mats << mat end
This code consistently freezes after I load 5 (of 20, in the test case) textures. Specifically, it freezes at the resizing step. If I comment out this step, it will get through 8 textures before freezing.
These texture files are pretty small (~ 8kb), and I have plenty of ram (12GB) on my iMac.
Does anyone have an idea as to what might be happening?
(Probably unrelated, but I also had a problem with a plugin that froze after I called Sketchup.active_model.active_view.camera.set more than ten times in a row. I got around this by breaking the camera_set transformations into loops of five and putting those into a larger loop. I have no idea why this worked. Sketchup freezing is a mystery to me, and I have a difficult time debugging it).
Thanks for your time.
-
I moved this topic to the developers forum. Any question about plugin-writing does not belong in the newbie section!
Have you tried removing all other plugins just in case one of them is messing with your plugin? Or are you doing this while running some sort of observer in your model?
-
There shouldn't be any issues with a longer list...
Presumably 'size' [in inches] is preset in earlier code that you haven't shown us ?
Otherwise you'd be trying to set it to 'nil' ! It defaults to 10" anyway ?
I already have a tool quite like this, which works fine [without texture resize]...
Incidentally, if you want each new material to be named after its texture, instead of using
name = File.split(j)[1]
try
name = File.basename(j, ".*")
So, if the full path were 'c:/users/me/desktop/images/brick.png'
your 'split' method returns 'brick.png'
whereas mine gives 'brick', because the extension is auto-stripped -
There shouldn't be any issues with a longer list...
Presumably 'size' [in inches - say
size=100
] is preset in earlier code that you haven't shown us ?
Otherwise you'd be trying to set it to 'nil' ! It defaults to 10" anyway ?
Do you mean to set the texture's size to match the image ?
then you might think to use:
mat.texture.size = [mat.texture.width, mat.texture.height]
which is somewhat circular ?
texture.image_width
andtexture.image_height
are in pixels, you could use those*factor
to make them into inch values instead ?I already have a tool quite like this, which works fine [without texture resize]...
Incidentally, if you want each new material to be named after its texture, instead of using
name = File.split(j)[1]
try
name = File.basename(j, ".*")
So, if the full path were 'c:/users/me/desktop/images/brick.png'
your 'split' method returns 'brick.png'
whereas mine gives 'brick', because the extension is auto-stripped -
Thanks for the replies.
Chris, thanks for moving the post to the Developer forum. This is my first post.
TIG, size is set to 35 elsewhere; I was trying to just simplify the code. Thanks for the tip on basename. I'm somewhat new to Ruby.
Some interesting developments:
The script seems to work on a SketchUp installation on Snow Leopard. I reinstalled SketchUp on my Lion machine, and I was able to get the script to work once using a different directory of textures, but now it's crashing every time again (not freezing this time, just crashing). I'll continue to investigate and post findings.
-
Could be the JPEG itself. I've had some models that acted strange because of they way the JPEG was created.
-
If the image files themselves might be suspect try it with half of them removed from the folder, then swap them around until you get fails, narrow it down to the rogues and then look at the culprit[s] in an image editor...
-
I still haven't isolated the problem. The script works fine on my Snow Leopard laptop but doesn't work on my Lion machine. I've tried different models, different sets of images, etc... It will work sometimes on Lion, but eventually it will crash if I try to more directories of textures (it seems to crash around 20 textures). Since it works fine on Snow Leopard, and this is just a personal script, I'm just going to stick with that for now. (I've also noticed that other, heavy 2D exporting scripts seem to do better on Snow Leopard). Thanks for all of your help.
-
I've had problems when exporting PNG files from Corel Photo-Paint & importing them into SKUP. I found that I had to re-save the image files in IrfanView before they would play ball.
Advertisement