About color
-
Below is my code, the result is only one face of the whole cube is blue, but what I want to see is the cube is painted all by "blue"
Create the new material
mats = Sketchup.active_model.materials
ct_mat = mats.add "Color_Texture"
ct_mat.color = "DodgerBlue"Draw a Face and set its material
ents = Sketchup.active_model.entities
face = ents.add_face [1, -1, 1], [1, 1, 1], [-1, 1, 1], [-1, -1, 1]
ents.material = ct_matIs there any method?
I really appretiate.Thanks
-
@zps222 said:
# Draw a Face and set its material ents = Sketchup.active_model.entities face = ents.add_face [1, -1, 1], [1, 1, 1], [-1, 1, 1], [-1, -1, 1] ents.material = ct_mat
You are applying material to the entities collection - no such method. That should produce an error.
face.material = ct_ma
should workHard to tell why only one face is blue for you, since your sample code just creates a single face.
-
@thomthom said:
@zps222 said:
# Draw a Face and set its material ents = Sketchup.active_model.entities face = ents.add_face [1, -1, 1], [1, 1, 1], [-1, 1, 1], [-1, -1, 1] ents.material = ct_mat
You are applying material to the entities collection - no such method. That should produce an error.
face.material = ct_ma
should workHard to tell why only one face is blue for you, since your sample code just creates a single face.
Thank you. I use the pushpull method to draw a cube, so is there any method to paint the other sides?
-
Are you drawing into an empty context? Or into a context with existing geometry?
-
If you draw into a context where it's only your group, you can do this:
entities.each { |e| next unless e.is_a?(Sketchup;;Face) e.material = 'blue' }
If you are drawing into a context with existing geometry:
cache = entities.to_a face = entities.add_face( [1, -1, 1], [1, 1, 1], [-1, 1, 1], [-1, -1, 1] ) face.pushpull(1) (entities.to_a - cache).each { |e| next unless e.is_a?(Sketchup;;Face) e.material = 'blue' }
-
Nice thomthom, but wouldn't it be simpler to apply color to the face and then pushpull and it will pick up the material of the originating face.
-
@adamb said:
Nice thomthom, but wouldn't it be simpler to apply color to the face and then pushpull and it will pick up the material of the originating face.
I did think of that - but when modelling I some times find PushPull to apply the material to the back-side of the extruded faces. Though not always - haven't worked out when it happens. (material is on the front face)
-
Good pragmatic solution.
-
Whoa there! Hammer to crack a nut there.
In a right handed system (like SU), the front of a Face is defined in an anti-clockwise direction. Imagine gripping the face normal with your right hand and your thumb in the direction of the normal - your fingers show the winding of the face.
Face#pushpull() takes a distance to move along the face normal. So if you define a Face and paint its front material with something, then apply a negative pushpull, you'll always get a cuboid with that material on the outside.
Adam
-
@thomthom said:
@adamb said:
Nice thomthom, but wouldn't it be simpler to apply color to the face and then pushpull and it will pick up the material of the originating face.
I did think of that - but when modelling I some times find PushPull to apply the material to the back-side of the extruded faces. Though not always - haven't worked out when it happens. (material is on the front face)
But if you applied to both the front and back before push-pulling, then all will have the color afterward.
-
The loop 'direction' does indeed force the
face.normal
except in one important case... If you draw a 'flat' face a z=0 it will always face downwards no matter which 'direction' you might have drawn the loop :!It's a quirk of Sketchup - it thinks you are going to PushPull the face 'up' in that case so it orients it for you to suit. It happens when making flat faces at z=0 both manually AND via the API.
Whenever I do this sort of face making I always check for
face.nornal.z==-1 and face.vertices[0].position.z==0
and iftrue
and if I want to PushPull the face 'up' I addface.reverse!
before Iface.pushpull(distance)
. Remember too that you also use a negative distance to make the 'box' above an 'upwards looking' face...Incidentally Adam is right that adding the material before doing the PushPull is the simplest way to make all of the new faces in that material. It is best to leave face-backs in the default 'blue' back-material as it's easier to see what's happening without resorting to Monochrome mode to check faces are correctly oriented...
-
@adamb said:
Whoa there! Hammer to crack a nut there.
Well not a hammer (you overreact..) nut..
The problem does not occur with the built-in pullpull tool.
Does SU automatically reverse the face that is colored differently, using the built-in Tool?
EDIT: I guess TIG answered the nitty-gritty there. The API doesn't say that
Face.pushpull
's second argument defaults tofalse
. -
-
Thank you all guys
Advertisement