Forum: Poser Python Scripting


Subject: Name of the file PZ3

minuitdixhuit opened this issue on Dec 15, 2021 ยท 7 posts


minuitdixhuit posted Wed, 15 December 2021 at 4:56 AM

Hello,
Is it possible in a python script to get the name of the current PZ3 file?

Thanks for your help.

M18


structure posted Wed, 15 December 2021 at 6:56 AM Forum Coordinator

import poser
scene = poser.Scene()
document = scene.DocumentPath()
if document is None:
    document = "Untitled"

Locked Out


minuitdixhuit posted Wed, 15 December 2021 at 9:24 AM

Maaaaaaaaaaaaaaaaaaany Thaaaaaaaaaaaaaaaaaaaaaaaaaaaaanks !

Work fine !


structure posted Sat, 18 December 2021 at 9:06 AM Forum Coordinator

another way 

import poser
scene = poser.Scene()

def get_document(scene):
    return scene.DocumentPath() if not scene.DocumentPath() is None else "Untitled"

document=get_document(scene)

Locked Out


adp001 posted Sun, 19 December 2021 at 9:26 AM

structure posted at 9:06 AM Sat, 18 December 2021 - #4432058

another way 

import poser
scene = poser.Scene()

def get_document(scene):
    return scene.DocumentPath() if not scene.DocumentPath() is None else "Untitled"

document=get_document(scene)

Sorry for the " lecturer mode": but this is a good example of when you achieve a degradation with supposed code optimizations.

The function that returns the result is always called twice. Just to save one line of code.

The better (and more readable) way:

def get_document(scene):
  d = scene.DocumentPath()
  return d if d else "Untitled"

(In the end it's even less typed keys :))




adp001 posted Sun, 19 December 2021 at 9:42 AM

Ok, i missed that scene.DocunebtPath() can return None.

The last line should be:

  return d if d is not None and d else "Untitled"




adp001 posted Sun, 19 December 2021 at 10:01 AM

A test for None should always look like this:
  <statement> is None
or
  <statement> is not None.

But not:
  not <statement> is None

Why?

Once, because it is recommended by the Python documentation :).

And if you think about it, you will see that from <statement> the value is evaluated and then negated. The result is then checked for None (== None). If you write it correctly, the parameter is directly checked for != None. Which is of course more effective, and depending on what is behind <statement>, possibly also much faster.