Skip to content

Instantly share code, notes, and snippets.

@johnfredcee
Created August 25, 2011 20:13
Show Gist options
  • Save johnfredcee/1171755 to your computer and use it in GitHub Desktop.
Save johnfredcee/1171755 to your computer and use it in GitHub Desktop.
Dumping Blender Scene as an S-Expression
import bpy
import mathutils
import types
# transform python property to lisp keyword
def lispify(s):
if ((s == None) or (s[-1:]==']')):
return None
result = s.split('.')[-1:]
result = ":" + result[0].upper()
return result
def introspect_obj(o, txt):
type_o = type(o)
if (type_o in [ types.CodeType, types.BuiltinFunctionType, types.BuiltinMethodType, types.FunctionType, types.LambdaType ]):
return
if (txt != None):
print()
print( '; ' + txt )a
keyword = lispify(txt)
if (keyword != None):
print ( keyword + " . ", end = '' )
if (type_o == bool):
if (o == False):
print("NIL ", end = '')
else:
print("T ", end = '')
return
if (type_o in [ int, float, type(None) ]):
print(o, " ", end='')
return
if (type(o) == str):
print ("\"" + o + "\" ", end='')
return
if txt == None:
return
# we do this explicitly to avoid the madness of swizzling (100s of members per vector!)
if (type_o == mathutils.Vector):
items = [ 'x', 'y', 'z', 'w' ]
print( " #(", end = '' )
for item in items:
newtxt = txt + '.' + item
val = getattr(o, item, None)
if (val != None):
introspect_obj( val, newtxt)
print( " ) ", end = '' )
return
# object members
try: __members__ = dir(o)
except: __members__ = []
# all kinds of stuff turns up in dir(), so filter it out
if (__members__ != []):
fields = []
for item in __members__:
if item.startswith("__"):
continue
if item in [ 'rna_type', 'bl_rna' ]:
continue
if item in txt:
continue
type_i = type(getattr(o, item, None))
if (type_i in [ types.CodeType, types.BuiltinFunctionType, types.BuiltinMethodType, types.FunctionType, types.LambdaType ]):
continue
fields += [ item ]
# if there's anything left, print it
if (len(fields) != 0):
print( "( ", end = '' )
for item in fields:
newtxt = txt + '.' + item
# newtxt = item
introspect_obj( getattr(o, item, None), newtxt)
print( " )" )
# now, try dict types
try: keys = o.keys()
except: keys = None
if keys:
print( "(", end = '' )
for k in keys:
newtxt = txt + "." + k
introspect_obj(o.__getitem__(k), newtxt)
print( ") " )
else:
# list/tuple
try: length = len( o )
except: length = 0
if (length != 0):
if ("__getitem__" in __members__):
print( "#( ", end = '' )
# print(txt)
for i in range(length):
newtxt = txt + '[' + str(i) + ']'
introspect_obj(o[i], newtxt)
print( " )" )
else:
# sets/nonindexable
print( "#( ", end = '' )
for i in o:
introspect_obj(i, None)
print( " ) " )
return
introspect_obj(bpy.data.objects, 'bpy.data.objects')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment