3
$\begingroup$

I'm importing an SVG file into my scene using bpy. But after importing, the curve is not selected nor active. Here is wat I am doing:

path = '/path/fo/file/'
file = 'contour.svg'

bpy.ops.import_curve.svg(filepath = path + file)

Can I just assume that the newly imported object is always the last one in the objects array? So that I can grab it like this:

active_object = bpy.context.scene.objects[-1]

Or is there a better way?

$\endgroup$
4
$\begingroup$

You can store the names of the objects before and after importing and find the difference to make sure you have the latest one:

import bpy
C = bpy.context

# Names of all objects before importing the SVG
names_pre_import = set([ o.name for o in C.scene.objects ])

# Import SVG as curve
fp = '/mylocalpath/svgcurve.svg'
bpy.ops.import_curve.svg(filepath = fp)

# Names of all objects after importing the SVG
names_post_import = set([ o.name for o in C.scene.objects ])

# Perform set difference to find the new name and store it
new_object_name = names_post_import.difference( names_pre_import ).pop()

# Reference new object
o = C.scene.objects[ new_object_name ]

# Set as selected and active in Blender 2.7x
o.select = True
C.scene.objects.active = o

# Set as selected and active in Blender 2.8
# o.select_set(True)
# C.view_layer.objects.active = o
$\endgroup$
  • 1
    $\begingroup$ Howdy 'Lousky. Related blender.stackexchange.com/a/48281/15543 This is prob a better way, or patching the importer to make it follow a more default behaviour and return the curves selected. btw Having issues with this addon in 2.8 setting the diffuse color, working ok at your end? $\endgroup$ – batFINGER Mar 22 at 4:44
  • $\begingroup$ Thanks @batFINGER. Are you having trouble with the import SVG as curve? I could change the color after importing. $\endgroup$ – TLousky Mar 22 at 15:46
  • $\begingroup$ My 2.8 build the importer is throwing an error when attempting to import blender.svg . Will check I've updated the addon repo correctly on next build. $\endgroup$ – batFINGER Mar 22 at 15:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.