User Tools

Site Tools


inkex

Inkex (inkscape python module)

get symbols attributes

inx file

<?xml version="1.0" encoding="UTF-8"?>
<inkscape-extension xmlns="http://www.inkscape.org/namespace/inkscape/extension">
  <name>Symbol attributes extractor</name>
  <id>org.inkscape.ssm2017.symbol_attributes_extractor</id>
  <effect>
    <effects-menu>
      <submenu name="Symbols"/>
    </effects-menu>
  </effect>
  <script>
    <command location="inx" interpreter="python">symbol_attributes_extractor.py</command>
  </script>
</inkscape-extension>

python file

#!/usr/bin/env python
# coding=utf-8
#
# Copyright (C) 2023 ssm2017, ssm2017@gmail.com
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
#
"""
This extension is getting a symbol custom attributes data-* and setting them to the object
"""

import inkex

class SymbolAttributesExtracterExtension(inkex.EffectExtension):

    def effect(self):

        for node in self.svg.selection:
            # check if the node is a <use> element
            if node.tag_name == "use":
                # get the symbol id
                symbol_id = node.get("xlink:href")
                # look for the symbol
                symbol = self.svg.getElementById(symbol_id)
                # get attributes and assign them to the <use>
                for attrib_name, attrib_value in symbol.attrib.items():
                    if attrib_name.startswith("data-"):
                        if not node.get(attrib_name):
                            node.set(attrib_name, attrib_value)

if __name__ == '__main__':
    SymbolAttributesExtracterExtension().run()

inkex.txt · Last modified: by ssm2017