Solved Get viewport image of inactive document.

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();
	                AutoAlloc<BaseBitmap> bitmap;
			// Get the viewport image from the viewport renderer
			maxon::ImageRef img;

			if (img == nullptr) {
				doc = doc->GetNext();

			// 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);
			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?

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.


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

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.


@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.

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.


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