[Code] material.delete

[Code] material.delete

Postby TIG » Tue Jun 23, 2009 11:26 am

This adds a new Method to Sketchup's Material Class - 'delete'
Usage: material.delete
It changes any entities use that material to the default-material, and then removes that material from the model and the materials-browser.
It has been streamlined as much as possible but will search potentially thousands of entities to remove that material...
Edit:
v1.1 has simplified defn changing and respond_to? material test - thanks to thomthom.
v1.2 materials made into array to remove self.
v1.3 empty temp groups used without detriment.
v1.4 operation removed as it should be used with another one.
material-delete.rb
0
Last edited by TIG on Wed Jun 24, 2009 10:28 am, edited 9 times in total.
TIG
User avatar
TIG 
Global Moderator
 

Re: [Code] material.delete

Postby thomthom » Tue Jun 23, 2009 11:43 am

If read the code right, you iterate all the entities of each component instance and group?
A quicker way would be to just run over the entities in the model.definitions collection and then the model.entities.
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: [Code] material.delete

Postby TIG » Tue Jun 23, 2009 11:49 am

It has to deal with nested groups and instances etc... It processes every instance as each could have different materials, if it hasn't processed an instance's definition (i.e. it's not in @done) it does that too, otherwise it skips it... Not sure if the other way'd be quicker ?
0
TIG
User avatar
TIG 
Global Moderator
 

Re: [Code] material.delete

Postby thomthom » Tue Jun 23, 2009 12:04 pm

You don't need to dig into the nesting.

Simplified example:
Code: Select all
model.entities.each { |e| e.material = nil if e.respond_to? :material }
model.definitions.each { |d|
    d.entities.each { |e| e.material = nil if e.respond_to? :material }
}

This will strip the material of ALL entities that has a material property. Including the materials applied to ComponentInstances and Groups. No need to keep track of which definitions has already been processed. (of course, back_material will have to be accounted for.

Other notes: won't your version choke if it runs into entities that doesn't have material property?

And would it be quicker if the dummy geometry was a single edge instead of a face (four edges and a face)? Or even just empty dummy groups?
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: [Code] material.delete

Postby thomthom » Tue Jun 23, 2009 12:52 pm

Just to illustrate with some high-tech ASCII art:

If you have a model with a entity tree like this: (Excluding edges)
Code: Select all
+ Model
+ model.entities
| Face1
| Face2
| Face3
| Group1 [Def1]
+--+ Face4
|  | Face5
|  | Instance3 [Def3]
|  +--+ Face7
|     | Face8
|     | Face9
| Group2 [Def1]
+--+ Face4
|  | Face5
|  | Instance3 [Def3]
|  +--+ Face7
|     | Face8
|     | Face9
| Group3 [Def2]
+--| Face6
| Instance1 [Def3]
+--| Face7
|  | Face8
|  | Face9
| Instance2 [Def3]
+--| Face7
   | Face8
   | Face9


The you can see how you get all entities when you loop over model.entities. and model.definitions...entities:
Code: Select all
+ model.entities
| Face1
| Face2
| Face3
| Group1 [Def1]
| Group2 [Def1]
| Group3 [Def2]
| Instance1 [Def3]
| Instance2 [Def3]

+ model.definitions
+--+ Def1.entities
|  | Face4
|  | Face5
|  | Instance3 [Def3]
|
+--+ Def2.entities
|  | Face6
|
+--+ Def3.entities
   | Face7
   | Face8
   | Face9
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: [Code] material.delete

Postby thomthom » Tue Jun 23, 2009 1:29 pm

Where does the face.copy method derive from? I can't find it in the API docs.


The self_to_nil method, is that embedded in .delete? Local to that method only? Never seen that method nesting before. Didn't even know it was possible. Feels very much like Javascript that way.
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: [Code] material.delete

Postby TIG » Tue Jun 23, 2009 1:57 pm

thomthom wrote:Where does the face.copy method derive from? I can't find it in the API docs.
The self_to_nil method, is that embedded in .delete? Local to that method only? Never seen that method nesting before. Didn't even know it was possible. Feels very much like Javascript that way.

The 'face.copy' is simply 'group.copy' - the group was called 'face' !!!
You can embed a def locally within another def - useful if you want to 'loop until' etc.

Having taken on board your comments I have tweaked the method... here's the updated one viewtopic.php?p=166978#p166978
0
TIG
User avatar
TIG 
Global Moderator
 

Re: [Code] material.delete

Postby thomthom » Tue Jun 23, 2009 2:22 pm

TIG wrote:The 'face.copy' is simply 'group.copy' - the group was called 'face' !!!

Ah. didn't see that. Thought it maybe wes undocumented.

TIG wrote:You can embed a def locally within another def - useful if you want to 'loop until' etc.

That sure is. Will have to remember that.

I think this method, and the layer version would fit well into the Skx library. Would you mind if this was included? I can add it to the repository for you if you don't want to bother with setting up Mercurial.
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: [Code] material.delete

Postby thomthom » Tue Jun 23, 2009 2:27 pm

Just had a though. What happens if this method is called from another method that already has done a model.start_operation?

can operations be nested? Or will it interrupt the previous? Maybe one of the transparency flags (SU7 only) can be used?
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: [Code] material.delete

Postby Jim » Tue Jun 23, 2009 2:36 pm

That's a good point, Thom. I'm not sure start_operation belongs in specific methods.

I asked somewhere here if nesting start_operations was allowed, or what happens if they are nested. It's possible to write some "guards" for start_operation that prevent nesting - which may be interesting to do just to see what is really happening with all the scripts you have already installed.
0
Hi

Jim 
Global Moderator
 

Re: [Code] material.delete

Postby thomthom » Tue Jun 23, 2009 2:43 pm

I'm wondering if trans or prev_trans flag can be used to inject these types of methods. But I don't fully understand the difference between the two. And I'm also unsure of the warning for trans apply to prev_trans.
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: [Code] material.delete

Postby TIG » Tue Jun 23, 2009 4:06 pm

...
I think this method, and the layer version would fit well into the Skx library. Would you mind if this was included? I can add it to the repository for you if you don't want to bother with setting up Mercurial.

Please add them for me...
0
TIG
User avatar
TIG 
Global Moderator
 

Re: [Code] material.delete

Postby TIG » Tue Jun 23, 2009 4:08 pm

As Jim says, perhaps best to remove the start/commit_operations unless 'we' can work out how best to 'transparencify' them... Advice welcome...
0
TIG
User avatar
TIG 
Global Moderator
 

Re: [Code] material.delete

Postby thomthom » Tue Jun 23, 2009 4:12 pm

Ok. I'll add it to the Skx. Thanks for sharing the snippet.

After this week I'll have some time to do some experiment with start operation. It'd be interesting if we could find a best practice.
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: [Code] material.delete

Postby TIG » Tue Jun 23, 2009 4:21 pm

Use v1.2: fixed an array bug.
viewtopic.php?p=166978#p166978
0
Last edited by TIG on Wed Jun 24, 2009 10:30 am, edited 1 time in total.
TIG
User avatar
TIG 
Global Moderator
 

Re: [Code] material.delete

Postby TIG » Wed Jun 24, 2009 9:35 am

v1.3 now uses empty temp groups without detriment and so must be a little quicker too ... viewtopic.php?p=166985#p166985
0
TIG
User avatar
TIG 
Global Moderator
 

Re: [Code] material.delete

Postby TIG » Wed Jun 24, 2009 10:29 am

v1.4 - now has operation properly removed ! ... viewtopic.php?p=166985#p166985
0
TIG
User avatar
TIG 
Global Moderator
 

SketchUcation One-Liner Adverts

by Ad Machine » 5 minutes ago



Ad Machine 
Robot
 



 

Return to Developers' Forum

Who is online

Users browsing this forum: No registered users and 17 guests

Visit our sponsors: