Give Method Time To Finish? [SOLVED]

On 23/09/2014 at 12:35, xxxxxxxx wrote:

User Information:
Cinema 4D Version:   13 
Platform:   Windows  ;   
Language(s) :     C++  ;

Hey Guys,
When making organic models like humans and monsters. We mostly use edge selections to create UV pelt maps to unwrap the UV's. And often it's quite hard to select the points of feet and hands.
There's usually some points that are buried and not easy to select them. And most of the time the C4D loop tools in C4D don't let me select them. Because they can't deal with the topology variations.
And Grow Selection grows in all directions. Not just in one direction. So that's not an option either.

So I have created a custom method that allows me to grow select edge loops in one direction. It's actually pretty slick.:slightly_smiling_face:
But the problem is this method does a lot of analyzing inside of it. And it requires more time to execute than the average method.
There's a loop in it that I jump back to several times until the correct vertice is found and selected.
So what happens is it works perfect if I run the method manually. But if try to run the method consecutively several times in a loop. It messes up, and I don't get good results from it.
I'm guessing that is because the method doesn't have enough time to finish before being called again.

Is there a way to give a method more time to finish without using threading?
something like this?

//My custom method  
LONG GetNextPoint()  
//Code that finds the next point along X  
//Does quite a lot of analyzing and thinking (very time consuming)  
Bool SimplePlugin::Execute(BaseDocument *doc)  
  for(LONG i=0; i<10; i++)  
      Wait for 5 seconds before running GetNextPoint() again in the next iteration  
  return TRUE;  

I've tried various types of sleeping and timers. But they never seem to work. They don't let the method finish working.
Am I forced to use threading?
I'd rather not do that if I can avoid it.


On 23/09/2014 at 16:33, xxxxxxxx wrote:

Why would it need more time or to wait?  The next loop invocation will never occur until the method (GetNextPoint()) returns.  And if you have to wait between invocations, then maybe there are variable scope issues, stack and/or heap usage problems.  It is easy to blow the stack, especially if there are recursive methods being called or deeply nested loops.

On 23/09/2014 at 17:13, xxxxxxxx wrote:

Hi Robert,
I think I found the root of the problem.

I'm using the ModelingAxis to get the point normal direction of the selected point. And I've noticed that this thing does not update itself properly when selecting new points with code.
But it does updates itself properly whenever I execute the plugin manually.

I can brute force it's position to update by getting the position of the selected point and setting to that location. That seems to update it's position OK.
But so far, I can't figure out how to update the direction the MA axis is pointing.
I might have to ditch it and get the point normals manually. But I as hoping to avoid that.
But if I can't get the MA to update itself properly. I guess I'll have no choice.

BTW: I'm using the MA axis direction to generate a ray(a spline actually)
And then I'm using the tip of the ray(spline) to find the closest next point.
It works very well when executing the plugin manually. But since the MA is not updating it's axis through code. My spline rays don't point in the correct direction when I try to execute my method concurrently. The rays always point in the same direction.

I just wish I could get the darned Modeling axis to update properly. :angry:


On 23/09/2014 at 18:05, xxxxxxxx wrote:

It is easy enough to get vertex normals by first getting the polygon normals, using the Neighbor class to find the polygons that share the vertex, and then doing a vector average (resultant vector) of the polygons' normals.  This may add a bit of time to your code but then you don't have to coordinate with C4D's response in updating to get that value.

Are you searching for the absolute closest point, non-neighbor by edge?  For instance, if you have two toe geometries next to each other, would a point on one toe with a closest point on the other toe be valid?  I ask because now you are going to need to consider local and global minima in your search criteria and what constitutes what.  For instance, in the aforementioned case, whether or not the point is 'closest' to the one in question might need to be considered against their topological distance across the surface as well as their spatial proximity.  Does that make sense?

On 23/09/2014 at 18:48, xxxxxxxx wrote:

The ray(spline) I'm using is very tiny. So I don't think there's any danger of that.

What I'm doing is using the X axis (v1) of the ModelingAxis option as the basis for the ray's vector direction. Then I'm creating a very, very short two point spline using that vector.
Then I do a GrowSelection command to select the surrounding points
Then I use a radius value to get the nearest selected vertice from the five selected vertices.
That gives me the nearest vertice from the current vertice along the X normal and to the right.
Which basically just means that it gets the neighboring vertice along X. Without using ID# or the neighbor class to do it.
Then I delete the ray(spline) after this next point is found and selected.

The only tricky part I had to solve was if the tip of the ray(spline) wasn't within range of any of the selected points. So I used a goto to handle that problem.
If the radius fails to find the nearest point. Then it increases the radius by a small amount. And then sends the code back to the loop again using the goto to loop through the five selected points again.
And it will do this until it eventually finds the nearest selected point.
It can take anywhere between milliseconds to roughly one whole second to find the nearest point. Depending how sharp the mesh is.

It sounds more complicated than it really is. But it works really great. Except for when I try to run the method multiple times. But I'm pretty sure that's because the MA is not updating itself properly.
The Modeling Axis' X axis is what my ray direction is based off of. If that's wrong the rest of it will fail. So I need to find a way to get it to update properly. Or find some other way to generate that vector by getting the point normals manually. And then using the Cross of those normals as my X vector.
I really, really didn't want to do all of that. That sounds rather difficult.


On 24/09/2014 at 12:44, xxxxxxxx wrote:

I gave up trying get the Modeling Axis to work. And I ended up writing the code to get the point normals by hand. And now it works properly when I execute it several times in a loop.

IMHO this is a bug.
You should not need to physically select points on an object to get the MA to update it's matrix values.
That really puts a lot limitations on it's usefuleness.