3

I just want to write a python script to select edges which are marked seams using a python code instead of Shift+G, that is my request, I have to merge this code with a bigger one so Shift+G won't work for me.

NamanDeep
  • 461
  • 3
  • 12

3 Answers3

3

Think this should do the trick.

import bpy
import bmesh

obj = bpy.context.active_object bm = bmesh.from_edit_mesh(obj.data)

for e in bm.edges[:]: if e.seam: e.select_set(True)

bmesh.update_edit_mesh(obj.data)

Jakemoyo
  • 4,375
  • 10
  • 20
  • There's no need to use [:] there to make a list copy, as you're not modifying the collection (as in the order or length doesn't change). – Markus von Broady Jun 03 '22 at 20:47
  • 1
    Thank you, Really thats all I wanted, appreciate that. – NamanDeep Jun 04 '22 at 03:56
  • @MarkusvonBroady good to know, Yeah I mainly learned everything I know about bmesh from this demo in the bmesh docs and they use that semantic basically everytime they access the geo like that. ¯_(ツ)_/¯ – Jakemoyo Jun 04 '22 at 10:30
  • @Jakemoyo it might be a good idea to get into the habit of using that to avoid nasty problems when you end up in the state of sin, though I think it's nice to know why something is being done, and I'd also argue usually defensive programming is bad because it's better to fail and learn from that :). In your example you need to sum edges and verts, and BM sequences don't support addition operator, so instead list copies are made and added together. – Markus von Broady Jun 04 '22 at 14:35
2

This will work in Object Mode:

import bpy
from bpy import context as C

for e in C.object.data.edges: e.select = e.use_seam

Or a faster way if you have a lot of geometry:

import bpy, numpy as np
from bpy import context as C

me = C.object.data

array = np.zeros(len(me.polygons), dtype=bool) me.polygons.foreach_set('select', array) array = np.zeros(len(me.vertices), dtype=bool) me.vertices.foreach_set('select', array)

array = np.zeros(len(me.edges), dtype=bool) me.edges.foreach_get('use_seam', array) me.edges.foreach_set('select', array)

Markus von Broady
  • 36,563
  • 3
  • 30
  • 99
1
import bpy

obj = bpy.context.active_object # make obj active object in your scene

if obj.type == 'MESH': # check if active object is a mesh (logical)

    # in order to select edges, you need to make sure that
    # previously you deselected everything in the Edit Mode
    # and set the select_mode to 'EDGE'
    bpy.ops.object.mode_set(mode = 'EDIT')
    bpy.ops.mesh.select_mode(type = 'EDGE')
    bpy.ops.mesh.select_all(action = 'DESELECT')

    # we need to return back to the OBJECT mode,
    # otherwise, the result won't be seen,
    # see https://blender.stackexchange.com/questions/43127 for info
    bpy.ops.object.mode_set(mode = 'OBJECT')

    # now we check all the edges
    for edge in obj.data.edges:
        if edge.use_seam: # if the edge uses seam
            edge.select = True # select it

    # as we did all selection in the OBJECT mode,
    # now we set to EDIT to see results
    bpy.ops.object.mode_set(mode = 'EDIT') 

else: 
    print("Object is not a mesh") # print if active object is not a mesh
kemplerart
  • 610
  • 1
  • 10