forked from jesterKing/rhipy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathselect_object_tweak_texture_mapping.literate
94 lines (73 loc) · 2.97 KB
/
select_object_tweak_texture_mapping.literate
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
# Tweak texture mapping of textures in render material of object
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.
In case you rather just get the final script, you can find it [here](https://github.com/jesterKing/rhipy/blob/master/select_object_tweak_texture_mapping.py)
We'll assume for now that we are dealing with Rhino Custom or Rhino PBR
materials.
```py : <<the script.*>>= ./select_object_tweak_texture_mapping.py
<<imports>>
<<assume already selected objects>>
<<loop over objects and tweak texture>>
```
## Get all selected objects
We'll use a list comprehension to get the list of selected objects. Note that
we're passing `False` to `IsSelected()`, since we are not interested in
sub-objects, but rather the top level object.
```py : <<assume already selected objects>>=
obs = [ob for ob in sc.doc.Objects if ob.IsSelected(False)]
```
## Looping and tweaking
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.
```py : <<loop over objects and tweak texture>>=
for ob in obs:
<<get render material>>
<<get child slot>>
<<find texture>>
<<fiddle with texture>>
```
Simple ask for `RenderMaterial`. This should always work, but just in case, skip
handling this any further if `None` was given.
```py : <<get render material>>=
render_material = ob.RenderMaterial
if not render_material:
continue
```
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.
```py : <<get child slot>>=
mat = render_material.SimulatedMaterial(Rhino.Render.RenderTexture.TextureGeneration.Allow)
if mat.IsPhysicallyBased and mat.PhysicallyBased:
slot = Rhino.Render.RenderMaterial.StandardChildSlots.PbrBaseColor
else:
slot = Rhino.Render.RenderMaterial.StandardChildSlots.Diffuse
```
Now that we have the correct slot we can actually get the texture from our
render material.
```py : <<find texture>>=
diffuse_texture = render_material.GetTextureFromUsage(slot)
```
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.
```py : <<fiddle with texture>>=
context = Rhino.Render.RenderContent.ChangeContexts.UI
repeat = diffuse_texture.GetRepeat()
print(repeat)
repeat *= 2
repeat.Z = 0
print(repeat)
diffuse_texture.BeginChange(context)
diffuse_texture.SetRepeat(repeat, context)
diffuse_texture.EndChange()
```
## Missing bits and pieces
We still have to import everything we need:
```py : <<imports>>=
import scriptcontext as sc
import Rhino
import Rhino.Render
```