Rewrite "separate_letters_2" for Blender 2.8


(NewVisitor) #1

This Script works in 2.79, it split a Text in separate Letters.
It doesn’t convert to Spline or Mesh.
The separate Letters are Text.

import bpy, mathutils
from mathutils import Vector

def get_spline_coords(cur):
    bpy.ops.object.select_all(action='DESELECT')
    sce.objects.active = cur
    cur.select = True
    bpy.ops.object.convert(target='CURVE', keep_original=True)
    tmp = sce.objects.active
    tmp_data = tmp.data
    coords = [spl.bezier_points[0].co[0:3] for spl in tmp_data.splines]
    sce.objects.unlink(tmp)
    bpy.data.objects.remove(tmp)
    bpy.data.curves.remove(tmp_data)
    sce.objects.active = cur
    cur.select = False
    return(coords)

sce = bpy.context.scene
txt = bpy.context.object
splindex = 0

if txt.type == 'FONT':
    pos = txt.location
    col = txt.dimensions[0]
    reference = get_spline_coords(txt)

    for i in range(len(txt.data.body)):
        chr = txt.data.body[i:i+1]
        if chr.isspace(): continue
        data = txt.data.copy()
        data.body = chr
        name = txt.name+'_'+'%03d'%i+'_'+data.body
        letra = bpy.data.objects.new(name, data)
        sce.objects.link(letra)
        orco = reference[splindex]
        nwco = get_spline_coords(letra)
        letra.location = Vector(orco) - Vector(nwco[0])
        splindex += len(nwco)

    sce.objects.active = txt
    txt.select = True

Error in Blender 2.8:
File “\Text”, line 26, in
File “\Text”, line 6, in get_spline_coords
AttributeError: bpy_prop_collection: attribute “active” not found

Can someone please rewrite this script for Blender 2.8?


(carbon2) #2

here you go

import bpy, mathutils
from mathutils import Vector

def get_spline_coords(c, cur):
    bpy.ops.object.select_all(action='DESELECT')
    view_layer = c.view_layer
    cur.select_set(True)
    view_layer.objects.active = cur
    bpy.ops.object.convert(target='CURVE', keep_original=True)
    tmp = view_layer.objects.active
    tmp_data = tmp.data
    coords = [spl.bezier_points[0].co[0:3] for spl in tmp_data.splines]
    collection = view_layer.active_layer_collection.collection
    collection.objects.unlink(tmp)
    bpy.data.objects.remove(tmp)
    bpy.data.curves.remove(tmp_data)
    cur.select_set(False)
    view_layer.objects.active = cur
    return(coords)

c = bpy.context
sce = c.scene
txt = c.object
view_layer = c.view_layer
collection = view_layer.active_layer_collection.collection
splindex = 0

if txt.type == 'FONT':
    pos = txt.location
    col = txt.dimensions[0]
    reference = get_spline_coords(c, txt)
    
    for i in range(len(txt.data.body)):
        chr = txt.data.body[i:i+1]
        if chr.isspace(): continue
        data = txt.data.copy()
        data.body = chr
        name = txt.name+'_'+'%03d'%i+'_'+data.body
        letra = bpy.data.objects.new(name, data)
        collection.objects.link(letra)
        orco = reference[splindex]
        nwco = get_spline_coords(c, letra)
        letra.location = Vector(orco) - Vector(nwco[0])
        splindex += len(nwco)
    
    txt.select_set(True)
    view_layer.objects.active = txt

(NewVisitor) #3

This is so great carbon2!
Thank you very very much.