def __addActionsToMenuBar( self, tree, parent ): entityId = tree.getCurrentItemId() if entityId: entity = Hammer.getEntity( entityId ) actions = Hammer.getActions( entity ) arg = {} parent.clean() if actions: for action in actions: parent.add( Anvil.core.Button(name=action.__name__, cmd=Forge.core.Process.partial( action, entity, arg, self ), w=110) )
def listParentName( entity ): parent = Hammer.getEntity( entity['parentId'] ) self.path.append( parent['name'] + '/' ) if parent['entityType'] in ['Show', 'BaseEntity']: return listParentName( parent )
def __addActionsToMenuBar( self, tree, parent ): entityId = tree.getCurrentItemId() itemName = tree.getCurrentItemName() version = eval( itemName.split( ':' )[0] ) if entityId: entity = Hammer.getEntity( entityId ) entity.setVersion( version=version ) actions = Hammer.getActions( entity, 'Version' ) actions += Hammer.getActions( entity ) arg = {} parent.clean() if actions: for action in actions: if action.__name__ in [ 'approved', 'setCurrent', 'openScene', 'openSourceScene', 'get' ]: parent.add( Anvil.core.Button(name=action.__name__, cmd=Forge.core.Process.partial( action, entity, arg, self ), w=110) )
def _doAction( self, entity, arg, ui ): import nuke import Hammer self.returnValue = [] entitiesInfo = Hammer.Hnuke.Actions.GetEntitiesInfoFromScene( entity=entity, arg=arg, ui=ui ).returnValue for entityInfo in entitiesInfo: currentEntityId = entityInfo.knob('entityId').value() self.returnValue.append( Hammer.getEntity(currentEntityId) )
def _doAction(self, entity, arg, ui): import maya.cmds import Hammer self.returnValue = [] entitiesInfo = Hammer.Hmaya.Actions.GetEntitiesInfoFromScene(entity=entity, arg=arg, ui=ui).returnValue for entityInfo in entitiesInfo: currentEntityId = maya.cmds.getAttr("%s.entityId" % (entityInfo)) self.returnValue.append(Hammer.getEntity(currentEntityId))
def setHierarchyList( childrenIds, parentId ): for childrenId in childrenIds: child = Hammer.getEntity( childrenId ) entityType = child.getType() parent = None if parentId: parent = Hammer.getEntity( parentId ).getName() hierarchyList.append( { 'name':child.getName(), 'id':childrenId, 'iconPath':'../core/icon/%s%s.png' %( entityType[0].lower(), entityType[1:] ), 'tooltip':'%s | entityId:%s, name:%s, assetId:%s' %( child.getType(), child.getEntityId(), child.getName(), child.getAssetId() ), 'iconTooltip':'type:%s' %( child.getType() ), 'parent':parent, 'parentId':parentId, } ) subChildren = child.getChildrenId() child.getParentId() if subChildren: setHierarchyList( subChildren, childrenId )
def editEntity( self, entityId, version=None, approved=None ): import Hammer entity = Hammer.getEntity( entityId ) newVersion = entity['version'] if version: newVersion = version newApproved = entity['approved'] if approved: newApproved = approved # define data with open( self.db, 'r' ) as file: data = file.readlines() # grab entity entityLine = self.__getDbLine( entity['entityId'] ) data[entityLine] = str({ 'entityId':entity['entityId'], 'name':entity['name'], 'type':entity['entityType'], 'path':entity['path'], 'version':newVersion, 'descriptions':entity['descriptions'], 'approved':newApproved, 'currentUser':entity['currentUser'], 'parentId':entity['parentId'], 'childrenId':entity['childrenId'], 'copyId':entity['copyId'], 'referenceId':entity['referenceId'], 'assetId':entity['assetId'], 'masterAssetId':entity['masterAssetId'], 'dependencyId':entity['dependencyId'], 'bundleId':entity['bundleId'], 'sources':entity['sources'], }) + '\n' with open( self.db, 'w' ) as file: file.writelines( data )
def upFolder( arg ): import Hammer if arg._currentEntityId != 1: # set current entityId to parentId entity = Hammer.getEntity( arg._currentEntityId ) arg._currentEntityId = entity.getParentId() # remove last folder in the urlPath field folders = arg.textfield_urlPath.getValue().split( '/' ) for i in [0,-2,-1]: del folders[i] path = '/' for folder in folders: path += '%s/' %(folder) arg.textfield_urlPath.setValue( path ) # rebuild tree arg._buildTreeEntity( entityIdOverride=True )
def _buildTreeEntity( self, entityIdOverride=False ): if not entityIdOverride: self._currentEntityId = self.__getEntityIdFromPath() entity = Hammer.getEntity( self._currentEntityId ) hierarchyList = [] def setHierarchyList( childrenIds, parentId ): for childrenId in childrenIds: child = Hammer.getEntity( childrenId ) entityType = child.getType() parent = None if parentId: parent = Hammer.getEntity( parentId ).getName() hierarchyList.append( { 'name':child.getName(), 'id':childrenId, 'iconPath':'../core/icon/%s%s.png' %( entityType[0].lower(), entityType[1:] ), 'tooltip':'%s | entityId:%s, name:%s, assetId:%s' %( child.getType(), child.getEntityId(), child.getName(), child.getAssetId() ), 'iconTooltip':'type:%s' %( child.getType() ), 'parent':parent, 'parentId':parentId, } ) subChildren = child.getChildrenId() child.getParentId() if subChildren: setHierarchyList( subChildren, childrenId ) setHierarchyList( entity.getChildrenId(), None ) # tree init self.tree_hierarchy.add( hierarchyList )
def _buildTreeEntity( self, entityId ): entity = Hammer.getEntity( entityId ) hierarchyList = [] # versions init for version in range( len(entity['approved']) ): version = version + 1 if entity['approved'][version]: iconType = 'approved' else: iconType = 'unapproved' if version == entity['version']: iconType += 'Actif' source = '' if entity['source']: source = entity['source'][version] hierarchyList.append( { 'name':' %i : %s' %( version, entity['descriptions'][version] ), 'id':entity['entityId'], 'iconPath':'../core/icon/%s.png' %( iconType ), 'tooltip':'source : %s ' %(source), 'iconTooltip':'', 'parent':None, 'parentId':None, } ) # tree init self.tree_hierarchy.add( hierarchyList )
def listAllChildren( parentId ): childrens = Hammer.getEntity( parentId ).getChildrenId() if childrens: for children in childrens: childrenList.append( children ) listAllChildren( children )
def menuBar( self, tree ): entityId = tree.getCurrentItemId() if entityId: entity = Hammer.getEntity( entityId ) print entity.getName()
def checkMatch( entityId, name ): childrenIds = Hammer.getEntity( entityId ).getChildrenId() for childrenId in childrenIds: if Hammer.getEntity( childrenId ).getName() == name: return childrenId return None
Here, se see that there are two *instance* attributes: `is_tool` and `tool_type`. The class `Tool` specifies that any `Tool` will have the value `True` for `is_tool` and `None` for `tool_type`, as this is the more general class. There's also the `Tool` method `use_tool`, which, for a given instance, when executed will print `'Using tool.'` As previously, we can initialize a `Tool` type object, storing it in `my_tool`. We can then access the attributes of `Tool` and carry out any `Tool` methods: # access attribute my_tool = Tool() my_tool.is_tool # use method my_tool.use_tool() However, what we're most interested in here is the concept of **inheritance**. In the code below, we see how you can inherit `Tool` properties within a new `class` object `Hammer`. By passing `Tool` in when creating the `class` `Hammer`, `Hammer`-type objects will have the attributes and methods specifed within `Tool`. We also see with the code here that we're specifying that `tool_type` for a `Hammer` will be `'Hammer'` and provide an additional instance attribute `why`, giving the `Hammer` a purpose - `'To hammer things.'` class Hammer(Tool): #inherit Tool class def __init__(self): self.tool_type = 'Hammer' self.why = 'To hammer things.' We can now create a `Hammer`-type object and use the methods from `Tool` - such as `use_tool`- since that is inherited. # Hammer has Tool attributes and methods my_hammer = Hammer() my_hammer.use_tool() And, we can also access the `Hammer`-specific attributes: # Hammer has Hammer attributes my_hammer.why However, you'll note that `Hammer` does *not* have access to the *attributes* from `Tool`: # this code will error my_hammer.is_tool This is becuase our `Hammer` `class` defines its own *instance attributes*, overriding the instance attributes of the inherited class. We'll see in just a second how to avoid this using `super()`. Further, note that inheritance is a one-way street. If you created a `Tool` class object (which is the parent class here) and does *not* inherit from `Hammer`, it only has access to `Tool` attributes and methods, so the following would execute without issue: # Tool objects only have Tool attributes & methods my_tool = Tool() my_tool.is_tool However, if you tried to access the `why` attribute on a `Tool` type object, you would encounter an `AttributeError`, as there is no attribute `why` in the `Tool` class object: # Tool does NOT inherit from Hammer # this code will produce an error my_tool.why Note in the example above that `tool_type` of `Hammer` class overrides the `tool_type` specified in the `Tool` class that it inherits. The `class` definition can and will override attributes and methods defined in the Parent class. ### `super()` `super()` allows you to refer to the inherited, parent class, without naming it specifically. This is particularly helpful when you want to extend the functionality of a class from which you are inheriting. In the example above, we inherited from `Tool` in our `Hammer` definition and saw that while the `Tool` method were availalbe, the `Tool` instance attraibutes were *not*, due to the fact that our `Hammer` definition had overridden the `Tool` instance attribute definition. So...how do we avoid that? This is where `super()` comes in. In the example below, we've added a single line of code (`super().__init__()`) to our `Hammer` definition from above. This specifies that we want to inherit the instance attributes from our parent class `Tool`. class Hammer(Tool): #inherit Tool class