For anyone reading this (including me) in the future this is how I ended up doing it. There are other ways that maybe better or more efficient.
For my case my SVG was being used in a template with a UDT so:
- Created my SVG in inkscape making sure to give all the object I will interact with names (and Id's)
- Created some custom properties on the root of the view template. The idea being these will be the "interface" to hold the script code (to run on property change); I can then just bind tags to these properties. If the SVG is deleted these props remain as they are on the root.
- Right clicked on the property and added the code to transverse the SVG structure and change the property I wanted. In one of my cases the code looked like this:
Debug = False
if Debug: system.perspective.print("Starting")
GroupName = "Alarm_Box"
ElementName = "Alarm_indicator"
ElementProperty = "fill"
ElementSubProperty = "paint"
#here is the logic for the new value of the property based off the tag value (currenValue)
if currentValue.value:
value = "#ff0000" #red
else:
value = "#00ff00" # green
#Link to the SVG drawing elements
SVG_Groups = self.getChild("ProtectionRelay").props.elements[1].elements
#Find the group we are intrested in
for SVG_Group in SVG_Groups:
if Debug: system.perspective.print("Searching Group:"+ SVG_Group.name)
if SVG_Group.name == GroupName:
#Now search for the Element we want to edit
SVG_Elements = SVG_Group.elements
for SVG_Element in SVG_Elements:
if Debug: system.perspective.print("Searching Elements:"+ SVG_Element.name)
if SVG_Element.name == ElementName:
if Debug: system.perspective.print("Found it")
#Now we look for the property and sub property and update its value
setattr(getattr(SVG_Element,ElementProperty),ElementSubProperty,value)
if Debug:
test = getattr(getattr(SVG_Element,ElementProperty),ElementSubProperty)
system.perspective.print("Read back: " + test)
system.perspective.print("Attempted: " + value)
# No need to search the rest of the elements
break
# No need to search the rest of the groups
break
- Now I can just bind my tags to the property I created on the root.
Now I can delete and re-import the SVG as much as I want provide I keep the group and element names the same.
Thanks to nader.chinichian for the inspiration for this idea from this post: