def duplicateObject(name, dupName, t=None, r=None, s=None): """ duplicates object with name and assigns dupName to the copy :param name: string, object to duplicate :param dupName: string, name for the duplicated object :param t: translate tuple or None if to take from original :param r: rotation tuple or None if to take from original :param s: scale tuple or None if to take from original :return: tuple (int,string) the int will be - 0 if no problem occured - 1 if the original object could not be found - 2 if the name for the duplicate is already taken - 3 if the name was changed by the editor - 4 udk error, reason unknown if the return value is 3, the string will be the name the Editor assigned and None otherwise If the return value is 1 or 2, the calling function should change the name(s) and try again. If the return value is (3,string) the calling function must assign the returned name to the original object in the Program or find a new fitting name and assign it to the duplicated object using the :func:`renameObject` function with the returned string as name. .. seealso:: :func:`renameObject` :func:`getFreeName` """ # TODO: this function is very similar to renameObject, maybe common # functionality can be delegated to a common function # TODO: make checking for old object and free name optional, because # this should be handled inside the program before actually calling # this function (because they search for a free name anyway) # or should they stupidly call this function and make use of the return code # instead? (means call getFreeName in here and return that name to the caller) # renameObject(name, newName, bSecure = True, bGetBestNameIfNoFit = True) # bSecure = check if name is unused # bGetBestNameblah = let udk find an unused name # checking if an object exists in the Program function would be bad, because # it creates an unnecessary overhead as we have to "get" the object here anyway # check if the object we want to duplicate exists objInfo = getObjectInfoFromName(name) if objInfo is None: #print "Error: Duplication failed, original object could not be found." _lg.error("Duplication failed, original object could not be found.") return (1, None) # check if the newName is already taken selectByName(dupName) unrtext = getUnrealTextFromSelection(False) if unrtext is not None: #print "Error: name '%s' already taken" % dupName _lg.error("Name '%s' already taken" % dupName) return (2, None) objInfo.name = dupName if t is not None: objInfo.position = t if r is not None: objInfo.rotation = r if s is not None: objInfo.scale = s pasteFromObjectInfoList([ objInfo, ]) # check if name of pasted object is ok # TODO: this maybe optional, because it may take a lot of processing time # so maybe give the function a flag to skip this checking? # note: udk auto-selects newly pasted objects for us. chktext = getUnrealTextFromSelection(False) if chktext is None: #print "Error: pasting new object failed" _lg.error("Pasting new object failed") # TODO: we might try to paste back the initally cutted object here # or let the user hit undo? return (4, None) chkObj = udkParser.parseActor(chktext) if chkObj.name == dupName: # renaming succesfull, yeah! return (0, None) else: # the object was renamed! but the editor changed the name... _lg.warn("Editor returned a different name than desired " "('%s' instead of '%s')." % (chkObj.name, dupName)) return (3, chkObj.name)
def getObjectInfoFromName(name, cut=False): selectByName(name) unrtext = getUnrealTextFromSelection(cut) if unrtext is None: return None return udkParser.parseActor(unrtext)
def renameObject(name, newName): """try to assign a new name to an object. :param name: current name of the object :param newName: desired name for the object :return: tuple (bool,string) True if no problem occured, False otherwise if False, the string will be the name the Editor assigned or None if no renaming took place The problem with assigning new names to objects is, that the desired name may already be in use or otherwise invalid. This function will TRY to rename an object and check the name that the Editor actually assigned. If they are the same, None is returned, if they differ, the actual Editor-created name is returned. It is your responsibility to handle this discrepancy then by either trying to rename again with another name, assigning the returned name to the object in the Program, informing the user that he must enter another name etc. You may use the function :func:`getFreeName` to find a name that is unused in UDK prior to actually trying to rename the object. You should still check the returned value. .. warning: UDK will always use the given name, even if an object already has that name, as long as the name is valid. The old object will lose it's name. To make sure other objects don't lose their name, we will first ask UDK if the name we want to assign is already taken (we can check that by trying to select the object) and return early if it is. If not, we will still check if the name we wanted to assign actually ended up that way in UDK. .. warning: UDK seems to have a very non-restrictive name-policy, accepting nearly everything you throw at it, although it may break stuff! .. seealso:: :func:`getFreeName` """ # check if the object we want to rename exists selectByName(name) unrtext = getUnrealTextFromSelection(True) if unrtext is None: #print "Error: no object with name '%s' exists" % name _lg.error(("No object with name '%s' exists" % name)) # TODO: this maybe should not print an error, maybe a warning or only debug? return (False, None) # check if the newName is already taken selectByName(newName) unrtext = getUnrealTextFromSelection(False) if unrtext is not None: #print "Error: name '%s' already taken" % newName _lg.error("Name '%s' already taken" % newName) return (False, None) # change name and paste back to Udk objInfo = udkParser.parseActor(unrtext) objInfo.name = newName pasteFromObjectInfoList([ objInfo, ]) # check if name of pasted object is ok # TODO: this maybe optional, because it may take a lot of processing time # so maybe give the function a flag to skip this checking? # note: udk auto-selects newly pasted objects for us. chktext = getUnrealTextFromSelection(False) if chktext is None: #print "Error: pasting renamed object failed" _lg.error("Pasting renamed object failed") # TODO: we might try to paste back the initally cutted object here # or let the user hit undo? return (False, None) chkObj = udkParser.parseActor(chktext) if chkObj.name == newName: # renaming succesfull, yeah! return (True, None) else: # the object was renamed! but the editor changed the name... _lg.warn("Rename returned a different name than desired " "('%s' instead of '%s')." % (chkObj.name, newName)) return (False, chkObj.name)
def duplicateObject(name, dupName, t=None, r=None, s=None): """ duplicates object with name and assigns dupName to the copy :param name: string, object to duplicate :param dupName: string, name for the duplicated object :param t: translate tuple or None if to take from original :param r: rotation tuple or None if to take from original :param s: scale tuple or None if to take from original :return: tuple (int,string) the int will be - 0 if no problem occured - 1 if the original object could not be found - 2 if the name for the duplicate is already taken - 3 if the name was changed by the editor - 4 udk error, reason unknown if the return value is 3, the string will be the name the Editor assigned and None otherwise If the return value is 1 or 2, the calling function should change the name(s) and try again. If the return value is (3,string) the calling function must assign the returned name to the original object in the Program or find a new fitting name and assign it to the duplicated object using the :func:`renameObject` function with the returned string as name. .. seealso:: :func:`renameObject` :func:`getFreeName` """ # TODO: this function is very similar to renameObject, maybe common # functionality can be delegated to a common function # TODO: make checking for old object and free name optional, because # this should be handled inside the program before actually calling # this function (because they search for a free name anyway) # or should they stupidly call this function and make use of the return code # instead? (means call getFreeName in here and return that name to the caller) # renameObject(name, newName, bSecure = True, bGetBestNameIfNoFit = True) # bSecure = check if name is unused # bGetBestNameblah = let udk find an unused name # checking if an object exists in the Program function would be bad, because # it creates an unnecessary overhead as we have to "get" the object here anyway # check if the object we want to duplicate exists objInfo = getObjectInfoFromName(name) if objInfo is None: #print "Error: Duplication failed, original object could not be found." _lg.error("Duplication failed, original object could not be found.") return (1,None) # check if the newName is already taken selectByName(dupName) unrtext = getUnrealTextFromSelection(False) if unrtext is not None: #print "Error: name '%s' already taken" % dupName _lg.error("Name '%s' already taken" % dupName) return (2,None) objInfo.name = dupName if t is not None: objInfo.position = t if r is not None: objInfo.rotation = r if s is not None: objInfo.scale = s pasteFromObjectInfoList([objInfo,]) # check if name of pasted object is ok # TODO: this maybe optional, because it may take a lot of processing time # so maybe give the function a flag to skip this checking? # note: udk auto-selects newly pasted objects for us. chktext = getUnrealTextFromSelection(False) if chktext is None: #print "Error: pasting new object failed" _lg.error("Pasting new object failed") # TODO: we might try to paste back the initally cutted object here # or let the user hit undo? return (4,None) chkObj = udkParser.parseActor(chktext) if chkObj.name == dupName: # renaming succesfull, yeah! return (0,None) else: # the object was renamed! but the editor changed the name... _lg.warn("Editor returned a different name than desired " "('%s' instead of '%s')." % (chkObj.name, dupName)) return (3, chkObj.name)
def renameObject(name, newName): """try to assign a new name to an object. :param name: current name of the object :param newName: desired name for the object :return: tuple (bool,string) True if no problem occured, False otherwise if False, the string will be the name the Editor assigned or None if no renaming took place The problem with assigning new names to objects is, that the desired name may already be in use or otherwise invalid. This function will TRY to rename an object and check the name that the Editor actually assigned. If they are the same, None is returned, if they differ, the actual Editor-created name is returned. It is your responsibility to handle this discrepancy then by either trying to rename again with another name, assigning the returned name to the object in the Program, informing the user that he must enter another name etc. You may use the function :func:`getFreeName` to find a name that is unused in UDK prior to actually trying to rename the object. You should still check the returned value. .. warning: UDK will always use the given name, even if an object already has that name, as long as the name is valid. The old object will lose it's name. To make sure other objects don't lose their name, we will first ask UDK if the name we want to assign is already taken (we can check that by trying to select the object) and return early if it is. If not, we will still check if the name we wanted to assign actually ended up that way in UDK. .. warning: UDK seems to have a very non-restrictive name-policy, accepting nearly everything you throw at it, although it may break stuff! .. seealso:: :func:`getFreeName` """ # check if the object we want to rename exists selectByName(name) unrtext = getUnrealTextFromSelection(True) if unrtext is None: #print "Error: no object with name '%s' exists" % name _lg.error(("No object with name '%s' exists" % name)) # TODO: this maybe should not print an error, maybe a warning or only debug? return (False, None) # check if the newName is already taken selectByName(newName) unrtext = getUnrealTextFromSelection(False) if unrtext is not None: #print "Error: name '%s' already taken" % newName _lg.error("Name '%s' already taken" % newName) return (False,None) # change name and paste back to Udk objInfo = udkParser.parseActor(unrtext) objInfo.name = newName pasteFromObjectInfoList([objInfo,]) # check if name of pasted object is ok # TODO: this maybe optional, because it may take a lot of processing time # so maybe give the function a flag to skip this checking? # note: udk auto-selects newly pasted objects for us. chktext = getUnrealTextFromSelection(False) if chktext is None: #print "Error: pasting renamed object failed" _lg.error("Pasting renamed object failed") # TODO: we might try to paste back the initally cutted object here # or let the user hit undo? return (False,None) chkObj = udkParser.parseActor(chktext) if chkObj.name == newName: # renaming succesfull, yeah! return (True,None) else: # the object was renamed! but the editor changed the name... _lg.warn("Rename returned a different name than desired " "('%s' instead of '%s')." % (chkObj.name, newName)) return (False, chkObj.name)