3
$\begingroup$

I'm writing a quick 'n dirty script to create a render-visibility based stop motion animation. In order to optimize things, I'd like to merge objects which have the same modifier configurations.

Unfortunately, I haven't found simple/clean way to check if two objects' modifiers are identical (or mostly identical; if the only way to do it is by comparing each setting of each modifier, I only care about settings which apply to the render).

How can modifiers be compared?

$\endgroup$
2
  • $\begingroup$ Could you elaborate with a couple of modifiers and their assoc. render props that need to be equal? $\endgroup$
    – batFINGER
    Commented Jan 6, 2017 at 14:11
  • $\begingroup$ @batFINGER Well, I was hoping for a general solution.. But I suppose I could live with only merging objects which only have mirror and/or subsurf modifiers, with the same render settings. $\endgroup$
    – gandalf3
    Commented Jan 6, 2017 at 15:45

1 Answer 1

8
$\begingroup$

A function that returns True if all the properties of mod1 match mod2

def mod_equality(mod1, mod2, ignore=["name"]):
    return all(getattr(mod1, prop, True)
             == getattr(mod2, prop, False)
             for prop in mod1.bl_rna.properties.keys()
             if prop not in ignore)

Edit: added an ignore list.

Simple test in console, Cube with Subsurf modifier and copied.

>>> m1
bpy.data.objects['Cube.001'].modifiers["Subsurf"]

>>> m2
bpy.data.objects['Cube'].modifiers["Subsurf"]

>>> m1 == m2
False

>>> def mod_equality(mod1, mod2):
...     return all(getattr(mod1, prop, True)
...              == getattr(mod2, prop, False)
...              for prop in mod1.bl_rna.properties.keys())
...              
>>> mod_equality(m1, m2)
True

A method to see if one object has same modifiers, all with same settings and in same order as another object

def same_modifier_stack_ordered(obj1, obj2):
    return all(mod_equality(m, obj2.modifiers[i])
                for i,m in enumerate(obj1.modifiers))
$\endgroup$
2
  • $\begingroup$ I think the square brackets of the list comprehension should be removed. The all() function will then treat the expression as a generator and execute faster in most cases. $\endgroup$
    – Omar Emara
    Commented Sep 7, 2018 at 18:16
  • $\begingroup$ Agreed. Been using that way for some time, thought for longer than Jan '17 / Thanks for the heads up. Feel free to edit in such changes. $\endgroup$
    – batFINGER
    Commented Sep 20, 2018 at 4:35

You must log in to answer this question.

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