[PLUGIN] Add Texture from MaterialColor

[PLUGIN] Add Texture from MaterialColor

Postby rami_lpm » Mon Oct 29, 2012 7:35 pm

This plugin checks for any material without a texture, then assigns one(4x4 px bmp) based on the color of the material.
This is useful when importing in Lumion. It makes editing material properties easier(no need to generate the texture bitmap).

This is my first ruby published, so I welcome any comments
2
Last edited by rami_lpm on Tue Apr 16, 2013 1:32 pm, edited 4 times in total.
User avatar
rami_lpm 
PluginStore Author
PluginStore Author
 

Re: [PLUGIN] Add Texture from MaterialColor

Postby Aerilius » Mon Oct 29, 2012 7:59 pm

Thanks for posting!

As for file paths, I don't know whether backslash paths will work for OS X users. Ruby usually abstracts filepaths and uses internally the standard unix paths.
Instead of doing:
matPath=File.dirname( Sketchup.active_model.path ) + '\mat\\'
thumbnail_fn = matPath + e.name + '.bmp'

you could do:
mat_path = File.join(File.dirname(Sketchup.active_model.path), 'mat')
thumbnail_fn = File.join(mat_path, e.name + '.bmp')

This way, Ruby will care about using the correct delimiter.
In addition to that, active_model.path is empty if the model has not been saved yet. In that case we either have to let the user choose a location, or we fallback to a default directory:
Sketchup.active_model.path || ENV["HOME"] || ENV["HOMEPATH"]
0

Aerilius 
PluginStore Author
PluginStore Author
 

Re: [PLUGIN] Add Texture from MaterialColor

Postby rami_lpm » Mon Oct 29, 2012 8:46 pm

well, testing under windows vista
mat_path = File.join(File.dirname(Sketchup.active_model.path), 'mat')
returns
c:\folder\subfolder/mat
so ruby can't create the folder

Sketchup.active_model.path || ENV["HOME"] || ENV["HOMEPATH"]
does nothing for me(afaik).
I prevent the user from running the script unless the model is saved.
cmd.set_validation_proc

I'd have to make a separate .rb for mac, and i can't test it.
0
User avatar
rami_lpm 
PluginStore Author
PluginStore Author
 

Re: [PLUGIN] Add Texture from MaterialColor

Postby Aerilius » Mon Oct 29, 2012 8:59 pm

Sorry, I oversaw that:
File.expand_path expands all truncated path (or links) and translates Windows into unix delimiters. For unsaved models, active_model.path is an empty string (not nil), so we add an explicit conditional:
model_path = File.expand_path(Sketchup.active_model.path)
model_path = ENV["HOME"] || ENV["HOMEPATH"] || return if model_path.empty?


I'd have to make a separate .rb for mac, and i can't test it.

I don't think so. As long as we can avoid introducing platform-specific calls, we should keep one single version (which is also much easier to maintain). Most scripts (that use the SketchUp API or standard Ruby) are cross-platform anyways. Here are a lot of helpful people that help testing.
0

Aerilius 
PluginStore Author
PluginStore Author
 

Re: [PLUGIN] Add Texture from MaterialColor

Postby rami_lpm » Mon Oct 29, 2012 9:07 pm

I think I can make that work, I'll upload the corrected version as soon as I have it. Thanks for the advise.
0
User avatar
rami_lpm 
PluginStore Author
PluginStore Author
 

Re: [PLUGIN] Add Texture from MaterialColor

Postby TIG » Mon Oct 29, 2012 11:40 pm

The hybrid c:\folder\subfolder/mat should still work when checking/making the folder - it just looks odd...
Aerilius's File.expand_path(...) will simply make all \ into /.
The only time you need to keep a \ as a delimiter in a path is if you are on a PC and you are going to use it in a system/DOS call like a .bat or .cmd file...
PCs can otherwise cope with either \ OR / MACs always want /, so .expand_path or .tr("\\","/") will both fix any path to suit most things, and at least display neater if it's printed out...
To make the path PC AND DOS suited you can reverse the path delimiter logic as .tr("/","\\") :?
0
TIG
User avatar
TIG 
Global Moderator
 

Re: [PLUGIN] Add Texture from MaterialColor

Postby rami_lpm » Tue Oct 30, 2012 2:07 pm

Done, I used File.expand_path(...) and modified to allow running the plugin without saving.
Anyways, it's a good idea to save before.
0
User avatar
rami_lpm 
PluginStore Author
PluginStore Author
 

Re: [PLUGIN] Add Texture from MaterialColor

Postby tetrochuan » Tue Apr 16, 2013 10:41 am

hi there,

could you provide a modified version of the script which gives textures of 1mx1m?

thanks in advance=)
0

tetrochuan 
 

Re: [PLUGIN] Add Texture from MaterialColor

Postby Aerilius » Tue Apr 16, 2013 11:22 am

Simply add after line 97:
Code: Select all
    e.texture.size = [1.m, 1.m]


Ramiro, the rescue block gives out an error assuming an error with non-ASCI characters. But there could be sooooo much else that causes errors (that's why it is sometimes good to output the original error message). If you add this in line 110, the folder deletes successfully:
Code: Select all
  Dir.entries(matPath).each{|f|
    file = File.join(matPath, f)
    next unless File.file?(file)
    File.delete(file)
  }
  Dir.rmdir(matPath)
0

Aerilius 
PluginStore Author
PluginStore Author
 

Re: [PLUGIN] Add Texture from MaterialColor

Postby rami_lpm » Tue Apr 16, 2013 1:37 pm

Hi Aerilius, thanks for the tip.
I modified the block to include the original error message. I also added your corrections, but the problem (in my PC) is that in a folder with non-ASCII chars, I can't even create the temporary mats subfolder.
0
User avatar
rami_lpm 
PluginStore Author
PluginStore Author
 

Re: [PLUGIN] Add Texture from MaterialColor

Postby deaneau » Thu Oct 29, 2015 12:40 am

Ok is an older thread however this is what i need however i would have a given name of material for the texture and don´t touch material this have textures.

the plugin give the noname Material Name and Texture Name

Snap_2015.10.29_00h31m17s_003_Materials.png


i would have this: Material Name and Texture Name (both the same)

Snap_2015.10.29_00h32m07s_004_Materials.png


this Material have a Texture

Snap_2015.10.29_00h33m35s_005_Materials.png


the Plugin change the name of the material, this i doesn´t want

Snap_2015.10.29_00h36m35s_006_Materials.png
0
MADE, BORN AND LIVING IN BERLIN
Big Thank You to all Programmers
Some German words are so long that they have a perspective. M.Twain
User avatar
deaneau 
 

SketchUcation One-Liner Adverts

by Ad Machine » 5 minutes ago



Ad Machine 
Robot
 



 

Return to Plugins

Who is online

Users browsing this forum: Bing [Bot], Google Bot, jkleinnagelvoort, Jonaniki15, jotaw86, katherine155, loblack, Londian04, Maleky, Yato Yatogami and 26 guests

Visit our sponsors: