Retrieving a shader from a LayerShaderLayer
-
Hi @ferdinand,
Many thanks for your very helpful comments and code. I've investigated this a bit further and I may have found where the problem lies. This is the slightly modified code snippet:
while(lslayer != nullptr) { if (lslayer->GetType() == LayerType::TypeShader) { if(lslayer->GetParameter(LAYER_S_PARAM_SHADER_LINK, gd) == true) { if(gd.GetType() == DA_ALIASLINK) { const BaseLink* link = gd.GetBaseLink(); if (link != nullptr)
The main difference is that I've added a check for the GeData type. This test always fails: that is, the GeData type returned is not DA_ALIASLINK but a value of 14, which appears to be DA_MISSINGPLUG - a mssing datatype plugin, whatever that means.
Given that, if that check is not carried out and it is assumed that it must be a DA_ALIASLINK (as I did in the previous code) it's not surprising that a nullptr is always returned, in fact it's surprising Cinema doesn't crash . In fact, in the debugger if you manually bypass that GetType() check, Cinema triggers a breakpoint in c4d_gedata.h but it doesn't actually crash.
Unless, again, I've done something wrong this is looking like a bug but it's odd that the Python version works correctly. Should I submit a bug report?
Cheers,
Steve -
Hey Steve,
DA_MISSINGPLUG
means that this parameter has been serialized with a data type plugin which is not present in the executing Cinema 4D instance. Imagine loading a scene which contains aFoo Light
for theFoo Renderer
and you do not have that renderer installed. The light would still load as a ?-object for the end user and you could still read its parameters in the API, as for example its name. When you then would try to read its parameterFOO_LIGHT_FALLOFF
which is of typeFooFalloffDatatype
, Cinema 4D would return you aGeData
of typeDA_MISSINGPLUG
as the data type implementation comes withFoo Renderer
.This should not be raised for
LAYER_S_PARAM_SHADER_LINK
since it is of typeDTYPE_BASELISTLINK
(orDA_ALIASLINK
in the terms ofGeData
), a built-in type. The only thing I could imagine, is that the file you are trying to load holds there a link in your layer shader to a node type realized by a plugin you do not have installed. E.g.: Your layer shader holds aBaseShader
of typeFoo
as its first layer, and you do not have theFoo Shader
plugin installed. I would still consider this a bit odd behaviour to mark such parameter as of typeDA_MISSINGPLUG
and I have personally never run in this behaviour before.Can you show us the scene file?
Cheers,
Ferdinand -
Hi Ferdinand,
There is no scene file worth sending because all I do is add my shader to a layer shader and try to retrieve a pointer to it from the layer shader. So at it's most simple all I would do is create a new standard C4D material (not Redshift or any other renderer) in a blank scene, add a layer shader to the mat's color channel and add my shader as the only layer in that layer shader.
To show what I mean, I've created an absolutely bare-bones shader to reproduce the problem. A zip with source and resource is attached to this post. It should be straightforward to build if anyone wants to do that.
It may be, of course, that what I'm trying to do is not possible. In case you're wondering what exactly I am trying to do, all I want is to discover which channel the instance of the shader is in. This is easy at render time by checking the cd->texflag in the Output function but I couldn't find a way to do it when not rendering. The reason I want to do this is because I want to disable parts of the shader's UI depending on the channel. For example, my shader has a colour gradient which is used in the Color channel but not in the Bump channel. Now, this all works fine if the shader is not in a Layer shader, but if it is I need to traverse the layer shader to find out if my shader is present. But I can't do that because I can't get a pointer to any of the shaders in the layer shader.
(There may well be a better/easier way to do this, in which case I'd really love to know what it is )
Cheers,
Steve
Attached: LayerShaderTest.zip -
Hey Steve,
I understand that it sometimes can seem a bit stupid when we insist on scene files and reproduction steps. But when you once have debugged something with no or only little directions from a user because things "were obvious", just to run one or even multiple days straight against a brick wall trying to reproduce the issue, only to later find out the user "of course" always did the uncommon thing X before doing the "obvious" thing, you start to look differently at such things.
Can you please provide a scene file with which this fails for your plugin/code? I will try to have a look in the next days. For me this all still sounds very much like a bug/issue on our side.
Cheers,
Ferdinand -
Hi Ferdinand,
No worries, attached is a zip with the compiled test plugin and a scene file. Just open the file, switch to the material in the AM and check the console for output. The plugin's source is in the previous file I uploaded.
Cheers,
Steve
Attached: LayerShaderTest_Scene.zip -
Just one small correction - the returned value of 14 when GeData::GetType() is called equates to DA_VOID, not DA_MISSINGPLUG. My mistake, sorry about that. That value makes a bit more sense; still fails though
Steve
-
Hey Steve,
I will have a look, but it will probably only be on Friday or so, as this will likely take me at least two hours to untangle, and I currently do not have the time for that. I will latest answer by the end of next week, but I'll try to make it happen this week.
Cheers,
Ferdinand -
Hi Ferdinand,
No worries, I know you must be very busy. The shader works in itself, it’s just that I’d like to tidy the interface to avoid confusing users. Whenever you have some time to look at it is fine.
Cheers,
Steve -
Hey @spedler,
you did everything correctly, layer shaders are just a bit of an oddball. Find below code which does what you want.
Cheers,
FerdinandResult
Code
INITRENDERRESULT LayerShaderTest::InitRender(BaseShader* shader, const InitRenderStruct& renderStruct) { if (!shader) return INITRENDERRESULT::UNKNOWNERROR; // Get the host of the shader. Although shaders can be owned by shaders, in this example file this shader is for // example owned by a layer shader, the branching relation will always be to a non-shader entity. Shaders are in // a hierarchial relationship to each other. E.g., this shader is a child of the layer shader in the test scene. See // https://developers.maxon.net/docs/cpp/2024_5_0/page_manual_baseshader.html#page_manual_baseshader_access // for more information. BaseList2D* const host = shader->GetMain(); if (!host || host->GetType() != Mmaterial) return INITRENDERRESULT::OK; // Attempt to get a layer shader in the color channel of a material host. GeData linkData; host->GetParameter(ConstDescID(DescLevel(MATERIAL_COLOR_SHADER)), linkData, DESCFLAGS_GET::NONE); if (linkData.GetType() != DA_ALIASLINK) return INITRENDERRESULT::OK; LayerShader* const layerShader = static_cast<LayerShader*>(linkData.GetLink(shader->GetDocument(), Xbase)); if (!layerShader) return INITRENDERRESULT::OK; // Iterate over the layers in the layer shader. LayerShaderLayer* layer = layerShader->GetFirstLayer(); while (layer) { // Youd did more or less everything correctly, layer shaders are just a bit of an oddball and pass on their links // as void pointers; who doesn't like a good void pointer? GeData data; layer->GetParameter(LAYER_S_PARAM_SHADER_LINK, data); if (data.GetType() != DA_VOID) return INITRENDERRESULT::UNKNOWNERROR; const BaseLink* const link = reinterpret_cast<const BaseLink*>(data.GetVoid()); if (!link) return INITRENDERRESULT::UNKNOWNERROR; // And finally, get the shader from the link and print its name. Please do not print in a production context from // this or any shader method (NodeData::Message and ::Init and some other NodeData methods are fine for printing). const BaseShader* const other = static_cast<const BaseShader*>(link->GetLink(shader->GetDocument(), Xbase)); ApplicationOutput("Shader in layer: @", other ? other->GetName() : "None"_s); layer = layer->GetNext(); } return INITRENDERRESULT::OK; }
-
Hi Ferdinand,
That’s great, really very helpful, thank you. I’m glad my code was right in principle even if it didn’t work! I would never have guessed the layer returned a void pointer. That should now work perfectly and do exactly what I need.
Thanks again for taking the time to look into this, very much appreciated.
Cheers,
Steve