Cmds style question

I’m reviewing a lot of code lately and I’ve seen a lot of different styles of using the cmds module. In particular, I find a lot of clutter in the commands that need to combine object names and attribute names. I’m curious to know what style feels most natural or readable to other TA’s (ignore, for the time being how to make things read like this and concentrate on what you’d find easiest to understand and maintain

  • cmds.setAttr(myobject + “.tx”, 10)
  • cmds.setAttr("{}.tx".format(myobject), 10)
  • set_attrib(myobj, “tx”, 10)
  • myobject.tx = 10

0 voters

And of course chime in to discuss below…

The object oriented approach is the most readable, but given cmds does not work that way my vote goes to “format”. More verbose, but if I recall correctly the input variables can be any type, making it less prone to throwing errors.

It’s correct that format is the ‘more correct’ general solution – although the things it fixes are not relevant to this specific use case, since it’s extremely unlikely you don’t just have two strings.
Ironically you’re supposed to avoid using + for string concatenation but in this precise case (with only two strings) it’s very fast . It breaks down if you do it repeatedly in a loop.

This thread is kind of funny in a “programmers are jerks” kind of way :frowning: