Maxon Developers Maxon Developers
    • Documentation
      • Cinema 4D Python API
      • Cinema 4D C++ API
      • Cineware 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

    Get viewport image of inactive document.

    General Talk
    2
    7
    1.1k
    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.
    • Danchyg1337D
      Danchyg1337
      last edited by Danchyg1337

      Hello! I asked about GetViewportImage() in this topic few days ago. Code worked as it should, but it seems like it only gets image from active document, even tho BaseDraw is different.
      Here is my code.

      BaseDocument* doc = GetFirstDocument();
      		while (doc) {
      			BaseDraw* bd = doc->GetActiveBaseDraw();
      			if (!bd) {
      				doc = doc->GetNext();
      				continue;
      			}
      	                AutoAlloc<BaseBitmap> bitmap;
      			// Get the viewport image from the viewport renderer
      			maxon::ImageRef img;
      			bd->GetViewportImage(img);
      
      
      			if (img == nullptr) {
      				doc = doc->GetNext();
      				continue;
      			}
      
      			// Transform ImageRef to BaseBitmap in order to show it in the PictureViewer
      			const Int w = img.GetWidth();
      			const Int h = img.GetHeight();
      			
      
      
      			maxon::PixelFormat rgbf = img.GetPixelFormat();
      			Int32 bitsPerColor = (Int32)(rgbf.GetBitsPerPixel().Get() / rgbf.GetChannelCount());
      			bitmap->Init((Int32)w, (Int32)h, bitsPerColor == 32 ? 96 : 32);
      
      			maxon::BaseArray<UChar> line;
      			line.Resize(w * rgbf.GetBitsPerPixel().GetBytes()).GetValue();
      
      			maxon::PixelMutableBuffer imageBuffer(line.GetFirst(), rgbf.GetBitsPerPixel());
      			maxon::GetPixelHandlerStruct getpixel = img.GetPixelHandler(rgbf, rgbf.GetChannelOffsets(), maxon::ColorProfile(), maxon::GETPIXELHANDLERFLAGS::NONE, nullptr).GetValue();
      
      			for (Int y = 0; y < h; y++)
      			{
      				getpixel.GetPixel(maxon::ImagePos(0, y, w), imageBuffer, maxon::GETPIXELFLAGS::NONE).GetValue();
      				bitmap->SetPixelCnt(0, (Int32)y, (Int32)w, line.GetFirst(), (Int32)rgbf.GetBitsPerPixel().GetBytes(), bitsPerColor == 32 ? COLORMODE::RGBf : COLORMODE::RGB, PIXELCNT::NONE);
      			}
      	                ShowBitmap(bitmap);
      			doc = doc->GetNext();
      		}
      

      Each showed bitmap contains only active document viewport.
      I also tried to switch document with SetActiveDocument() and then call DrawViews() before getting viewport image, but that didnt work.
      Is there a way to get viewpirt image of inactive documents?

      1 Reply Last reply Reply Quote 0
      • M
        m_adam
        last edited by

        Hi @Danchyg1337 may I ask how often you need to call this method? And in which context are you? (objectData, CommandData?)
        Another way could be to just call RenderDocument with the OpenGL render see Render filtered Hardware Preview.

        Cheers,
        Maxime.

        MAXON SDK Specialist

        Development Blog, MAXON Registered Developer

        Danchyg1337D 1 Reply Last reply Reply Quote 0
        • Danchyg1337D
          Danchyg1337 @m_adam
          last edited by

          @m_adam Thanks for your reply.
          As many as there opened documents.
          Context is CommandData.

          1 Reply Last reply Reply Quote 0
          • M
            m_adam
            last edited by m_adam

            If it's only a one-time solution, (aka a CommandData) then RenderDocument should do the job you don't really need a realtime performance solution that GetViewportImage could somehow offer you.

            If you need help with RenderDocument please let me know, but the previous topic should already answer all your questions.
            If not feel free to ask.

            Cheers,
            Maxime.

            MAXON SDK Specialist

            Development Blog, MAXON Registered Developer

            Danchyg1337D 1 Reply Last reply Reply Quote 0
            • Danchyg1337D
              Danchyg1337 @m_adam
              last edited by

              @m_adam Unfortunately, we are looking for real-time performance. RenderDocument is fast only on light projects.
              We are also looking for a workaround.
              We are trying to create plugin which tracks switching between documents. What is the most efficient way to catch last image of document viewport before switching the document? There is MSG_DOCUMENTINFO_TYPE_SETACTIVE, but as i understand, it will be too late to get viewport image after that event.

              1 Reply Last reply Reply Quote 0
              • M
                m_adam
                last edited by

                Hi @Danchyg1337 unfortunately unactive document doesn't maintain their cache (otherwise you will crank up pretty quickly your ram) so that means, in any case, the document will need to be rebuilt and this can take some time.

                Changing the document also release all previously active viewports.
                So RenderDocument should be the fastest solution.

                Regarding your question about Document switching unfortunately you are right there is no way to intercept any message before the document is active.

                Cheers,
                Maxime.

                MAXON SDK Specialist

                Development Blog, MAXON Registered Developer

                Danchyg1337D 1 Reply Last reply Reply Quote 0
                • Danchyg1337D
                  Danchyg1337 @m_adam
                  last edited by

                  @m_adam Thanks for your help. That made things clear.

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