Notifications
Clear all

[Closed] Vertex Color and Vertex Alpha in C#

I’m in the process of converting a bunch of scripts into .NET Assemblies and have finally started to get along on the process. But here is one thing I can’t figure out. I cannot get the vertex color as I expect. I actually want the Vertex Alpha… but I’m stuck at the phase of simply getting the color data.

Following is a simple loop over the Verts in Imesh b.


  //IMesh b;
   string R = "";
   for (int i=0; i <= (b.Verts.Count - 1); i++) 
{
 IPoint3 theColor = b.VertCol[i];
 R += theColor.X.ToString()+"
";
   }

But whenever this code gets executed, I’m getting:

   Object reference not set to an instance of an object.

Any help? I’m obviously missing some step.

7 Replies

The first thing to find out would be what is actually null: b, theColor, or theColor.X?

Hi Pier,

I haven’t been able to get the debugger to work… my break points won’t work.

So at the moment I just have the function returning the Exception.Message and it tells me that.

I know only that b is defined since it is used before and after in the code without incident.

What I am trying to do here is emulate this code from MAXScript:


	b.SetSelection #Vertex #{i}
	local theVertColor =b.getVertexColor #Alpha
 

But IMesh does not have any method that I can find like getVertexColor() (or IMNMesh, which I’m also experimenting with).

2 Replies
(@pjanssen)
Joined: 11 months ago

Posts: 0

If you attach the visual studio debugger to the 3dsmax process, it should pop up once the exception is thrown. Regardless of breakpoints.

(@wallworm)
Joined: 11 months ago

Posts: 0

Aha… Much thanks. I come mainly from the web developer environment… so my handling of debuggers is generally a lot different. Now I can see debug info.

It appears that the problem is that b.VertCol.Count == 0 … so b.VertCol[i] is failing.

Now the question is why? I know these objects have vertex color data in all the vertices… So again I am missing something in accessing this info.

Can you post a more complete bit of code which shows the failure?

And a little pedantic note, you for loop can be a bit simpler

for (int i=0; i < b.Verts.Count; i++)

one thing that comes to mind as in with scripting max meshes the same applies to the SDK

number of verts does not always equal number of color verts

… it was simply a result of me having confusion for a bit because of 0-based arrays in .NEt and 1-based in MAXScript… I was changing those around as I messed with it and left it at the last iteration that worked.

Yes, that is true. But in these specific objects, there is always a 1-to-1 number of verts > mapverts > color verts. The type of object I’m dealing with isn’t valid any other way… and the ordering of the data is always in the ordering of the verts… which is why the loop is on the verts rather than the faces–because this specific function needs to get all the info from vert 0/1 (depending on SDK/MAXScript) to the end in the mesh and output in that order.

I think my dilemma is solved, though. I had been assuming that the data for Vertex Colors was stored separately than the Map Verts. It turns out that getting the Map Verts is all I need.

So in the context of the example above (modified for a Value between 0-255, which is what I really wanted but hadn’t mentioned it because it didn’t matter to me yet):


R+= (b.MapVerts(-2)[i].X * 255).ToString();