Tue, Nov 19, 6:10 AM CST

Renderosity Forums / Poser Python Scripting



Welcome to the Poser Python Scripting Forum

Forum Moderators: Staff

Poser Python Scripting F.A.Q (Last Updated: 2024 Sep 18 2:50 am)

We now have a ProPack Section in the Poser FreeStuff.
Check out the new Poser Python Wish List thread. If you have an idea for a script, jot it down and maybe someone can write it. If you're looking to write a script, check out this thread for useful suggestions.

Also, check out the official Python site for interpreters, sample code, applications, cool links and debuggers. This is THE central site for Python.

You can now attach text files to your posts to pass around scripts. Just attach the script as a txt file like you would a jpg or gif. Since the forum will use a random name for the file in the link, you should give instructions on what the file name should be and where to install it. Its a good idea to usually put that info right in the script file as well.

Checkout the Renderosity MarketPlace - Your source for digital art content!



Subject: Replace a multiline XML string between two delimiters


HartyBart ( ) posted Wed, 08 September 2021 at 8:19 PM · edited Thu, 14 November 2024 at 4:19 AM

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.

working--replace-between.jpg

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.


bagginsbill ( ) posted Thu, 09 September 2021 at 1:37 PM · edited Thu, 09 September 2021 at 1:42 PM

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)


bagginsbill ( ) posted Thu, 09 September 2021 at 1:44 PM · edited Thu, 09 September 2021 at 1:45 PM

Because I'm lazy, I'm screen grabbing from some other python tutorial.

image.png

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)


bagginsbill ( ) posted Thu, 09 September 2021 at 1:47 PM


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)


HartyBart ( ) posted Thu, 09 September 2021 at 2:59 PM

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:

  1. extract a remote copy of the target file,
  2. duplicate and timestamp it so there's a safe backup,
  3. then do the replace,
  4. then paste the changed file back to overwrite the original.

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.


adp001 ( ) posted Fri, 10 September 2021 at 1:14 AM

Since you are reading the XML file completely into memory anyway, you might as well process the data in memory.

Like this:

Bildschirmfoto vom 2021-09-10 08-11-52.png




adp001 ( ) posted Fri, 10 September 2021 at 1:22 AM · edited Fri, 10 September 2021 at 1:23 AM

Use os.access(...) to check if the user is allowed to read/write that file:

if os.access(os.R_OK) ... # read ok

if os.access(os.W_OK) ... # write ok

if os.access(os.R_OK | os.W_OK) ... # read and write ok

https://docs.python.org/2.7/library/os.html#os.access




Privacy Notice

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.