Maxon Developers Maxon Developers
    • Documentation
      • Cinema 4D Python API
      • Cinema 4D C++ API
      • Cineware API
      • ZBrush Python API
      • ZBrush GoZ API
      • Code Examples on Github
    • Forum
    • Downloads
    • Support
      • Support Procedures
      • Registered Developer Program
      • Plugin IDs
      • Contact Us
    • Categories
      • Overview
      • News & Information
      • Cinema 4D SDK Support
      • Cineware SDK Support
      • ZBrush 4D SDK Support
      • Bugs
      • General Talk
    • Unread
    • Recent
    • Tags
    • Users
    • Login

    GetNearestPolygon returns NULL

    SDK Help
    0
    3
    359
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • H
      Helper
      last edited by

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

      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.

      1 Reply Last reply Reply Quote 0
      • H
        Helper
        last edited by

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

        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.

        1 Reply Last reply Reply Quote 0
        • H
          Helper
          last edited by

          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.

          1 Reply Last reply Reply Quote 0
          • First post
            Last post