Stopping a cycle of renders
-
On 24/01/2016 at 16:39, xxxxxxxx wrote:
This is my version. You can pass the C4DThread to RenderDocument() which will then use it to check if
rendering should continue. Calling the End() method of the thread will then abort the rendering immediately.# Copyright (c) 2016 Niklas Rosenstein # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE. import c4d class RenderThread(c4d.threading.C4DThread) : def __init__(self, doc, bmp, renderflags = None) : super(RenderThread, self).__init__() if renderflags is None: renderflags = ( c4d.RENDERFLAGS_NODOCUMENTCLONE | c4d.RENDERFLAGS_SHOWERRORS) self.doc = doc self.bmp = bmp self.renderflags = renderflags def Main(self) : rdata = self.doc.GetActiveRenderData().GetData() rdata[c4d.RDATA_XRES] = self.bmp.GetBw() rdata[c4d.RDATA_YRES] = self.bmp.GetBh() c4d.documents.RenderDocument(self.doc, rdata, self.bmp, self.renderflags, self.Get()) class RenderArea(c4d.gui.GeUserArea) : def __init__(self) : super(RenderArea, self).__init__() self.bmp = None def DrawMsg(self, x1, y1, x2, y2, msg) : self.DrawSetPen(c4d.COLOR_BG) self.DrawRectangle(x1, y1, x2, y2) if self.bmp: self.DrawBitmap(self.bmp, 0, 0, self.GetWidth(), self.GetHeight(), 0, 0, self.bmp.GetBw(), self.bmp.GetBh(), c4d.BMP_NORMAL) class RenderDialog(c4d.gui.GeDialog) : def CreateLayout(self) : self.ua = RenderArea() self.thread = None self.AddUserArea(1000, c4d.BFH_SCALEFIT | c4d.BFV_SCALEFIT, 300, 70) self.AttachUserArea(self.ua, 1000) self.AddButton(1001, c4d.BFH_SCALEFIT, name="Start") return True def Command(self, param, bc) : if param == 1001: if not self.thread or not self.thread.IsRunning() : doc = c4d.documents.GetActiveDocument() width, height = self.ua.GetWidth(), self.ua.GetHeight() bmp = c4d.bitmaps.BaseBitmap() bmp.Init(width, height) self.thread = RenderThread(doc, bmp) self.thread.Start() self.SetString(1001, "Stop") self.SetTimer(250) self.ua.bmp = bmp return True else: self._Stop() return True return False def Timer(self, bc) : if not self.thread or not self.thread.IsRunning() : self._Stop() self.ua.Redraw() def _Stop(self) : self.thread.End(True) self.SetString(1001, "Start") self.SetTimer(0) if __name__ == '__main__': RenderDialog().Open(c4d.DLG_TYPE_MODAL_RESIZEABLE)
-
On 25/01/2016 at 01:58, xxxxxxxx wrote:
Thank you, both
I will take a look at both the listing and I guess I will make this work, now.
Once again, thank you much. -
On 25/01/2016 at 04:37, xxxxxxxx wrote:
Well, it kind of works.
I used the Scott method with some additional information from Niklas method.
It does render and it does update.
However, when I press the "Abort"button, I get a spinning wheel and Cinema4D hangs
Also, I need to perform some calculations on each rendered bitmap, after it gets rendered.
So, instead of rendering a sequence of images, I need to create a cycle that renders from frame A to frame B, performing some operations on the bitmap after being rendered, before saving it.
Can this be done? -
On 25/01/2016 at 05:49, xxxxxxxx wrote:
Ok, solved the hanging problem by ending the thread with self.thread.End(False) instead of just self.thread.End()
Now, Still haven't solved the processing of the bitmaps after each render.
-
On 25/01/2016 at 07:28, xxxxxxxx wrote:
Actually, it is not really solved
If I abort and press the render button again, it hangs.
Damn!!! -
On 25/01/2016 at 08:34, xxxxxxxx wrote:
Something to keep in mind.
I know very little about threads. I never use them.
When I was playing around with this yesterday. I tried using the python threading instead of the C4D threading. And I looked around the internet about how to stop threads. And I was surprised to learn that there is no such thing as stopping a thread. Stopping a thread apparently is a very, very bad idea.
There are hacks to do it. But it's generally a bad idea.In the C4D thread class. We have the thread.end(True/False) method to stop them.
Setting the param to True (or keeping it empty) will try to stop the thread immediately.
Setting the param to False will try to stop the thread. But it won't stop until what it's working on has finished.
But based on my research. I would say that Maxon knows that "stopping" a thread is a very bad idea. So I'm guessing that their end() method does it softly and safely. And not abruptly.So it sounds to me that you are trying to work on the image before the RenderDocument() method has finished running.
I think you need to worry more about if the RenderDocument() method has finished before attempting to do something with the images. Rather than if the thread has stopped.Keep at it. I think you'll get it working eventually.
-ScottA
-
On 25/01/2016 at 09:31, xxxxxxxx wrote:
I'm stubborn
I'm still trying and I will get there, eventually
Do you know how can I check if the RenderDocument finished? -
On 25/01/2016 at 10:17, xxxxxxxx wrote:
Nope.
I spent a little bit of time yesterday trying to find a python method that checks if a file is being written to or not as a way to check if the RenderDocument() was writing to a file. And I didn't find anything worth while.
I mostly saw people posted things like checking the time files were written as a way to tell if they were being written to or not.I'm sure there's a better way. I only spent about 15mins. looking around.
I was too caught up in watching the football games.-ScottA
*Edit- I don't know if you can rely on the return from RenderDocument() in cases where you're rendering multiple frames. Or calling it many times in a thread. It does have a return that can be checked. But I don't know if it can be used in your situation or not.
res = documents.RenderDocument(doc, rd, bmp, c4d.RENDERFLAGS_EXTERNAL) if res == c4d.RENDERRESULT_OK: print" We've finished"
-
On 25/01/2016 at 10:50, xxxxxxxx wrote:
Originally posted by xxxxxxxx
In the C4D thread class. We have the thread.end(True/False) method to stop them.
Setting the param to True (or keeping it empty) will try to stop the thread immediately.
Setting the param to False will try to stop the thread. But it won't stop until what it's working on has finished.
But based on my research. I would say that Maxon knows that "stopping" a thread is a very bad idea. So I'm guessing that their end() method does it softly and safely. And not abruptly.Not quite. The End() method does the same in "stopping" the thread independent from whether you
pass True or False to it. All it does is set a flag in the thread object that will cause TestBreak() to return
false. The RenderDocument() function will call that method to check if it should continue to process or
not. There's no real "stopping" happening in terms of what you might think of relating to processor
(where you can just kill a process with the Task Manager [Win] or Activity Monitor [Mac]).This is in fact the way you "stop" a thread: By telling it to stop. Note that the example below should
just outline the way these two components are implemented. You can not pass a Python Thread to
the RenderDocument function, it must be a C4DThread.from threading import Thread def MyRenderDocument(doc, bmp, thread) : ''' Sample implementation of c4d.documents.RenderDocument(). ''' while not thread.TestBreak() : # continue to render ... class MyC4DThread(Thread) : ''' Sample implementation of C4DThread.End(). ''' def __init__(self) : super(MyC4DThread, self).__init__() self.stopped = threading.Event() def End(self, wait) : self.stopped.set() if wait: self.join() def TestBreak(self) : return self.stopped.is_set() def run(self) : MyRenderDocument(doc, self)
-
On 25/01/2016 at 10:54, xxxxxxxx wrote:
@Rui If you want to render multiple frames, I think you should call RenderDocument() for each frame.
Afaik there is no way to receive some sort of callback when a frame finished. You know that RenderDocument()
finished when the function returned (ie. all code that you place after the RenderDocument() code is
executed after it finished).@Scott Afaik RenderDocument() does not do the "Save" part for you, so you would have to save it
by yourself after the rendering.TL;DR if you're still having problems with C4D hanging, you have to make sure RenderDocument()
finishes. You should use the C4DThread.TestBreak() interaction with RenderDocument() to stop the
function (and with it the thread) rather than the way Scott did it in his example using
RENDERFLAGS_EXTERNAL + CallCommand() to stop the rendering. -
On 25/01/2016 at 11:22, xxxxxxxx wrote:
The script Niklas posted does seem to stop the rendering Rui. Although I never tested it by trying to do anything with the resulting image.
I would not use my example at all. It was just a wild guess on my part.
-ScottA
-
On 25/01/2016 at 14:05, xxxxxxxx wrote:
I will have to save the bitmap myself, I know that
And, to make sure the render finishes, for each frame, my abort button will only set a variable to True and that variable will be tested after the RenderDocument.
So, the render will stop after the frame has finished render, if the user pressed the Abort button, in the meanwhile.
Well, at least that is what I'm going to try to do -
On 25/01/2016 at 15:30, xxxxxxxx wrote:
And you're sure you don't want the user to be able to stop the render process mid-render, eg. if the
frame would still take 5 hours to render? -
On 26/01/2016 at 01:59, xxxxxxxx wrote:
The renders are always much smaller than a full render (they are almost thumbnails) and with antialias off.
So, they don't take up much time rendering.
Also, this is for a plugin that will be useful for animations, not for stills. I don't think that animators will create animations that take up five hours per frame (although... possible ) -
On 27/01/2016 at 03:24, xxxxxxxx wrote:
If you say so. I just don't understand what the problem is in doing it the "right way". I'd get furious
if the plugin lets me wait 4 seconds after pressing stop. Since the possibility to stop the rendering right
away is given and its easy to implement, why not do it. -
On 27/01/2016 at 04:23, xxxxxxxx wrote:
Well, I would prefer to do it the right way.
But I'm still not able to do it
This is much harder than I expected. -
On 27/01/2016 at 04:42, xxxxxxxx wrote:
In the...
def MyRenderDocument(doc, bmp, thread) :
''' Sample implementation of c4d.documents.RenderDocument(). '''while not thread.TestBreak() :
# continue to render ...The # continue to render is just a call to the RenderDocument?
Will this stop the render in mid render? Will it not leave things hanging and requiring house cleaning? -
On 27/01/2016 at 04:57, xxxxxxxx wrote:
This is my main user thread, used for rendering:
Class MyThread(c4d.threading.C4DThread) :
thumbnail = RenderDisplay(None) # the user area
v_quality=0def Main(self) :
doc=c4d.documents.GetActiveDocument()
rd=doc.GetActiveRenderData()max_frames=doc.GetMaxTime().GetFrame(doc.GetFps())
if self.thumbnail.end_frame>max_frames:
self.thumbnail.end_frame=max_frames# bitmap size
ww=int(rd[c4d.RDATA_XRES]*self.v_quality)
hh=int(rd[c4d.RDATA_YRES]*self.v_quality)new_rd=doc.GetActiveRenderData().GetData()
new_rd[c4d.RDATA_MULTIPASS_ENABLE] = False
new_rd[c4d.RDATA_PROJECTFILE] = False
new_rd[c4d.RDATA_FRAMESEQUENCE] = 1
new_rd[c4d.RDATA_SAVEIMAGE] = False
new_rd[c4d.RDATA_MULTIPASS_SAVEIMAGE] = False
new_rd[c4d.RDATA_XRES]=ww
new_rd[c4d.RDATA_YRES]=hh
new_rd[c4d.RDATA_ANTIALIASING] = 0 # no antialiasingrImage.Init(ww,hh,24)
while self.thumbnail.current_frame<=self.thumbnail.end_frame:
new_rd[c4d.RDATA_FRAMEFROM]=c4d.BaseTime(self.thumbnail.current_frame,doc.GetFps())
new_rd[c4d.RDATA_FRAMETO]=c4d.BaseTime(self.thumbnail.current_frame,doc.GetFps())if self.thumbnail.abort==False:
res=c4d.documents.RenderDocument(doc,new_rd,rImage,c4d.RENDERFLAGS_EXTERNAL)
#res=c4d.documents.RenderDocument(doc,new_rd,rImage,c4d.RENDERFLAGS_NODOCUMENTCLONE|c4d.RENDERFLAGS_EXTERNAL,None)
self.thumbnail.current_frame=self.thumbnail.current_frame+1
else:
self.calculating=FalseAnd it is not working