Plugin Cafe Homepage
Forum Home Forum Home > Plugin Cafe > SDK Help
  New Posts New Posts
  FAQ FAQ  Forum Search   Register Register  Login Login

GetNearestPolygon returns NULL

 Post Reply Post Reply
Author
Message
peterakos View Drop Down
Member
Member


Joined: 2015 Jul 13
Online Status: Offline
Posts: 238
Post Options Post Options   Quote peterakos Quote  Post ReplyReply Direct Link To This Post Topic: GetNearestPolygon returns NULL
    Posted: 2017 Feb 17 at 7:37am

User Information:

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

---------

Hello.

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

void pickObject(BaseDocument* doc){
doc->StopPickSession(true);
pick_session_data->multi = true;
pick_session_data->callback = pickCallback;
doc->StartPickSession(pick_session_data);
}

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;
GetInputState(BFM_INPUT_MOUSE, BFM_INPUT_MOUSELEFT, 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
C4DObjectList::Free(object_list);
}
}
}
}

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.




Edited by peterakos - 2017 Feb 17 at 7:39am
Back to Top
Andreas Block View Drop Down
Forum Moderator
Forum Moderator
Avatar

Joined: 2014 Oct 01
Location: Hannover
Online Status: Offline
Posts: 1268
Post Options Post Options   Quote Andreas Block Quote  Post ReplyReply Direct Link To This Post Posted: 2017 Feb 20 at 12:38pm
Hi,

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.



Edited by Andreas Block - 2017 Feb 20 at 12:38pm
Cheers,
Andreas
SDK Support Engineer
Back to Top
peterakos View Drop Down
Member
Member


Joined: 2015 Jul 13
Online Status: Offline
Posts: 238
Post Options Post Options   Quote peterakos Quote  Post ReplyReply Direct Link To This Post Posted: 2017 Feb 20 at 3:40pm
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.
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down

Bulletin Board Software by Web Wiz Forums® version 9.61 [Free Express Edition]
Copyright ©2001-2009 Web Wiz

This page was generated in 0.094 seconds.