0
$\begingroup$

The code for importing an svg from documentation is bpy.ops.import_curve.svg(filepath='', filter_glob='*.svg')

The code I tried

I

import bpy 
bpy.ops.import_curve.svg("D:\Coding_Projects\manim\media\Tex\abc.svg")

II

import bpy 
bpy.ops.import_curve.svg("D:\Coding_Projects\manim\media\Tex\abc.svg",filter_glob=".svg")

III

import bpy 
a=bpy.ops.import_curve.svg("D:\Coding_Projects\manim\media\Tex\abc.svg",filter_glob=".svg")

The error I got for all of them

Traceback (most recent call last):
  File "\rough.py", line 3, in <module>
  File "C:\Program Files\Blender Foundation\Blender 2.83\2.83\scripts\modules\bpy\ops.py", line 199, in __call__
    ret = op_call(self.idname_py(), C_dict, kw, C_exec, C_undo)
TypeError: Calling operator "bpy.ops.import_curve.svg" error, expected a string enum in ('INVOKE_DEFAULT', 'INVOKE_REGION_WIN', 'INVOKE_REGION_CHANNELS', 'INVOKE_REGION_PREVIEW', 'INVOKE_AREA', 'INVOKE_SCREEN', 'EXEC_DEFAULT', 'EXEC_REGION_WIN', 'EXEC_REGION_CHANNELS', 'EXEC_REGION_PREVIEW', 'EXEC_AREA', 'EXEC_SCREEN')
Error: Python script failed, check the message in the system console

Checking if my path is correct pasting the path opens the expected file

I checked this answer and they have no problems in importing an svg


Edit:

On using

a=bpy.ops.import_curve.svg(filepath="D:\Coding_Projects\manim\media\Tex\abc.svg",filter_glob=".svg")

The new error


Traceback (most recent call last):
  File "C:\Program Files\Blender Foundation\Blender 2.83\2.83\scripts\addons\io_curve_svg\__init__.py", line 59, in execute
    return import_svg.load(self, context, filepath=self.filepath)
  File "C:\Program Files\Blender Foundation\Blender 2.83\2.83\scripts\addons\io_curve_svg\import_svg.py", line 1882, in load
    load_svg(context, filepath, do_colormanage)
  File "C:\Program Files\Blender Foundation\Blender 2.83\2.83\scripts\addons\io_curve_svg\import_svg.py", line 1871, in load_svg
    loader = SVGLoader(context, filepath, do_colormanage)
  File "C:\Program Files\Blender Foundation\Blender 2.83\2.83\scripts\addons\io_curve_svg\import_svg.py", line 1809, in __init__
    node = xml.dom.minidom.parse(filepath)
  File "C:\Program Files\Blender Foundation\Blender 2.83\2.83\python\lib\xml\dom\minidom.py", line 1958, in parse
    return expatbuilder.parse(file)
  File "C:\Program Files\Blender Foundation\Blender 2.83\2.83\python\lib\xml\dom\expatbuilder.py", line 910, in parse
    with open(file, 'rb') as fp:
OSError: [Errno 22] Invalid argument: 'D:\\Coding_Projects\\manim\\media\\Tex\x07bc.svg'

location: C:\Program Files\Blender Foundation\Blender 2.83\2.83\scripts\modules\bpy\ops.py:201

location: C:\Program Files\Blender Foundation\Blender 2.83\2.83\scripts\modules\bpy\ops.py:201
Error: Traceback (most recent call last):
  File "C:\Program Files\Blender Foundation\Blender 2.83\2.83\scripts\addons\io_curve_svg\__init__.py", line 59, in execute
    return import_svg.load(self, context, filepath=self.filepath)
  File "C:\Program Files\Blender Foundation\Blender 2.83\2.83\scripts\addons\io_curve_svg\import_svg.py", line 1882, in load
    load_svg(context, filepath, do_colormanage)
  File "C:\Program Files\Blender Foundation\Blender 2.83\2.83\scripts\addons\io_curve_svg\import_svg.py", line 1871, in load_svg
    loader = SVGLoader(context, filepath, do_colormanage)
  File "C:\Program Files\Blender Foundation\Blender 2.83\2.83\scripts\addons\io_curve_svg\import_svg.py", line 1809, in __init__
    node = xml.dom.minidom.parse(filepath)
  File "C:\Program Files\Blender Foundation\Blender 2.83\2.83\python\lib\xml\dom\minidom.py", line 1958, in parse
    return expatbuilder.parse(file)
  File "C:\Program Files\Blender Foundation\Blender 2.83\2.83\python\lib\xml\dom\expatTraceback (most recent call last):
  File "\rough.py", line 3, in <module>
  File "C:\Program Files\Blender Foundation\Blender 2.83\2.83\scripts\modules\bpy\ops.py", line 201, in __call__
    ret = op_call(self.idname_py(), None, kw)
RuntimeError: Error: Traceback (most recent call last):
  File "C:\Program Files\Blender Foundation\Blender 2.83\2.83\scripts\addons\io_curve_svg\__init__.py", line 59, in execute
    return import_svg.load(self, context, filepath=self.filepath)
  File "C:\Program Files\Blender Foundation\Blender 2.83\2.83\scripts\addons\io_curve_svg\import_svg.py", line 1882, in load
    load_svg(context, filepath, do_colormanage)
  File "C:\Program Files\Blender Foundation\Blender 2.83\2.83\scripts\addons\io_curve_svg\import_svg.py", line 1871, in load_svg
    loader = SVGLoader(context, filepath, do_colormanage)
  File "C:\Program Files\Blender Foundation\Blender 2.83\2.83\scripts\addons\io_curve_svg\import_svg.py", line 1809, in __init__
    node = xml.dom.minidom.parse(filepath)
  File "C:\Program Files\Blender Foundation\Blender 2.83\2.83\python\lib\xml\dom\minidom.py", line 1958, in parse
    return expatbuilder.parse(file)
  File "C:\Program Files\Blender Foundation\Blender 2.83\2.83\python\lib\xml\dom\expatbuilder.py", line 910, in parse
    with open(file, 'rb') as fp:
OSError: [Errno 22] Invalid argument: 'D:\\Coding_Projects\\manim\\media\\Tex\x07bc.svg'

location: C:\Program Files\Blender Foundation\Blender 2.83\2.83\scripts\modules\bpy\ops.py:201
```
$\endgroup$
4
  • $\begingroup$ May be this video will help in importing SVG file into blender. m.youtube.com/watch?v=7nmYKSDKDwY $\endgroup$
    – Shardul
    Commented Oct 3, 2020 at 3:19
  • $\begingroup$ bpy.ops.import_curve.svg(filepath="D:\whatever") $\endgroup$
    – scurest
    Commented Oct 3, 2020 at 3:33
  • $\begingroup$ @Y-King I do not want to import svg via gui but python because later I want to mass import. @scurest I put filepath=... and it now gives a new error Invalid argument: 'D:\\Coding_Projects\\manim\\media\\Tex\x07bc.svg' I added the full error in the question's last part $\endgroup$ Commented Oct 3, 2020 at 3:47
  • $\begingroup$ Suggest use the python console to test whether the file exists for a valid path: os.path.isfile(my_file_path) -> docs.python.org/3/library/os.path.html#os.path.isfile $\endgroup$
    – brockmann
    Commented Oct 3, 2020 at 7:56

2 Answers 2

1
$\begingroup$
bpy.ops.import_curve.svg(filepath=r"D:\Coding_Projects\manim\media\Tex\abc.svg")

Notice that the r modifier before the string, indicates that the path will evaluate to a raw string without escaping the backslashes (\).

Python docs: https://docs.python.org/3//library/re.html#raw-string-notation

Alternatively you can change all \ to /.

$\endgroup$
1
$\begingroup$

Using a python script you can specify the svg file to import. After the import you can identify the curves added and adjust their names and properties to match the filename used.

As importing an svg file can create multiple curve objects I would start by making a copy of the existing objects list, then import, then compare the current objects list to the starting list to find what was added during the import.

import bpy
import os

svg_files = [f for f in os.listdir('svgtest') if f.endswith('.svg')]

for f in svg_files:
    start_objs = bpy.data.objects[:]
    bpy.ops.import_curve.svg(filepath=os.path.join('svgtest',f))
    new_curves = [o for o in bpy.data.objects if o not in start_objs]

    n = f[:-8] # the start of the filename
    s = float(f[-6:-4]) # the scale factor as in the 99 from name_S99.svg
    for obj in new_curves:
        obj.name = n
        obj.scale = (s,s,s)

Is this helpful>>>

$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .