Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '''
- LazyFakePython
- goathead 2010 - License GNU LGPL
- single call per line only
- attribute access ok
- argument types limited
- '''
- class LazyPython(object):
- def module( self, name ): return Proxy(name, space=self)
- def __init__(self):
- self.Commands = []
- self.VarCount = 0
- def flush(self):
- self.VarCount = 0
- a = ''
- while self.Commands: a += self.Commands.pop(0) + ';'
- return a
- class Proxy(object):
- def __init__(self,name, parent=None, space=None):
- self._name = name
- self._parent = parent
- self._space = space
- def __call__(self,*args,**kw):
- Commands = self._space.Commands
- #print( 'call ->', self._name )
- a = ''
- for arg in args:
- if isinstance(arg, Proxy):
- if arg._parent: a += '%s,' %Commands.pop(-1)
- else: a += '%s,' %arg._name
- elif type(arg) in (str,unicode): a += '"%s",' %arg
- else: a += '%s,' %arg
- for key in kw:
- arg = kw[key]
- if isinstance(arg, Proxy):
- if arg._parent: a += '%s=%s,' %(key,Commands.pop(-1))
- else: a += '%s=%s,' %(key,arg._name)
- elif type(arg) in (str,unicode): a += '%s="%s",' %(key,arg)
- else: a += '%s=%s,' %(key,arg)
- name = 'var%s' %self._space.VarCount
- cmd = Commands[-1]
- Commands[-1] = '%s = %s(%s)' %(name,cmd,a)
- self._space.VarCount += 1
- p = Proxy(name, space=self._space)
- return p
- def __getattr__(self,name):
- #print( 'getattr ->', name)
- if not self._parent: self._space.Commands.append( self._name+'.'+name )
- else: self._space.Commands[-1] += '.' + name
- p = Proxy(name, parent=self, space=self._space)
- return p
- if __name__ == '__main__':
- zpy = LazyPython()
- bpy = zpy.module('bpy')
- bpy.ops
- bpy.data.objects
- v = bpy.nothing.nowhere.nohow(1,2,3,"hi", True, {'dict-test':1}, key="test" )
- bpy.new.func(v.x)
- #bpy.this.will.fail( v.x() ) # only one call per line allowed
- for cmd in zpy.Commands:
- print( cmd )
- pipe2blender = zpy.flush()
- print( 'this string could be executed in blender by exec(s)' )
- print( pipe2blender )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement