VRay Plugin: How to get primitive id of polygon

hi,

I’m trying to implement a texture plugin to retrieve primitive id. I use the following snippet but still got failed:

AColor TexColoredPrimitive::getTexColor(const VRayContext &rc) {

RayResult ray_result = rc.rayresult;
    VRayExport IntersectionData intersect_data = ray_result.getIntersectionData();
    VRayExport GenericPrimitive* primitive = intersect_data.getPrimitive();
    int id = primitive->getIndex();
    …
}

The “id” returned is always -1. How to deal with this issue ?

Thanks.

getIndex() will return the index of the primitive inside it’s parent. What exactly you are trying to achieve?

The final result i want to achieve is to make each quad of a polygon object (such as a sphere create in maya) has different color. I try to take use of getfaceID() in VUtils::SurfaceInfoInterface but that only produce triangles with different color. Any walkaround to achieve the fore-mentioned effect ?

I still can’t understand the usage of getIndex(). Since i consider a sphere is composed by quads, these quads are primitives of sphere object(I think it as parent). The result of always return -1 is incomprehensible for me. Am i misunderstanding something ?

V-Ray doesn’t hold directly information about the actual polygons (i.e. quads), everything is triangulated. However, the information is still present indirectly in the edges visibility. For example, you ca see the edges textures in vraysdk\samples\vray_plugins\textures\vray_texedges
It’s using SurfaceInfoInterface which can give you getEdgeVisibility()/getSubTriangleData(). But I can’t think of a way to use just this info for coloring.

Another approach is to use computePolygonData() in meshinfobase.h. It will restore the polygon data and then you can map from triangle index to polygon index.

The computePolygonData() requires a MeshInfoSubdiv object as a parameter, is it possible to access the corresponding MeshInfoSubdiv object from current ray context (i.e. from VRayContext.RayResult.si or sd or primitive or something else) ?

Let me back to my original question about getIndex(). Since the primitive attribute of RayResult doesn’t represent the quad of polygon mesh, what it stand for ? I still can’t figure out it’s usage because it’s return value doesn’t reveal anything meaningful (as i mentioned before it always return -1) and it seems no example take use of this attribute. Could you provide a simple example to reveal the correct way to use primitive information ? Thanks.

You must call computePolygonData() as a pre-process step (in frameBegin()). The texture can get the geometry plugin as a parameter. So in some sense, you need two textures - one is to get you the polygon ID, and another one to something with it. If you have multiple objects, you must switch between a couple of polygon ID textures. I think it will be much easier to just create a color set in Maya for this particualr case.

getIndex() is used mostly internally. For example, the hair sytem will create strands as its children. Each strand will get it’s own index within the hair system. We use this index for example to do the “random by strand” feature. But it’s a bit more specific since this index represents a strand in this case. In the case of polygons, V-Ray doesn’t care about them except in some rare cases (like subdivision)

I try to implement a simple texture plugin to retrieve the polygon ID but the problem arise from connection between “outMesh” (or “worldMesh”) attribute to my node’s geometry attribute is not permitable.
Another problem is even i can get to the meshInfoInterface, how could i convert it to MeshInfoSubdiv such that i can pass it to computePolygonData function ?

This is my sample code:


// Parameters
struct TexPolyMeshID_Params: VRayParameterListDesc {
    TexPolyMeshID_Params(void) {
	// A parameter that can recieve output of a geometry
	addParam("mesh", paramtype_object, -1, "The input mesh.");
    }
};

struct TexPolyMeshID: VRayTexture {
    TexPolyMeshID(VRayPluginDesc *pluginDesc):VRayTexture(pluginDesc) {
        // Set parameter caches
	paramList->setParamCache("mesh", &meshPlugin);   // Can't connect mesh shape node to this attribute ?
    }

    void frameBegin(VR::VRayRenderer *vray) {
        VRayTexture::frameBegin(vray);

        if(meshPlugin != NULL)
        {
            meshInfoInterface = (MeshInfoInterface*)GET_INTERFACE(meshPlugin, EXT_MESH_INFO);

            InterfaceID id =  meshInfoInterface->getInterfaceID();
            const tchar* name = meshInfoInterface->getInterfaceName ();

            printf("Interface Name = %s\n", name);
        }
    }

    // VRayTexture member functions
    AColor getTexColor(const VRayContext &rc);
    void getTexColorBounds(AColor &cmin, AColor &cmax);
    Vector getColorBumpGradient(const VRayContext &rc);

    virtual PluginInterface* newInterface(InterfaceID id) { return VRayTexture::newInterface(id); }

    // VRayTexture Member function
    PluginBase* getPlugin(void) { return static_cast<PluginBase*>(this); }

    // These pure virtual functions have to be implemented if support RTTexInterface
    //int getDataSize(void) { return 0; }
    //void getData(float *fdata) { }
    //int getSource(const char *part,const char *outParam,const char *lang,VUtils::ShaderSource &, OCLData &mtlData);

    private:
        MeshInfoInterface* meshInfoInterface;
        MeshInfo* meshInfo;
};

#define TexPolyMeshID_PluginID PluginID(LARGE_CONST(2004110829))
SIMPLE_PLUGIN_LIBRARY(TexPolyMeshID_PluginID, EXT_TEXTURE, "TexPolyMeshID", "Polymesh ID texture plugin for VRay", TexPolyMeshID, TexPolyMeshID_Params);

AColor TexPolyMeshID::getTexColor(const VRayContext &rc) {
    ...
}

Do you use desc.txt to export the parameters? It can export meshes, but I think only inside sets via the geomset keyword. So you will need a list parameter, like this:
addParamPlugin(“meshes”, EXT_VRAY_PLUGIN, 0, “List of meshes.”);
This is used by the dirt and distance texture for example.

Another way is using post translate python script. It it can export nodes (via the exportNode function), but you can read the “geometry” parameter which contains the mesh, and assign it to the texture.
When you get the MeshInfoInterface, you can then call newMeshInfo() and get a MeshInfo object which inherits MeshInfoSubdiv, so you can use it directly with computePolygonData. When you are done, you call deleteMeshInfo()

hi ivaylo,

I’m interesting in the second method you mentioned above but i still need more details to implement it. Here are some questions about the issue:
1. Where are these python modules ? I don’t see them in chaos group folder.
2. Is there any help document about how to use these python modules ? I see just one line in SDK document that mention about python scripting: “Note that V-Ray for Maya also includes a Python API for access to the translated scene - check out the V-Ray for Maya help files for more details” and i find no V-Ray for Maya help …
3. Let me double check the concept of strategy: Do you means that i can feed a string attribute of my node with a mesh’s name, then before rendering the python script will parse the scene file (.vrscene file), retrieving required geometry data and replace the mesh name string with that data ? (So i can expect the string attribute is filled with geometry data when implementing my shadrer)
4. Is there any API function that can help to retrieve arbitrary objects in scene rather than just object related to current ray context (if my shader is not just for Vray for Maya, i also want it for Vray standalone) ? or Is there any API function that can help to retrieve information of scene file (.vrscene file)? Could this only be achieved by python script ?

Thanks.

The pyhon module is loaded automatically. It’s in the vray\scripts\vray folder in the Maya installation. This is the help page:

It’s called post translate because the API already has the whole scene in memory, you can directly work with it. The API has some custom types like “plugin”, you can export it from a maya node, you can get it from a parameter. It’s just a reference to the internal V-Ray plugin and you can do some basic stuff with it as setting parameters, assign to another plugin, etc. You can access the whole scene. This is before the rendering phase.

I try to modify the frameBegin member function as following but the returned result is always “Number of polygons = 0”. My test scene is just a default maya sphere mesh with shape node named “pSphereShape1”. I’ve also check the “geometry” attribute of my texture node in .vrscene has filled with the name of geometry plugin node, Could you help to figure out what part i still go wrong ?

And here are some additional questions about the detail of implementing plugin:

1. Since the framebegin() function is also invoked at the time of node generation, a mechanism to check it’s input connection is required to further get valid interfce object. In this example, i try to take use of “getParam” function to check if there’s a conection to specified attribute, but i not sure whether this is the correct(or good) way to do the job or not.

2. I find two similar statements about casting interface to MeshInfoInterface*:
    meshInfoInterface = static_cast<MeshInfoInterface*>(geometry_Plugin->newInterface(EXT_MESH_INFO)); // I find this in “environmentfog.cpp”
    meshInfoInterface = (MeshInfoInterface*)GET_INTERFACE(geometry_Plugin, EXT_MESH_INFO); // I find this in “geom_hair.cpp”
    I think the differece is the first one explicitly declare as a static casting, and the question about is when should i casting interface using the first statement(static casting) and when to use the other ?

3. I still not very clear about the “plugin” custom type you mentioned:

The API has some custom types like “plugin”, you can export it from a maya node, you can get it from a parameter. It’s just a reference to the internal V-Ray plugin and you can do some basic stuff with it as setting parameters, assign to another plugin, etc.
In my example i add “geometry” attribute by using addParamPlugin so i think now it’s type is “plugin”, but it still represent as a plug that can’t make connection with other type of attributes except color\texture. I also put my node to exportNode() function in python API but no differences in resulting .vrscene. Could you provide more details about the “export” mechanism you mentioned ? Thanks a lot !


(In parameters class)
...
addParamPlugin("geometry", EXT_STATIC_GEOM_SOURCE);

(In plugin class)
...
paramList->setParamCache("geometry", &geometry_Plugin);
...
void frameBegin(VR::VRayRenderer *vray) {
    printf("Frame Begin Called !!!\n");
    VRayTexture::frameBegin(vray);
    double time = vray->getFrameData().t;

    VRayPluginParameter* geometry_Param = paramList->getParam("geometry");	

    if(geometry_Param)
    {
        meshInfoInterface = (MeshInfoInterface*)GET_INTERFACE(geometry_Plugin, EXT_MESH_INFO);

        if(meshInfoInterface)
        {
            meshInfoInterface = static_cast<MeshInfoInterface*>(geometry_Plugin->newInterface(EXT_MESH_INFO));
            // meshInfoInterface = (MeshInfoInterface*)GET_INTERFACE(geometry_Plugin, EXT_MESH_INFO);

            if(meshInfo)
            {
                int poly_num = meshInfo->getNumPolygons();

                printf("Number of polygons = %d\n", poly_num);
            }    
        }           
    }
}

...

PluginBase* geometry_Plugin;
MeshInfoInterface* meshInfoInterface;

And this is the python script i use in post translator


from vray.utils import *

nodes = findByName("InputTypeTestTex")
inputTypeTest_node = nodes[0]
print inputTypeTest_node.name()    # Check node exist

nodes = findByName("pSphereShape1@mesh1")
geometry_node = nodes[0]
print geometry_node.name()    # Check node exist

inputTypeTest_node.set("geometry", geometry_node)

frameBegin() will cache your parameters, you don’t need to call getParam() manually. Check how this is done in the V-Ray SDK plugins.
GET_INTERFACE is doing just a static_cast, it’s a matter of preference what you will use.
Call meshInfo->buildMeshInfo(vray->getSequenceData().progress, NULL, NULL); before getNumPolygons()

In addParamPlugin(“geometry”, EXT_STATIC_GEOM_SOURCE); notice that you pass EXT_STATIC_GEOM_SOURCE, which means that you want the plugin to support this interface. If it doesn’t support it, the setting of the parameter will fail. This is just for safety so you don’t assign wrong plugins. You can totally skip it, and any plugin will be assigned. Most of the cases you should write there the proper interface that you expect.

The post translate script will not change the vrscene, it works only for the scene in the memory that V-Ray will render.

I modify the add parameter call as addParamPlugin(“geometry”) but some errors arise when compiling, neglecting the second parameter seems not permitted.

I also notice that even i can connect the “message” parameter from other nodes to input parameter of type “plugin” in my plugin (the parameter is added by addParamPlugin(“otherNode”, EXT_VRAY_PLUGIN)), it still not generate a statement like “otherNode=name_of_other_plugin” in .vrscene file. Is it possible to make any type of nodes connectable and recongnizable to my plugin node without using post translation ?

Like you’ve written yourself addParamPlugin(“otherNode”, EXT_VRAY_PLUGIN)) will require only a valid V-Ray plugin.You ca attach any plugin by addParam(“otherNode”, paramtype_object);
As I told you, python post translate is modifying the scene in memory before rendering, it will not change your original vrscene. If you don’t want post translate, you must use desc.txt as a temlate how and what can be exported. Most of our textures are fully exported from there, but it’s of course more limited then a programming language.

I try to modify my VRay plugin and Maya plugin again and double check the desc.txt file is correct. Now i can make other type of attributes (such as outmesh or message attribute) connectable to my node, but when i do render an error arise and no .vrscene file generated (Error: There was a fatal error building the scene for V-Ray.).

This is my example code:
https://drive.google.com/open?id=0B7j7J0yU9_sgXzI3MWRSUDd2SGM

Could you help me to figure out where still go wrong ? Thanks.

This is an exception that was caught by the exporter. You can try Debug → Exceptions from Visual Studio to break and see from where it is thrown.

I turn on all the exception type when perform rendering. There’s only a Win32 exception i got and no further information about the problem:

First-chance exception at 0x00007FFF9D961FD0 (vray.dll) in maya.exe: 0xC0000005: Access violation reading location 0x0000000000000000.
If there is a handler for this exception, the program may be safely continued.

Is this information useful for you ?

Ok, will compile the code here and see what’s wrong

The main problem is that the output attribute in your desc.txt is called OutColor, whilie it’s outColor. So your texture doesn’t export at all. This didn’t crash for me though, make sure you don’t replace the original desc.txt with yours. Or you put your desc.txt in another path?

The crash may be related to using uninitialized textures in your VRay plugin. In frameBegin() I can see only commented code. You should call at least the frameBegin() of the parent. It will cache your plugins, so they have valid values.

hi ivaylo,

Thanks for your kindly reply and I can get rid of crash now.

Let me back to the problem about connect any maya node to vray plugin node. As mentioned before i add one of the input parameter of my vray plugin node with statement like “addParam(“any_node”, paramtype_object, -1);” and i can connect maya node’s(for example, a polygon shape node) “message” attribute to it. However when i generate the vrscene the connection is not recognized so that i can’t have a description like this:

TexInputTypeTest InputTypeTestTex {
…
any_node=pSphereShape2@mesh2;
…
}

How to make vray aware of the connection and produce the corresponding statement in vrscene without doing post translate ?