line length ?

I can calculate continuous line length with edges length!

but how do you test if it is a continuous line ?

and if line has several segments how do you calculate each segment length ?

thanks
happy bl

Attachments


It’s Curv or Mesh?

at that point it has been converted to mesh with verts and edges

thanks
happy bl

I do not know much English and it is incorrect that this is what you need


import bpyimport bmesh
me = bpy.context.active_object.data
bm = bmesh.new()
bm.from_mesh(me)


global save_verts
global save_edge
global len_edge
global count_segm
global rec_cou


save_verts = []
save_edge = []
len_edge = []
count_segment = 0
rec_cou = 0


def rec(bm, index):
    global save_verts
    global save_edge
    global len_edge
    global count_segm
    global rec_cou


    bm.verts.ensure_lookup_table()
    v = bm.verts[index]
    save_verts.append(v.index)
    
    edge = v.link_edges
    if len(edge) == 2:
        for e in edge:
            if e.index not in save_edge:
                if e.verts[0].index == v.index or e.verts[1].index == v.index :
                    save_edge.append(e.index)
                    len_edge.append(e.calc_length())
                    if e.verts[0] == v:
                        rec(bm, e.verts[1].index)
                    else:
                        rec(bm, e.verts[0].index)
        
    else:
        if edge[0].verts[0].index in save_verts:
            save_verts.append(edge[0].verts[1].index)
        else:
            save_verts.append(edge[0].verts[0].index)
    return 1


for i in bm.verts:
    if i.index not in save_verts:
        count_segment += rec(bm, i.index)
    else:
        continue
    
print(count_segment)



can you upload the script I will look at it
and see how to use it

I do prefer using Bmesh
I think old API might not be there anyway in 2.8 next year !

thanks
happy bl


import bpyimport bmesh
me = bpy.context.active_object.data
bm = bmesh.new()
bm.from_mesh(me)


global save_verts
global save_edge
global len_edge
global count_segm
global rec_cou


len_segment = []


save_verts = []
save_edge = []
len_edge = []
count_segment = 0
rec_cou = 0


def rec(bm, index):
	global save_verts
	global save_edge
	global len_edge
	global count_segm
	global rec_cou


	bm.verts.ensure_lookup_table()
	v = bm.verts[index]
	save_verts.append(v.index)
	
	edge = v.link_edges
	if len(edge) == 2:
		for e in edge:
			if e.index not in save_edge:
				if e.verts[0].index == v.index or e.verts[1].index == v.index :
					save_edge.append(e.index)
					len_edge.append(e.calc_length())
					if e.verts[0] == v:
						rec(bm, e.verts[1].index)
					else:
						rec(bm, e.verts[0].index)
		
	else:
		if edge[0].verts[0].index in save_verts:
			save_verts.append(edge[0].verts[1].index)
		else:
			save_verts.append(edge[0].verts[0].index)
	return 1


for i in bm.verts:
	if i.index not in save_verts:
		count_segment += rec(bm, i.index)
	else:
		cunt_len = 0
		for j in len_edge:
			cunt_len += j
		len_segment.append(cunt_len)
		len_edge= []
		continue
	
count = 1
print('count segment = ', count_segment)
for i in len_segment:
	print ('Len Sigment #', count, ' = ', i)
	count += 1

can you explain what this return does ?

else:
save_verts.append(edge[0].verts[0].index)

return 1

and what does the count segments count ?

did a test on a 3 segments line and I get 5 segments!

thanks
happy bl

I can explain in Russian :slight_smile:

if the vertex has only 1 edge, then this means that the segment has ended. But we need to keep this peak. In the list there are only 1 edge so edge [0], then I see whether or not there is 0 vertex in the list

return 1 is to add a unit to the number of segments

just did a test with an horizontal line with 3 Segment and it does not work !
try file here

thanks
happy bl

Attachments

simple-file1.blend (744 KB)

it is work!
video - https://drive.google.com/file/d/0B44RnfYzKt3cTjJPdFR5dFRvRFU/view?usp=drivesdk
scene - https://drive.google.com/file/d/0B44RnfYzKt3ceUMyS1RjSjhUWGM/view?usp=drivesdk

The number of segments is correct. The number of lengths of segments is displayed


import bpyimport bmesh
me = bpy.context.active_object.data
bm = bmesh.new()
bm.from_mesh(me)


global save_verts
global save_edge
global len_edge
global count_segm
global rec_cou


len_segment = []


save_verts = []
save_edge = []
len_edge = []
count_segment = 0
rec_cou = 0


def rec(bm, index):
	global save_verts
	global save_edge
	global len_edge
	global count_segm
	global rec_cou


	bm.verts.ensure_lookup_table()
	v = bm.verts[index]
	save_verts.append(v.index)
	
	edge = v.link_edges
	if len(edge) == 2:
		for e in edge:
			if e.index not in save_edge:
				if e.verts[0].index == v.index or e.verts[1].index == v.index :
					save_edge.append(e.index)
					len_edge.append(e.calc_length())
					if e.verts[0] == v:
						rec(bm, e.verts[1].index)
					else:
						rec(bm, e.verts[0].index)
		
	else:
		if edge[0].verts[0].index in save_verts:
			save_verts.append(edge[0].verts[1].index)
		else:
			save_verts.append(edge[0].verts[0].index)
	return 1


for i in bm.verts:
	if i.index not in save_verts:
		count_segment += rec(bm, i.index)
	else:
		cunt_len = 0
		for j in len_edge:
			cunt_len += j
		len_segment.append(cunt_len)
		len_edge= []
		continue
	
print('number of segments = ', count_segment)
count = 1
len = len_segment
len_segment = []
for i in len:
	if i != 0:
		len_segment.append(i)
		


for i in len_segment:
	print ('Len Sigment #', count, ' = ', i)
	count += 1

not yet try with horiz line see pic


thanks
happy bl

what does the edge decription gives ?
is it like number of verts on one segment ?

what does the Verts/0 or verts/5 gives ?

trying to better understand it !

thanks
happy bl

yes there was a problem if the segment consists of one edge
Fix it


import bpyimport bmesh
me = bpy.context.active_object.data
bm = bmesh.new()
bm.from_mesh(me)


global save_verts
global save_edge
global len_edge
global count_segm
global rec_cou


len_segment = []


save_verts = []
save_edge = []
len_edge = []
count_segment = 0
rec_cou = 0


def rec(bm, index):
    global save_verts
    global save_edge
    global len_edge
    global count_segm
    global rec_cou
    rec_cou +=1
    bm.verts.ensure_lookup_table()
    v = bm.verts[index]
    save_verts.append(v.index)
    
    edge = v.link_edges
    if len(edge) == 2:
        for e in edge:
            if e.index not in save_edge:
                if e.verts[0].index == v.index or e.verts[1].index == v.index :
                    save_edge.append(e.index)
                    len_edge.append(e.calc_length())
                    if e.verts[0] == v:
                        rec(bm, e.verts[1].index)
                    else:
                        rec(bm, e.verts[0].index)
                    
    else:
#        if rec_cou == 1:
#            if edge[0].index not in save_edge:
#                save_edge.append(edge[0].index )
#                len_edge.append(edge[0].calc_length())


        if edge[0].verts[0].index in save_verts:
            save_verts.append(edge[0].verts[1])
        else:
            save_verts.append(edge[0].verts[0].index)
    rec_cou = 0
    return 1




for i in bm.verts:
    edge = i.link_edges
    if len(edge) == 1:
        for e in edge:
            for v in e.verts:
                if v != i:
                    e2 = v.link_edges
                    if len(e2) == 1 and e2[0] == edge[0]:
                        if e2[0].index not in save_edge:
                            len_edge.append(e2[0].calc_length())
                            save_edge.append(e2[0].index)
                            save_verts.append(i.index)
                            save_verts.append(v.index)
                            
                            cunt_len = 0
                            for j in len_edge:
                                cunt_len += j
                            len_segment.append(cunt_len)
                            len_edge= []
                            count_segment += 1
        continue
                
    if i.index not in save_verts:
        count_segment += rec(bm, i.index)
    else:
        cunt_len = 0
        for j in len_edge:
            cunt_len += j
        len_segment.append(cunt_len)
        len_edge= []
        continue
    
print('number of segments = ', count_segment)
count = 1
len = len_segment
len_segment = []


for i in len:
    if i != 0:
        len_segment.append(i)
        


for i in len_segment:
    print ('Len Sigment #', count, ' = ', i)
    count += 1

seems to be working
thanks for feedback

do you have a problem to run the script several times ?

if I select first line it runs fine
then I select other line and then it refuses to run it
have to reload the file and run it again!

is this a bug or what ?

thanks
happy bl

No I’m fine

may be add in end bm.free

bm.free() # free and prevent further access
del bm

but it does not do that all the time which is very strange indeed !
not certain why !

got to modify my more advance script
have to find a way to remove curve depth on a curve now !

I can start with a curve or curve with some depth on it or directly a mesh line
but need to remove that depth before converting to mesh or I get like a tube instead of a line!

it is an interesting challenge to help calculate length of lines !

thanks
happy bl

I did not understand what you need. And I’ve never written a code for curves, so I can not help

i think I have some examples for curves around
I will try to find these and it should work

still trying to figure out the algo for Bmesh !

can you try to explain how the recursive algo works ?

thanks
happy bl

the recursive function takes the vertex, gets the edges with which it is connected, gets the vertices of the edges, takes the next vertex at the edge. Rekrsiya needed because we do not know the number of passes

I will try to continue this weekend on this
got other things to do this week

interesting way to work with Bmesh here !

thanks for feedback
happy cl