osdir.com


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Newbie question about Python syntax


On 23Aug2019 13:49, Paul St George <email at paulstgeorge.com> wrote:
>Context:
>I am using Python to interrogate the value of some thing in Blender 
>(just as someone else might want to use Python to look at an email in 
>a Mail program or an image in Photoshop).
>
>Assumptions:
>So, I want to look at the attribute of an instance of a class called 
>CompositorNodeMapValue. The code in the Python tutorial seems to be 
>for creating an instance of a class, but I assume Blender (in my case) 
>has already created the instance that I want to interrogate.

That would be the expectation. And to interrogate it, you need that 
instance to hand in a variable.

>Question:
>If this is so, should I find the code to get a list of the instances 
>that have been made (maybe using inspect?) and then, when I have its 
>name, the attributes of the one that interests me?

Have you not got one of these handed to you from something?

Or are you right at the outside with some "opaque" blender handle or 
something? (Disclaimer: I've never used Blender.)

You can inspect objects with the inspect module. You can also be more 
direct. Given an object "o", you can do an assortment of things:

dir(o) gets a list of its attribute names.

help(o) prints out the docstring, somewhat rendered.

o.__dict__ is usually a dict mapping attribute names to their values.

type(o) gets you its type, so "print(type(o))" or 
"print(type(o).__name__)" can be handy.

A crude probe function (untested):

  def probe(o):
    print(o)
    for attr, value in sorted(o.__dict__.items()):
      print(" ", attr, type(value).__name__, value)

Enjoy,
Cameron Simpson <cs at cskk.id.au> (formerly cs at zip.com.au)

"Are we alpinists, or are we tourists" followed by "tourists! tourists!"
        - Kobus Barnard <kobus at cs.sfu.ca> in rec.climbing,
          on things he's heard firsthand