Notifications
Clear all

[Closed] Changing color when select different component

I wonder If we could shade object in different color when selected different component

For example when I choose vertex. The object is shaded in blue and selected vertices is yellow

In face mode, the object is shade green and selected face is red

I guess there is some macro run in background and “listen” to what we do,

THanks for any help

3 Replies

Here’s a snippet.


-- remove any pre-existing callback here
callbacks.removeScripts id:#test

-- will hold the object we're messing with
global orgObj = undefined
global orgMat = undefined

-- some materials pre-defined so we don't create new instances all the time
global redMat = Standard diffuse:(color 128 0 0)
global greenMat = Standard diffuse:(color 0 128 0)
global blueMat = Standard diffuse:(color 0 0 128)
global greyMat = Standard diffuse:(color 128 128 128)

-- the function that does it all
-- it gets the current and previous sub-object level from a callback, see below.
fn colorBySOLevel SOLevels = (
	local newSOLevel = SOLevels[1]
	local oldSOLevel = SOLevels[2]

	-- get the currently selected object
	local sel = getCurrentSelection()

	-- if the old SOLevel was zero (no sub-object level)
	if (oldSOLevel == 0) then (
		-- then store a reference to this object, and to its original material
		orgObj = sel[1]
		orgMat = sel[1].material
	)

	-- now assign it a new material based on the subObjectLevel.
	sel[1].material = case newSOLevel of (
		0: orgMat -- restore the original material if we're going out of SO mode.
		1: greyMat -- vertices, typically
		2: greenMat -- edges, typically
		3: greenMat -- ditto, but in loop form
		4: blueMat -- faces, typically
		5: redMat -- elements, typically
		default: greyMat -- anything else (just in case)
	)
)

-- this callback tracks modifier panel SO level changes
-- and feeds the function the old and new SO levels.
callbacks.addScript #ModPanelSubObjectLevelChanged "colorBySOLevel (callbacks.notificationParam())" id:#test

-- while we use this one to make sure that if you exit SO level 'uncleanly'..
-- ( for example, by switching to the Create panel directly )
-- ..the original material will still get restored.
-- It does check if the object is still valid, in case the node was deleted or so.
callbacks.addScript #modPanelObjPostChange "if (isValidNode orgObj) then ( orgObj.material = orgMat )" id:#test

as for actual selections… I’m not too sure if/where one might change that. I know that the SO selection color is in Customize > Customize User Interface -> Elements: Geometry / -> SubSelection |^|… but script access to it, short of changing the .clr and then loading it in? hmm.

Thanks for your hint, I will look into this snippet