0
$\begingroup$

I found this python script to add a subdivision surface modifier to all objects

import bpy

#Iterate over all members inside the Object struct
for ob in bpy.data.objects:
     #Check if object is a Mesh
     if ob.type == 'MESH':
         #Add a SUBSURF Modifier with the name "My SubDiv".
         ob.modifiers.new('My SubDiv', 'SUBSURF')

Is it possible to modify this so that it instead changes the view setting of all subdision surface modifiers?

$\endgroup$
0
$\begingroup$

Rough script for blender 2.79

import bpy

objects = bpy.context.scene.objects
viewlvl = 1

for ob in objects:
    if ob.type == 'MESH':
        objects.active = ob
        for mod in ob.modifiers:
            if mod.type == 'SUBSURF':
                mod.levels = viewlvl
$\endgroup$
1
$\begingroup$

Use the old one if it exists, else create new one.

  • Iterated over scene objects rather than all objects in blend.
  • If the object already has a modifier named "foo" use it, otherwise create a new one.

test code

import bpy
context = bpy.context

for ob in context.scene.objects:
     if ob.type == 'MESH':
         # get old one or add one
         m = ob.modifiers.get("My SubDiv") or ob.modifiers.new('My SubDiv', 'SUBSURF')
         m.levels = 1
         m.render_levels = 2
         m.quality = 3

oh and in case in testing you have ended up with a cluster of modifiers, here is an example of removing all modifiers with a name starting with "My SubDiv".

for ob in context.scene.objects:
     if ob.type == 'MESH':
         mods = [m for m in ob.modifiers
                 if m.name.startswith("My SubDiv")]
         while mods:
             ob.modifiers.remove(mods.pop())
$\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.