diff options
Diffstat (limited to 'io_curve_svg/import_svg.py')
-rw-r--r-- | io_curve_svg/import_svg.py | 75 |
1 files changed, 41 insertions, 34 deletions
diff --git a/io_curve_svg/import_svg.py b/io_curve_svg/import_svg.py index 6b45d91d..c7302711 100644 --- a/io_curve_svg/import_svg.py +++ b/io_curve_svg/import_svg.py @@ -122,14 +122,15 @@ def SVGParseFloat(s, i=0): return token, i -def SVGCreateCurve(): +def SVGCreateCurve(context): """ Create new curve object to hold splines in """ cu = bpy.data.curves.new("Curve", 'CURVE') obj = bpy.data.objects.new("Curve", cu) - bpy.context.scene.objects.link(obj) + + context['collection'].objects.link(obj) return obj @@ -217,8 +218,8 @@ def SVGMatrixFromNode(node, context): m = Matrix.Translation(Vector((x, y, 0.0))) if has_user_coordinate: if rect[0] != 0 and rect[1] != 0: - m = m * Matrix.Scale(w / rect[0], 4, Vector((1.0, 0.0, 0.0))) - m = m * Matrix.Scale(h / rect[1], 4, Vector((0.0, 1.0, 0.0))) + m = m @ Matrix.Scale(w / rect[0], 4, Vector((1.0, 0.0, 0.0))) + m = m @ Matrix.Scale(h / rect[1], 4, Vector((0.0, 1.0, 0.0))) if node.getAttribute('viewBox'): viewBox = node.getAttribute('viewBox').replace(',', ' ').split() @@ -241,11 +242,11 @@ def SVGMatrixFromNode(node, context): tx = (w - vw * scale) / 2 ty = (h - vh * scale) / 2 - m = m * Matrix.Translation(Vector((tx, ty, 0.0))) + m = m @ Matrix.Translation(Vector((tx, ty, 0.0))) - m = m * Matrix.Translation(Vector((-vx, -vy, 0.0))) - m = m * Matrix.Scale(scale, 4, Vector((1.0, 0.0, 0.0))) - m = m * Matrix.Scale(scale, 4, Vector((0.0, 1.0, 0.0))) + m = m @ Matrix.Translation(Vector((-vx, -vy, 0.0))) + m = m @ Matrix.Scale(scale, 4, Vector((1.0, 0.0, 0.0))) + m = m @ Matrix.Scale(scale, 4, Vector((0.0, 1.0, 0.0))) return m @@ -267,7 +268,7 @@ def SVGParseTransform(transform): if proc is None: raise Exception('Unknown trasnform function: ' + func) - m = m * proc(params) + m = m @ proc(params) return m @@ -308,7 +309,6 @@ def SVGGetMaterial(color, context): mat = bpy.data.materials.new(name='SVGMat') mat.diffuse_color = diffuse_color - mat.diffuse_intensity = 1.0 materials[color] = mat @@ -354,8 +354,8 @@ def SVGTransformScale(params): m = Matrix() - m = m * Matrix.Scale(sx, 4, Vector((1.0, 0.0, 0.0))) - m = m * Matrix.Scale(sy, 4, Vector((0.0, 1.0, 0.0))) + m = m @ Matrix.Scale(sx, 4, Vector((1.0, 0.0, 0.0))) + m = m @ Matrix.Scale(sy, 4, Vector((0.0, 1.0, 0.0))) return m @@ -399,7 +399,7 @@ def SVGTransformRotate(params): tm = Matrix.Translation(Vector((cx, cy, 0.0))) rm = Matrix.Rotation(ang, 4, Vector((0.0, 0.0, 1.0))) - return tm * rm * tm.inverted() + return tm @ rm @ tm.inverted() SVGTransforms = {'translate': SVGTransformTranslate, 'scale': SVGTransformScale, @@ -1038,7 +1038,7 @@ class SVGGeometry: """ self._context['transform'].append(matrix) - self._context['matrix'] = self._context['matrix'] * matrix + self._context['matrix'] = self._context['matrix'] @ matrix def _popMatrix(self): """ @@ -1046,7 +1046,7 @@ class SVGGeometry: """ matrix = self._context['transform'].pop() - self._context['matrix'] = self._context['matrix'] * matrix.inverted() + self._context['matrix'] = self._context['matrix'] @ matrix.inverted() def _pushStyle(self, style): """ @@ -1071,7 +1071,7 @@ class SVGGeometry: v = Vector((point[0], point[1], 0.0)) - return self._context['matrix'] * v + return self._context['matrix'] @ v def getNodeMatrix(self): """ @@ -1219,7 +1219,7 @@ class SVGGeometryPATH(SVGGeometry): Create real geometries """ - ob = SVGCreateCurve() + ob = SVGCreateCurve(self._context) cu = ob.data if self._node.getAttribute('id'): @@ -1255,7 +1255,7 @@ class SVGGeometryPATH(SVGGeometry): act_spline = cu.splines[-1] act_spline.use_cyclic_u = spline['closed'] else: - act_spline.bezier_points.add() + act_spline.bezier_points.add(1) bezt = act_spline.bezier_points[-1] bezt.co = co @@ -1399,7 +1399,7 @@ class SVGGeometryRECT(SVGGeometry): co = self._transformCoord(coord) if not firstTime: - spline.bezier_points.add() + spline.bezier_points.add(1) bezt = spline.bezier_points[-1] bezt.co = co @@ -1450,7 +1450,7 @@ class SVGGeometryRECT(SVGGeometry): radius = (rx, ry) # Geometry creation - ob = SVGCreateCurve() + ob = SVGCreateCurve(self._context) cu = ob.data if self._styles['useFill']: @@ -1560,7 +1560,7 @@ class SVGGeometryELLIPSE(SVGGeometry): return # Create circle - ob = SVGCreateCurve() + ob = SVGCreateCurve(self._context) cu = ob.data if self._node.getAttribute('id'): @@ -1599,7 +1599,7 @@ class SVGGeometryELLIPSE(SVGGeometry): spline = cu.splines[-1] spline.use_cyclic_u = True else: - spline.bezier_points.add() + spline.bezier_points.add(1) bezt = spline.bezier_points[-1] bezt.co = co @@ -1677,7 +1677,7 @@ class SVGGeometryLINE(SVGGeometry): y2 = SVGParseCoord(self._y2, crect[1]) # Create cline - ob = SVGCreateCurve() + ob = SVGCreateCurve(self._context) cu = ob.data coords = [(x1, y1), (x2, y2)] @@ -1691,7 +1691,7 @@ class SVGGeometryLINE(SVGGeometry): spline = cu.splines[-1] spline.use_cyclic_u = True else: - spline.bezier_points.add() + spline.bezier_points.add(1) bezt = spline.bezier_points[-1] bezt.co = co @@ -1748,7 +1748,7 @@ class SVGGeometryPOLY(SVGGeometry): Create real geometries """ - ob = SVGCreateCurve() + ob = SVGCreateCurve(self._context) cu = ob.data if self._closed and self._styles['useFill']: @@ -1767,7 +1767,7 @@ class SVGGeometryPOLY(SVGGeometry): spline = cu.splines[-1] spline.use_cyclic_u = self._closed else: - spline.bezier_points.add() + spline.bezier_points.add(1) bezt = spline.bezier_points[-1] bezt.co = co @@ -1819,7 +1819,7 @@ class SVGGeometrySVG(SVGGeometryContainer): if self._node.getAttribute('inkscape:version'): raw_height = self._node.getAttribute('height') document_height = SVGParseCoord(raw_height, 1.0) - matrix = matrix * Matrix.Translation([0.0, -document_height , 0.0]) + matrix = matrix @ matrix.Translation([0.0, -document_height , 0.0]) self._pushMatrix(matrix) self._pushRect(rect) @@ -1845,16 +1845,22 @@ class SVGLoader(SVGGeometryContainer): return None - def __init__(self, filepath, do_colormanage): + def __init__(self, context, filepath, do_colormanage): """ Initialize SVG loader """ + import os + + svg_name = os.path.basename(filepath) + scene = context.scene + collection = bpy.data.collections.new(name=svg_name) + scene.collection.children.link(collection) node = xml.dom.minidom.parse(filepath) m = Matrix() - m = m * Matrix.Scale(1.0 / 90.0 * 0.3048 / 12.0, 4, Vector((1.0, 0.0, 0.0))) - m = m * Matrix.Scale(-1.0 / 90.0 * 0.3048 / 12.0, 4, Vector((0.0, 1.0, 0.0))) + m = m @ Matrix.Scale(1.0 / 90.0 * 0.3048 / 12.0, 4, Vector((1.0, 0.0, 0.0))) + m = m @ Matrix.Scale(-1.0 / 90.0 * 0.3048 / 12.0, 4, Vector((0.0, 1.0, 0.0))) rect = (0, 0) @@ -1866,7 +1872,8 @@ class SVGLoader(SVGGeometryContainer): 'materials': {}, 'styles': [None], 'style': None, - 'do_colormanage': do_colormanage} + 'do_colormanage': do_colormanage, + 'collection': collection} super().__init__(node, self._context) @@ -1903,7 +1910,7 @@ def parseAbstractNode(node, context): return None -def load_svg(filepath, do_colormanage): +def load_svg(context, filepath, do_colormanage): """ Load specified SVG file """ @@ -1911,7 +1918,7 @@ def load_svg(filepath, do_colormanage): if bpy.ops.object.mode_set.poll(): bpy.ops.object.mode_set(mode='OBJECT') - loader = SVGLoader(filepath, do_colormanage) + loader = SVGLoader(context, filepath, do_colormanage) loader.parse() loader.createGeom(False) @@ -1922,7 +1929,7 @@ def load(operator, context, filepath=""): # non SVG files can give useful messages. do_colormanage = context.scene.display_settings.display_device != 'NONE' try: - load_svg(filepath, do_colormanage) + load_svg(context, filepath, do_colormanage) except (xml.parsers.expat.ExpatError, UnicodeEncodeError) as e: import traceback traceback.print_exc() |