<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>MaxScript Archive - LEFX</title>
	<atom:link href="https://www.lefx.de/en/tag/maxscript/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.lefx.de/en/tag/maxscript/</link>
	<description>manufacturing realities</description>
	<lastBuildDate>Wed, 18 Nov 2020 14:13:20 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	
	<item>
		<title>Workflow Tips &#124; Random Material ID</title>
		<link>https://www.lefx.de/en/workflow-tips-random-material-id/</link>
					<comments>https://www.lefx.de/en/workflow-tips-random-material-id/#respond</comments>
		
		<dc:creator><![CDATA[Tom Micklich]]></dc:creator>
		<pubDate>Sat, 19 Dec 2015 11:47:14 +0000</pubDate>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[3ds Max]]></category>
		<category><![CDATA[MaxScript]]></category>
		<category><![CDATA[Workflow]]></category>
		<guid isPermaLink="false">http://www.lefx.de/?p=572</guid>

					<description><![CDATA[<p>One of the main problems of rendering is its perfection. Tree objects, that consist of many leaf elements with the same material applied to them, are often lack the natural variations the viewer would expect. In our production workflow we faced this problem on a daily basis. As we always try to improve the quality [&#8230;]</p>
<p>Der Beitrag <a href="https://www.lefx.de/en/workflow-tips-random-material-id/">Workflow Tips | Random Material ID</a> erschien zuerst auf <a href="https://www.lefx.de/en">LEFX</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>One of the main problems of rendering is its perfection. Tree objects, that consist of many leaf elements with the same material applied to them, are often lack the natural variations the viewer would expect. In our production workflow we faced this problem on a daily basis. As we always try to improve the quality of our architectural renderings we decided to create an easy to use script to set random material IDs to give the perception of natural variations.</p>
<p>Our main content creation software is 3ds Max, so we created a Macro Script (in MAXScript) for this problem.</p>
<p><img decoding="async" class="img-responsive" src="https://www.lefx.de/src-content/uploads/2016/02/DE_intro.png" alt="DE_intro" /></p>
<h2>Recursive or iterative approach</h2>
<p>There are basically two possible approaches to handle this problem. The iterative first oneone iterates through all faces for setting a random material ID while the recursive one version divides the big vast amount of faces into smaller parts groups(groups?) and then assigns a material ID to all the elements contained in this group. ?.connected to the face selection.</p>
<p>At first we implemented the iterative approach to take a look at its possibilities and restrictions. After that we thought about possible optimizations and other ideas how to solve this problem more efficiently with an optimum balance of between speed and a visual qualitygood result. That’s why we created further looked into the a recursive version to solve the problem from another perspective point of view.</p>
<h2>Algorithms</h2>
<h3>Iterative version</h3>
<pre style="color: #000000; background: #f1f0f0;"><span style="color: #400000; font-weight: bold;">function</span> changeIDsIterative obj minID maxID stepSize <span style="color: #806030;">=</span>
<span style="color: #806030;">(</span>
	getElement <span style="color: #806030;">=</span> polyop<span style="color: #8c0000;">.</span>getElementsUsingFace
	setFaceID <span style="color: #806030;">=</span> polyop<span style="color: #8c0000;">.</span>setFaceMatID
			
	range <span style="color: #806030;">=</span> <span style="color: #8c0000;">1</span>
			
	<span style="color: #400000; font-weight: bold;">for</span> i <span style="color: #806030;">=</span> <span style="color: #8c0000;">1</span> <span style="color: #400000; font-weight: bold;">to</span> obj<span style="color: #8c0000;">.</span>numfaces by stepSize <span style="color: #400000; font-weight: bold;">do</span> <span style="color: #806030;">(</span>
				
		randomID <span style="color: #806030;">=</span> random minID maxID
				
		range <span style="color: #806030;">=</span> i <span style="color: #806030;">+</span> stepSize
				
		<span style="color: #400000; font-weight: bold;">if</span><span style="color: #806030;">(</span>range <span style="color: #806030;">&gt;</span> obj<span style="color: #8c0000;">.</span>numfaces<span style="color: #806030;">)</span> <span style="color: #400000; font-weight: bold;">do</span> <span style="color: #806030;">(</span>
			range <span style="color: #806030;">=</span> obj<span style="color: #8c0000;">.</span>numfaces
		<span style="color: #806030;">)</span>
		
		element <span style="color: #806030;">=</span> getElement obj <span style="color: #806030;">#</span>{i<span style="color: #8c0000;">..</span>range}
		setFaceID obj element randomID
	<span style="color: #806030;">)</span>
<span style="color: #806030;">)</span>
</pre>
<p>Basically this script iterates through all faces with a given step. There it takes the takes the elements which are connected with the faces within the step range and gives this selection of faces a random material ID in the range of the minimal ID and the maximal ID (minID &lt;= ID &lt;= maxID).</p>
<p>Larger values of stepsizestep size would give result in more speed by skipping a lot of faces but would result in a visible repetition of the same used material ID.</p>
<p>So the number of function calls when to setting a random material ID depends on the step size s:</p>
<p><img decoding="async" class="img-responsive" src="https://www.lefx.de/src-content/uploads/2016/02/Formel0.png" alt="Formel0" /></p>
<p>You may have noticed that in the first two lines two function calls are assigned to variables.</p>
<pre style="color: #000000; background: #f1f0f0;">getElement <span style="color: #806030;">=</span> polyop<span style="color: #8c0000;">.</span>getElementsUsingFace
setFaceID <span style="color: #806030;">=</span> polyop<span style="color: #8c0000;">.</span>setFaceMatID
</pre>
<p>That’s because ofThis is done for performance reasons. By Iinitializing the function call as a variable these many function callswill allow it to run are slightly faster.</p>
<h3>Recursive version</h3>
<pre style="color: #000000; background: #f1f0f0;"><span style="color: #400000; font-weight: bold;">function</span> changeIDs depth obj minFace maxFace minID maxID <span style="color: #806030;">=</span>
<span style="color: #806030;">(</span>
	<span style="color: #400000; font-weight: bold;">if</span><span style="color: #806030;">(</span>depth <span style="color: #806030;">&gt;</span> <span style="color: #8c0000;">0</span><span style="color: #806030;">)</span> <span style="color: #400000; font-weight: bold;">then</span> <span style="color: #806030;">(</span> <span style="color: #806030;">-</span><span style="color: #806030;">-</span>Clustering<span style="color: #806030;">,</span> <span style="color: #400000; font-weight: bold;">if</span> depth !<span style="color: #806030;">=</span> <span style="color: #8c0000;">0</span>
		partSize <span style="color: #806030;">=</span> <span style="color: #806030;">(</span><span style="color: #806030;">(</span>maxFace <span style="color: #806030;">-</span> minFace<span style="color: #806030;">)</span> <span style="color: #806030;">/</span> <span style="color: #8c0000;">2</span> <span style="color: #806030;">+</span> <span style="color: #8c0000;">1</span><span style="color: #806030;">)</span> <span style="color: #400000; font-weight: bold;">as</span> <span style="color: #400000; font-weight: bold;">Integer</span> <span style="color: #806030;">-</span><span style="color: #806030;">-</span>calculate the clustersize
		
		changeIDs <span style="color: #806030;">(</span>depth<span style="color: #806030;">-</span><span style="color: #8c0000;">1</span><span style="color: #806030;">)</span> obj minFace <span style="color: #806030;">(</span>partSize <span style="color: #806030;">+</span> minFace<span style="color: #806030;">)</span> minID maxID <span style="color: #806030;">-</span><span style="color: #806030;">-</span> first part
		changeIDs <span style="color: #806030;">(</span>depth<span style="color: #806030;">-</span><span style="color: #8c0000;">1</span><span style="color: #806030;">)</span> obj <span style="color: #806030;">(</span>partSize <span style="color: #806030;">+</span> minFace <span style="color: #806030;">+</span> <span style="color: #8c0000;">1</span><span style="color: #806030;">)</span> maxFace minID maxID <span style="color: #806030;">-</span><span style="color: #806030;">-</span> <span style="color: #400000; font-weight: bold;">second</span> part 
		
	<span style="color: #806030;">)</span> <span style="color: #400000; font-weight: bold;">else</span> <span style="color: #806030;">(</span> <span style="color: #806030;">-</span><span style="color: #806030;">-</span><span style="color: #400000; font-weight: bold;">set</span> material IDs
		getElement <span style="color: #806030;">=</span> polyop<span style="color: #8c0000;">.</span>getElementsUsingFace
		setFaceID <span style="color: #806030;">=</span> polyop<span style="color: #8c0000;">.</span>setFaceMatID
		
		randomID <span style="color: #806030;">=</span> random minID maxID 
                <span style="color: #806030;">-</span><span style="color: #806030;">-</span><span style="color: #400000; font-weight: bold;">get</span> a random ID between the given values
		element <span style="color: #806030;">=</span> getElement obj <span style="color: #806030;">#</span>{minFace<span style="color: #8c0000;">..</span>maxFace} 
                <span style="color: #806030;">-</span><span style="color: #806030;">-</span><span style="color: #400000; font-weight: bold;">select</span> all elements <span style="color: #400000; font-weight: bold;">with</span> faces in the given cluster of faces
		setFaceID obj element randomID 
                <span style="color: #806030;">-</span><span style="color: #806030;">-</span><span style="color: #400000; font-weight: bold;">set</span> the random ID <span style="color: #400000; font-weight: bold;">to</span> all faces in the selection
	<span style="color: #806030;">)</span>
<span style="color: #806030;">)</span>
</pre>
<p>The recursive version divides the huge amount of into smaller clusters of faces before setting a material ID.</p>
<p>The algorithm basically divides the amount of faces into halves until the depth value is zero. Then for every subpart the script selects all elements which are connected with the faces of the subpart and assigns a random ID. So the second part is equal to the iterative version.</p>
<p>The only difference is the manner of selecting the faces.</p>
<p>Example for depth = 3</p>
<p><img decoding="async" class="img-responsive" src="https://www.lefx.de/src-content/uploads/2016/02/DepthValue.png" alt="DepthValue" /></p>
<p>So the number of parts where to set a random material ID is:</p>
<p><img decoding="async" class="img-responsive" src="https://www.lefx.de/src-content/uploads/2016/02/Formel2.png" alt="Formel2" /></p>
<p>Dividing the amount of faces recursively into subparts is very fast. The main bottleneck of this script is the access of the 3dx Max-Interfaces getElement and setFaceID.</p>
<p>The equivalent parameters for iterative and recursive function calls can be calculated by:</p>
<p><img decoding="async" class="img-responsive" src="https://www.lefx.de/src-content/uploads/2016/02/Formel1.png" alt="Formel1" /></p>
<h3>Which one is better now?</h3>
<p>Basically both version do the same work and should be able to give a comparable result. The main differences are the parameters and the internal procedure.</p>
<p>If you have an object with a lot of polygons and (nearly) every element should have a random material ID then the iterative version will be easier to use because you simply enter 1 or comparable low values for the step size s. or other small values. Then the script would go assignto a random ID to every face and set a random ID. But On the other hand if the elements have a lot of faces the work would often be done more than once. This will be also very slow because of many interface accesses of 3ds Max.</p>
<p>Then it would be easier to use the recursive approach and begin with small values for the depth until you get a satisfying result with enough mixture by increasing the depth value.</p>
<h2>Examples</h2>
<h3>Different material IDs: 3</h3>
<p><img decoding="async" class="img-responsive" src="https://www.lefx.de/src-content/uploads/2016/02/DE_mat.png" alt="DE_mat" /></p>
<h3>Duration &#8211; iterative</h3>
<p><img decoding="async" class="img-responsive" src="https://www.lefx.de/src-content/uploads/2016/02/itereative.png" alt="Iterative" /></p>
<h3>Duration &#8211; recursive</h3>
<p><img decoding="async" class="img-responsive" src="https://www.lefx.de/src-content/uploads/2016/02/rekursiv.png" alt="Rekursiv" /></p>
<p>It was a pleasure for us to show you these little thoughts about our workflow. We hope you had fun while reading the text and looking to the pictures. To see more of our projects, visit our <a href="https://www.lefx.de/showroom/">Showroom</a>.</p>
<p>Der Beitrag <a href="https://www.lefx.de/en/workflow-tips-random-material-id/">Workflow Tips | Random Material ID</a> erschien zuerst auf <a href="https://www.lefx.de/en">LEFX</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.lefx.de/en/workflow-tips-random-material-id/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
