Are the functions able to have double names for their arguments in Python? I mean a short and full form of the variable name.
I'll try to be more clear. Everybody who is familiar with Autodesk Maya knows a function to make constraints. It has some flags and you can use the short or the long form of it's name:
maintainOffset(mo), weight(w), layer(l) and so on..
So you can call this function with different names of it's arguments but it gives you the same result:
cmds.parentConstraint(driverObj, drivenObj, maintainOffset=True, weight=1.0,..)
cmds.parentConstraint(driverObj, drivenObj, maintainOffset=True, w=1.0,..)
cmds.parentConstraint(driverObj, drivenObj, mo=True, weight=1.0,..)
cmds.parentConstraint(driverObj, drivenObj, mo=True, w=True,..)
How to implement this type of behaviour in Python 2.7.x? I'm using the documentation actively but still can't find the answer.
Besides I have defined 4 functions for various types of the contraints:
# Parent Constraint
def doParentConstraint(lst, mo = True, w = 1.0, sr = 'None', st = 'None', l = 'None'):
for pair in lst:
cmds.parentConstraint(pair[0], pair[1], maintainOffset = mo, weight = w,
skipRotate = sr, skipTranslate = st, layer = l)
# Orient Constraint
def doOrientConstraint(lst, mo = False, w = 1.0, o = (0.0, 0.0, 0.0), sk = 'None', l = 'None'):
for pair in lst:
cmds.orientConstraint(pair[0], pair[1], maintainOffset = mo, weight = w,
offset = o, skip = sk, layer = l)
# Point Constraint
def doPointConstraint(lst, mo = False, w = 1.0, o = (0.0, 0.0, 0.0), sk = 'None', l = 'None'):
for pair in lst:
cmds.orientConstraint(pair[0], pair[1], maintainOffset = mo, weight = w,
offset = o, skip = sk, layer = l)
# Scale Constraint
def doScaleConstraint(lst, mo = False, w = 1.0, o = (0.0, 0.0, 0.0), sk = 'None', l = 'None'):
for pair in lst:
cmds.orientConstraint(pair[0], pair[1], maintainOffset = mo, weight = w,
offset = o, skip = sk, layer = l)
Connection List:
cLst = produceConnectionList(tNodesA, tNodesB)
And some sort of wrapper function for all of these:
def batchConnect(type = "parentConstraint", ???):
cLst = produceConnectionList(tNodesA, tNodesB)
if type is "parentConstraint": doParentConstraint(cLst, ???)
if type is "orientConstraint": doOrientConstraint(cLst, ???)
if type is "pointConstraint": doPointConstraint(cLst, ???)
if type is "scaleConstraint": doScaleConstraint(cLst, ???)
But I don't know how to pass values between these functions and the wrapper because although they have the same number of arguments but the names of arguments and those types a little bit differ.
Also I want to have an opportunity to use short and the full form of the flag names when I'm calling the wrapper:
batchConnect("pointConstraint", mo=False, offset=(0,0,0), weight=1)
batchConnect("pointConstraint", maintainOffset=False, o=(0,0,0), w=1)
Must do the same.
batchConnect("pointConstraint")
Without arguments must call doPointConstraint() with default values.
batchConnect()
Without specifying the type and arguments at all must call by default doParentConstraint() with these default values
If I've correctly understood what you're after, the easiest way to do this is with **kwargs "magic" and dict.get, for example:
def demo(maintain_offset=None, **config):
if maintain_offset is None: # if not supplied by 'real name'
maintain_offset = config.get('mo', 'baz') # check alias, fall back to default
print maintain_offset
(note compliance with the style guide). In use:
>>> demo(maintain_offset="foo")
foo
>>> demo(mo="bar")
bar
>>> demo()
baz
If this syntax is unfamiliar, see What does ** (double star) and * (star) do for parameters?
For a more generic approach, you can use the decorator syntax to wrap a function with a set of aliases:
import functools
def alias(aliases):
def decorator(func):
@functools.wraps(func)
def wrapper(**kwargs):
for name, alias in aliases.items():
if name not in kwargs and alias in kwargs:
kwargs[name] = kwargs[alias]
return func(**kwargs)
return wrapper
return decorator
In use:
>>> @alias({'maintain_offset': 'mo'})
def demo(maintain_offset='baz', **kwargs):
print maintain_offset
>>> demo(maintain_offset="foo")
foo
>>> demo(mo="bar")
bar
>>> demo()
baz
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With