0
$\begingroup$

I am able to bulk import hundreds of SVGs using a script with the command:

bpy.ops.import_curve.svg(filepath = path_to_file)

However, this results in multiple unneeded collections and unnamed curves like this:

3D_Printer.svg Collection
    Curve.1
Biometrics.svg Collection
    Curve.2
Drones.svg Collection
    Curve.3

Manually deleting all the collections would just leave the unnamed curves:

Curve.1
Curve.2
Curve.3

Is there some way, possibly through a modified SVG script or rename/collection script to get a result like:

3D_Printer.svg
Biometrics.svg
Drones.svg

My SVGs are optimized so there is only one curve per file, so the extra collections and generically named curves are quite inconvenient. I'm not sure which is easier, modifying the io_curve_svg scripts, or creating a script to rename the curve and collapse the collection after the fact.

$\endgroup$
1
  • 1
    $\begingroup$ Given that the import curves svg is an addon within Blender I would suggest you do not personally modify the addon but instead contact its author with a request for additional functionality to improve the usage for everyone as it is shipped with Blender. in the mean time I will offer a script example to import files from a given directory and rename the curves, remove unused collections below. $\endgroup$
    – Ratt
    Mar 13, 2021 at 2:22

1 Answer 1

1
$\begingroup$

The following script is intended as a limited example tested in Blender 2.9: create a list of current collections; given a specified directory: get a list of all files in that specific directory; if the file name ends with .svg perform the import curve operation; create a list of collections after import operation (exclude original collections); for each collection created during the import operation: rename the objects in the collection; link to scene's master collection; unlink from existing collection; remove the collection;

import bpy
import os

dir = "c:\\tmp\\svg"
files = os.listdir(path=dir)
tgt_coll = bpy.context.collection # master scene collection
colls = [coll for coll in bpy.data.collections]

for f in files:
    if not f.endswith(".svg"):
        continue
    bpy.ops.import_curve.svg(filepath = os.path.join(dir, f))

new_colls = [coll for coll in bpy.data.collections if coll not in colls]

for coll in new_colls:
    for obj in coll.objects:
        obj.name = coll.name # coll.name[:-4] to strip .svg
        tgt_coll.objects.link(obj)
        coll.objects.unlink(obj)
    if len(coll.objects) == 0:
        bpy.data.collections.remove(coll)
$\endgroup$
1
  • $\begingroup$ Thank you! It worked perfectly. I will try to contact the authors of the SVG import add-on to see if they can add a toggle for not placing SVG files with single curves in collections. $\endgroup$
    – Heardr
    Mar 13, 2021 at 3:01

You must log in to answer this question.

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