1
$\begingroup$

I would like to subdivide all edges of the selected objects. This is what I've done so far:

def execute(self, context):
    theScene = bpy.context.scene

    # Store selected objects
    theSelected = bpy.context.selected_objects
    theObjects = set([obj for obj in theSelected if obj.type == 'MESH'])

    # Iterate over selected objects
    for obj in theObjects:
        # Set this object as active
        theScene.objects.active = obj

        # Select all edges in edit mode
        bpy.ops.object.mode_set(mode='EDIT')
        bpy.ops.mesh.select_mode(type='EDGE')
        bpy.ops.mesh.select_all(action = 'SELECT')

        # Subdivide
        bpy.ops.mesh.subdivide()

    return {'FINISHED'}

How to adjust this operator to process meshes only once when they are used in multiple objects?

$\endgroup$
2
$\begingroup$

You could use an array to check if a mesh has been already modified.

theScene = bpy.context.scene

# Store selected objects
theSelected = bpy.context.selected_objects
theObjects = set([obj for obj in theSelected if obj.type == 'MESH'])

#all the meshes here:
meshes = []


# Iterate over selected objects
for obj in theObjects:
    # Set this object as active
    theScene.objects.active = obj
    mesh = obj.data

    # Do something if the mesh isn't in the array yet
    if (not mesh in meshes):
        meshes.append(mesh)

        # Select all edges in edit mode
        bpy.ops.object.mode_set(mode='EDIT')
        bpy.ops.mesh.select_mode(type='EDGE')
        bpy.ops.mesh.select_all(action = 'SELECT')

        print(meshes)

        # Subdivide
        bpy.ops.mesh.subdivide()

        # Toggle Object Mode to select next object
        bpy.ops.object.mode_set(mode='OBJECT')
$\endgroup$
  • $\begingroup$ That's a good method, thanks. I see that you reset the mode to OBJECT before the next iteration, is this mandatory? $\endgroup$ – mins Dec 28 '15 at 9:16
  • $\begingroup$ Honestly I don't know. I've added it just because I've got an error while running the script :) $\endgroup$ – parameciostudio Dec 28 '15 at 9:53
0
$\begingroup$

I think names are unique for each data type in a scene, so use those. Either that set should be a dict with names of meshes as keys, or collect the names of processed meshes into a separate set and process only if the name is not in that set. You get the mesh name from obj.data.name.

$\endgroup$

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.