Advertisement
TheOldHunter

control_class_instantiation_wip

Apr 13th, 2020
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.85 KB | None | 0 0
  1. import maya.api.OpenMaya as om
  2. from maya import cmds
  3. import os
  4. import logging
  5. logger = logging.getLogger(__name__)
  6.  
  7. cmds.file(new=True, f=1)
  8. cmds.polySphere()
  9. cmds.polySphere()
  10. cmds.select('pSphere1', r=1)
  11.  
  12. class BaseComponent(object):
  13.     def __new__(cls, *args, **kwargs):
  14.         node = None
  15.         if args:
  16.             node = args[0]
  17.         elif 'node' in kwargs:
  18.             node = kwargs.get('node')
  19.  
  20.         if not node:
  21.             print("NEW:NOINPUT: ", cls, args, kwargs)
  22.             return cls.from_selection()
  23.  
  24.  
  25.         elif isinstance(node, str):
  26.             print("NEW:STR: ", cls, args, kwargs)
  27.             obj = cls.from_string(node)
  28.             # return super(BaseComponent, obj).__new__(cls)
  29.             return obj
  30.  
  31.         if isinstance(node, om.MObjectHandle):
  32.             print("NEW:HANDLE: ", cls, args, kwargs)
  33.             obj = cls.from_mobject_handle(node)
  34.             return obj
  35.  
  36.         if isinstance(node, om.MObject):
  37.             print("NEW:MOB: ", cls, args, kwargs)
  38.             return super(BaseComponent, cls).__new__(cls)
  39.         else:
  40.             raise ValueError('Input not supported.')
  41.  
  42.  
  43.     def __init__(self, *args, **kwargs):
  44.         node = None
  45.         if args:
  46.             node = args[0]
  47.         elif 'node' in kwargs:
  48.             node = kwargs.get('node')
  49.  
  50.         if not isinstance(node, om.MObject):
  51.             # print(node)
  52.             logger.warning("Supported types are selection, str, mobject_handle, mobject")
  53.             return
  54.  
  55.         # print("INIT: ", node)#args, kwargs)
  56.  
  57.         # # Base properties
  58.         self.name = ''
  59.         # # Extract Component name
  60.         node_name = om.MFnDependencyNode(node).name()
  61.         self.name = node_name
  62.  
  63.     @classmethod
  64.     def create(cls, *args, **kwargs):
  65.         obj = cls.__new__(cls, *args, **kwargs)
  66.         if isinstance(obj, cls):
  67.             cls.__init__(obj, *args, **kwargs)
  68.         return obj
  69.  
  70.     @classmethod
  71.     def from_selection(cls):
  72.         """
  73.        Initiate BaseComponent from maya selection
  74.        Returns
  75.        -------
  76.        BaseComponent
  77.        """
  78.         try:
  79.             selection = cmds.ls(sl=1)[0]
  80.             # return super(BaseComponent, cls).__new__(cls, node=selection)
  81.             return cls.from_string(selection)
  82.         except IndexError:
  83.             logger.error('No node selected.')
  84.             pass
  85.  
  86.     @classmethod
  87.     def from_string(cls, node):
  88.         """
  89.        Initiate BaseComponent from str
  90.        Parameters
  91.        ----------
  92.        node : str
  93.            Node Name
  94.        Returns
  95.        -------
  96.        BaseComponent
  97.        """
  98.         try:
  99.             sel_list = om.MSelectionList()
  100.             sel_list.add(node)
  101.             mob = sel_list.getDependNode(0)
  102.             # return super(BaseComponent, cls).__new__(cls, mob)
  103.             return cls(mob)
  104.         except RuntimeError:
  105.             logger.error("Node does not exist. Component not created.")
  106.             pass
  107.  
  108.     @classmethod
  109.     def from_mobject_handle(cls, mob_hdl):
  110.         mob = mob_hdl.object()
  111.         # return super(BaseComponent, cls).__new__(cls, mob)
  112.         return cls(mob)
  113.  
  114.  
  115. test = BaseComponent()
  116. if test:
  117.     print("TEST1:SELECTION: {}{} \r\n".format(test, test.__dict__))
  118. else:
  119.     print("")
  120.  
  121. test2 = BaseComponent(node='pSphere2')
  122. if test2:
  123.     print("TEST2:STR: {}{} \r\n".format(test2, test2.__dict__))
  124. else:
  125.     print("")
  126.  
  127. sel = om.MGlobal.getActiveSelectionList()
  128. mob = sel.getDependNode(0)
  129. mob_hdl = om.MObjectHandle(mob)
  130. test3 = BaseComponent(node=mob_hdl)
  131. if test3:
  132.     print("TEST3:MOBHANDLE: {}{} \r\n".format(test3, test3.__dict__))
  133. else:
  134.     print("")
  135.  
  136. sel = om.MGlobal.getActiveSelectionList()
  137. mob = sel.getDependNode(0)
  138. test4 = BaseComponent(node=mob)
  139. if test4:
  140.     print("TEST4:MOB: {}{} \r\n".format(test4, test4.__dict__))
  141. else:
  142.     print("")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement