Python, VRED

Custom Key Value Pairs in VRED (Resolved

Ok so I am trying for some time now to get something like custom attributes on a VRED node. I would like to use this as storage for meta data in the scene. But the VRED Python API, as always, does not give much help.

But so far I managed to store some information on an ValuePair attachment which is stored on every node in the AttachmentMap attachment.

vred_attachment_valuepair

RESOLVED:
Ok so apparently this was just an issue due to the old API (VRED 2016) where the function getMString was missing!

Two function to set and get key value pairs may look like this (fast prototype – no testing):
Note that this code only works in VRED > 2016

import vrFieldAccess
import vrNodeUtils

def get_value_pair(node, key):
    fields = node.fields()

    if not fields.hasAttachment("ValuePair"):
        raise IndexError("Node '{0}' has no key '{1}'"
                         .format(node.getName(), key))

    attachment = vrFieldAccess.vrFieldAccess(fields.getAttachment("ValuePair"))

    keys = attachment.getMString("key")

    if key in keys:
        return attachment.getMString("value")[keys.index(key)]

    raise IndexError("Node '{0}' has no key '{1}'"
                     .format(node.getName(), key))

def set_value_pair(node, key, value):
    fields = node.fields()

    if not fields.hasAttachment("ValuePair"):
        attachment = vrNodeUtils.createAttachment("ValuePair")
        fields.addAttachment(attachment)
        attachment = vrFieldAccess.vrFieldAccess(attachment)
    else:
        attachment = vrFieldAccess.vrFieldAccess(fields.getAttachment("ValuePair"))

    keys = attachment.getMString("key")
    values = attachment.getMString("value")

    if key in keys:
        values[keys.index(key)] = value
    else:
        keys.append(key)
        values.append(value)

    attachment.setMString("key", keys)
    attachment.setMString("value", values)

    return True
Advertisements
Standard