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

2 thoughts on “Custom Key Value Pairs in VRED (Resolved

  1. Tobi says:

    Hi Elias, interessante Seite!

    bin ich grade drüber gestolpert… die lösung deines Problems sollte lediglich sein:

    fcontainer = material.fields().getMString(“key”)

    Like

    • Hey Tobi,

      sry ich hab deine Antwort zwar damals gelesen aber dann mich mit dem Thema auch nicht weiter beschäftigt.
      Ich finde leider keine Funktion getMString. Kann es sein das die erst in einer späteren API Version dazu kam?

      Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s