mrv.maya.ui.typ
Covered: 69 lines
Missed: 1 lines
Skipped 50 lines
Percent: 98 %
  2
"""
  3
Module containing helpers to create the UI types at runtime.
  4
"""
  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
 17
_typetree = None
 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"
 27
	global _typetree
 28
	_typetree = mrvmaya.dag_tree_from_tuple_list( mrvmaya.tuple_list_from_file( mfile ) )
 31
def initWrappers( ):
 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
 41
	typetree.
 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'
 60
	 - assign an event:
 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
 68
	"""
 70
	melcmd_attrname = '__melcmd__'
 73
	def __new__( metacls, name, bases, clsdict ):
 74
		""" Called to create the class with name """
 77
		cmdname = uncapitalize( name )
 78
		if hasattr( mcmds, cmdname ):
 79
			melcmd = getattr( mcmds, cmdname )
 80
			clsmelcmd = staticmethod( melcmd )
 81
			clsdict['__melcmd__'] = clsmelcmd
 82
		else:
 83
			pass # don't bother, can be one of our own classes that will
 90
		propertynames = clsdict.get( "_properties_", list() )
 91
		for pname in propertynames:
 92
			attrname = "p_%s" % pname
 94
			if attrname not in clsdict:
 95
				clsdict[ attrname ] = propertyQE( pname )
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
110
			if attrname not in clsdict:
111
				clsdict[ attrname ] = EventSenderUI._UIEvent( ename, **event_kwargs )
114
		newcls = super( MetaClassCreatorUI, metacls ).__new__( _typetree, _uipackage,
115
																metacls, name, bases, clsdict )
117
		return newcls