My test environment:
Cinema 4D version: CINEMA 4D R19.068
Renderer: Octane renderer 3.07 R2
This is my code:
# -*- coding: UTF-8 -*-
import c4d
from time import sleep
# Save path
save_path = "D:\\temp" # It's my dir for test. Change it if you dont want to save your bmp at here.
class RenderThread(c4d.threading.C4DThread):
global save_path
# Override
def __init__(self, doc, rd, bmp):
self.break_flag = False
self.doc = doc
self.rd = rd
self.bmp = bmp
self.res = None
# Override
def Main(self):
self.res = c4d.documents.RenderDocument(
self.doc,
self.rd,
self.bmp,
c4d.RENDERFLAGS_EXTERNAL
)
# Just a test for distinguish the result file is saved by which thread.
# If i get this file, it indicates that the render thread has run completed.
if self.res == c4d.RENDERRESULT_OK:
self.bmp.Save(
"{}\\RenderThread_SaveResult.jpg".format(save_path),
c4d.FILTER_JPG
)
# Override
def TestDBreak(self):
return self.break_flag
# This method is main thread.
def main():
global save_path
doc = c4d.documents.GetActiveDocument()
rd = doc.GetActiveRenderData().GetData()
bmp = c4d.bitmaps.BaseBitmap()
bmp.Init(rd[c4d.RDATA_XRES], rd[c4d.RDATA_YRES])
th = RenderThread(doc, rd ,bmp)
th.Start()
count = 0 # When "count" equals 10, end render thread.
while True:
print "Current count ->", count
# Show bmp to picture viewer
c4d.CallCommand(430000740, 430000740) # Remove All Images --> Clear history, just show current bmp
c4d.CallCommand(430000774, 430000774) # Auto Zoom Mode --> Zoom picture to fit the picture viewer
c4d.bitmaps.ShowBitmap(bmp)
# Listen for the running state of the render thread.
is_run = th.IsRunning()
print "Is render thread running ->", is_run
if not is_run:
print "End Render Thread"
# If i get this file, it indicates that the render thread has run completed.
if th.res == c4d.RENDERRESULT_OK:
th.bmp.Save(
"{}\\MainThread_SaveResult.jpg".format(save_path),
c4d.FILTER_JPG
)
break
# Test of break render thread
if count == 10:
print "Break Render Thread"
th.break_flag = True
break_res = th.TestBreak()
print "TestBreak() result ->", break_res
if break_res:
th.End(False) # It doesn't work... and the bottom of result image have a state bar...
print "Execute End()..."
print "Is render thread still running ->", th.IsRunning()
# If i get this file, it indicates that the render thread has break.
th.bmp.Save(
"{}\\Break_SaveResult.jpg".format(save_path),
c4d.FILTER_JPG
)
break
count += 1
sleep(1)
if __name__ == "__main__":
c4d.CallCommand(13957) # Clear Console
main()
There has two questions:
- In fact, render thread is not break... this is my c4d console log:
After "End()" has executed, render thread is still running...
Emmmm... how to really end render thread ?
- I got a image after "End()", but the image has a state bar at bottom...
So, can I get the image without state bar when I break render thread ?
This is the image:
Finally, I will got two images, because render thread doesn't break: