My scene contains a cone named 'MyCone'
I'm trying to construct a Python script: get_XYZ_for_tip( 'MyCone' )
How could I go about doing this?
Blender Stack Exchange is a question and answer site for people who use Blender to create 3D graphics, animations, or games. It only takes a minute to sign up.
Sign up to join this communityMy scene contains a cone named 'MyCone'
I'm trying to construct a Python script: get_XYZ_for_tip( 'MyCone' )
How could I go about doing this?
This script finds the vertex that is referenced by more edges than other vertices, this should meet the 'tip-criteria' to make a function getTip
simply return the vert.co
value.
import bpy
obj = bpy.context.scene.objects.active
mesh = obj.data
v_edge_tot = [0] * len(mesh.vertices)
maxedges = 0
maxvert = None
for edge in mesh.edges:
for vert in edge.vertices:
v_edge_tot[vert] += 1
count = v_edge_tot[vert]
if count > maxedges:
maxedges = count
maxvert = vert
vert = mesh.vertices[maxvert]
print(maxvert)
print('v %f %f %f' % (vert.co[:]))
Using BMesh this can be a one liner :), assuming your in editmode.
import bpy
import bmesh
tip_index = max((len(v.link_edges), i) for i, v in enumerate(bmesh.from_edit_mesh(bpy.context.object.data).verts))[1]
The same idea expanded (longhand):
import bpy
import bmesh
obj = bpy.context.object
bm = bmesh.from_edit_mesh(obj.data)
vert_user_index = []
for i, v in enumerate(bm.verts):
vert_user_index.append((len(v.link_edges), i))
tip_index = max(vert_user_index)[1]
Note that its building a list, where the one liner version uses a generator (which is more efficient)