Automate my renderings with Cycles

@gdomecq

Attached a simple (and not fault-tolerant) script, along with a sample model.

materialswitcher_raytraced_capturer.py (1.5 KB)

MaterialSwitcherAndAutomatedRendering.3dm (993.0 KB)

The script looks like this

# Material switcher
# and Raytraced viewcapturing

import scriptcontext as sc
import Rhino.Render as rr
import Rhino as r
import Rhino.DocObjects as rdob

import System

av = sc.doc.Views.ActiveView

cycles = sc.doc.Views.ActiveView.RealtimeDisplayMode
oldpasses = cycles.MaxPasses
cycles.MaxPasses = 2

def capture_viewport(name):
	cycles.Paused = True
	view_capture = r.Display.ViewCapture()
	view_capture.Width = av.ActiveViewport.Size.Width
	view_capture.Height = av.ActiveViewport.Size.Height
	view_capture.ScaleScreenItems = False
	view_capture.DrawAxes = False
	view_capture.DrawGrid = False
	view_capture.DrawGridAxes = False
	view_capture.TransparentBackground = False
	view_capture.RealtimeRenderPasses = 10
	bitmap = view_capture.CaptureToBitmap(av)
	if bitmap:
		folder = System.Environment.SpecialFolder.Desktop
		path = System.Environment.GetFolderPath(folder)
		filename = System.IO.Path.Combine(path, name + "_raytraced_capture.png")
		print("saving ", filename)
		bitmap.Save(filename, System.Drawing.Imaging.ImageFormat.Png)

selobs = [ob for ob in sc.doc.Objects if ob.IsSelected(False)>0]

if len(selobs) > 0:

	showroom_materials = [rm for rm in sc.doc.RenderMaterials if rm.Tags and rm.Tags.find("ShowRoom")>-1]
	
	for rm in showroom_materials:	
		for ob in selobs:
			ob.RenderMaterial = rm
			ob.CommitChanges()
			sc.doc.Views.Redraw()
		print("capturing with ", rm.Name)
		capture_viewport(rm.Name)
else:
	print("no capturing today")

cycles.MaxPasses = oldpasses
cycles.Paused = False
``

It works by having a view set to `Raytraced` and the object(s) you want to have the materials switched on selected.

The materials you want to use should be in the material editor. Those you wish to use also need to have the tag `ShowRoom` set.  This tag is used to gather all the materials in a list.

With the object selected use  `_EditPythonScript` and load the attached Python file (or copy/paste in empty script from this post).

When you select to run it you'll see that the maximum passes of the  active `Raytraced` view is changed to two. After each material application `Raytraced` is paused and then a view capture is generated of that view. Once successfully completed the capture is saved to the desktop with the name of the material and "_raytraced_capture.png"  combined.

In this sample script I have set the passes to 10 - increase that to get the required quality.

Also set the Width and Height of the view capture to something you want - if the current viewport size is good just leave the current code in.

With the sample model loaded running the script should give you four captures on your desktop.

On purpose I left a lot of niceties and handling out - I wanted to give a springboard for the community to build out a user-friendlier script :) At least filename handling should be taken care of. Right now you shouldn't use characters in material names that are forbidden in filenames, like colons, equals, asterisk, etc.

I hope that despite that it can be already useful in its current state.