Control a shapes Fill Paint property with a script

I’d like to set a shape objects Fill Paint property using a script but I cannot figure out how to do it. I have an integer tag from my PLC that is made up of status bits. If a certain bit in that tag is ON, I want to change the Fill Paint of a rectangle. Seems like a script would be the best way to do this but maybe there’s another way.

You can do this by placing a custom property on the rectangle that is bound to the tag. Then, use the style customizer to set what color the rectangle will be based on the integer value. You shouldn’t need to do any scripting for this.

I recommend using binding instead of scripting. You can bind the fill paint to an expression that checks for a bit:if(getBit({Path/To/Tag}, 1) = 1, toColor("red"), toColor("blue"))

But if I have many shapes that need to have their fill paint changed then I have to re-create that expression for every one of them. Same thing with the style customizer.

If I had a script then I could just call that script based on a property change. Also makes life a lot easier if I have to change my color scheme later. I think its really a Python question. I haven’t been able to find any useful info on the “Paint” data type anywhere and simply trying to assign a color to the Fill Paint property causes an error. See my code.

if event.propertyName == "MotorStatus": if event.newValue == 1: event.source.fillPaint = (0, 255, 0)

I have done something similar to be able to store a projects color scheme in a database table.

try this out:

app script -

def getColor(ColorNum=0, clrDb = 'TT_Base'): #Retieves a color from the color scheme table import system qryClr = system.db.runPrepQuery('select r,b,g,a from clrscheme where clrscheme_ndx = ?',[ColorNum],clrDb) myColor = system.gui.color(qryClr[0][0],qryClr[0][1],qryClr[0][2],qryClr[0][3]) return myColor

then for the propertyChange script your code would like something like-

if event.propertyName == "MotorStatus":
	bgColor = app.util.getColor(event.newValue)
	event.source.fillPaint = bgColor

The only other thing to do is create a table with your hex rgba colors mapped to your motor status states.

1 Like

Is there any way to edit a shape path by scripting?.
I want to create a template with custom properties and a shape I can edit to overlay machine statuses.

Try event.source.setStrokeStyle, event.source.setStrokePaint, event.source.setFillPaint. This might help: Animating a gradient