1

I'm making a python script that is intended to create a face and extrude it (kind of a CAD mode).

Here is my code :

class Line():
    """ Create a Line from two points
:param tuple point1: Tuple of 3 float representing the first point in space
:param tuple point2: Tuple of 3 float representing the second point in space

:returns: A Line instance.
"""
def __init__(self, point1, point2):
    self.obj = [(point1[0]*scale, point1[1]*scale, point1[2]*scale),
                (point2[0]*scale, point2[1]*scale, point2[2]*scale)]

class Wire(): """ Create a wire from two or more lines.

:param list(Line) lines: List of lines used to make the wire.

:returns: A Wire instance.
"""
def __init__(self, lines):
    self.obj = [l.obj for l in lines]

class Face(): """ Create a face from a closed wire. """ def init(self, name, wire): super().init() # Create new mesh and a new object me = bpy.data.meshes.new(name + "Mesh") self.obj = bpy.data.objects.new(name, me) # Make a mesh from a list of vertices/edges/faces coords = [] for l in wire.obj : coords.append(l[0]) coords.append(l[1]) edges = [] for i in range(len(wire.obj)): edges.append((i2, i2+1)) faces=[[i for i in range(len(coords))]] print("coords:", coords) print("edges: ", edges) print("faces: ", faces)

    me.from_pydata(coords, edges, faces)
    # Display name and update the mesh
    #self.obj.show_name = True
    me.update()
    bpy.context.collection.objects.link(self.obj)

The face is well created.

Then I try to extrude this face along a path (a simple vector) :

class Extrude(Objects):
    """ Create a object by extruding a face
:param Face face: The face from wich the shape will be created
:param path: The vector defining the direction and length of the extrusion.

:returns: The extruded face along the specified path
"""
def __init__(self, name, face, path):
    super().__init__()

    # Go to edit mode, face selection mode and select all faces

    bpy.ops.object.select_all(action = 'DESELECT')
    face.obj.select_set(True)
    bpy.ops.object.mode_set(mode = 'EDIT')

    bpy.ops.mesh.extrude_region_move(MESH_OT_extrude_region={"use_normal_flip":False, "use_dissolve_ortho_edges":False, "mirror":False}, TRANSFORM_OT_translate={"value":path, "orient_type":'GLOBAL', "orient_matrix":((1, 0, 0), (0, 1, 0), (0, 0, 1)), "orient_matrix_type":'GLOBAL', "constraint_axis":(True, False, False), "mirror":False, "use_proportional_edit":False, "proportional_edit_falloff":'SMOOTH', "proportional_size":1, "use_proportional_connected":False, "use_proportional_projected":False, "snap":False, "snap_target":'CLOSEST', "snap_point":(0, 0, 0), "snap_align":False, "snap_normal":(0, 0, 0), "gpencil_strokes":False, "cursor_transform":False, "texture_space":False, "remove_on_cancel":False, "release_confirm":False, "use_accurate":False, "use_automerge_and_split":False})


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

but I get a : RuntimeError: Operator bpy.ops.object.mode_set.poll() failed, context is incorrect.

Do you have an idea of what is causing this error ?

Thank you very much.

Gregoire
  • 11
  • 1
  • 2
    https://blender.stackexchange.com/questions/14728/how-to-find-out-why-context-is-incorrect speculate issue above is re not having a selected face. IMO look at using bmesh.https://blender.stackexchange.com/search?q=%5Bpython%5D+extrude+face – batFINGER Jun 09 '21 at 08:24

0 Answers0