1
$\begingroup$

i'd like to experiment with code generation with the blender API, but i first need to understand how i could loop over the API within my code

for example bpy.context.object have tons of properties, we can naturally see those properties within the console autocomplete or within the Otuliner Data Api

enter image description here

enter image description here

let's take a modifier for example. (here above) we can clearly see from the outlined or console that this 'Array' modifier have many properties (or functions)

i'd like to know how i can print each property module followed by the value assigned. something like so:

import bpy 

obj = bpy.data.objects['my_object']
mod = obj.modifiers['Array']

print('Properties of Array modifier:')

for modules,value in mod:

   print(f"property {module} : {value}"

i already saw this kind of solution used before, but it seemed to only work with bpy and bpy.utils

import pkgutil
import bpy
import email

package = bpy.utils
prefix = package.__name__ + "."
for importer, modname, ispkg in pkgutil.iter_modules(package.__path__, prefix):
    print("Found submodule %s (is a package: %s)" % (modname, ispkg))

$\endgroup$
2
$\begingroup$

You can take advantage of the dir() method in python to access "public" attributes of any object. You can use it for each modifier in the selected object :

import bpy

obj = bpy.context.object

for mod in obj.modifiers:
    print(mod.name, 'Modifier')
    for attr in dir(mod)[4::]:  # We skip the first 4 attributes which are internal
        print('  ', attr, ' --> ', getattr(mod, attr))

Exemple with an object with an 'Array' and a 'Curve' modifier :

enter image description here

$\endgroup$
1
  • 1
    $\begingroup$ You can also use print(*inspect.getmembers(my_object), sep='\n') $\endgroup$ – unwave Oct 12 '20 at 22:24
1
$\begingroup$

well that was easy, bl_rna.properties is all i needed

import bpy

obj = bpy.data.objects['Terrain']
mod = obj.modifiers['Array']

for key,value in mod.bl_rna.properties.items():

    print('')
    print('Dict KEY  :',key)
    print('Dict VALUE:',value)

    n = value.name
    print('NAME      :',n)

    v = eval(f'mod.{key}')
    print('VALUE     :',v)

    a = f"bpy.data.objects['{obj.name}'].modifiers['{mod.name}'].{key}"
    print('API       :',a)

still the

a = f"bpy.data.objects['{obj.name}'].modifiers['{mod.name}'].{key}"

part is a bit dirty, i should be able to extract the API automatically somehow ? kinda transforming mod.{key} into the full API line.

code generation here i come

$\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.