minspect.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. """
  2. Copyright (c) Contributors to the Open 3D Engine Project.
  3. For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. SPDX-License-Identifier: Apache-2.0 OR MIT
  5. """
  6. # -------------------------------------------------------------------------
  7. import pymel.core as pmc
  8. import sys
  9. import types
  10. def syspath():
  11. print 'sys.path:'
  12. for p in sys.path:
  13. print ' ' + p
  14. def info(obj):
  15. """Prints information about the object."""
  16. lines = ['Info for %s' % obj.name(),
  17. 'Attributes:']
  18. # Get the name of all attributes
  19. for a in obj.listAttr():
  20. lines.append(' ' + a.name())
  21. lines.append('MEL type: %s' % obj.type())
  22. lines.append('MRO:')
  23. lines.extend([' ' + t.__name__ for t in type(obj).__mro__])
  24. result = '\n'.join(lines)
  25. print result
  26. def _is_pymel(obj):
  27. try: # (1)
  28. module = obj.__module__ # (2)
  29. except AttributeError: # (3)
  30. try:
  31. module = obj.__name__ # (4)
  32. except AttributeError:
  33. return None # (5)
  34. return module.startswith('pymel') # (6)
  35. def _py_to_helpstr(obj):
  36. if isinstance(obj, basestring):
  37. return 'search.html?q=%s' % (obj.replace(' ', '+'))
  38. if not _is_pymel(obj):
  39. return None
  40. if isinstance(obj, types.ModuleType):
  41. return ('generated/%(module)s.html#module-%(module)s' %
  42. dict(module=obj.__name__))
  43. if isinstance(obj, types.MethodType):
  44. return ('generated/classes/%(module)s/'
  45. '%(module)s.%(typename)s.html'
  46. '#%(module)s.%(typename)s.%(methname)s' % dict(
  47. module=obj.__module__,
  48. typename=obj.im_class.__name__,
  49. methname=obj.__name__))
  50. if isinstance(obj, types.FunctionType):
  51. return ('generated/functions/%(module)s/'
  52. '%(module)s.%(funcname)s.html'
  53. '#%(module)s.%(funcname)s' % dict(
  54. module=obj.__module__,
  55. funcname=obj.__name__))
  56. if not isinstance(obj, type):
  57. obj = type(obj)
  58. return ('generated/classes/%(module)s/'
  59. '%(module)s.%(typename)s.html'
  60. '#%(module)s.%(typename)s' % dict(
  61. module=obj.__module__,
  62. typename=obj.__name__))
  63. def test_py_to_helpstr():
  64. def dotest(obj, ideal):
  65. result = _py_to_helpstr(obj)
  66. assert result == ideal, '%s != %s' % (result, ideal)
  67. dotest('maya rocks', 'search.html?q=maya+rocks')
  68. dotest(pmc.nodetypes,
  69. 'generated/pymel.core.nodetypes.html'
  70. '#module-pymel.core.nodetypes')
  71. dotest(pmc.nodetypes.Joint,
  72. 'generated/classes/pymel.core.nodetypes/'
  73. 'pymel.core.nodetypes.Joint.html'
  74. '#pymel.core.nodetypes.Joint')
  75. dotest(pmc.nodetypes.Joint(),
  76. 'generated/classes/pymel.core.nodetypes/'
  77. 'pymel.core.nodetypes.Joint.html'
  78. '#pymel.core.nodetypes.Joint')
  79. dotest(pmc.nodetypes.Joint().getTranslation,
  80. 'generated/classes/pymel.core.nodetypes/'
  81. 'pymel.core.nodetypes.Joint.html'
  82. '#pymel.core.nodetypes.Joint.getTranslation')
  83. dotest(pmc.joint,
  84. 'generated/functions/pymel.core.animation/'
  85. 'pymel.core.animation.joint.html'
  86. '#pymel.core.animation.joint')
  87. dotest(object(), None)
  88. dotest(10, None)
  89. dotest([], None)
  90. dotest(sys, None)
  91. def test_py_to_helpstrFAIL():
  92. assert 1 == 2, '1 != 2'
  93. import webbrowser # (1)
  94. HELP_ROOT_URL = ('http://help.autodesk.com/cloudhelp/2018/ENU/Maya-Tech-Docs/PyMel/')# (2)
  95. def pmhelp(obj): # (3)
  96. """Gives help for a pymel or python object.
  97. If obj is not a PyMEL object, use Python's built-in
  98. `help` function.
  99. If obj is a string, open a web browser to a search in the
  100. PyMEL help for the string.
  101. Otherwise, open a web browser to the page for the object.
  102. """
  103. tail = _py_to_helpstr(obj)
  104. if tail is None:
  105. help(obj) # (4)
  106. else:
  107. webbrowser.open(HELP_ROOT_URL + tail) # (5)
  108. if __name__ == '__main__':
  109. test_py_to_helpstr()
  110. print 'Tests ran successfully.'