GetNearestPolygon returns NULL

On 17/02/2017 at 07:37, xxxxxxxx wrote:

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


I'm trying to click on viewport and get the pointed Polygon.
Here is my code:

void pickObject(BaseDocument* doc){
	pick_session_data->multi = true;
	pick_session_data->callback = pickCallback;
void pickCallback(Int32 flags, const PickSessionDataStruct* pick_session_data) {
	AtomArray* atom_array = pick_session_data->active;
	BaseDocument* doc = GetActiveDocument();
	Int32 atom_count = atom_array->GetCount();
	for (Int32 i = 0; i < atom_count; ++i ) {
		C4DAtom* current_atom = atom_array->GetIndex(i);
		if (curret_atom && current_atom->IsInstanceOf(Obase)) {
			BaseObject* ob = (BaseObject* )current_atom;
			if (ob) {
				BaseContainer bc;
				Int32 input_x = bc.GetInt32(BFM_INPUT_X), input_y = bc.GetInt32(BFM_INPUT_Y);
				BaseDraw* base_draw = doc->GetRenderBaseDraw();
				Int32 left, top, right, bottom, width, height;
				base_draw->GetFrame(&left,  &top, &right, &bottom);
				width = right - left + 1;
				height = bottom - top + 1;
				C4DObjectList* object_list = C4DObjectList::Alloc();
				AutoAlloc<ViewportSelect> viewport_select;
				Bool init_res = viewport_select->Init( width, height, base_draw, ob, Mpolygons, true, VIEWPORTSELECTFLAGS_0 );
				ViewportPixel* vpp = viewport_select->GetNearestPolygon(ob, input_x, input_y, 1, false);
				//Do something here

In the for loop I get all the clicked objects. Calling GetNearestPolygon I should get the clicked polygon right ?
Even though the clicked object is correct, GetNearestPolygon returns NULL.
Is there anything else I should take into consideration ?

Thank you for your time.

On 20/02/2017 at 12:38, xxxxxxxx wrote:


actually you are mixing things, which are not really made to work together.
First of all PickSession, while it sounds attractive for the job, is made for those small little "pick buttons" next to link fields, in order to pick an object from the object manager to link to.

Then inside that PickSession callback you try to make use of GetInputState(), but at that point in time the mouse event you are looking for is most likely already consumed by the PickSession (although I have to admit I didn't test this, it's more any educated guess).

And then you use GetRenderBaseDraw() in order to use it with the ViewportSelect, while you have no guarantee, that it is really corresponding to the viewport the user clicked into.

Instead the only real way to approach this and thus reliably make use of the ViewportSelect is in a ToolData plugin, which we would recommend to use here.

On 20/02/2017 at 15:40, xxxxxxxx wrote:

Thank you very much for your response Andreas !

The problem I have noticed is that viewport clicks do not always return the correct polygon.
So, probably the mouse X and Y I request using GetInputState are wrong.

I'll remove pick sessions and I'll implement it with ToolData !

Thnx again.