Replace Several Textures?
-
**Hello,
I'm currently developing Click-Cuisine 2 and I want to bring a very important new feature.
This feature should allow multiple materials to be replaced with a single click on an icon.
The goal is to display all IKEA colors in any kitchen project by a simple click.
It remains for me to discover how to write the method and I need your help to succeed. **
Explanation Stage by Stage of my final goal:
**1st click on the icon:
The materials with the pictures, "1-100x40.jpg", "1-80x40.jpg", "1-60x40.jpg" ....
Which are applied on furniture are replaced by:
The images, "2-100x40.jpg", "2-80x40.jpg", "2-60x40.jpg" ....
These images are in the folder "Plugins\TNT_ClickCuisine2\Materials"
2 nd Click on the icon:
These are the images, "3-100x40.jpg","3-80x40.jpg", "3-60x40.jpg", which are loaded.
This will allow the different colors to be displayed quickly and easily on all furniture.
Here is the organization of the Materials file:
I need your experience to find the simplest method possible!
1.How would you proceed?
Here's the little I know about the materials:
Model = Sketchup.active_model Materials = model.materials Material = materials.add ('100x40') Material.texture = "C; /Materials/1-100x40.jpg" Texture = material.texture
This creates a material with an image from a raw path.
2.How to do the same with a path of type, " Plugins\TNT_ClickCuisine2\Materials " ?
3.Then how to replace the image of the materials?
4.Is this the right approach or do you have a simpler solution?
Thank you in advance for your help.
David**
-
This seems to work for the 3 'sets' of materials I created.
def cycle_materials mat_names = ['60x40','80x40','100x40'] materials = Sketchup.active_model.materials mat_names.each{|mn| materials.add(mn) unless materials[mn]} mat_folder = File.join(File.dirname(__FILE__).gsub('\\','/'),"TNT_ClickCuisine2/Materials/") @last ||= '0' @last=='3' ? @last='1' ; @last=@last.next for mn in mat_names material = materials[mn] material.texture = mat_folder + "#{@last}-#{mn}.jpg" end end
-
**Hello sdmitch,
Thank you for your help !
I tested the code with the Ruby console which returns "nil".
Can you tell me for what?
Can you explain how your code works?
Thank you
David**
-
I wrote the code in a .rb file and then I called it with an icon.
When I click on the icon, 3 black materials are created but no "jpg" image is imported.
You can download the file.rbz, below:
Here is the code:
module ClickCuisine2 class << self @@path_to_resources = File.join(File.dirname(__FILE__), 'Resources') def materials mat_names = ['60x40','80x40','100x40'] materials = Sketchup.active_model.materials mat_names.each{|mn| materials.add(mn) unless materials[mn]} mat_folder = File.join(File.dirname(__FILE__).gsub('\\','/'),"TNT_ClickCuisine2/Materials/") @last ||= '0' @last=='3' ? @last='1' ; @last=@last.next for mn in mat_names material = materials[mn] material.texture = mat_folder + "#{@last}-#{mn}.jpg" end end if !file_loaded?(__FILE__) su_menu = UI.menu("Plugins") cc_menu = su_menu.add_submenu(%Q(Click_Cuisine2)) tb = UI;;Toolbar.new(%Q(Click-Cuisine2)) command1 = UI;;Command.new("Nex Material"){ ClickCuisine2.materials } command1.small_icon = File.join(@@path_to_resources,"add_00.png") command1.large_icon = File.join(@@path_to_resources,"add_00.png") command1.tooltip = %Q(Nex Material) command1.status_bar_text = %Q(Nex Material) command1.menu_text = %Q(Nex Material) cc_menu.add_item(command1) tb.add_item(command1) tb.restore file_loaded(__FILE__) end end end
Thank you in advance for your help.
-
There is no need to import the materials. The code simply re-defines the texture to be used. If the newly created materials are still 'black', then the textures are not where they should be.
-
The textures should not be in the folder, "C:\Users\Dada\AppData\Roaming\SketchUp\SketchUp 2014\SketchUp\Plugins\TNT_ClickCuisine2\Materials" ?
If not, in what folder should the images be?
Thank you
-
That would be correct if the plugin is in "C:\Users\Dada\AppData\Roaming\SketchUp\SketchUp 2014\SketchUp\Plugins"
-
The plugin is in the right place!
I think the problem has to be tied to something else.
Can you share your "rbz" file?
Your soluition is great and very interesting.
Thank you
David
-
-
Thank you very much!
Moving from one material to another with Click-Kitchen 2, will be much faster and easier with your code.
I would make a small demo as soon as the code is applied to the plugin.
Ps: My error was indeed the path to the materials file.
File.join (File.dirname (__ FILE __). Gsub ('\\', '/'), "TNT_ClickCuisine2 / Materials /"
Corrected like this:
File.join (File.dirname (__ FILE __). Gsub ('\\', '/'), "Materials /"
-
NEVER include a <space> between a method and its parenthesized arguments !
AND you show some weird Capitalization and added spaces !So it IS NOT:
File.join (File.dirname (__ FILE __). Gsub ('\\', '/'), "Materials /"
BUT:
File.join( File.dirname(__FILE__).gsub('\\', '/'), "Materials/" )
In fact it'd be better as:
File.join( File.dirname(__FILE__).tr("\\", "/"), "Materials" )
Then the path can be joined to the files without the trailing '/' ??
Suitable for all OSs.BUT the exactly correct way would be:
File.expand_path( File.join(File.dirname(__FILE__), "Materials") )
Which always uses / rather than \ ...
-
**OK TIG
How to do the same for components?
Example:
I have 3 boxes, "1-Box", "2-Box", "3-Box", they are in the folder "Plugins/ Click-Cuisine2/Components".
I want to create an icon that calls a method of this type:
1st Click
Selects all components with the definition "1-Box ##", and replaces with "2-Box", which is in the "Components" folder.
If no component with the definition " 1-Box" was found, send the message, "Import a Clik-Cuisine 2 furniture, to replace these components ".
2nd Click
Selects all components with the definition "2-Box ##", and replaces it with "3-Box".
3rd Click
Selects all components with the definition "3-Box ##", and replaces with "1-Box".
Final objective:
Click-Cuisine 2, will offer more than 20 IKEA handles that I want to interchange with one click.
This can be done in Dynamic Components, but it is not suitable for such a large number of 3D models.
If you can help me write this method, I would be very glad. **
thank you
David
-
Clicks 1/2/3:
Find all component instances matching a certain name.
If none then messagebox, but if some are collected, then check if the particular replacement component definition exists and use that, otherwise load it from your SKP subfolder... swap the collected instances' definitions for the desired one...
Here is an outline - assuming your clicks are suitably trapped in a Tool, or are buttons on a webdialog etc...
Currently this is for filtered selected objects only:path_to_SKP_folder = "????????" # you must set this from knowing your File.dirname(__FILE__) # which can be joined with the SKP subfolder's name etc model = Sketchup.active_model defns = model.definitions ss = model.selection name = "Box-1" # we'll match the start of the names only /^.../ instances = ss.grep(Sketchup;;ComponentInstance).find_all{|e| e.definition.name =~ /^#{name}/ } unless instances[0] UI.messagebox("#{name} NOT matched !") return else rname = name.next # e.g. Box-2 unless defn = defns[rname] defn = defns.load(File.join(path_to_SKP_folder, rname+".skp")) # you set up SKP path earlier ! end instances.each{|e| e.definition = defn } end
If you want to do it globally, then you need to grep model.entities and then all defns' entities in turn - filtering out .image? - to process only groups and component contexts...
-
@tntdavid said:
**OK TIG
How to do the same for components?
Example:
I have 3 boxes, "1-Box", "2-Box", "3-Box", they are in the folder "Plugins/ Click-Cuisine2/Components".
I want to create an icon that calls a method of this type:
1st Click
Selects all components with the definition "1-Box ##", and replaces with "2-Box", which is in the "Components" folder.
If no component with the definition " 1-Box" was found, send the message, "Import a Clik-Cuisine 2 furniture, to replace these components ".
2nd Click
Selects all components with the definition "2-Box ##", and replaces it with "3-Box".
3rd Click
Selects all components with the definition "3-Box ##", and replaces with "1-Box".
Final objective:
Click-Cuisine 2, will offer more than 20 IKEA handles that I want to interchange with one click.
This can be done in Dynamic Components, but it is not suitable for such a large number of 3D models.
If you can help me write this method, I would be very glad. **
thank you
David
This seems to work for me.
@mod = Sketchup.active_model @ent = @mod.active_entities @last ||= '1'; @next = @last.next; @next='1' if @next=='4' last_name = @last + '-Box'; next_name = @next + '-Box' cmp_folder = File.join(File.dirname(__FILE__).gsub('\\','/'),"TNT_ClickCuisine2/Components/") cis = @ent.grep(Sketchup;;ComponentInstance).find_all{|ci|ci.definition.name[0..4]==last_name} unless cis.empty? for ci in cis cd = @mod.definitions[next_name] unless cd cmp = cmp_folder+"#{next_name}.skp" cd = @mod.definitions.load cmp end ci.definition = cd end @last = @next else UI.messagebox "#{last_name} not found." end
-
**Great! Your two examples work perfectly.
TIG with the method, I must manually select "Box-1", so that "Box-2", replaces it.
How to automatically select all the "Box-1" components, even those that are nested in a piece of furniture?
And How to switch from "Box-2" to "Box-3"?
Sdmitch your code is very surprising!
All "1-Box", "2-Box" and "3-Box" components are selected and replaced automatically.
This works for all components at the first level of the structures list.
For "Boxes" that are inside a group or components, this does not work anymore.
How to solve the problem ?
Sorry, I forgot to specify this detail.
The objectives are more and more precise:
1 - The user clicks on the icon "Change the Handles", to replace all the handles present in all the furniture. ( We are almost there. )
2 - If the user no longer wishes to modify some handles, he must select the furniture that he no longer wishes to modify then click on the icon "Freezing the Handles".
( This action must rename the definition of all the handles present inside the selected furniture. )Rename Definitions = Prevent the first code from acting on the handles.
How do you write this method?
Great thanks to both of you for your help.
See you.
David**
-
If you want to do it globally, then you need to grep model.entities and then all defns' entities in turn - filtering out .image? - to process only groups and component contexts...
instances = model.entities.grep(Sketchup;;ComponentInstance).find_all{|e| e.definition.name =~ /^#{name}/ } model.definitions.each{|d| next if d.image? next unless d.name =~ /^#{name}/ d.instances.each{|e| instances << e } }
Now you have an array 'instances' that match that name pattern.
To process different names have your method take an argument - e.g.
def swapper(name=nil) return unless name ### 'name' is set when calling the method ### main code here end
You then run the method as
swapper("Box-1")
orswapper("Box-2")
etc...
Provided that you always swap by incrementing the name it works as thename.next
turns "Box-1" into "Box-2" for the replacement etc...To process just a selected instance's definition you use a context-menu that tests for ss.length==1 && ss[0].is_a?(Sketchup::ComponentInstance) ...
Then it runs some 'freezer' method code...name = "some_name_???" ### how is this to be set ? defn = ss[0].definition ### selected instance instances = defn.entities.grep(Sketchup;;ComponentInstance).find_all{|e| e.definition.name =~ /^#{name}/ } ds = [] instances.each{|e| ds << e.definition } ds.uniq! ### all matching definitions ds.each{|d| d.name = 'x' + d.name } ### or however you will rename each definition...
-
@tntdavid said:
**> How to automatically select all the "Box-1" components, even those that are nested in a piece of furniture?
David**
Following up on TIG's idea of collecting instances, I think this solves the problem of the embedded components.
@mod = Sketchup.active_model @ent = @mod.active_entities @last ||= '1'; @next = @last.next; @next = '1' if @next == '4' last_name = "#{@last}-Box"; next_name = "#{@next}-Box" if @mod.definitions[last_name] && @mod.definitions[last_name].instances[0] cd = @mod.definitions[next_name] unless cd cmp = File.join(Sketchup.find_support_file('plugins'),"TNT_ClickCuisine2/Components/#{next_name}.skp") cd = @mod.definitions.load cmp end @mod.definitions.find{|d|d.name==last_name}.instances.each{|ci|ci.definition=cd} @last = @next else UI.messagebox "#{last_name} not found." end
-
**I'm sorry to take so much time to answer you but I'm overworked.
First tests on the cubes, all seems to work wonderfully.
I will test your codes directly on Click-Cuisine 2 and post here an example of the revolutions that it will bring to my tool.
I do not know how to thank you because your help allows me to move forward as never before.
See you soon.**
David
-
**Hello,
After some tests of your codes on Click-Cuisine 2, I noticed a small problem that I hope to solve with your help.
The change of component changes perfectly from 1 to 2 then to 3, except if one goes backwards.
Here is an example in image:
Same problem, if I close and I open the file because the script restarts to "1-Component".
How to work around the problem?
I think it is best to delete the error message, even though "1-Component" is not found.
You can download skp example below:
And below the rbz plugin:
Thank you in advance for your help.
David**
-
Without even looking at the code consider this...
The 'file's suffix is to change thus:1 >>> 2
2 >>> 3
3 >>> 1So you don't use 'next' to increment the suffix just a 'case'...
In 'pseudo' code...
case suffix when '1'; suffix = '2' when '2'; suffix = '3' when '3'; suffix = '1' end#case
Attach the suffix the the files' base names as you look through...
If you want to remember the last used 'suffix' then use @suffix in a module or @@suffix in a class.
Then the next time the tool is used the last used enduring variable is recalled...
OR if you are getting the current value from a DC each tim then you can skip the enduring variable route ?
Advertisement