Could anyone help in making a script for selecting all objects that are specified in the different modifiers that an object has stacked? For example if my object has a Boolean modifier and an Array modifier, which both refer to an empty or another object (in the Array modifier there is for example an Object offset you can specify). Would it be possible to select all those object with one click? Thanks.
-
4$\begingroup$ What have you tried so far, could you post what you have achieved and show us were exactly you are stuck? As it stands now it sounds like you want someone to do it for you, which is not really the spirit of this site. $\endgroup$– Duarte Farrajota Ramos ♦Sep 18, 2017 at 18:11
-
$\begingroup$ Fair enough. I will do it myself and share it. $\endgroup$– Peeter MaimikSep 18, 2017 at 18:17
2 Answers
Using the bl_rna.properties
collection. This gives the name, type and other properties of the properties of the modifier. If the property is an object it is most likely, both a PointerProperty
(prop.type == 'POINTER')
and points to a prop.fixed_type
of bpy.types.Object
. If so getattr(modifier, propertyname)
will be either the object if set, or None if not.
import bpy
context = bpy.context
obj = context.object
for m in obj.modifiers:
print("modifier (%s): %s" % (m.type, m.name))
for k, v in m.bl_rna.properties.items():
if v.type == 'POINTER' and v.fixed_type.name == "Object":
print("\t", k, getattr(m, k))
# select mod obj if not None
ob = getattr(m, k, None)
if ob is not None:
ob.select_set(True)
It is a little brute-force, but here is my attempt : http://web.purplefrog.com/~thoth/blender/python-cookbook/select-objects-in-modifiers.html
import bpy
import inspect
def select_objects_in_modifiers(obj):
rna = obj.bl_rna
refs = set()
for mod in obj.modifiers:
for key,val in inspect.getmembers(mod):
if val is None:
continue
if hasattr(val, "bl_rna") and val.bl_rna == rna:
refs.add(val)
for o2 in refs:
o2.select = True
#print(o2)
select_objects_in_modifiers(bpy.context.active_object)
We use inspect.getmembers
to scan all the fields of a modifier and check to see if the .bl_rna
matches the bl_rna for an object. If so, that must be an object referenced by a modifier.