3
Module containing helpers to create the UI types at runtime.
5
__docformat__ = "restructuredtext"
7
import mrv.maya as mrvmaya
8
from mrv.util import uncapitalize
9
import mrv.maya.util as mutil
10
from mrv.path import make_path
11
_uipackage = __import__( "mrv.maya.ui", globals(), locals(), ['ui'] )
12
import maya.cmds as mcmds
13
from util import propertyQE, EventSenderUI
18
_typemap = { "floatingWindow" : "window", "field" : "textField" }
22
def init_classhierarchy( ):
23
""" Read a simple hiearchy file and create an Indexed tree from it"""
24
mfile = make_path( __file__ ).parent().parent() / "cache/UICommandsHierachy.hf"
28
_typetree = mrvmaya.dag_tree_from_tuple_list( mrvmaya.tuple_list_from_file( mfile ) )
32
""" Create Standin Classes that will delay the creation of the actual class till
33
the first instance is requested"""
34
mrvmaya.initWrappers( _uipackage.__dict__, _typetree.nodes_iter(), MetaClassCreatorUI )
39
class MetaClassCreatorUI( mutil.MetaClassCreator ):
40
""" Builds the base hierarchy for the given classname based on our
42
Additional support for :
44
**AUTOMATIC PROPERTY GENERATION**:
45
- if flags are simple get and set properties, these can be listed in the
46
_properties_ attribute ( list ). These must be queriable and editable
48
- Properties will be available as:
49
inst.p_myProperty to access myProperty ( equivalent to cmd -q|e -myProperty
51
- This only works if our class knows it's mel command in the __melcmd__ member
52
variable - inheritance for it does not work
54
**AUTOMATIC UI-EVENT GENERATION**:
55
- define names of mel events in _events_ as list of names
57
- these will be converted into Events sitting at attribute names like
58
e_eventName ( for even called 'eventName'
61
windowinstance.e_restoreCommand = func
62
whereas func takes: ``func( windowinstance, *args, **kwargs )``
64
**ADDITIONAL CONFIGURAITON**:
65
- strong_event_handlers:
66
if True, events will use strong references to their handlers
70
melcmd_attrname = '__melcmd__'
73
def __new__( metacls, name, bases, clsdict ):
74
""" Called to create the class with name """
76
#######################
77
cmdname = uncapitalize( name )
78
if hasattr( mcmds, cmdname ):
79
melcmd = getattr( mcmds, cmdname )
80
clsmelcmd = staticmethod( melcmd )
81
clsdict['__melcmd__'] = clsmelcmd
83
pass # don't bother, can be one of our own classes that will
84
#raise ValueError( "Did not find command for " + cmdname )
88
# read the properties attribute to find names to automatically create
89
# query and edit properties
90
propertynames = clsdict.get( "_properties_", list() )
91
for pname in propertynames:
92
attrname = "p_%s" % pname
93
# allow user overrides
94
if attrname not in clsdict:
95
clsdict[ attrname ] = propertyQE( pname )
96
# END for each property
100
# read the event description and create Event instances that will
101
# register themselves on first use, allowing multiple listeners per maya event
102
eventnames = clsdict.get( "_events_", list() )
103
event_kwargs = dict()
104
if clsdict.get( "strong_event_handlers", False ):
105
event_kwargs[ "weak" ] = False
107
for ename in eventnames:
108
attrname = "e_%s" % ename
109
# allow user overrides
110
if attrname not in clsdict:
111
clsdict[ attrname ] = EventSenderUI._UIEvent( ename, **event_kwargs )
112
# END for each event name
114
newcls = super( MetaClassCreatorUI, metacls ).__new__( _typetree, _uipackage,
115
metacls, name, bases, clsdict )