Import PNG Image


I am new to Ruby and Sketchup API.
I am searching how to import and apply a transformation to a PNG image.
I want to automate the same behaviour than when you use import a PNG from the file menu, set the origin and set the width of the image, but automatically with the API.
I did experiments the model.import:

model = Sketchup.active_model
status = model.import(“/private/tmp/background.jpg”, false)

The image is imported, but stick to the mouse pointer and I have still to click and put the scale from mouse.
How could I also set the origin and width from the API only ?

As a bonus, I would like to download the png from a URL and not anymore from a file.png.


It seems I managed to do it from texture :

model = Sketchup.active_model`
model.start_operation(‘Create background’, true)
materials = model.materials
material = materials.add(‘Background’)
material.texture = “/private/tmp/background.jpg”
material.texture.size = [,]
entities = model.active_entities
points = [, 0, 0),, 0, 0),,, 0),,, 0)
face = entities.add_face(points)
face.material = material
face.back_material = material

Is it the good way to do it ?
It possible to load the texture directly from a png through an URL

In my opinion, I believe it is best if you become familiar with Ruby before you can apply the additions that the SketchUp API adds to the Ruby environment.

The simpliest way is to use this method:


Try giving a valid URL for the path argument, to your remote file.

But be aware that some files types (with regard to certain operations) are edition or platform dependant (ie, Pro only, or Mac only.) Ref:

Breaking down you desire a bit more:

You already found the Sketchup::Model#import() method.

In the API images are imported wrapped within a special component subclass.
This class has methods for setting it’s transformation, height, width, origin, etc.

1 Like

Thank you very much. You just replace 30 lines of codes by 10 ^^.
Have a nice day.

I wouldn’t necessarily agree on this point. You always need to start somewhere and it can just as well be making plugins/scripts for SketchUp as anything else. Having a practical goal can be very motivating when learning something new.

Fine, I edited my previous statement, viz:

This is my opinion, I’ve stated it manys times. There is nothing that will ever make me feel any different.

Again and again, over and over I see newbies struggle needlessly, when they’d not do so if they had done like I, and read the “PickAxe” book cover to cover before even attempting any SketchUp programming.

1 Like

And my opinion that I’ve stated numerous times is that learning by doing is the best way to learn. I guess we just have to agree to disagree.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.