博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

Hiero中修改BinView中binitem对象名字的方法

Posted on 2015-10-10 18:54  SolHe  阅读(341)  评论(0编辑  收藏  举报

之前博客提到了scan for more version这样一个功能,该功能会放宽查询条件,这就会导致BinItem的名称与activeVersion的名称不符。这篇博客提供了一个方法去统一名称。

该方法直接修改xml工程并关闭hiero,重新打开的时候就会发现binitem对象的名称全部都修改成当前activeversion的名称了,因为需要重启一下hiero,所以该功能还显得不完善,想要改善这个功能还需要好好学习PySide。

代码如下:

#################################################################################################################################

 

import hiero.core
import xml.dom.minidom as xml
import re
import hiero.core
from PySide.QtGui import *
from PySide.QtCore import *

class fixname_MenuAction:

    def __init__(self):

        self._binViewActionAddNoteToClipSequence = self.createMenuAction("Correct Name (VHQ)", self.Modify_XML)
        #add a events when left click the item
        hiero.core.events.registerInterest("kShowContextMenu/kBin", self.eventHandler)

    #This function is using to get the items from objects
    def get_item_list(self):
        list_a = [];list_A = [];indexlist = [];
        for item in hiero.core.projects()[-1].clipsBin().items():
            if isinstance(item,hiero.core.BinItem):
                list_a.append(item.activeItem().name())
            else:
                [list_a.append(items.activeItem().name()) for items in item.items()]
        for item in list_a:
            if re.match('(\w+)(_[vV]\d+)',item):
                list_A.append(re.match('(\w+)(_[vV]\d+)',item).group(1))
            else:
                list_A.append(item)
        return list_A

    
    #This function is using to get the items from xml
    def get_xml_list(self):
        #second way to get item rootname
        context = self.get_xml_context()
        list_b = [item.getAttribute('name') for item in context.getElementsByTagName('SequenceProjectItemRoot')]
        return list_b


    #This function is using to get the context of xml file
    def get_xml_context(self):
        xmlfile = xml.parse(hiero.core.projects()[-1].path())
        context = xmlfile.documentElement
        return context


    #This function is using to get the difference item's index in project
    def find_difference(self):
        indexlist = []
        list_A = self.get_item_list()
        list_B = self.get_xml_list()
        #Try to compare two list
        for i in range(len(list_A)):
            if list_A[i] != list_B[i]:
                indexlist.append(i)
        return indexlist



    #This function is using to modify the xml file
    def Modify_XML(self):
        import sys
        reload(sys)
        sys.setdefaultencoding('utf-8')


        #save the project
        hiero.core.projects()[-1].save()
    
        #get parameter
        indexlist = self.find_difference()
    
        xmlfile = xml.parse(hiero.core.projects()[-1].path())
        context = xmlfile.documentElement
    
        #modify xml
        for index in indexlist:
            targetvalue = self.get_item_list()[index]
            print context.getElementsByTagName('SequenceProjectItemRoot')[index].getAttribute('name')
            context.getElementsByTagName('SequenceProjectItemRoot')[index].setAttribute('name',targetvalue)
            print context.getElementsByTagName('SequenceProjectItemRoot')[index].getAttribute('name')


        xmldata = open(hiero.core.projects()[-1].path(),'wb')
        #xmlrecode = codecs.lookup('utf-8')[3](xmldata)
        
        #xmlfile.writexml(xmlrecode,encoding = 'utf-8')
        
        #xmlfile.unlink()
        xmldata.write(xmlfile.toxml())
        xmldata.close()
    
        sys.setdefaultencoding('ASCII')
        hiero.core.quit()

        #del sys,xml,re,hiero.core



    def createMenuAction(self, title, method):
        action = QAction(title,None)
        action.triggered.connect( method )
        return action



    def eventHandler(self, event):
        self._binViewActionAddNoteToClipSequence.setEnabled(True)
        event.menu.addAction(self._binViewActionAddNoteToClipSequence)


# Instantiate the action to get it to register itself.
action = fixname_MenuAction()