Hoping for some help with this, I am trying to understand Class construction(instantiation before init).
I have a component class which encapsulates a hierarchy of nodes in Maya. Instantiated using a mObject as input. BaseComponent(mObject) I want to automate the instance construction of this class based on selection, mObjectHandle, string and have created classmethods (as alternative constructors) for the same. I wanted to allow initiation just by BaseComponent() that will take the selection and avoid BaseComponent.some_method as default. Goal: BaseComponent() # Takes selection BaseComponent.fromString() # Takes string name of an object in scene BaseComponent.fromMObjectHandle() # Takes an mObjectHandle for an object in scene BaseComponent(mObject) # Takes mObject as default Currently trying to implement this with __new__. Previously, all the "isinstance" checks were done in init which I wanted to simplify. I couldn't get it to work exactly how I wanted to, but I did get it to work with all four arg input types (selection, str, mObjectHandle, mObject). Problems: - The problem is there's no clear error/warning if the end-user makes a mistake. - All alternative constructors also pass to the init where I had to put a mObject isinstance check for it to work. If possible, I want to remove mObject check in init as mObject should be the only one passed to init. I have attached my extracted current working code in Pastebin. It has print statements and can be run in a new Maya session: https://pastebin.com/tN5bs6g0 Any suggestions/recommendations are really appreciated. Please let me know if I wasn't clear enough. (I have a basic idea about metaclasses and class decorators, so any advice that includes those is welcome.) Thank you! -- You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/e97d389f-19c1-49b4-a868-79afafe965fc%40googlegroups.com.
