0
$\begingroup$

Any suggestions as to how to select (in python) one specific vertex of a septagon (seven vertex mesh circle)? When I do it manually, it doesn't show up as a command or function in the python info window for me to copy into my python code.

So far I have this code which doesn't throw an error, but doesn't achieve the desired effect, either ... after running, all vertices are selected instead of just vertex 0.

import bpy
import bmesh
bpy.ops.mesh.primitive_circle_add(vertices=7, radius=1, enter_editmode=True, location=(0, 0, 0))

bpy.data.meshes['Circle'].vertices[0].select=True
bpy.data.meshes['Circle'].vertices[1].select=False
bpy.data.meshes['Circle'].vertices[2].select=False
bpy.data.meshes['Circle'].vertices[3].select=False
bpy.data.meshes['Circle'].vertices[4].select=False
bpy.data.meshes['Circle'].vertices[5].select=False
bpy.data.meshes['Circle'].vertices[6].select=False

$\endgroup$
1
$\begingroup$

Have to select in object mode

To select mesh vertices using me.vertices[i].select need to set in object mode, and toggle into edit mode.

When a new primitive is added all the geometry is selected by default.

Here the mesh is added in object mode (without toggling into edit mode) all geometry is deselected (brute force) , vertex index 0 selected, and then toggle into edit mode.

import bpy
context = bpy.context

bpy.ops.mesh.primitive_circle_add(vertices=7, radius=1, enter_editmode=False, location=(0, 0, 0))
ob = context.object
me = ob.data
for g in me.vertices[:] + me.edges[:] + me.polygons[:]:
    g.select = False

me.vertices[0].select = True

bpy.ops.object.mode_set(mode='EDIT')

Please note. When an object is created with an operator the object and mesh are given default names, "Circle" in this case. If another is added then the names may or may not be "Circle" if there is another with same name.

The newly created object will always be ob = context.object and its data part, mesh in this case ob.data. (suppose I'm saying, pays to get out of the habit of bpy.data.objects["What the first one's name is"])

or use bmesh, and load in the edit mesh and fix. (as answered by Leander while writing this), however this uses bmesh in conjunction with the add primitive operator.

import bpy
context = bpy.context
import bmesh
bpy.ops.mesh.primitive_circle_add(vertices=7, radius=1, enter_editmode=True, location=(0, 0, 0))
ob = context.edit_object
me = ob.data
bm = bmesh.from_edit_mesh(me)
bm.select_flush(False)
for v in bm.verts:
    v.select = not v.index
bmesh.update_edit_mesh(me)
$\endgroup$
1
$\begingroup$

Please use the bmesh module to edit and manipulate meshes. This answer describes how read and write from regular meshes. Use the bmesh operator to create the circle.

import bpy
import bmesh

scene = bpy.context.scene

# create new mesh
me = bpy.data.meshes.new('mesh')
# create new object
ob = bpy.data.objects.new("object", me)

# link object to scene and set it as active
scene.collection.objects.link(ob)
bpy.context.view_layer.objects.active = ob
ob.select_set(True)

# 2.79
#scene.objects.link(ob)
#scene.objects.active = ob
#ob.select = True

# create bmesh from mesh
bm = bmesh.new()
bm.from_mesh(me)

# create a circle
bmesh.ops.create_circle(
        bm,
        cap_ends=False,
        radius=1.0,
        #diameter=0.5, # < 2.79
        segments=7)

# select bmesh vertex, need to ensure_lookup_table on the
# bmverts sequence before
bm.verts.ensure_lookup_table()
bm.verts[0].select = True

# write bmesh to mesh and update mesh
bm.to_mesh(me)
me.update()

# enter edit mode
bpy.ops.object.mode_set(mode='EDIT')
$\endgroup$
  • $\begingroup$ The code above threw an error on the following line: scene.objects.link(ob) ... The error message was 'AttributeError: 'bpy_prop_collection' object has no attribute 'link' ... I am running 2.8 and cut-and-pasted all of your code into a clean sheet of the text editor. $\endgroup$ – vndep Jul 15 at 14:58
  • 1
    $\begingroup$ @vndep For future reference, please add "2.8" somewhere in the question. I have updated the code for 2.8. Also try using the console to debug missing functions. $\endgroup$ – Leander Jul 15 at 15:10

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.