Change face color of component
-
I have a script that creates cylinders turns them into components and displays them, I then export an picture of the view. I would like to then change the color of the components faces and export it again to an image like so: File1 and File 2 attached
Any help is appreciated, thanks,
MikeThe script is as follows:
def createComp(compColor, yaxis) normal_vector = Geom;;Vector3d.new(0,0,1) radius = 0.5 center_point = Geom;;Point3d.new(yaxis,1,0) mygroup = Sketchup.active_model.entities.add_group myedgearray = mygroup.entities.add_circle center_point, normal_vector, radius myfirst_edge = myedgearray[0] myface = mygroup.entities.add_face(myedgearray) myface.material = compColor myface.pushpull(15) return mygroup.to_component end # Get "handles" to our model and the Entities collection it contains. model = Sketchup.active_model entities = model.entities cylinder1 = createComp("red", 1) cylinder2 = createComp("white", 1) cylinder3 = createComp("blue", 1) for i in 1..10 do transformation = Geom;;Transformation.new([0,i,0]) entities.add_instance(cylinder1.definition, transformation) transformation2 = Geom;;Transformation.new([1,i,0]) entities.add_instance(cylinder2.definition, transformation2) transformation3 = Geom;;Transformation.new([2,i,0]) entities.add_instance(cylinder3.definition, transformation3) end view = model.active_view status = view.write_image "redwhiteblue.jpg"
-
What would be better here it to apply the materials to the component instances. That way you don't have to colour every face, and you don't have to make unique components for each colour variance.
-
cylinder1, cylinder2, and cylinder3 would be references to your 3 component object instances.
Sketchup::ComponentInstance is a subclass of Sketchup::DrawingElement (just as Sketchup::Face is also a subclass of Sketchup::DrawingElement.)
Sketchup::DrawingElement class has a material= method so Sketchup::ComponentInstance (as it's subclass,) will inherit a material= method.
You could try assigning the color to the ComponentInstance objects as a whole, instead of the nested Face(s).
Make sure ColorByLayer is OFF, so you see the material colors displayed and not the Layer colors displayed for Faces.
-
Make one cylinder component (referenced as definition
defn
).
It's faces'material=nil
by default.
Place the required instances in three goes...
` cyls1=[]
0.upto(10) do |i| ### 10 OR how many you want ?set incrementing transformation here
inst=entities.add_instance(defn, transformation)
cyls1<< inst
end#doRepeat for cyls2 and cyls3... Next: Make groups for each set...
g1=entities.add_group(cyls1)Repeat for g2/g3... Now give the groups some colors - the color will trickle down and color any face within the group/instances etc that has
material=nil
g2.material="white"
g3.material="blue"Move the groups around by 'transforming' them... Change the three groups' colors as desired... *Alternatively* you can color the instances within a group rather than the group itself - you have made a list of these as
cyls1etc which you can step through, do partial blocks, or even give individual colors as desired...
cyls1.each{|e|e.material="pink"}OR
cyls1[0..5].each..........or
cyls1[0].material="black"`
......................... -
refering to TIG's example:
Sketchup::Group is also subclass of Sketchup::DrawingElement (so Sketchup::Group instances will inherit a material= method from it's superclass Sketchup::DrawingElement.)
Advertisement