12
$\begingroup$

Due to the lack of a class diagramm I need to explore the data structures by dumping objects to see what is in there.

How can I dump the Objects internals to the console?

In this snippet I tried to print the objects but I only get error messages 'TypeError'

def assignPose( plIdx ):
    rig = bpy.data.objects.get("rig")

    if rig != None:
        print("rig=" + str(tuple( rig ))) # TypeError: 'Object' object is not iterable
        print( "" + rig.__str__ )         # TypeError: Can't convert 'method-wrapper' object to str implicitly
        pl = rig.pose_library
        pl.poselib_apply_pose( plIdx )
    else:
        print("couldn't find rig.")
$\endgroup$
  • $\begingroup$ What do you want to see as output exactly? If you want bones of the rig try tuple(rig.data.bones). If you want to get names of properties of rig itself, try dir(rig) $\endgroup$ – mg007 Jul 18 '13 at 6:55
  • 1
    $\begingroup$ Pure Python questions like this are better placed over at Stack Overflow. As you've found, there are many pre-existing ones there too, here's another: How do I look inside a Python object? $\endgroup$ – Aldrik Jul 18 '13 at 9:37
  • $\begingroup$ @Aldrik I had to try several approaches since not all of them work with blenders data structures, so I posted the answer to document this for later reference. $\endgroup$ – stacker Jul 18 '13 at 10:42
  • $\begingroup$ @stacker Python has built-in functions to do what your after, see link in previous comment. $\endgroup$ – Aldrik Jul 18 '13 at 11:32
13
$\begingroup$

I found a solution to get started:

def dump(obj):
   for attr in dir(obj):
       if hasattr( obj, attr ):
           print( "obj.%s = %s" % (attr, getattr(obj, attr)))

This is based on one of the methods suggested on this Stackoverflow post and this

Instead of invoking print call dump( anyobject )

This would print something like:

...
obj.copy = <bpy_func Object.copy()>
obj.cycles_visibility = <bpy_struct, CyclesVisibilitySettings("")>
obj.data = <bpy_struct, Armature("rig")>
obj.delta_location = <Vector (0.0000, 0.0000, 0.0000)>
obj.delta_rotation_euler = <Euler (x=0.0000, y=0.0000, z=0.0000), order='XYZ'>
obj.delta_rotation_quaternion = <Quaternion (w=1.0000, x=0.0000, y=0.0000, z=0.0
000)>
obj.delta_scale = <Vector (1.0000, 1.0000, 1.0000)>
obj.dimensions = <Vector (0.0000, 0.0000, 0.0000)>
obj.draw_bounds_type = BOX
obj.draw_type = WIRE
obj.dupli_faces_scale = 1.0
...
$\endgroup$
  • 2
    $\begingroup$ Please add a reference (stackoverflow.com/a/192184/155813 ?) from where you found the solution, so that others can explore related solutions/comments. $\endgroup$ – mg007 Jul 18 '13 at 7:06
  • $\begingroup$ @mg007 Good idea, added a reference btw. this was the only one working with blenders objects. $\endgroup$ – stacker Jul 18 '13 at 8:19
  • 1
    $\begingroup$ I've built off this idea and extended it to do some recursion / memo-ization: blender_dumpy.py - example snippet from the Race Spaceship demo: gist snippet $\endgroup$ – Eric Cousineau Oct 19 at 0:42
3
$\begingroup$

There is already an addon called API Navigator to explore the API. It has to be enabeld first in the User Preferences

enter image description here

enter image description here

$\endgroup$
  • $\begingroup$ Looked for this in 2.80, but guessing it may not be maintained :( See above comment for my hacky workaround based on object enumeration. $\endgroup$ – Eric Cousineau Oct 19 at 0:45

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.