Choose Material in UI.inputbox

Dear Friends,
I want user can select material in UI.inputbox (prefer material have up and down key for select.) Is it possible? Can you give me some ideas for it? Thank you in advance.

Okay … ideas.
Build an array of material objects using Sketchup::Model#materials.
Use the name properties of the members in the array to populate the list in the inputbox.


For adding materials from disk, see this old topic …

It also occurs to me that you might not know of the nifty Array#join method.

def choose_material(my_matl_names)
  matl_list = my_matl_names.join('|')
  # default = matl_list.first   # error
  default = my_matl_names.first # fix - Thanx dezmo
  title = "Material Picker"
  prompt = "Choose Material:  "
  choice = UI.inputbox( [prompt], [default], [matl_list], title )
  choice ? choice.first : false
end

You would check the the return value was a “truthy” string name and not false.

1 Like

Dear Dan, My problem is not material. My problem is UI.inputbox.

mat = Sketchup.active_model.materials
mat.each do |mt|
  @floor_mat = mt if mt.name == "[Carrera Marble]"
end
defaults = [@fh, @floor_mat.name]
prompts = ["Floor Height (0...100)", "Floor Material"]
floorin = UI.inputbox prompts, defaults, "Floor Information"

I don’t know how to change material by using UI.inputbox. To change material maybe I need up and down key (I don’t know how to show it). Also when press up key a counter increase and when down key counter decrease. Material can be @floor_mat = mat[counter].

Once you are created the inputbox there is no way to modify it from “outside” with up down keys
But you can create a drop down box as Dan’s example shows. And pass your material list as an array to his choose_material(my_matl_names) method something like

choose_material(["Carrera Marble", "Other Material"])

or

mat_names = Sketchup.active_model.materials.map(&:name)
choose_material(mat_names)

In your example You are using the UI.inputbox with three params. But you have to check the second example here with four params UI.inputbox-class_method .

BTW, I guess there is a small mistake in Dan’s code in line 3:
default = matl_list.first
default = my_matl_names.first
or
default = @floor_mat.name #(from your example)

2 Likes

Yup. Got me. :wink: very early morning code writing. :blush:


BTW …

You can use Material#display_name so as to avoid the square brackets …

mat_names = Sketchup.active_model.materials.map(&:display_name)
2 Likes

Dear Dan,

choice = UI.inputbox( prompt, default, [@fh ,mat_list], title )

This code make drop down box for both @fh and mat_list but I wish to have drop down box only for mat_list. Can you help me for it?

Slightly colored and aligned example of:

image

5 Likes

@dezmo I very much appreciate the time an effort you put in helping others!

4 Likes

Thank you for your example. It is clear and shows different types for data that we need. In all of your examples, we have one column. Is it possible to have 2 columns in UI.inputbox?

Dezmo is always worthy of very especial thanks…

No. That’s all what the Inputbox “knows”. :frowning:

For more sophisticated user interface you have to use Ruby with Html and JavaScript.

2 Likes