{"id":269,"date":"2008-12-04T18:15:45","date_gmt":"2008-12-05T00:15:45","guid":{"rendered":"http:\/\/www.mrbluesummers.com\/?page_id=269"},"modified":"2012-02-21T16:56:22","modified_gmt":"2012-02-21T16:56:22","slug":"scripted-clay-rendering-tutorial","status":"publish","type":"post","link":"http:\/\/www.mrbluesummers.com\/269\/3d-tutorials\/scripted-clay-rendering-tutorial","title":{"rendered":"Scripted Clay Renders"},"content":{"rendered":"

I was thinking hard about the 3d Studio Max clay rendering tutorial I wrote a few days ago, and was pondering about what an extension might be. Then it struck me! “Gosh, wouldn’t it be cool to have a button in the 3dsMax interface that you could click, and have it automatically turn out a clean render while you go make a sandwich?”<\/p>\n

The answer is “of course”!<\/p>\n

\"The<\/a><\/p>\n

Adding the button to your tool bar.<\/p>\n<\/div>\n

So let’s have a look at that. What kind of 3d Studio knowledge would we need to take that on? Well, first we need our trusty Maxscript help file (Help > Maxscript help…) and a then a healthy dose of patience. Note: While I was writing and coding for this tutorial, I hit a few snags myself, so don’t think that it’s always a piece of cake. I’ll clue you in to some of my difficulties along the way so that you can learn from my mistakes!<\/p>\n

Step 1: Copy the basic macroscript code.<\/h1>\n

Now this is a good step 1 for any<\/em> project you take on in Maxscript. For this case, you can either copy my code below or you can go into the document and search for it yourself. I’d recommend finding it yourself (or better yet, typing it in manually) because it’s a better learning experience. However, I won’t hold it against you if you want to copy it- just this once.<\/p>\n

macroScript Clay_Renderer\r\ncategory:\"Custom\"\r\ntooltip:\"Take a clay render\"\r\nbuttontext:\"Clay Render\"\r\n(\r\n)<\/pre>\n

Let’s run through the lines of code so we get a good grip on what it means. The first line declares everything inside as a macroscript, and gives it an internal name. The category string lets 3d Studio Max easily group it with other user interface items so you can put it in the toolbar or in a modular toolbar later. The tooltip is what appears when you hover over the macroscript button when it’s in the user interface, and the button text is what the button says on it.<\/p>\n

Step 2: Think.<\/h1>\n

Inside this codeblock, we need to put what will happen when a user clicks on it. What’re some things we want it to do? Well, hopefully you’ve read my last tutorial<\/a>, so you have a good grip on the ins and outs of clay rendering. We know we want to a) apply a white material to everything in the scene, b) have the scanline renderer in charge, and c) use a skylight.<\/p>\n

I’d like to take a theoretical aside here and talk about a major hang-up on this particular script. You’ll notice that we’re messing with the materials in the scene and the lights. These changes need to be undoable or they’ll ruin everything. Can you imagine if you took a clay render and it took all your materials off your objects? Or if your lights disappeared? I’ll be including bits of code from here on out that help make this script more applicable and safer for scenes with materials and lights already in them!<\/p>\n

Step 3: Lay down the foundations.<\/h1>\n
macroScript Clay_Renderer\r\ncategory:\"Custom\"\r\ntooltip:\"Take a clay render\"\r\nbuttontext:\"Clay Render\"\r\n(\r\n\tundo on\r\n\t(\r\n\t  -- Create a white material and assign it to everything.\r\n\r\n\t  -- Delete all the lights and create a skylight instead.\r\n\r\n\t  -- Store the current render settings, and set to scanline.\r\n\r\n\t  -- Render\r\n\r\n\t  -- Revert to the old renderer\r\n\t)\r\n\t-- Undo everything we did, leaving only the render.\r\n)<\/pre>\n

So I’ve only made a single structural change here, which is adding the “undo on” wrapping. This makes it so that the entire macroscript takes up only a single block in 3d Studio Max’s undo history. This way any changes made within this codeblock can be undone with a single command. Neat huh?<\/p>\n

The remaining stuff is all comments. I usually start my scripts off by hammering out comments that say what needs to get done. I recommend that you consider doing the same; it helps you stay organized and think things through. In this case, you see we need to create a white material and apply it to all the objects in the scene. Then we need to delete all the lights, which is easier than turning them off and back on again. We also have to create the shadowing skylight so that we get the smooth shading effect. After that, we can store the current render settings, and switch over to scanline. Finally, we render and then revert back to the old Renderer.<\/p>\n

Step 4: Fill code.<\/h1>\n
macroScript Clay_Renderer\r\ncategory:\"Custom\"\r\ntooltip:\"Take a clay render\"\r\nbuttontext:\"Clay Render\"\r\n(\r\n\tundo on\r\n\t(\r\n\t\t-- Create a white material and assign it to everything.\r\n\t\tWhiteMat = standard diffuse:(color 220 220 220) twosided:true\r\n\t\tfor obj in objects do obj.material = WhiteMat\r\n\r\n\t\t-- Delete all the lights and create a skylight instead.\r\n\t\tfor lux in lights do delete lux\r\n\t\tThisSkylight = skylight castShadows:true rays_per_sample:12\r\n\r\n\t\t-- Store the current render settings, and set to scanline.\r\n\r\n\t\t-- Render\r\n\r\n\t\t-- Revert to the old renderer\r\n\t)\r\n\t-- Undo everything we did, leaving only the render.\r\n)<\/pre>\n

So we’ve added the information regarding the material. In this case we’re creating a material called “WhiteMat” which is a standard material with a diffuse color of 220 grey and it’s two-sided. Why? Because if you think about it, a clay render doesn’t show off all the geometry if it can’t render backfacing polygons. Worse yet, even though backfacing polygons wouldn’t show up in the render, they’ll still affect the lighting solution which would create some unpleasant artifacts. It’s best to just set this parameter to true.<\/p>\n

Next I’ve added in the code for deleting the lights and creating the skylight. What we’re basically saying in the first line is “For each light ‘lux’, delete it”. This can always be undone at the end of the script. In the next line, we create a new skylight called “ThisSkylight”, which is casting shadows at a rate of 12 rays per sample.<\/p>\n

macroScript Clay_Renderer\r\ncategory:\"Custom\"\r\ntooltip:\"Take a clay render\"\r\nbuttontext:\"Clay Render\"\r\n(\r\n\tundo on\r\n\t(\r\n\t\t-- Create a white material and assign it to everything.\r\n\t\tWhiteMat = standard diffuse:(color 220 220 220) twosided:true\r\n\t\tfor obj in objects do obj.material = WhiteMat\r\n\r\n\t\t-- Delete all the lights and create a skylight instead.\r\n\t\tfor lux in lights do delete lux\r\n\t\tThisSkylight = skylight castShadows:true rays_per_sample:12\r\n\r\n\t\t-- Store the current render settings, and set to scanline.\r\n\t\tExistingRenderSettings = renderers.production\r\n\t\trenderers.production = default_scanline_renderer()\r\n\r\n\t\t-- Render\r\n\t\trender()\r\n\r\n\t\t-- Revert to the old renderer\r\n\t\trenderers.production = ExistingRenderSettings\r\n\t)\r\n\t-- Undo everything we did, leaving only the render.\r\n\tmax undo\r\n)<\/pre>\n

Here we see the storage and changing of the renderer. We take this extra step because if you’re working on a scene in mental ray, but want a clay render, you shouldn’t have to go through the trouble of a) writing a totally different clay render script or b) storing your settings manually. We store the production renderer to a variable, and then set it to a fresh instance of the default scanline renderer. A fresh instance has all the default values, so we know there won’t be any kind of crazy settings getting in the way of the clay render.<\/p>\n

Finally, we can take the render. Once that is complete, the script resets the production renderer back to what it should be, and undoes any weird changes (like deleted lights) that the script executed. We’re back to where we started except now we have an awesome clay render in the render window!<\/p>\n

The best part of this is that now we have a button that we can place anywhere in the 3dsMax user interface that will create a clay render on command!<\/p>\n

Hit the hotkey Ctrl+E to run the script so that it will appear as a macroscript. You can save the file to your <3dsMax Root>\/UI\/Macroscripts directory, and it’ll be loaded automatically every time you start 3d Studio Max. To add your new “Clay Render” button to the main toolbar, just click on the menu “Customize > Customize User Interface”. Go to the “Toolbars” tab, and from the category pulldown select “Custom”, and you’ll see your clay render button. Just drag and drop it from the “Action” window to the main toolbar, and the button will appear.<\/p>\n

\"The<\/a><\/p>\n

Adding the button to your tool bar.<\/p>\n<\/div>\n

And there you have it! Now you can get a clean clay render no matter what your material, lighting, or rendering setup is!<\/p>\n

Until next time, happy rendering!<\/p>\n","protected":false},"excerpt":{"rendered":"

I was thinking hard about the 3d Studio Max clay rendering tutorial I wrote a few days ago, and was pondering about what an extension might be. Then it struck me! “Gosh, wouldn’t it be cool to have a button in the 3dsMax interface that you could click, and have it automatically turn out a […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12,111,20,16],"tags":[2567,22,2565,88],"_links":{"self":[{"href":"http:\/\/www.mrbluesummers.com\/wp-json\/wp\/v2\/posts\/269"}],"collection":[{"href":"http:\/\/www.mrbluesummers.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.mrbluesummers.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.mrbluesummers.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.mrbluesummers.com\/wp-json\/wp\/v2\/comments?post=269"}],"version-history":[{"count":0,"href":"http:\/\/www.mrbluesummers.com\/wp-json\/wp\/v2\/posts\/269\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.mrbluesummers.com\/wp-json\/wp\/v2\/media?parent=269"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.mrbluesummers.com\/wp-json\/wp\/v2\/categories?post=269"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.mrbluesummers.com\/wp-json\/wp\/v2\/tags?post=269"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}