Importing The Maya cmds Module


#1

OK, so I’ve been working on Python frameworks for Maya for a couple of years.
I have a basic question, and it’s bugging me!
Why is the standard way to import cmds as follows:

import maya.cmds as cmds

Isn’t it more Pythonic to do this?:

from maya import cmds

Obviously, this is not a blocker for me, more of a technicality. Truth be told, I use pymel.core rather than cmds as it’s object based, but some commands only exist in cmds.


#2

I doubt one way or the other would be considered more Pythonic. Maybe from maya import cmds, but nothing to lose sleep over.
The import maya.cmds as cmds idiom is what is used in most (all?) of the maya documentation, which could easily explain its popularity.

I tend to use both, and I’m not super disciplined about when I choose one or the other. But personally I prefer from maya import cmds because its easy to extend out as from maya import cmds, utils, mel, standalone if I need to pull in more from the maya namespace.


#3

Depicts the traditional maya.cmds as cmds used everywhere, I saw:

  • maya.cmds as mc in some coding styles to save two spaces (in 80 width limited environment)
  • and simply import maya.cmds on another coding style implying to write the full namespace for each command (it was a 80 width limited environment too).

Just to say on some code base, Pythonic and PEP8 doesn’t always mean good but mostly good enough and sometimes, kinda OK.


#4

Yeah, the most correct answer, is the one that follows your teams local style guide.
If you’re the one creating the guide, go with what feels most readable to yourself and others.

One reason I’ve done import maya.cmds as mc in the past is so that I would get parity with the import pymel.core as pm .
I’ve also seen people do import pymel.core as pmc probably for similar reasons.

Currently our internal style guide prefers using the fully qualified path, so we do import maya.cmds while it feels almost silly to do it with such a common namespace in maya land, it keeps us stylistically in line with all our other python code.


#5

I definitely import pymel.core as pm because ‘core’ is far too generic a name for a module.