0
$\begingroup$

Code:

class CurveCreate(bpy.types.Operator):
    bl_idname = "curve.create_operator"
    bl_label = "Curvecreate"
    bl_description = "Duplicates the selected edges and converts them into curves"
    bl_options = {"REGISTER"}    

    def execute(self, context):
        bpy.ops.mesh.duplicate_move(MESH_OT_duplicate={"mode":1}, TRANSFORM_OT_translate={"value":(0, 0, 0), "orient_type":'GLOBAL', "orient_matrix":((0, 0, 0), (0, 0, 0), (0, 0, 0)), "orient_matrix_type":'GLOBAL', "constraint_axis":(False, 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})       
        bpy.ops.mesh.separate(type='SELECTED')
        bpy.ops.object.mode_set(mode='OBJECT')
        bpy.ops.object.convert(target='CURVE')
        bpy.context.object.data.fill_mode = 'FULL'
        bpy.context.object.data.bevel_depth = 0.15
        bpy.context.object.data.bevel_resolution = 3
        bpy.context.object.data.splines[0].use_smooth = True
        
        self.report({'INFO'}, "Curve Created !")
        return {"FINISHED"}

Question:

This is what I want to do:

What I want the code to do:

  1. Duplicate the selected edges.
  2. Separate the selected edges.
  3. Go back to object mode.
  4. Select the newly separated edges (this is what I cant do).
  5. Convert the selected separated edges into curve.
  6. Adjust some curve settings.

Now the problem is that when Step 3 has been executed that is, going back to object mode I find that the Active object is actually the cube and not the separated edges. Hence the remaining steps get applied to the cube instead. Both the cube and separated edges are already selected when returning to object mode but the cube is the actual Active object.

How do I set the newly separated edges as the Active object in this case?

$\endgroup$

1 Answer 1

1
$\begingroup$

Assume that you just selected one object to do the procedure:

Then after the separation, the new created object will be "selected" by default, but not active. You can check whether your selected object is not active, then that should be the one you've separated.

# After separation
C = bpy.context # select a correct context first
for obj in [
    o for o 
    in C.selected_objects 
    if o is not C.view_layer.objects.active #remove active object from list
]:
    C.view_layer.objects.active = obj
    # you can do further comparison here to make sure you select the right object
    break # Break before accessing other
# Do the rest of the code

$\endgroup$
2
  • 1
    $\begingroup$ The C = bpy.context should be C = context if your using a passed context, as your class do $\endgroup$
    – HikariTW
    Commented Jul 25, 2020 at 3:29
  • $\begingroup$ Worked perfectly, thanks. $\endgroup$
    – Retrax
    Commented Jul 25, 2020 at 3:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .