Skip to content

Format Custom Messages

maybites edited this page Feb 7, 2021 · 9 revisions

NodeOsc has from Version 2 onwards the ability to dynamically format the data-path string.

To expand on the previous example and working with an osc message whose arguments are looking the same:

/objects/Variable/location Cube 10.0 15.2 -5.3

We can see we receive the message on /objects/Variable/location.

However, now we want to use one of the arguments to format the data-path. For this we have to enable 'Format' and the new format-field appears.

Relying on Pythons string format() method, we can now take the arguments tuple to format the data-path. Its worthwhile to have a closer loop at this reference.

You can see the format-field says args, which stands for the arguments tuple.

And looking at the data-path more closely: bpy.data.objects['{0}'].location, there is a {0} inside the brackets. Which means we take the first element of the arguments tuple (in our case 'Cube') and put it inside the brackets, while making sure we don't forget the '' on both ends.

the following lines illustrates in python code what NodeOsc is doing behind the scene:

# it creates a variable 'args' containing the tuple from the arguments
args = ('Cube', 10.0, 15.2, -5.3)

# it evaluate the format used in the next step:
formatTuple = eval('args');

# it applies the format method on the datapath
datapath = "bpy.data.objects['{0}'].location".format(*formatTuple)

The resulting datapath that is used further on is:

bpy.data.objects['Cube'].location

the rest stays the same: the message contains 4 arguments and the positional information are on the indices 1, 2 and 3, args[idx] is set to (1, 2, 3)

but we can do even better -> Loop-Custom-Messages