How to create create a Uniform PrimVar via python

127 views
Skip to first unread message

aurora

unread,
Feb 28, 2024, 5:01:40 PM2/28/24
to gaffer-dev
Is these a way, whether with Gaffer/IECore or USD Python APIs to create a Uniform type Prim Var and set different values to the indexes?
Cheers!
A

Daniel Dresser

unread,
Feb 29, 2024, 3:35:18 AM2/29/24
to gaffer-dev
You can create primvars using OSLObject, which can access the index of the primvar via `getattribute( "shading:index", myIntVar )`. I'll paste a little example network at the bottom of this message.

The thing that might be missing depending on what you need to do is an easy way to pipe array data into OSL from Python - we really ought to just support array inputs on OSLCode, I don't think this would be difficult.

This is all assuming you want to do this in a Gaffer network - if you just want to manipulate IECore objects in a Python script, you can certainly do that, and I'm sure the USD python API would also let you do that if you just want to manipulate USD files in a Python script.

-Daniel
Sample Network Follows:


import Gaffer
import GafferOSL
import GafferScene
import IECore
import imath

Gaffer.Metadata.registerValue( parent, "serialiser:milestoneVersion", 1, persistent=False )
Gaffer.Metadata.registerValue( parent, "serialiser:majorVersion", 3, persistent=False )
Gaffer.Metadata.registerValue( parent, "serialiser:minorVersion", 9, persistent=False )
Gaffer.Metadata.registerValue( parent, "serialiser:patchVersion", 0, persistent=False )

__children = {}

__children["Plane"] = GafferScene.Plane( "Plane" )
parent.addChild( __children["Plane"] )
__children["Plane"].addChild( Gaffer.V2fPlug( "__uiPosition", defaultValue = imath.V2f( 0, 0 ), flags = Gaffer.Plug.Flags.Default | Gaffer.Plug.Flags.Dynamic, ) )
__children["OSLObject"] = GafferOSL.OSLObject( "OSLObject" )
parent.addChild( __children["OSLObject"] )
__children["OSLObject"]["primitiveVariables"].addChild( Gaffer.NameValuePlug( "customInt", Gaffer.IntPlug( "value", defaultValue = 0, ), True, "primitiveVariable", Gaffer.Plug.Flags.Default | Gaffer.Plug.Flags.Dynamic ) )
__children["OSLObject"].addChild( Gaffer.V2fPlug( "__uiPosition", defaultValue = imath.V2f( 0, 0 ), flags = Gaffer.Plug.Flags.Default | Gaffer.Plug.Flags.Dynamic, ) )
__children["OSLCode"] = GafferOSL.OSLCode( "OSLCode" )
parent.addChild( __children["OSLCode"] )
__children["OSLCode"]["out"].addChild( Gaffer.IntPlug( "test", direction = Gaffer.Plug.Direction.Out, defaultValue = 0, flags = Gaffer.Plug.Flags.Default | Gaffer.Plug.Flags.Dynamic, ) )
__children["OSLCode"].addChild( Gaffer.V2fPlug( "__uiPosition", defaultValue = imath.V2f( 0, 0 ), flags = Gaffer.Plug.Flags.Default | Gaffer.Plug.Flags.Dynamic, ) )
__children["PathFilter"] = GafferScene.PathFilter( "PathFilter" )
parent.addChild( __children["PathFilter"] )
__children["PathFilter"].addChild( Gaffer.V2fPlug( "__uiPosition", defaultValue = imath.V2f( 0, 0 ), flags = Gaffer.Plug.Flags.Default | Gaffer.Plug.Flags.Dynamic, ) )
__children["Plane"]["dimensions"]["x"].setValue( 4.0 )
__children["Plane"]["dimensions"]["y"].setInput( __children["Plane"]["dimensions"]["x"] )
__children["Plane"]["divisions"]["x"].setValue( 4 )
__children["Plane"]["divisions"]["y"].setInput( __children["Plane"]["divisions"]["x"] )
__children["Plane"]["__uiPosition"].setValue( imath.V2f( 44.8000031, 23.2999992 ) )
__children["OSLObject"]["in"].setInput( __children["Plane"]["out"] )
__children["OSLObject"]["filter"].setInput( __children["PathFilter"]["out"] )
__children["OSLObject"]["interpolation"].setValue( 2 )
__children["OSLObject"]["primitiveVariables"]["primitiveVariable"]["name"].setValue( 'test' )
__children["OSLObject"]["primitiveVariables"]["primitiveVariable"]["value"].setInput( __children["OSLCode"]["out"]["test"] )
__children["OSLObject"]["__uiPosition"].setValue( imath.V2f( 44.8000031, 15.1359367 ) )
__children["OSLCode"]["code"].setValue( 'int index;\ngetattribute( "shading:index", index );\n\ntest = index;' )
__children["OSLCode"]["__uiPosition"].setValue( imath.V2f( 27.8000011, 15.7359371 ) )
__children["PathFilter"]["paths"].setValue( IECore.StringVectorData( [ '/plane' ] ) )
__children["PathFilter"]["__uiPosition"].setValue( imath.V2f( 57.8000031, 21.2179699 ) )


del __children


aurora

unread,
Feb 29, 2024, 11:20:57 AM2/29/24
to gaffer-dev
Nice!
Thank you Daniel.
A

aurora

unread,
Feb 29, 2024, 3:19:28 PM2/29/24
to gaffer-dev
Ok need a bit more help. This is literally my first OSL adventure.
Trying to assign a uniform string value to certain faces. This example crashes Gaffer when the commented section is enabled. 

import Gaffer
import GafferOSL
import GafferScene
import IECore
import imath

Gaffer.Metadata.registerValue( parent, "serialiser:milestoneVersion", 1, persistent=False )

Gaffer.Metadata.registerValue( parent, "serialiser:majorVersion", 4, persistent=False )
Gaffer.Metadata.registerValue( parent, "serialiser:minorVersion", 0, persistent=False )


Gaffer.Metadata.registerValue( parent, "serialiser:patchVersion", 0, persistent=False )

__children = {}

__children["Plane"] = GafferScene.Plane( "Plane" )
parent.addChild( __children["Plane"] )
__children["Plane"].addChild( Gaffer.V2fPlug( "__uiPosition", defaultValue = imath.V2f( 0, 0 ), flags = Gaffer.Plug.Flags.Default | Gaffer.Plug.Flags.Dynamic, ) )

__children["PathFilter2"] = GafferScene.PathFilter( "PathFilter2" )
parent.addChild( __children["PathFilter2"] )
__children["PathFilter2"].addChild( Gaffer.V2fPlug( "__uiPosition", defaultValue = imath.V2f( 0, 0 ), flags = Gaffer.Plug.Flags.Default | Gaffer.Plug.Flags.Dynamic, ) )
__children["OSLCode2"] = GafferOSL.OSLCode( "OSLCode2" )
parent.addChild( __children["OSLCode2"] )
__children["OSLCode2"]["parameters"].addChild( Gaffer.StringPlug( "test_string", defaultValue = '', flags = Gaffer.Plug.Flags.Default | Gaffer.Plug.Flags.Dynamic, ) )
__children["OSLCode2"]["parameters"].addChild( Gaffer.StringPlug( "int_list", defaultValue = '', flags = Gaffer.Plug.Flags.Default | Gaffer.Plug.Flags.Dynamic, ) )
__children["OSLCode2"]["out"].addChild( Gaffer.StringPlug( "string_out", direction = Gaffer.Plug.Direction.Out, defaultValue = '', flags = Gaffer.Plug.Flags.Default | Gaffer.Plug.Flags.Dynamic, ) )
__children["OSLCode2"].addChild( Gaffer.V2fPlug( "__uiPosition", defaultValue = imath.V2f( 0, 0 ), flags = Gaffer.Plug.Flags.Default | Gaffer.Plug.Flags.Dynamic, ) )
__children["OSLObject2"] = GafferOSL.OSLObject( "OSLObject2" )
parent.addChild( __children["OSLObject2"] )
__children["OSLObject2"]["primitiveVariables"].addChild( Gaffer.NameValuePlug( "customString", Gaffer.StringPlug( "value", defaultValue = '', flags = Gaffer.Plug.Flags.Default | Gaffer.Plug.Flags.Dynamic, ), True, "primitiveVariable", Gaffer.Plug.Flags.Default | Gaffer.Plug.Flags.Dynamic ) )
__children["OSLObject2"].addChild( Gaffer.V2fPlug( "__uiPosition", defaultValue = imath.V2f( 0, 0 ), flags = Gaffer.Plug.Flags.Default | Gaffer.Plug.Flags.Dynamic, ) )


__children["Plane"]["dimensions"]["x"].setValue( 4.0 )
__children["Plane"]["dimensions"]["y"].setInput( __children["Plane"]["dimensions"]["x"] )
__children["Plane"]["divisions"]["x"].setValue( 4 )
__children["Plane"]["divisions"]["y"].setInput( __children["Plane"]["divisions"]["x"] )

__children["Plane"]["__uiPosition"].setValue( imath.V2f( -51.8708267, -2.62412763 ) )
__children["PathFilter2"]["paths"].setValue( IECore.StringVectorData( [ '/plane' ] ) )
__children["PathFilter2"]["__uiPosition"].setValue( imath.V2f( -19.7563572, -10.466156 ) )
__children["OSLCode2"]["parameters"]["test_string"].setValue( 'test' )
__children["OSLCode2"]["parameters"]["int_list"].setValue( '1,2,3,4,5' )
__children["OSLCode2"]["code"].setValue( 'int index;\ngetattribute( "shading:index", index );\n\nstring_out = "";\n\nif (int_list != "")\n{\n\tstring int_array[5];\n\tsplit( int_list, int_array, "," );\n\n\tint start_index, end_index;\n\t\n\tstart_index = stoi(int_array[0]);\n\tend_index = stoi(int_array[-1]);\n\t\n\t// for (int n=start_index+1; n=end_index+1; n++){\n\t// \tif (n == index){\n\t// \t\tstring_out = test_string;\n\t// \t}\n\t// }\n}\n\n' )
__children["OSLCode2"]["__uiPosition"].setValue( imath.V2f( -71.8838272, -20.8342361 ) )
__children["OSLObject2"]["in"].setInput( __children["Plane"]["out"] )
__children["OSLObject2"]["filter"].setInput( __children["PathFilter2"]["out"] )
__children["OSLObject2"]["interpolation"].setValue( 2 )
__children["OSLObject2"]["primitiveVariables"]["primitiveVariable"]["value"].setInput( __children["OSLCode2"]["out"]["string_out"] )
__children["OSLObject2"]["__uiPosition"].setValue( imath.V2f( -51.8708267, -21.4342365 ) )


del __children

aurora

unread,
Mar 1, 2024, 12:03:18 PM3/1/24
to gaffer-dev

Reporting some success after more help from Daniel.
A bit ugly, but happy is working.


import Gaffer
import GafferOSL
import GafferScene
import IECore
import imath

Gaffer.Metadata.registerValue( parent, "serialiser:milestoneVersion", 1, persistent=False )
Gaffer.Metadata.registerValue( parent, "serialiser:majorVersion", 4, persistent=False )
Gaffer.Metadata.registerValue( parent, "serialiser:minorVersion", 0, persistent=False )
Gaffer.Metadata.registerValue( parent, "serialiser:patchVersion", 0, persistent=False )

__children = {}

__children["Plane"] = GafferScene.Plane( "Plane" )
parent.addChild( __children["Plane"] )
__children["Plane"].addChild( Gaffer.V2fPlug( "__uiPosition", defaultValue = imath.V2f( 0, 0 ), flags = Gaffer.Plug.Flags.Default | Gaffer.Plug.Flags.Dynamic, ) )
__children["PathFilter2"] = GafferScene.PathFilter( "PathFilter2" )
parent.addChild( __children["PathFilter2"] )
__children["PathFilter2"].addChild( Gaffer.V2fPlug( "__uiPosition", defaultValue = imath.V2f( 0, 0 ), flags = Gaffer.Plug.Flags.Default | Gaffer.Plug.Flags.Dynamic, ) )
__children["OSLCode2"] = GafferOSL.OSLCode( "OSLCode2" )
parent.addChild( __children["OSLCode2"] )
__children["OSLCode2"]["parameters"].addChild( Gaffer.StringPlug( "test_string", defaultValue = '', flags = Gaffer.Plug.Flags.Default | Gaffer.Plug.Flags.Dynamic, ) )
__children["OSLCode2"]["parameters"].addChild( Gaffer.StringPlug( "int_list", defaultValue = '', flags = Gaffer.Plug.Flags.Default | Gaffer.Plug.Flags.Dynamic, ) )
__children["OSLCode2"]["parameters"].addChild( Gaffer.IntPlug( "array_length", defaultValue = 0, flags = Gaffer.Plug.Flags.Default | Gaffer.Plug.Flags.Dynamic, ) )

__children["OSLCode2"]["out"].addChild( Gaffer.StringPlug( "string_out", direction = Gaffer.Plug.Direction.Out, defaultValue = '', flags = Gaffer.Plug.Flags.Default | Gaffer.Plug.Flags.Dynamic, ) )
__children["OSLCode2"].addChild( Gaffer.V2fPlug( "__uiPosition", defaultValue = imath.V2f( 0, 0 ), flags = Gaffer.Plug.Flags.Default | Gaffer.Plug.Flags.Dynamic, ) )
__children["OSLObject2"] = GafferOSL.OSLObject( "OSLObject2" )
parent.addChild( __children["OSLObject2"] )
__children["OSLObject2"]["primitiveVariables"].addChild( Gaffer.NameValuePlug( "customString", Gaffer.StringPlug( "value", defaultValue = '', flags = Gaffer.Plug.Flags.Default | Gaffer.Plug.Flags.Dynamic, ), True, "primitiveVariable", Gaffer.Plug.Flags.Default | Gaffer.Plug.Flags.Dynamic ) )
__children["OSLObject2"].addChild( Gaffer.V2fPlug( "__uiPosition", defaultValue = imath.V2f( 0, 0 ), flags = Gaffer.Plug.Flags.Default | Gaffer.Plug.Flags.Dynamic, ) )
__children["Expression8"] = Gaffer.Expression( "Expression8" )
parent.addChild( __children["Expression8"] )
__children["Expression8"]["__in"].addChild( Gaffer.StringPlug( "p0", defaultValue = '', flags = Gaffer.Plug.Flags.Default | Gaffer.Plug.Flags.Dynamic, ) )
__children["Expression8"]["__out"].addChild( Gaffer.IntPlug( "p0", direction = Gaffer.Plug.Direction.Out, defaultValue = 0, flags = Gaffer.Plug.Flags.Default | Gaffer.Plug.Flags.Dynamic, ) )
__children["Expression8"].addChild( Gaffer.V2fPlug( "__uiPosition", defaultValue = imath.V2f( 0, 0 ), flags = Gaffer.Plug.Flags.Default | Gaffer.Plug.Flags.Dynamic, ) )

__children["Plane"]["dimensions"]["x"].setValue( 4.0 )
__children["Plane"]["dimensions"]["y"].setInput( __children["Plane"]["dimensions"]["x"] )
__children["Plane"]["divisions"]["x"].setValue( 4 )
__children["Plane"]["divisions"]["y"].setInput( __children["Plane"]["divisions"]["x"] )
__children["Plane"]["__uiPosition"].setValue( imath.V2f( -51.8708267, -2.66945958 ) )

__children["PathFilter2"]["paths"].setValue( IECore.StringVectorData( [ '/plane' ] ) )
__children["PathFilter2"]["__uiPosition"].setValue( imath.V2f( -19.7563572, -10.466156 ) )
__children["OSLCode2"]["parameters"]["test_string"].setValue( 'test' )
__children["OSLCode2"]["parameters"]["int_list"].setValue( '1,4,5,7,8,9' )
__children["OSLCode2"]["parameters"]["array_length"].setInput( __children["Expression8"]["__out"]["p0"] )
__children["OSLCode2"]["code"].setValue( 'int index;\ngetattribute( "shading:index", index );\n\nstring_out = "";\n\nif (int_list != "")\n\n{\n\tstring int_array[50];\n\tsplit( int_list, int_array, "," );\n\tint start_index, end_index;\n\tstart_index = stoi(int_array[0]);\n\tfor (int n=start_index; n<array_length; n++){\n\t\tstring cur_index = int_array[n];\n\t\tif (stoi(cur_index) == index){\n\t\t\tstring_out = test_string;\n\t\t}\n\t}\n}' )
__children["OSLCode2"]["__uiPosition"].setValue( imath.V2f( -71.2491608, -21.4342365 ) )

__children["OSLObject2"]["in"].setInput( __children["Plane"]["out"] )
__children["OSLObject2"]["filter"].setInput( __children["PathFilter2"]["out"] )
__children["OSLObject2"]["interpolation"].setValue( 2 )
__children["OSLObject2"]["primitiveVariables"]["primitiveVariable"]["value"].setInput( __children["OSLCode2"]["out"]["string_out"] )
__children["OSLObject2"]["__uiPosition"].setValue( imath.V2f( -51.8708267, -21.4342365 ) )
__children["Expression8"]["__in"]["p0"].setInput( __children["OSLCode2"]["parameters"]["int_list"] )
__children["Expression8"]["__uiPosition"].setValue( imath.V2f( -82.2778931, -22.2639465 ) )
__children["Expression8"]["__engine"].setValue( 'python' )
__children["Expression8"]["__expression"].setValue( 'indices = parent["__in"]["p0"]\nparent["__out"]["p0"] = len(indices.split(","))' )


del __children

Daniel Dresser

unread,
Mar 1, 2024, 1:04:16 PM3/1/24
to gaffer-dev
The part of my message which I accidentally sent to Aurora instead of to the list which I should repeat:

Using string tokenizing to pass a vector here is a clever hack, but not efficient. We really need to add support for vector input types on OSLCode so this could be done properly - I think this will be fairly easy.

-Daniel

aurora

unread,
Mar 1, 2024, 1:35:22 PM3/1/24
to gaffer-dev
Thanks again Daniel... now... how about this one?
Can I be this dynamic with OSLObject?


import Gaffer
import GafferOSL
import GafferScene
import IECore
import imath

Gaffer.Metadata.registerValue( parent, "serialiser:milestoneVersion", 1, persistent=False )
Gaffer.Metadata.registerValue( parent, "serialiser:majorVersion", 4, persistent=False )
Gaffer.Metadata.registerValue( parent, "serialiser:minorVersion", 0, persistent=False )
Gaffer.Metadata.registerValue( parent, "serialiser:patchVersion", 0, persistent=False )

__children = {}

__children["Plane"] = GafferScene.Plane( "Plane" )
parent.addChild( __children["Plane"] )
__children["Plane"].addChild( Gaffer.V2fPlug( "__uiPosition", defaultValue = imath.V2f( 0, 0 ), flags = Gaffer.Plug.Flags.Default | Gaffer.Plug.Flags.Dynamic, ) )
__children["PathFilter2"] = GafferScene.PathFilter( "PathFilter2" )
parent.addChild( __children["PathFilter2"] )
__children["PathFilter2"].addChild( Gaffer.V2fPlug( "__uiPosition", defaultValue = imath.V2f( 0, 0 ), flags = Gaffer.Plug.Flags.Default | Gaffer.Plug.Flags.Dynamic, ) )
__children["OSLCode3"] = GafferOSL.OSLCode( "OSLCode3" )
parent.addChild( __children["OSLCode3"] )
__children["OSLCode3"]["parameters"].addChild( Gaffer.StringPlug( "test_string", defaultValue = '', flags = Gaffer.Plug.Flags.Default | Gaffer.Plug.Flags.Dynamic, ) )
__children["OSLCode3"]["parameters"].addChild( Gaffer.StringPlug( "int_list", defaultValue = '', flags = Gaffer.Plug.Flags.Default | Gaffer.Plug.Flags.Dynamic, ) )
__children["OSLCode3"]["parameters"].addChild( Gaffer.IntPlug( "array_length", defaultValue = 0, flags = Gaffer.Plug.Flags.Default | Gaffer.Plug.Flags.Dynamic, ) )
__children["OSLCode3"]["out"].addChild( Gaffer.StringPlug( "string_out", direction = Gaffer.Plug.Direction.Out, defaultValue = '', flags = Gaffer.Plug.Flags.Default | Gaffer.Plug.Flags.Dynamic, ) )
__children["OSLCode3"].addChild( Gaffer.V2fPlug( "__uiPosition", defaultValue = imath.V2f( 0, 0 ), flags = Gaffer.Plug.Flags.Default | Gaffer.Plug.Flags.Dynamic, ) )
__children["OSLObject3"] = GafferOSL.OSLObject( "OSLObject3" )
parent.addChild( __children["OSLObject3"] )
__children["OSLObject3"]["primitiveVariables"].addChild( Gaffer.NameValuePlug( "customString", Gaffer.StringPlug( "value", defaultValue = '', flags = Gaffer.Plug.Flags.Default | Gaffer.Plug.Flags.Dynamic, ), True, "primitiveVariable", Gaffer.Plug.Flags.Default | Gaffer.Plug.Flags.Dynamic ) )
__children["OSLObject3"].addChild( Gaffer.V2fPlug( "__uiPosition", defaultValue = imath.V2f( 0, 0 ), flags = Gaffer.Plug.Flags.Default | Gaffer.Plug.Flags.Dynamic, ) )
__children["Expression10"] = Gaffer.Expression( "Expression10" )
parent.addChild( __children["Expression10"] )
__children["Expression10"]["__in"].addChild( Gaffer.StringPlug( "p0", defaultValue = '', flags = Gaffer.Plug.Flags.Default | Gaffer.Plug.Flags.Dynamic, ) )
__children["Expression10"]["__out"].addChild( Gaffer.IntPlug( "p0", direction = Gaffer.Plug.Direction.Out, defaultValue = 0, flags = Gaffer.Plug.Flags.Default | Gaffer.Plug.Flags.Dynamic, ) )
__children["Expression10"].addChild( Gaffer.V2fPlug( "__uiPosition", defaultValue = imath.V2f( 0, 0 ), flags = Gaffer.Plug.Flags.Default | Gaffer.Plug.Flags.Dynamic, ) )
__children["Expression11"] = Gaffer.Expression( "Expression11" )
parent.addChild( __children["Expression11"] )
__children["Expression11"]["__out"].addChild( Gaffer.StringPlug( "p0", direction = Gaffer.Plug.Direction.Out, defaultValue = '', flags = Gaffer.Plug.Flags.Default | Gaffer.Plug.Flags.Dynamic, ) )
__children["Expression11"]["__out"].addChild( Gaffer.StringPlug( "p1", direction = Gaffer.Plug.Direction.Out, defaultValue = '', flags = Gaffer.Plug.Flags.Default | Gaffer.Plug.Flags.Dynamic, ) )
__children["Expression11"].addChild( Gaffer.V2fPlug( "__uiPosition", defaultValue = imath.V2f( 0, 0 ), flags = Gaffer.Plug.Flags.Default | Gaffer.Plug.Flags.Dynamic, ) )

__children["Plane"]["dimensions"]["x"].setValue( 4.0 )
__children["Plane"]["dimensions"]["y"].setInput( __children["Plane"]["dimensions"]["x"] )
__children["Plane"]["divisions"]["x"].setValue( 4 )
__children["Plane"]["divisions"]["y"].setInput( __children["Plane"]["divisions"]["x"] )
__children["Plane"]["__uiPosition"].setValue( imath.V2f( 9.71208, -52.8344879 ) )

__children["PathFilter2"]["paths"].setValue( IECore.StringVectorData( [ '/plane' ] ) )
__children["PathFilter2"]["__uiPosition"].setValue( imath.V2f( 34.939537, -58.0828819 ) )
__children["OSLCode3"]["parameters"]["test_string"].setInput( __children["Expression11"]["__out"]["p1"] )
__children["OSLCode3"]["parameters"]["int_list"].setInput( __children["Expression11"]["__out"]["p0"] )
__children["OSLCode3"]["parameters"]["array_length"].setInput( __children["Expression10"]["__out"]["p0"] )
__children["OSLCode3"]["code"].setValue( 'int index;\ngetattribute( "shading:index", index );\n\nstring_out = "";\n\nif (int_list != "")\n\n{\n\tstring int_array[20000];\n\tsplit( int_list, int_array, "," );\n\tint start_index, end_index;\n\tstart_index = stoi(int_array[0]);\n\tfor (int n=start_index-1; n<array_length; n++){\n\t\t\n\t\tstring cur_index = int_array[n];\n\t\tif (stoi(cur_index) == index){\n\t\t\tstring_out = test_string;\n\t\t}\n\t}\n}' )
__children["OSLCode3"]["__uiPosition"].setValue( imath.V2f( -9.23995018, -62.1448555 ) )
__children["OSLObject3"]["in"].setInput( __children["Plane"]["out"] )
__children["OSLObject3"]["filter"].setInput( __children["PathFilter2"]["out"] )
__children["OSLObject3"]["adjustBounds"].setValue( False )
__children["OSLObject3"]["interpolation"].setValue( 2 )
__children["OSLObject3"]["primitiveVariables"]["primitiveVariable"]["name"].setValue( 'material' )
__children["OSLObject3"]["primitiveVariables"]["primitiveVariable"]["value"].setInput( __children["OSLCode3"]["out"]["string_out"] )
__children["OSLObject3"]["__uiPosition"].setValue( imath.V2f( 9.71208, -62.744854 ) )
__children["Expression10"]["__in"]["p0"].setInput( __children["OSLCode3"]["parameters"]["int_list"] )
__children["Expression10"]["__uiPosition"].setValue( imath.V2f( -21.2805729, -62.2548141 ) )
__children["Expression11"]["__uiPosition"].setValue( imath.V2f( -19.7258472, -57.1683273 ) )
__children["Expression10"]["__engine"].setValue( 'python' )
__children["Expression10"]["__expression"].setValue( 'indices = parent["__in"]["p0"]\nparent["__out"]["p0"] = len(indices.split(","))' )
__children["Expression11"]["__engine"].setValue( 'python' )
__children["Expression11"]["__expression"].setValue( 'data = {"foo": [0,1,2,3,4,5,6,7,8,9], "bar":[10,11,12,13,14,15]}\n\nfor k, v in data.items():\n\ttest_string = k\n\tstr_index = \',\'.join(str(num) for num in v)\n\tparent["__out"]["p1"] = k\n\tparent["__out"]["p0"] = str_index \n\t# break\n\t\n' )


del __children

Daniel Dresser

unread,
Mar 2, 2024, 2:58:27 AM3/2/24
to gaffer-dev
The OSLObject only runs once per object, so no, that isn't going to do what you want. Currently, you've got a loop in an expression that sets target plugs to different values, but it can't trigger anything to happen on each iteration, so it just overwrites the first value on the next iteration without the first value having done anything.

What you could do is have all your logic on the Python side, and just pass a big array of all the values you want to assign to OSL, which could achieve any possible result ... but again, would be far more effective once we let you hook up array inputs to OSLCode.

-Daniel

aurora

unread,
Mar 2, 2024, 11:28:01 AM3/2/24
to gaffer-dev
Thank you Daniel. 
No worries, I figure out how to do what I want with the USD Python. Link to the example:
https://discord.com/channels/722635732384219146/1144364027707936808/1213383726604615702
Cheers!
A
Reply all
Reply to author
Forward
0 new messages