forked from jesterKing/rhipy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathselect_object_tweak_texture_mapping.html
113 lines (105 loc) · 4.9 KB
/
select_object_tweak_texture_mapping.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<html>
<head>
<link rel="stylesheet" type="text/css" href="./style.css">
</head>
<body>
<h1>Tweak texture mapping of textures in render material of object</h1>
<p>This is a simple and straight-forward script. We import a bunch of modules and
namespaces we need, get the list of selected objects, loop over them and tweak
the diffuse or base color textures.</p>
<p>In case you rather just get the final script, you can find it <a href="https://github.com/jesterKing/rhipy/blob/master/select_object_tweak_texture_mapping.py">here</a></p>
<p>We'll assume for now that we are dealing with Rhino Custom or Rhino PBR
materials.</p>
<div class="codefragment">
<div class="fragmentname"><<the script.*>>=</div>
<div class="code">
<pre><code><<imports>>
<<assume already selected objects>>
<<loop over objects <span class="hljs-keyword">and</span> tweak texture>>
</code></pre>
</div>
</div><h2>Get all selected objects</h2>
<p>We'll use a list comprehension to get the list of selected objects. Note that
we're passing <code>False</code> to <code>IsSelected()</code>, since we are not interested in
sub-objects, but rather the top level object.</p>
<div class="codefragment">
<div class="fragmentname"><<assume already selected objects>>=</div>
<div class="code">
<pre><code>obs = [ob <span class="hljs-keyword">for</span> ob <span class="hljs-keyword">in</span> sc.doc.Objects <span class="hljs-keyword">if</span> ob.IsSelected(<span class="hljs-literal">False</span>)]
</code></pre>
</div>
</div><h2>Looping and tweaking</h2>
<p>Now that we have our object list we'll loop over each object and get its render
material. For the material we'll determine the correct child slot, retrieve the
texture, then change the repeat of its mapping.</p>
<div class="codefragment">
<div class="fragmentname"><<loop over objects and tweak texture>>=</div>
<div class="code">
<pre><code><span class="hljs-keyword">for</span> ob <span class="hljs-keyword">in</span> obs:
<<get render material>>
<<get child slot>>
<<find texture>>
<<fiddle <span class="hljs-keyword">with</span> texture>>
</code></pre>
</div>
</div><p>Simple ask for <code>RenderMaterial</code>. This should always work, but just in case, skip
handling this any further if <code>None</code> was given.</p>
<div class="codefragment">
<div class="fragmentname"><<get render material>>=</div>
<div class="code">
<pre><code>render_material = ob.RenderMaterial
<span class="hljs-keyword">if</span> <span class="hljs-keyword">not</span> render_material:
<span class="hljs-keyword">continue</span>
</code></pre>
</div>
</div><p>For now we assume either Rhino native materials (basic material or PBR). To that
end we'll simulate the render material and see if it is a PBR material.</p>
<div class="codefragment">
<div class="fragmentname"><<get child slot>>=</div>
<div class="code">
<pre><code>mat = render_material.SimulatedMaterial(Rhino.Render.RenderTexture.TextureGeneration.Allow)
<span class="hljs-keyword">if</span> mat.IsPhysicallyBased <span class="hljs-keyword">and</span> mat.PhysicallyBased:
slot = Rhino.Render.RenderMaterial.StandardChildSlots.PbrBaseColor
<span class="hljs-keyword">else</span>:
slot = Rhino.Render.RenderMaterial.StandardChildSlots.Diffuse
</code></pre>
</div>
</div><p>Now that we have the correct slot we can actually get the texture from our
render material.</p>
<div class="codefragment">
<div class="fragmentname"><<find texture>>=</div>
<div class="code">
<pre><code>diffuse_texture = render_material.GetTextureFromUsage(slot)
</code></pre>
</div>
</div><p>All that is left is to get the repeat, tweak it and set it back. For render
content to be properly updated we have to bracket it with BeginChange and
EndChange, using a suitable change context. Since our script is like a UI we'll
use the UI change context. Program or RealTimeUI would also work.</p>
<div class="codefragment">
<div class="fragmentname"><<fiddle with texture>>=</div>
<div class="code">
<pre><code>context = Rhino.Render.RenderContent.ChangeContexts.UI
repeat = diffuse_texture.GetRepeat()
<span class="hljs-built_in">print</span>(repeat)
repeat *= <span class="hljs-number">2</span>
repeat.Z = <span class="hljs-number">0</span>
<span class="hljs-built_in">print</span>(repeat)
diffuse_texture.BeginChange(context)
diffuse_texture.SetRepeat(repeat, context)
diffuse_texture.EndChange()
</code></pre>
</div>
</div><h2>Missing bits and pieces</h2>
<p>We still have to import everything we need:</p>
<div class="codefragment">
<div class="fragmentname"><<imports>>=</div>
<div class="code">
<pre><code><span class="hljs-keyword">import</span> scriptcontext <span class="hljs-keyword">as</span> sc
<span class="hljs-keyword">import</span> Rhino
<span class="hljs-keyword">import</span> Rhino.Render
</code></pre>
</div>
</div>
</body>
</html>