[Plugin] Gradientator v1.21

[Plugin] Gradientator v1.21

Postby steve r » Tue Jun 12, 2012 9:00 pm

Gradientator is my first ever plugin, and it's really something that I created alongside my main project. Actually, I'm mostly posting this in the hopes that you guys will catch any horrible coding practices I've developed, and let me know how to improve upon them. In any case, I thought it might be useful or interesting for others, so here it is. Feedback/criticism is greatly appreciated!

Usage: Select a bunch of stuff (this plugin only works on faces, but will automatically apply the gradient only to faces) and pick "Gradientate" from the Plugins menu. This will open a window where you can pick the three colors in the gradient, and clicking "OK" will begin the process. It will take a few seconds depending on the number of faces selected.

Version 1.2 introduces some interesting functionality, with a WebDialog that is, in part, generated by SketchUp. The result is three drop-down boxes with each of SketchUp's default named colors as options, with each option colored appropriately. Here's an image, to give you an idea:



Per request, some before & after shots. Click to embiggen:







gradientator.rb


Version notes:

v1.21:
- Added version notes
- Made WebDialog resizable, and taller.

v1.2:
- Changed variables to instance variables
- Modified the way colors are calculated; guarantees the first, middle, and last colors will be as chosen by the user.
- Changed the Inputbox to a WebDialog, with procedurally generated drop-down boxes.

v1.1:
- Fixed a ton of bad coding practices (wrapped plugin in a module, etc)
- Added an inputbox at the beginning to allow the user to select the three colors in the gradient
- Added "Distance from Origin" as a gradient method. Much better results! See images.

v1.0:
- Basic random gradient
- Red-Yellow-Lime colors only

Older versions:
3
Last edited by steve r on Fri Jun 22, 2012 1:25 pm, edited 4 times in total.

steve r 
 

Re: [Plugin] Gradientator v1.0

Postby pilou » Tue Jun 12, 2012 9:09 pm

Can yout put an image of the result : before / after ?
0
Frenchy Pilou
Is beautiful that please without concept!
Speedy Galerie
User avatar
pilou 
Top SketchUcator
 

Re: [Plugin] Gradientator v1.0

Postby Edson » Tue Jun 12, 2012 10:24 pm

what is it intended for?
0
edson mahfuz, architect | porto alegre • brasil
http://www.mahfuz.arq.br
User avatar
Edson 
Global Moderator
 

Re: [Plugin] Gradientator v1.0

Postby brookefox » Tue Jun 12, 2012 11:24 pm

Edson wrote:what is it intended for?


From order, :twisted: let there be chaos.

:enlight:
1
~ Brooke
User avatar
brookefox 
 

Re: [Plugin] Gradientator v1.0

Postby thomthom » Wed Jun 13, 2012 9:04 am

Congrats on your first plugin. :)

I have some notes on the coding: (I tend to check the code of plugins from new SketchUp coders)

1. You haven't wrapped your code in a module - meaning all the methods you added are added to the core Object and therefore in every object in the Ruby environment. Please wrap all your code in your own namespace. See this article for more information: http://www.thomthom.net/thoughts/2012/0 ... velopment/

2. You are extending the Float class with some methods to round number, this is the same as adding methods without a namespace, you are very likely to clash with other plugins. Some other plugin might try to add the same method to Float, that behave slightly differently which will cause unexpected results in either plugin. Please avoid such extension and use helper methods within your own namespace. This is also explain in the first link I posted.

In your code it's also redundant, as you use it like so: redness.round_to(0).to_i which is the same as redness.round.

3. You are using typename to determine the type of entity - this is very slow. Use .is_a? instead. See this article for more information: http://www.thomthom.net/thoughts/2011/1 ... -typename/

4. You test against version 8 of SketchUp, but I see nothing in the code that requires version 8. You're not even using the disable_ui flag of model.start_operation - which is a SU 7 feature (and recommended to give you a speed boost). As far as I can tell, it'd work with at least SU6, maybe even older. Also, avoid diaplying message boxes upon loading a plugin, it makes the startup experience of SketchUp unpleasant and might cause the toolbars to shuffle about.

5. I'm wondering what this is about:
Code: Select all
# Setting VERBOSE to nil is a simple speed boost tip from Dan Rathbun on the Sketchucation forums.
$VERBOSE = nil

Can you link to the thread where this is talked about?
Since it's a shared environment I stay away from adjusting global settings like that. But I'm interesting in knowing what speed improvement there is.

6. selection.each{ |f| selection.remove f if f.typename != "Face" }
If you are removing lots of entities from the selection it's best to do it in bulk, otherwise the operation can be very slow.
Code: Select all

not_faces 
= selection.reject { |e| e.is_a?(Sketchup::Face) }
selection.remove( not_faces )
 

However, there is no need for you to modify the user's selection, maybe the user wants to keep the selection. Instead of modifying the selection just inspect it for what you are interested in. In your case the faces.
Code: Select all

faces 
= selection.select{ |e| e.is_a?(Sketchup::Face) }
 

Now you can just refer to the faces collection instead of selection.




Is the plugin creating a colour for each selected face? What happens if you run this multiple times? I think you can easily end up with thousands of materials, which can really slow down the model - material list.
0
Thomas Thomassen — SketchUp Monkey & Coding addict
List of my plugins and link to the CookieWare fund
User avatar
thomthom 
PluginStore Author
PluginStore Author
 

Re: [Plugin] Gradientator v1.0

Postby steve r » Wed Jun 13, 2012 1:29 pm

Edson wrote:what is it intended for?


After looking at the results, I admit it is less useful than I hoped. I'm going to leave the current (highly random) gradient option available, but add another that performs the gradient function based on distance from the origin. I'm also going to allow the user to specify the three colors that will form the gradient. With these options, I'm hoping that the intended usefulness of the plugin -- to quickly apply a gradient to a large number of faces -- will work as intended.
0

steve r 
 

Re: [Plugin] Gradientator v1.0

Postby thomthom » Wed Jun 13, 2012 1:48 pm

steve r wrote:Note that the gradient appears to be somewhat random; this was an unintended consequence. If anyone can think of a neat way to order the gradient, say by closest to origin first, let me know!

For each face find the vertex closest to the origin and sort the faces based on that.
0
Thomas Thomassen — SketchUp Monkey & Coding addict
List of my plugins and link to the CookieWare fund
User avatar
thomthom 
PluginStore Author
PluginStore Author
 

Re: [Plugin] Gradientator v1.0

Postby thomthom » Wed Jun 13, 2012 1:54 pm

thomthom wrote:
steve r wrote:Note that the gradient appears to be somewhat random; this was an unintended consequence. If anyone can think of a neat way to order the gradient, say by closest to origin first, let me know!

For each face find the vertex closest to the origin and sort the faces based on that.

Or, maybe just use the bounds for quicker processing.

Code: Select all

stack 
= {}
faces.each { |facestack[face] = face.bounds.center }
sorted stack.sort { |a,ba[1] <=> b[1] }.map { |nn[0] }
 

(untested)
0
Thomas Thomassen — SketchUp Monkey & Coding addict
List of my plugins and link to the CookieWare fund
User avatar
thomthom 
PluginStore Author
PluginStore Author
 

Re: [Plugin] Gradientator v1.0

Postby sdmitch » Wed Jun 13, 2012 4:36 pm

Using face.bounds.center failed in the sort but using ORIGIN.distance(face.bounds.center) did

Code: Select all
faces=selection.select{|f| f.is_a?(Sketchup::Face)}
stack = {}
faces.each{|face| stack[face]=ORIGIN.distance(face.bounds.center)}
sorted = stack.sort { |a,b| a[1] <=> b[1] }.map { |n| n[0] }
redness=0.0;greenness=255.0;blueness=0.0;colorincrement=(510/faces.length.to_f);
sorted.each{|f|
   f.material = [redness.round,greenness.round,blueness.round];
 redness += colorincrement
 if redness >= 255.0
  redness = 255.0
  greenness -= colorincrement
  if greenness <= 0.0
   greenness = 0.0
  end
 end
}


Color Gradiant.png
0
Nothing is worthless, it can always be used as a bad example.

http://sdmitch.blogspot.com/
User avatar
sdmitch 
PluginStore Author
PluginStore Author
 

Re: [Plugin] Gradientator v1.0

Postby thomthom » Wed Jun 13, 2012 4:56 pm

sdmitch wrote:Using face.bounds.center failed in the sort but using ORIGIN.distance(face.bounds.center) did

Duh! Yes - that was suppose to be there... that's what you get for not testing the code. Good Catch. :oops:
0
Thomas Thomassen — SketchUp Monkey & Coding addict
List of my plugins and link to the CookieWare fund
User avatar
thomthom 
PluginStore Author
PluginStore Author
 

Re: [Plugin] Gradientator v1.0

Postby steve r » Wed Jun 13, 2012 8:11 pm

First of all, let me say that I really appreciate all the help with my (somewhat silly) plugin. I really hoped that I would get some feedback on my general coding practices, and I got more than I expected!

thomthom wrote:1. You haven't wrapped your code in a module - meaning all the methods you added are added to the core Object and therefore in every object in the Ruby environment. Please wrap all your code in your own namespace. See this article for more information: http://www.thomthom.net/thoughts/2012/0 ... velopment/


I didn't even know this was a thing! I'll implement that in v1.1 as well as my future plugins, definitely.

thomthom wrote:2. You are extending the Float class with some methods to round number, this is the same as adding methods without a namespace, you are very likely to clash with other plugins. Some other plugin might try to add the same method to Float, that behave slightly differently which will cause unexpected results in either plugin. Please avoid such extension and use helper methods within your own namespace. This is also explain in the first link I posted.

In your code it's also redundant, as you use it like so: redness.round_to(0).to_i which is the same as redness.round.


Interesting! In another thread I asked about Ruby's round() function, but I was told (correctly) that it was not supported. I didn't understand that there is still a round function. That clears up a lot! I was able to dump that whole class thing and just use round.

thomthom wrote:3. You are using typename to determine the type of entity - this is very slow. Use .is_a? instead. See this article for more information: http://www.thomthom.net/thoughts/2011/1 ... -typename/


Oh man, my bad! I totally saw this in the optimization thread... then promptly forgot it. Fixed!

thomthom wrote:4. You test against version 8 of SketchUp, but I see nothing in the code that requires version 8. You're not even using the disable_ui flag of model.start_operation - which is a SU 7 feature (and recommended to give you a speed boost). As far as I can tell, it'd work with at least SU6, maybe even older. Also, avoid diaplying message boxes upon loading a plugin, it makes the startup experience of SketchUp unpleasant and might cause the toolbars to shuffle about.


Good point! I copypasta'd that from another plugin, as I thought it would prevent issues with earlier versions of plugins, but I don't know what the major differences between SketchUp versions even are. In any case, I've enabled disable_ui and changed version check to look for 7.0.

thomthom wrote:5. I'm wondering what this is about:
Code: Select all
# Setting VERBOSE to nil is a simple speed boost tip from Dan Rathbun on the Sketchucation forums.
$VERBOSE = nil

Can you link to the thread where this is talked about?
Since it's a shared environment I stay away from adjusting global settings like that. But I'm interesting in knowing what speed improvement there is.


Sure! It's here in the optimization thread. It's possible I totally did not understand how to properly use that information.

thomthom wrote:6. selection.each{ |f| selection.remove f if f.typename != "Face" }
If you are removing lots of entities from the selection it's best to do it in bulk, otherwise the operation can be very slow.
Code: Select all

not_faces 
= selection.reject { |e| e.is_a?(Sketchup::Face) }
selection.remove( not_faces )
  

However, there is no need for you to modify the user's selection, maybe the user wants to keep the selection. Instead of modifying the selection just inspect it for what you are interested in. In your case the faces.
Code: Select all

faces 
= selection.select{ |e| e.is_a?(Sketchup::Face) }
  

Now you can just refer to the faces collection instead of selection.


I figured there was an easier way to do that! I just didn't know how... thanks for the tip!

thomthom wrote:Is the plugin creating a colour for each selected face? What happens if you run this multiple times? I think you can easily end up with thousands of materials, which can really slow down the model - material list.


It's true... running this multiple times floods your "In Model" materials. Is there a way to apply a color without affecting the list? Perhaps that's a consequence of this tool, regardless of implementation.

sdmitch wrote:Using face.bounds.center failed in the sort but using ORIGIN.distance(face.bounds.center) did

Code: Select all
faces=selection.select{|f| f.is_a?(Sketchup::Face)}
stack = {}
faces.each{|face| stack[face]=ORIGIN.distance(face.bounds.center)}
sorted = stack.sort { |a,b| a[1] <=> b[1] }.map { |n| n[0] }
redness=0.0;greenness=255.0;blueness=0.0;colorincrement=(510/faces.length.to_f);
sorted.each{|f|
   f.material = [redness.round,greenness.round,blueness.round];
 redness += colorincrement
 if redness >= 255.0
  redness = 255.0
  greenness -= colorincrement
  if greenness <= 0.0
   greenness = 0.0
  end
 end
}


This is incredibly helpful! I had been trying today to figure out how to get thomthom's method to work, and I kept getting errors. Does anyone feel like explaining this line?

Code: Select all
sorted = stack.sort { |a,b| a[1] <=> b[1] }.map { |n| n[0] }


I really can't make heads or tails of it. If not, that's okay, I'll probably investigate it on my own when I can.

Again, thanks to everyone who offered tips!
0

steve r 
 

Re: [Plugin] Gradientator v1.1

Postby steve r » Wed Jun 13, 2012 9:01 pm

With all that help, I was able to update the plugin. See the OP for pictures of the new "Distance from Origin" function! You can also now select the colors to include in your gradient. The default is red-yellow-lime. Notice that SketchUp's "Lime" is actually pure green (RGB = 0,255,0).

Also, if anyone does download this thing, let me know how the drop-down list for each color looks. I'm on Windows 7 and it won't stay open unless I hold down the mouse button, and even then I can't scroll down the list. Is there a better way to do this? It seems to be a problem with SketchUp's implementation of the drop-down list.
0
Last edited by steve r on Wed Jun 13, 2012 9:18 pm, edited 1 time in total.

steve r 
 

Re: [Plugin] Gradientator v1.1

Postby brookefox » Wed Jun 13, 2012 9:18 pm

Thanks much tt, Sam and steve. Look forward to checking it out.
0
~ Brooke
User avatar
brookefox 
 

Re: [Plugin] Gradientator v1.0

Postby thomthom » Wed Jun 13, 2012 11:17 pm

steve r wrote:
Code: Select all
sorted = stack.sort { |a,b| a[1] <=> b[1] }.map { |n| n[0] }

This does two things:

1:
stack.sort { |a,b| a[1] <=> b[1] }
It sorts the stack hash by the values in the hash - and returns an array with [key,value] arrays.
In my case I just added a point as the value, though it should have been the distance from ORIGIN to that point, as sdmitch pointed out.

2:
.map { |n| n[0] }
This maps the multidimensional [key,value] paired array into an array with just the faces - which is now ordered by distance from origin.

Did that make it clearer?
0
Thomas Thomassen — SketchUp Monkey & Coding addict
List of my plugins and link to the CookieWare fund
User avatar
thomthom 
PluginStore Author
PluginStore Author
 

Re: [Plugin] Gradientator v1.0

Postby steve r » Fri Jun 15, 2012 6:29 pm

thomthom wrote:1:
stack.sort { |a,b| a[1] <=> b[1] }
It sorts the stack hash by the values in the hash - and returns an array with [key,value] arrays.
In my case I just added a point as the value, though it should have been the distance from ORIGIN to that point, as sdmitch pointed out.

This part's function makes intuitive sense, I suppose, but I don't think I would have ever arrived at this on my own. I've used code like hash.each{ |e| code } before, but what does having two variables in the || do? Does that return the "address" of each value in the hash, in addition to the value itself? Is <=> some sort of comparator?
thomthom wrote:2:
.map { |n| n[0] }
This maps the multidimensional [key,value] paired array into an array with just the faces - which is now ordered by distance from origin.

So instead of creating one array with the [key,value] values inside, and then mapping that array into an array without the keys, we're directly mapping the values (which are all faces) into another array (which is now ordered by distance)?

I think this makes sense, but this definitely serves to remind me how much I have yet to learn about Ruby. Thanks again for all the help.
0

steve r 
 

Re: [Plugin] Gradientator v1.1

Postby jeff hammond » Fri Jun 15, 2012 6:42 pm

looks cool.. thank you for sharing it!
0
dotdotdot
User avatar
jeff hammond 
Global Moderator
 

Re: [Plugin] Gradientator v1.0

Postby thomthom » Sat Jun 16, 2012 1:57 am

steve r wrote:I've used code like hash.each{ |e| code } before, but what does having two variables in the || do? Does that return the "address" of each value in the hash, in addition to the value itself? Is <=> some sort of comparator?

A hash always has keys and values, so when you iterate them you traverse them both - hence the two variables between the two ||. An array only has values, which is why you only use one variables. I don't even know if you get keys or values if you only use one variable when traversing hashes... :?
0
Thomas Thomassen — SketchUp Monkey & Coding addict
List of my plugins and link to the CookieWare fund
User avatar
thomthom 
PluginStore Author
PluginStore Author
 

Re: [Plugin] Gradientator v1.2

Postby steve r » Thu Jun 21, 2012 10:23 pm

Just wanted to mention that I've updated the plugin substantially, with new ways to calculate the colors. More importantly, this replaces the earlier inputbox with a WebDialog that is partially generated by the plugin, using SketchUp's named colors array to create three drop-down boxes for user input, with each color in each drop-down box colored according to each color. It's hard to describe, but check out the OP for an image. It turned out much better than I had planned!

This will probably be the last update, unless I decide to use color.blend to generate each part of the gradient later; I didn't know that this function existed until recently.
0

steve r 
 

Re: [Plugin] Gradientator v1.2

Postby thomthom » Fri Jun 22, 2012 12:24 am

:thumb: :thumb:

steve r wrote:This will probably be the last update, unless I decide to use color.blend to generate each part of the gradient later; I didn't know that this function existed until recently.


I hope you got more plugin creations coming! :)
0
Thomas Thomassen — SketchUp Monkey & Coding addict
List of my plugins and link to the CookieWare fund
User avatar
thomthom 
PluginStore Author
PluginStore Author
 

Re: [Plugin] Gradientator v1.2

Postby wyatt » Fri Jun 22, 2012 4:04 am

I've been looking forward to this plugin developing a bit more. This upgrade is a really big improvement, and it works well. I can't see the OK button in the dialog though unless I maximize the window. Is there a way to make the dialog bigger or adjustable? Anyone else see this too?
0

wyatt 
 

Re: [Plugin] Gradientator v1.2

Postby ivreich » Fri Jun 22, 2012 7:40 am

Would it be possible to apply a colour gradient to a selection of groups/components like in this example:

While still maintaining component definitions?

joel
1

ivreich 
 

Re: [Plugin] Gradientator v1.2

Postby steve r » Fri Jun 22, 2012 1:18 pm

Wyatt wrote:I've been looking forward to this plugin developing a bit more. This upgrade is a really big improvement, and it works well. I can't see the OK button in the dialog though unless I maximize the window. Is there a way to make the dialog bigger or adjustable? Anyone else see this too?


What OS are you using? Could you take a screenshot of the dialog before you maximize it?

EDIT: I've updated the plugin so the WebDialog is resizeable, and I've made it a bit taller. Let me know if that helps!
0

steve r 
 

Re: [Plugin] Gradientator v1.21

Postby wyatt » Fri Jun 22, 2012 2:23 pm

I was at home on Windows 7 last night. I tested it this morning at work, also Win7 and came up with this:
screenshot.jpg

At home I couldn't see the tops of the Ok and Cancel button. I'm not sure why I can now at work. I can test 1.2 again at home later today and post that screenshot too. The 1.21 update fixes it, at least at work. I was able to resize and it seems to remember the setting even after I close and restart SU. Thanks for the quick response.

One request if you decide to continue updating this script would be to add the ability to sort the colors by color in addition to by name. This option would put all the greens together for instance. Just an idea.
0

wyatt 
 

Re: [Plugin] Gradientator v1.21

Postby steve r » Fri Jun 22, 2012 6:55 pm

thomthom wrote:I hope you got more plugin creations coming! :)


I do! This was just a small project to get my feet wet, so to speak. My main SketchUp Ruby project has been in development for over a year, but a small part of it required dynamic gradient generation, and I thought this might be useful for others. I hope to release my other project this year, but who knows.

Also, thanks for all your help! I was really hoping I could get some coding tips, and it was extremely helpful to have such a thorough review.

ivreich wrote:Would it be possible to apply a colour gradient to a selection of groups/components... While still maintaining component definitions?


I don't know anything about how Sketchup handles applying colors to components, but I know you can apply the paint bucket in SketchUp "outside" of a component manually. This is a good idea -- I'll have to look into it!

Wyatt wrote:At home I couldn't see the tops of the Ok and Cancel button... One request if you decide to continue updating this script would be to add the ability to sort the colors by color in addition to by name. This option would put all the greens together for instance. Just an idea.


Sorry about the problems, I thought I had fixed the width and height to fit all the contents, but having the option to adjust the size of the window is probably better. Sorting by color is a good thought. I made a few attempts at this, the best I could do is sort by saturation, or by one of the three channels (red, green, blue) which is close but not what most people would want. If I can get this to work, I'll update for sure!
0

steve r 
 

Re: [Plugin] Gradientator v1.21

Postby steve r » Fri Jun 22, 2012 7:14 pm

Oh, one more thing: I messed with color.blend a little, and it seems to be pretty accurate. The SketchUp developer page only provides examples of 0.0, 0.5, and 1.0, but it seems to work quite well with Float values of any precision. However, it seems to truncate the results, not round them. So, if you're blending Red [255,0,0] and Lime [0,255,0] with these values:

color1.blend color2, 0.2156
color1.blend color2, 0.2157

In the first case, you'd expect 54.978 ( = 0.2156 * 255 ) for the red channel, which should round to 55, but instead you'll get 54 for the red channel. In the second case, you'd expect 55.0035 ( = 0.2157 * 255 ) for the red channel, which SketchUp makes 55.

So, it appears that color.blend will truncate its results, not round them. Just thought that might be useful bit of information.
0

steve r 
 

Re: [Plugin] Gradientator v1.21

Postby thomthom » Fri Jun 22, 2012 9:57 pm

steve r wrote:So, it appears that color.blend will truncate its results, not round them. Just thought that might be useful bit of information.

That is interesting. I replicated blend in a C Extension for speed - not sure if I caught that detail. Will have to check.
0
Thomas Thomassen — SketchUp Monkey & Coding addict
List of my plugins and link to the CookieWare fund
User avatar
thomthom 
PluginStore Author
PluginStore Author
 

Re: [Plugin] Gradientator v1.21

Postby stuartmitch » Wed Jul 18, 2012 1:18 pm

Have loaded gradientator v 1.2. When using, only the first colour is applied to the face, not all three.

Any ideas?

Cheers....Scotty
0

stuartmitch 
 

Re: [Plugin] Gradientator v1.21

Postby thomthom » Wed Jul 18, 2012 4:36 pm

stuartmitch wrote:Have loaded gradientator v 1.2. When using, only the first colour is applied to the face, not all three.

Any ideas?

Cheers....Scotty

You selected only one face? It doesn't do a gradient over a single face, but apply plain colours to a set of faces based on their distance from each other.
0
Thomas Thomassen — SketchUp Monkey & Coding addict
List of my plugins and link to the CookieWare fund
User avatar
thomthom 
PluginStore Author
PluginStore Author
 

Re: [Plugin] Gradientator v1.21

Postby stuartmitch » Thu Jul 19, 2012 2:15 am

Thanks Thom Thom, is there anything you don't know about???

I have tried it with adjoining and separate faces and it still doesn't seem to provide a Gradient of colors. Rather, it simply adds color 1 to the first face, color 2 to the second face and so on. Do I need to stipulate the degree of gradient somewhere?

Cheers....Scotty
0

stuartmitch 
 

Re: [Plugin] Gradientator v1.21

Postby gring » Tue Jul 24, 2012 11:46 am

thank you for sharing it!
0

gring 
 

SketchUcation One-Liner Adverts

by Ad Machine » 5 minutes ago



Ad Machine 
Robot
 

Next


 

Return to Plugins

Who is online

Users browsing this forum: Google Bot, hkh123, ZElena and 14 guests

Visit our sponsors: