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?

  • 1
    $\begingroup$ This Q is a bit vague - do you know which vertex is the tip of the cone? - or would the script have to analyze the mesh and detect this? $\endgroup$ – ideasman42 Jan 10 '14 at 6:47

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('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)


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.