SketchUcation Plugin Store

 

 

frontside and backside colour

frontside and backside colour

Postby ChrisKa » Sun Jan 15, 2012 9:33 am

Hello,

if I import a COLLADA-File I can change the (frontside) colour with the following Code:
Sketchup.active_model.active_entities.each{|e|e.material="green"};

I want to do the same with the backside of the areas, but this Code doesn't work:
Sketchup.active_model.active_entities.each{|e|e.back_material="green"};

Can somebody help me why this Code doesn't work?

Thanks,
Chris
ChrisKa
 
Posts: 8
Joined: Sun Nov 20, 2011 5:57 pm

Re: frontside and backside colour

Postby thomthom » Sun Jan 15, 2012 11:54 am

You're getting errors, right?

Only faces have the .back_material method - so when you iterate a whole entity collection you're also iterating over edges, guides etc. You need to ensure you're applying back material to only faces.

If that is not the case - then you need to provide more info on what you are doing and exactly what "isn't working". (Errors or unexpected results. Sample models help.)
Thomas Thomassen — SketchUp Monkey & Coding addict
List of my plugins and link to the CookieWare fund
User avatar
thomthom
Global Moderator
 
Posts: 17915
Joined: Tue Nov 13, 2007 12:47 pm
Location: Trondheim, Norway
Name: Thomas Thomassen
Operating system: Windows
SketchUp version: 2013
License type: Pro
SketchUp use: architecture
Level of SketchUp: Advanced

Re: frontside and backside colour

Postby ChrisKa » Sun Jan 15, 2012 12:15 pm

Hy thomthom,

this is my COLLADA-File: COLLADAfile.zip

The frontside colour I can change. This you can see on the file "change_frontside_colour.jpg".

And now my problem is that I can't change the backside colour!
"change_backside_colour_ERROR.jpg"

Thanks a lot for your help,
ChrisKa
Please, register (free) to access all the attachments on the forums.
ChrisKa
 
Posts: 8
Joined: Sun Nov 20, 2011 5:57 pm

Re: frontside and backside colour

Postby thomthom » Sun Jan 15, 2012 12:27 pm

That is it - you are trying to call .back_material on a ComponentInstance.

You need to ensure you got a Face.

Sketchup.active_model.active_entities.each{|e|e.back_material="green" if e.is_a?(SketchUp::Face)};

But note that if the geometry of yours is inside a group or component your code won't dig into the nested groups/components. As you have the code now, the currently active context (open component) must be the one containing the faces.
Thomas Thomassen — SketchUp Monkey & Coding addict
List of my plugins and link to the CookieWare fund
User avatar
thomthom
Global Moderator
 
Posts: 17915
Joined: Tue Nov 13, 2007 12:47 pm
Location: Trondheim, Norway
Name: Thomas Thomassen
Operating system: Windows
SketchUp version: 2013
License type: Pro
SketchUp use: architecture
Level of SketchUp: Advanced

Re: frontside and backside colour

Postby ChrisKa » Sun Jan 15, 2012 12:46 pm

Sorry, but I don't understand.
Do you mean that I have to explode the component for working this code.

Is there a possibility that I can change the backside-colour inside the component like the frontside-colour?
ChrisKa
 
Posts: 8
Joined: Sun Nov 20, 2011 5:57 pm

Re: frontside and backside colour

Postby thomthom » Sun Jan 15, 2012 12:55 pm

For that simple example you provided you only need to open the component before you run the script.

But I don't know the bigger context where you use this.


Here is a snippet that process the entire model:

Code: Select all
# Process entities in the root model context
for e in Sketchup.active_model.entities
  next unless e
.is_a?(SketchUp::Face)
  e.material="green"
  e.back_material="green"
end
# Process all groups and components
for d in Sketchup.active_model.definitions
  next if d
.image?
  for e in d.entities
    next unless e
.is_a?(SketchUp::Face)
    e.material="green"
    e.back_material="green"
  end
end
Thomas Thomassen — SketchUp Monkey & Coding addict
List of my plugins and link to the CookieWare fund
User avatar
thomthom
Global Moderator
 
Posts: 17915
Joined: Tue Nov 13, 2007 12:47 pm
Location: Trondheim, Norway
Name: Thomas Thomassen
Operating system: Windows
SketchUp version: 2013
License type: Pro
SketchUp use: architecture
Level of SketchUp: Advanced

Re: frontside and backside colour

Postby ChrisKa » Sun Jan 15, 2012 4:23 pm

I would like to import the COLLADA-File with the "Ruby-Console".
The code should be in one line.

The result should look like this file result-OK.jpg

I still have the problem with the backside! See result-false.jpg

Do you have an idea?
Thanks,
Chris
Please, register (free) to access all the attachments on the forums.
ChrisKa
 
Posts: 8
Joined: Sun Nov 20, 2011 5:57 pm

Re: frontside and backside colour

Postby TIG » Sun Jan 15, 2012 6:13 pm

Your code currently makes everything in the model.active_entities have the specified material.
Because the imported geometry arrives inside a component your code gives that material to the instance - NOT its contents.
Faces inside a component [or group] that have no material applied to them [front or back] will initially show in the default material.
Then if you give the 'container' a material then those faces will be displayed as if they had that material, BUT they are actually still in the default material.
If you want to give a material to the faces inside a component you need to use something like this.
Assuming that you want every face in the model to use that material...
Sketchup.active_model.definitions.each{|d|next if d.image?; d.entities.each{|e|next unless e.class==Sketchup::Face; e.material="green"; e.back_material="green"}}
To ALSO process any faces in the model's entities use this:
Sketchup.active_model.entities.each{|e|next unless e.class==Sketchup::Face; e.material="green"; e.back_material="green"}
You can obviously mess around with materials and so on...
You could even limit the change to the imported component [its component name ISN'T the same as the dae file so we must get all components before we start and then find which one was [or ones were] added by the import]...
ds=Sketchup.active_model.definitions.to_a; ###add your importer code here...; ds=Sketchup.active_model.definitions.to_a-ds;###ds is an array of just the imported component[s]; ds.each{|d|d.entities.each{|e|next unless e.class==Sketchup::Face; e.material="green"; e.back_material="green"}}
:geek:
TIG
User avatar
TIG
Global Moderator
 
Posts: 14310
Joined: Mon Nov 12, 2007 7:24 pm
Location: Northumbria UK
Name: TIG
Operating system: Windows
SketchUp version: 2013
License type: Pro
SketchUp use: architecture
Level of SketchUp: Advanced

Re: frontside and backside colour

Postby ChrisKa » Sun Jan 15, 2012 8:26 pm

Thanks a lot for your help.
Now this code is very helpful for me!

ChrisKa
ChrisKa
 
Posts: 8
Joined: Sun Nov 20, 2011 5:57 pm

SketchUcation One-Liner Adverts

by Ad Machine » 5 minutes ago

Are you a Premium Member? Get your freebies here. Are you not a Premium Member yet? Upgrade your account to grab these freebies instantly.

Ad Machine
Robot
 
Posts: 2012


Return to Developers' Forum

Who is online

Users browsing this forum: No registered users and 3 guests