Here is a bunch of code which is functionally identical:
import maya.cmds as cmds
from maya.api.OpenMaya import MVector
# functional access
def get_translation(obj):
return MVector(*cmds.getAttr("{}.t".format(obj)))
def set_translation(obj, v_as_tuple):
cmds.setAttr("{}.t".format(obj), *v_as_tuple)
# one function for both get and set
print get_translation('pSphere1')
set_translation('pSphere1', (1,2,3))
def translation(obj, **kwargs):
set_value = kwargs.get('set', None)
if set_value is not None:
return set_translation(obj, set_value)
return get_translation(obj)
# function with kwargs to get or set
print translation('pSphere1')
translation('pSphere1', set = (1,1,1))
# accessor object -----------------
class Translation(object):
def __init__(self, obj):
self.target = obj
@property
def value(self):
return get_translation(self.target)
@value.setter
def value (self, v):
set_translation(self.target, v)
t = Translation('pSphere1')
print t.value
t.value = (5,4,3)
# pymel-like property access -------------
class TranslationProperty(object):
def __init__(self):
pass
def __get__(self, owner, _):
return get_translation(owner)
def __set__ (self, owner, v):
set_translation(owner, v)
class Proxy(str):
translation = TranslationProperty()
p = Proxy('pSphere1')
print p.translation
p.translation = (3,2,1)
All does the same thing. Which one do people like, and why? This is toy code, in production I’d use cmds.xform
to set translation in world or object space and there are other things to do, I’m just interested to here how people feel about the difference between functions, accessors, dot-style access, or possibly other kinds of syntax.
Thoughts?