I'm using Blender 2.79 with Animation Nodes. This was compiled with Python 3.6 in Linux. I need to find out how to access ANStruct elements within a Python script (it's easy to access elements with the GUI). FYI, this revolves around simulating a stopwatch based on frames in an aircraft video.
I can easily use the Animation Nodes GUI to create three key/value pairs bound together as an "ANStruct" such that I can pass to Invoke Subprogram. This part is clear and works well. I'm combining a series of titled stopwatch start and stop frames into a list and then iterating through the list for individual titles. The picture below shows two Titles with StartFrame and StopFrame turned into a List with two items (each item is a matching struct).
Here is an excerpt of the start script code to explain:
import bpy
import sys
print("======================================")
print (sys.version + "\n")
scene = bpy.context.scene
frame = int(scene.frame_current)
last_frame = scene.frame_end
print("type of lap_list: ", type(lap_list), "\n")
print("lap_list (list with " + str(len(lap_list)) + " elements) content:\n", lap_list)
count = 0
for laps in lap_list:
count = count + 1
print("\n_____ lap ", count, " _______")
print("Type of this lap within all laps: ", type(laps))
print("Items of this lap within all laps: ", laps.items())
for key, value in laps:
print(" _____ key/value _____")
print(" Type of key: ", type(key))
print(" Type of value: ", type(value))
print(" Type of owner: ", type(laps))
print(" 1. key: ", key)
print(" 2. value: ", value)
try:
print(" 3. ALT key access \"laps[" + key + "]\" syntax value: ", laps[key])
except Exception as ex:
print(" 3. Failed ALT key access syntax \"laps[" + key + "]\" with exception: ", ex)
try:
print(" 4. ALT2 key access syntax \"laps[StartFrame]\" value: ", laps['StartFrame'], "\n")
except Exception as ex:
print(" 4. Failed ALT2 key access syntax \"laps[StartFrame]\" with exception: ", ex, "\n")
print("\n________________________")
Here is the output:
====================================== 3.6.5 (default, Apr 4 2018, 15:01:18) [GCC 7.3.1 20180303 (Red Hat 7.3.1-5)] type of lap_list: <class 'list'> lap_list (list with 2 elements) content: [<AN Struct: 'Title': AeleronRoll1, 'StartFrame': 75, 'StopFrame': 153 >, <AN Struct: 'Title': AeleronRoll2, 'StartFrame': 153, 'StopFrame': 232 >] _____ lap 1 _______ Type of this lap within all laps: <class 'animation_nodes.data_structures.struct.ANStruct'> Items of this lap within all laps: dict_items([(('Text', 'Title'), 'AeleronRoll1'), (('Integer', 'StartFrame'), 75), (('Integer', 'StopFrame'), 153)]) _____ key/value _____ Type of key: <class 'str'> Type of value: <class 'str'> Type of owner: <class 'animation_nodes.data_structures.struct.ANStruct'> 1. key: Text 2. value: Title 3. Failed ALT key access syntax "laps[Text]" with exception: 'Text' 4. Failed ALT2 key access syntax "laps[StartFrame]" with exception: 'StartFrame' _____ key/value _____ Type of key: <class 'str'> Type of value: <class 'str'> Type of owner: <class 'animation_nodes.data_structures.struct.ANStruct'> 1. key: Integer 2. value: StartFrame 3. Failed ALT key access syntax "laps[Integer]" with exception: 'Integer' 4. Failed ALT2 key access syntax "laps[StartFrame]" with exception: 'StartFrame' _____ key/value _____ Type of key: <class 'str'> Type of value: <class 'str'> Type of owner: <class 'animation_nodes.data_structures.struct.ANStruct'> 1. key: Integer 2. value: StopFrame 3. Failed ALT key access syntax "laps[Integer]" with exception: 'Integer' 4. Failed ALT2 key access syntax "laps[StartFrame]" with exception: 'StartFrame' _____ lap 2 _______ Type of this lap within all laps: <class 'animation_nodes.data_structures.struct.ANStruct'> Items of this lap within all laps: dict_items([(('Text', 'Title'), 'AeleronRoll2'), (('Integer', 'StartFrame'), 153), (('Integer', 'StopFrame'), 232)]) _____ key/value _____ Type of key: <class 'str'> Type of value: <class 'str'> Type of owner: <class 'animation_nodes.data_structures.struct.ANStruct'> 1. key: Text 2. value: Title 3. Failed ALT key access syntax "laps[Text]" with exception: 'Text' 4. Failed ALT2 key access syntax "laps[StartFrame]" with exception: 'StartFrame' _____ key/value _____ Type of key: <class 'str'> Type of value: <class 'str'> Type of owner: <class 'animation_nodes.data_structures.struct.ANStruct'> 1. key: Integer 2. value: StartFrame 3. Failed ALT key access syntax "laps[Integer]" with exception: 'Integer' 4. Failed ALT2 key access syntax "laps[StartFrame]" with exception: 'StartFrame' _____ key/value _____ Type of key: <class 'str'> Type of value: <class 'str'> Type of owner: <class 'animation_nodes.data_structures.struct.ANStruct'> 1. key: Integer 2. value: StopFrame 3. Failed ALT key access syntax "laps[Integer]" with exception: 'Integer' 4. Failed ALT2 key access syntax "laps[StartFrame]" with exception: 'StartFrame' ________________________
So it turns out that I am not truly accessing a dict in the Python script. The type I am accessing is: animation_nodes.data_structures.struct.ANStruct
In my specific example I need to find the value associated with keys "Title", "StartFrame", and "StopFrame". Unfortunately an ANStruct is not a dict, and yet somewhere within this struct the final content is apparently a dict...I don't know how to get to the dict of ANStruct through Python. Can anyone tell me how to access individual members of animation_nodes.data_structures.struct.ANStruct via Python?
EDIT: I see file "struct.py" of Animation Nodes defines the class:
from .. sockets.info import getCopyFunction
class ANStruct(dict):
def copyValues(self):
s = ANStruct()
for (dataType, name), value in self.items():
s[(dataType, name)] = getCopyFunction(dataType)(value)
return s
def findDataTypesWithName(self, name):
return [dataType for dataType, _name in self.keys() if name == _name]
def findNamesWithDataType(self, dataType):
return [name for _dataType, name in self.keys() if dataType == _dataType]
def __repr__(self):
elements = [repr(name) + ": " + str(value) for (_, name), value in self.items()]
return "<AN Struct: {} >".format(", ".join(elements))