Forum Moderators: Staff
Poser Python Scripting F.A.Q (Last Updated: 2024 Sep 18 2:50 am)
While this is correct, it can be a disaster if something goes wrong. If some other user is starting with this as a template and they change the code at line 18 to suit their purposes, it is easily possible that the program throws an exception (due to some temporary mistake on the user's part) which will result in the loss of all the file contents after the start tag.
It would be safer to open a DIFFERENT file for writing, write all the lines and close it, and ONLY THEN perform a delete and a couple file renames to safeguard that the original file BEFORE AND AFTER the replacement is retained, and that if a new content isn't produced properly, the original is not touched at all.
For similar reasons the f.close() should not be explicitly done by the program. Instead, perform the temp file production with a "with" statement. That will ensure that even if the program aborts, the file is properly closed. Otherwise, the Poser process will continue to have the file "open" and it may be difficult for the user to delete the file.
Renderosity forum reply notifications are wonky. If I read a follow-up in a thread, but I don't myself reply, then notifications no longer happen AT ALL on that thread. So if I seem to be ignoring a question, that's why. (Updated September 23, 2019)
Because I'm lazy, I'm screen grabbing from some other python tutorial.
You do the same on the write as on the read. You'll never accidentally leave a file open that way.
Renderosity forum reply notifications are wonky. If I read a follow-up in a thread, but I don't myself reply, then notifications no longer happen AT ALL on that thread. So if I seem to be ignoring a question, that's why. (Updated September 23, 2019)
This article explains in more detail https://stackoverflow.com/questions/1812115/how-to-safely-write-to-a-file
Renderosity forum reply notifications are wonky. If I read a follow-up in a thread, but I don't myself reply, then notifications no longer happen AT ALL on that thread. So if I seem to be ignoring a question, that's why. (Updated September 23, 2019)
Many thanks, especially for the advice on closing files properly if using this method. I'm assuming the advanced user has of course made a safe backup of the target file. I did think of going further and having the script:
But since changing the XML does not affect Poser's UI when running (I assume it's loaded at start and then resides in memory), there seemed no point going further. But obviously if someone had need for this sort of search-and-replace between X and Y, then they might put those sort of safety measure in place.
Learn the Secrets of Poser 11 and Line-art Filters.
This site uses cookies to deliver the best experience. Our own cookies make user accounts and other features possible. Third-party cookies are used to display relevant ads and to analyze how Renderosity is used. By using our site, you acknowledge that you have read and understood our Terms of Service, including our Cookie Policy and our Privacy Policy.
This script is working with Poser 11, and is a Python 2.7 script to replace a string of multiline text between two named delimiters. In this case the delimiters are imaginary XML tags, and the replacement that is being made between them is a multiline block. The script shows how to correctly format this block in Python, so as to replace the existing block without mangling the XML indenting and line-wrapping.
Does not appear to work on XML that is live and in use by Poser, as that has probably already been taken up into memory.
Not tested on other types of file, but I expect it may work on various Poser formats that are really only text files and are not actively in use and in-memory inside Poser.
Learn the Secrets of Poser 11 and Line-art Filters.