cables-gl / cables_docs

cables documentation docs.cables.gl
https://cables.gl/docs/docs
45 stars 16 forks source link

CustomShader triggers `cyclic object value` TypeError when connected to `SaveJsonFile` #885

Open LeStahL opened 3 weeks ago

LeStahL commented 3 weeks ago

Backstory: I'd like to drag around & midi control CustomShaderuniform values in cables and then proceed to export the resulting parameter sets into my pc4k intro code. This should happen automatically to a certain degree. When trying to auto-download the CustomShader json representation, for example using the patch below:

{"ops":[{"opId":"04596a28-9563-4f42-8756-2d2a831baa60","id":"baaa3303-10f6-4196-b9fd-64f3c1b0ead6","uiAttribs":{"translate":{"x":132,"y":177.4468085106383},"notWorkingMsg":null,"color":"#5dc0fd","subPatch":0},"portsIn":[{"name":"Filename","value":"test"}]},{"opId":"a165fc89-a35b-4d39-8930-7345b098bd9d","id":"dmenf7dvk","uiAttribs":{"subPatch":0,"translate":{"x":132,"y":126.80851063829783}},"portsIn":[{"name":"Fragment Code","value":"\nIN vec2 texCoord;\n{{MODULES_HEAD}}\nvoid main()\n{\n    vec4 col=vec4(0.5,0.5,0.5,1.0);\n    {{MODULE_COLOR}}\n    outColor = col;\n}"},{"name":"Vertex Code","value":"{{MODULES_HEAD}}\nIN vec3 vPosition; //!@\nIN vec2 attrTexCoord;\nIN vec3 attrVertNormal;\nIN vec3 attrTangent,attrBiTangent;\n\nIN float attrVertIndex;\n\nOUT vec2 texCoord;\nOUT vec3 norm;\nUNI mat4 projMatrix;\nUNI mat4 viewMatrix;\nUNI mat4 modelMatrix;\n\nvoid main()\n{\n    texCoord=attrTexCoord;\n    norm=attrVertNormal;\n    vec4 pos=vec4(vPosition,  1.0);\n    vec3 tangent=attrTangent;\n    vec3 bitangent=attrBiTangent;\n    mat4 mMatrix=modelMatrix;\n    gl_PointSize=10.0;\n\n    {{MODULE_VERTEX_POSITION}}\n\n    mat4 modelViewMatrix=viewMatrix*mMatrix;\n    {{MODULE_VERTEX_MOVELVIEW}}\n\n    gl_Position = projMatrix * modelViewMatrix * pos;\n}\n"},{"name":"Use As Material","value":1}],"portsOut":[{"name":"trigger","links":[{"portIn":"download","portOut":"trigger","objIn":"baaa3303-10f6-4196-b9fd-64f3c1b0ead6","objOut":"dmenf7dvk"}]},{"name":"Shader","links":[{"portIn":"Object","portOut":"Shader","objIn":"baaa3303-10f6-4196-b9fd-64f3c1b0ead6","objOut":"dmenf7dvk"}]},{"name":"Has Errors","value":false}]},{"opId":"21630924-39e4-4df5-9965-b9136510d156","id":"ro4qwwzx7","uiAttribs":{"subPatch":0,"translate":{"x":132,"y":78.78727494411106}},"portsOut":[{"name":"Next","links":[{"portIn":"render","portOut":"Next","objIn":"dmenf7dvk","objOut":"ro4qwwzx7"}]}]}]}

Cables shows the following error:

[LoadingStatus] finished loading 1 assets in 0.24s
[LoadingStatus] finished loading 1 assets in 0.215s
[core_port] exception in port: SaveJsonFile op: SaveJsonFile
[core_port] [TypeError] cyclic object value
Port.prototype._onTriggered sandbox.cables.gl/ui/js/cables.js@3242:52
[core_port] Ops.Json.SaveJsonFile/download.onTriggered  sandbox.cables.gl/api/ops/code@92845:79
[LogTab] file: "code" line 92844: const dataStr = "data:text/json;charset=utf-8," + encodeURIComponent(JSON.stringify(inObject.get(), null, 2));

A possible workaround to get the exact parameter set is to ctrl+c the CustomShader op and paste it into VSCode, which works, but is a slightly clunky workflow.

I use the version at cables.gl to reproduce.

pandrr commented 3 weeks ago
Screenshot 2024-10-28 at 15 17 45

that object is not json but a class. you can use the operator "shaderInfoUniforms" to get an array that is [uniformName,value,type,uniformName,value,type....] this should be easily exportable. nowadays i would write the op differently, so it output json objects. and it could have a shader input but it was just a debug op when i wrote it

pandrr commented 3 weeks ago

the op is stupid, let me make a new version on dev...

pandrr commented 3 weeks ago
Screenshot 2024-10-28 at 15 26 04

this is the v2 on dev.cables.gl this should be usable