0

I manually created an object, and then, in edit mode, I rebuild same object, at cursor position. there I'm using existing geometry to find the best way to rebuild it. but I could do a copy of the result of the vertices list (and other list about faces and edges) and rebuild the mesh from this. so this is a quick way to create an object from edit mode after... here is the script I did

import bpy, bmesh
from bpy import context

cao = context.object me = cao.data l =len(me.vertices) loc = context.scene.cursor.location

v_coords = [ (v.co) for v in me.vertices]

faces_verts_idx = [face.vertices[:] for face in me.polygons] edges_verts_idx = [edge.vertices[:] for edge in me.edges]

edges = [tuple(x+l for x in tup) for tup in edges_verts_idx] faces = [tuple(x+l for x in tup) for tup in faces_verts_idx]

new vert at curs pos

bm = bmesh.from_edit_mesh(me) mw = cao.matrix_world new_verts = [bm.verts.new(mw.inverted()@coord + loc) for coord in v_coords] bm.verts.ensure_lookup_table() for tup in edges: bm.edges.new((bm.verts[tup[0]], bm.verts[tup[1]]))

for tup in faces: vert_tup=() for i in tup: vert_tup += (bm.verts[i],) bm.faces.new(vert_tup) for v in bm.verts: v.select = True bm.select_flush(True) bmesh.update_edit_mesh(me) me.update() bpy.ops.mesh.remove_doubles() bpy.ops.mesh.normals_make_consistent(inside=False)

enter image description here

this is working, but on more advanced meshes, some faces are inverted, and the part on the mesh rebuild seems clumsy. not sure there is not a much simpler way, under bmesh, to do this.

2 Answers2

2

Edit mode Dupe.

Not 100% sure on if this is what you want since the question code bunks out after one copy

EDIT.

Given comment and OP's answer Q is pretty much a dupe of

How do I Create a script for geometry I create?

which for example, could be converted to a generic add primitive operator,

enter image description here

To make an copy at cursor location Would simply just read in the mesh again using from_mesh and translate the appended verts. Test Script, with result as illustrated in GIF.

import bpy
import bmesh

from bpy import context

scene = context.scene ob = context.edit_object mwi = ob.matrix_world.inverted() me = ob.data

loc = scene.cursor.location

bm = bmesh.from_edit_mesh(me) n = len(bm.verts)

read again

bm.from_mesh(me) bmesh.ops.translate( bm, verts=bm.verts[n:], vec=mwi @ scene.cursor.location )

bmesh.update_edit_mesh(me)

batFINGER
  • 84,216
  • 10
  • 108
  • 233
0

add an object (a cone), go to edit mode then copy geometry with the commented lines and past the result change cursor location and run the script

import bpy
import bmesh
from bpy import context
from mathutils import Vector

edit_obj = context.edit_object me = edit_obj.data loc =context.scene.cursor.location mw = context.object.matrix_world

#get geo from an existing object

#verts = [vertices.co for vertices in me.vertices] #print("verts=", verts)#Dbg #faces = [face.vertices[:] for face in me.polygons] #print("faces=", faces)#Dbg #edges = [edge.vertices[:] for edge in me.edges]
#print("edges=", edges)#Dbg

#past result

verts = [Vector((0.0, 1.0, -1.0)), Vector((0.19509032368659973, 0.9807852506637573, -1.0)), Vector((0.3826834559440613, 0.9238795042037964, -1.0)), Vector((0.5555702447891235, 0.8314695954322815, -1.0)), Vector((0.7071067690849304, 0.7071067690849304, -1.0)), Vector((0.8314695954322815, 0.5555702447891235, -1.0)), Vector((0.9238795042037964, 0.3826834261417389, -1.0)), Vector((0.9807852506637573, 0.19509035348892212, -1.0)), Vector((1.0, -4.371138828673793e-08, -1.0)), Vector((0.9807852506637573, -0.19509032368659973, -1.0)), Vector((0.9238795638084412, -0.3826833963394165, -1.0)), Vector((0.8314696550369263, -0.5555701851844788, -1.0)), Vector((0.7071067690849304, -0.7071067690849304, -1.0)), Vector((0.5555701851844788, -0.8314696550369263, -1.0)), Vector((0.38268348574638367, -0.9238795042037964, -1.0)), Vector((0.19509030878543854, -0.9807853102684021, -1.0)), Vector((-8.742277657347586e-08, -1.0, -1.0)), Vector((-0.19509024918079376, -0.9807853102684021, -1.0)), Vector((-0.3826834261417389, -0.9238795042037964, -1.0)), Vector((-0.5555703043937683, -0.8314695358276367, -1.0)), Vector((-0.7071067094802856, -0.7071068286895752, -1.0)), Vector((-0.8314694762229919, -0.5555704236030579, -1.0)), Vector((-0.9238795042037964, -0.38268357515335083, -1.0)), Vector((-0.9807852506637573, -0.1950903832912445, -1.0)), Vector((-1.0, 1.1924880638503055e-08, -1.0)), Vector((-0.9807852506637573, 0.1950904130935669, -1.0)), Vector((-0.9238794445991516, 0.3826836049556732, -1.0)), Vector((-0.831469714641571, 0.5555700659751892, -1.0)), Vector((-0.70710688829422, 0.7071067094802856, -1.0)), Vector((-0.5555703043937683, 0.8314695954322815, -1.0)), Vector((-0.3826834261417389, 0.9238795638084412, -1.0)), Vector((-0.19509023427963257, 0.9807853102684021, -1.0)), Vector((0.0, 0.0, 1.0))] faces = [(0, 32, 1), (1, 32, 2), (2, 32, 3), (3, 32, 4), (4, 32, 5), (5, 32, 6), (6, 32, 7), (7, 32, 8), (8, 32, 9), (9, 32, 10), (10, 32, 11), (11, 32, 12), (12, 32, 13), (13, 32, 14), (14, 32, 15), (15, 32, 16), (16, 32, 17), (17, 32, 18), (18, 32, 19), (19, 32, 20), (20, 32, 21), (21, 32, 22), (22, 32, 23), (23, 32, 24), (24, 32, 25), (25, 32, 26), (26, 32, 27), (27, 32, 28), (28, 32, 29), (29, 32, 30), (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31), (30, 32, 31), (31, 32, 0)] edges = [(0, 1), (0, 32), (32, 1), (1, 2), (32, 2), (2, 3), (32, 3), (3, 4), (32, 4), (4, 5), (32, 5), (5, 6), (32, 6), (6, 7), (32, 7), (7, 8), (32, 8), (8, 9), (32, 9), (9, 10), (32, 10), (10, 11), (32, 11), (11, 12), (32, 12), (12, 13), (32, 13), (13, 14), (32, 14), (14, 15), (32, 15), (15, 16), (32, 16), (16, 17), (32, 17), (17, 18), (32, 18), (18, 19), (32, 19), (19, 20), (32, 20), (20, 21), (32, 21), (21, 22), (32, 22), (22, 23), (32, 23), (23, 24), (32, 24), (24, 25), (32, 25), (25, 26), (32, 26), (26, 27), (32, 27), (27, 28), (32, 28), (28, 29), (32, 29), (29, 30), (32, 30), (30, 31), (32, 31), (31, 0)]

new_verts = [(mw.inverted()@coord + loc) for coord in verts]

new_mesh = bpy.data.meshes.new("test") new_mesh.from_pydata(new_verts, edges, faces)

bm = bmesh.from_edit_mesh(edit_obj.data) bm.clear() bm.from_mesh(new_mesh) bmesh.update_edit_mesh(edit_obj.data)

so this is simple to create your own object copy his geometry in a file to use it later under edit mode. an add-on could be done from this...