-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy path01_material_basic.py
250 lines (187 loc) · 9.54 KB
/
01_material_basic.py
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
import c4d
import maxon
import Renderer
from Renderer import Redshift, Arnold, Octane, Vray, CentiLeo, EasyTransaction, TextureHelper
from pprint import pprint
"""
These examples show how to create and modify materials
We can use Renderer.NodeGraghHelper to modify node materials who used new node graph,
And we can use Redshift.Material and Arnold.Material to get some preset helper methods.
For some reasons, AddShader-like methods(maxon.GraphModelInterface.AddChild) will add the node in the center
If we call those methods on exsited material, it will be a mess, you can call Renderer.ArrangeAll() after.
Due to Octane use his Custom UserArea UI base on old layer system, and didn't support python
We can only modify in material level, but can not interactive with selections in octane node editor.
"""
# Create a TextureHelper instance
# 创建一个 TextureHelper 实例
tex_helper: TextureHelper = TextureHelper()
# 获取资产路径, "UV Test Grid.png" with AssetId : file_5b6a5fe03176444c
url: maxon.Url = tex_helper.GetAssetUrl("file_5b6a5fe03176444c")
# "si-v1_fingerprints_02_15cm.png" with AssetId : file_fa9c42774dd05049
disp: maxon.Url = tex_helper.GetAssetUrl("file_fa9c42774dd05049")
# How to reate a redshift material and modify
def create_material():
"""
How to reate a redshift material and modify the gragh with EasyTransaction.
"""
# Create Redshift Node Material instance, if no material filled, we create a new STD material.
# 创建一个Redshift节点材质实例,如果没有材质传入,创建新的STD材质
material: c4d.BaseMaterial = Redshift.Material("MyMaterial")
# Use EasyTransaction to modify the graph
# 使用EasyTransaction来修改材质
with EasyTransaction(material) as tr:
# the attribute #tr is the instance of Redshift.MaterialHelper,
# we got it with just apply to the #material to the EasyTransaction
# it will inherit from NodeGraghHelper class
# 属性tr是Redshift.MaterialHelper的实例,通过将材质赋予EasyTransaction获得,继承自NodeGraghHelper
# Use Redshift.MaterialHelper methods : add a texture + displacement to the Output node
# 使用Redshift.MaterialHelper中的方法: 添加图片+置换节点到Output节点
tr.AddDisplacementTree(filepath = disp, shadername = "DISP")
# Use NodeGraghHelper methods: get the Output(endNode)
# 使用NodeGraghHelper中的方法: 获取输出节点
output = tr.GetOutput()
print(f"{output = }")
# Insert the material to the document
# 导入材质(来自Redshift MaterialHelper)
tr.InsertMaterial()
# Auto apply GraphTransaction.Commit() to the graph
# 退出with后, 自动执行GraphTransaction.Commit()
# Modify an exist material
# select the material we just create to call this example
def modify_material():
# 选择material
material: c4d.BaseMaterial = c4d.documents.GetActiveDocument().GetActiveMaterial()
# 自定义EasyTransaction
# 将材质转换为支持的MaterialHelper实例
with EasyTransaction(material) as tr:
# 使用NodeGraghHelper中的方法
output = tr.GetOutput()
# 使用NodeGraghHelper中的SetName方法,更改节点名称
tr.SetName(output, "My Output Node")
# Find brdf node (in this case : standard surface)
# 查找Standard Surface节点
standard_surface = tr.GetRootBRDF()
# Change a shader name
# 更改Standard Surface节点名称
tr.SetName(standard_surface,'My BRDF Shader')
# 获取Standard Surface上的base color端口
tar = tr.GetPort(standard_surface,'com.redshift3d.redshift4c4d.nodes.core.standardmaterial.base_color')
# 将纹理节点树(triplaner)到 base color 节点中
tr.AddTextureTree(shadername = 'Asset UV Map', filepath = url, raw = True, triplaner_node = True, scaleramp = False, target_port = tar)
# 添加置换树
tr.AddDisplacementTree()
# 清理未连接的节点,如果没有指认filterId,则清理全部未连接
tr.RemoveIsolateNodes()
# 退出with后, 自动执行Commit()
# Modify an exist material with selections
def access_material_data():
# 选择material
material: c4d.BaseMaterial = c4d.documents.GetActiveDocument().GetActiveMaterial()
# 自定义EasyTransaction
# 将材质转换为支持的MaterialHelper实例
with EasyTransaction(material) as tr:
# 获取选择的节点,当single_mode为True时,如果只有一个节点被选中,则返回节点(而不是列表)
act_node: maxon.GraphNode = tr.GetActiveNodes(single_mode=True)
act_port: maxon.GraphNode = tr.GetActivePorts(single_mode=True)
act_wire: list[maxon.GraphNode, maxon.GraphNode, maxon.Wires] = tr.GetActiveWires(single_mode=True)
# when we select a true node in redshift node space
# if the selected node is a image node, add a unitransform group to it
if act_node:
# Redshift空间
if tr.nodespaceId == Renderer.RS_NODESPACE:
# 如果选择的节点是纹理节点
if tr.GetShaderId(act_node) == "texturesampler":
# Set the preview image "OFF"
tr.FoldPreview(act_node, False)
# 创建一个UniTranform自定义组,统一控制纹理节点的PSR
tr.AddUniTransform(tex_shader=act_node)
# when we select a port with data type "color"
# we add a node tree to this port.
if act_port:
# 颜色 / 浮点端口
if tr.GetPortDataTypeId(act_port) == maxon.Id("net.maxon.parametrictype.col<3,float64>"):
tr.AddTexture(filepath=url, shadername="Color Texture", raw=False, target_port=act_port)
# when we select a wire,
# we insert a cc node into the wire.
if act_wire:
tr.InsertShader("com.redshift3d.redshift4c4d.nodes.core.rscolorcorrection",
act_wire,
['com.redshift3d.redshift4c4d.nodes.core.rscolorcorrection.input'],
'com.redshift3d.redshift4c4d.nodes.core.rscolorcorrection.outcolor')
# Get all connected ports.
connect_ports: list = tr.GetAllConnectedPorts()
print("All Connected Ports:")
for port in connect_ports:
print(tr.GetName(port))
print("="*10)
# 退出with后, 自动执行Commit()
# Modify an exist Octane material
def modify_octane_material():
# 如果不传入material,则初始化一个Octane material(Standard/Universal)
material: Octane.Material = Renderer.Octane.Material(create_standard=False)
# Add a float texture to roughness port
material.AddFloat(parentNode = c4d.OCT_MATERIAL_ROUGHNESS_LINK)
# Add a node tree to Albedo port, and set path and props
url: maxon.Url = tex_helper.GetAssetStr("file_ed38c13fd1ae85ae")
material.AddTextureTree(texturePath = url, nodeName = 'Albedo', isFloat = False, gamma = 2.2,
invert = False, parentNode = c4d.OCT_MATERIAL_DIFFUSE_LINK)
# Get all shader in the material
node_list = material.GetAllShaders()
# Get all the image nodes, we just have one, so we get it
imageNode = material.GetNodes(Octane.ID_OCTANE_IMAGE_TEXTURE)[0]
nodesAfter = material.GetNextNodes(imageNode)
# Print the info
print(f'We create an Octane Material with name {material.material.GetName()}')
print('#-----Shader-----#')
pprint(node_list)
print('#-----Image-----#')
pprint(imageNode)
print('#-----Shader Tree-----#')
pprint(nodesAfter)
print('#----- End -----#')
# Insert the material to the doc, activ doc is defult when None passed.
material.InsertMaterial()
# Update the material
material.Refresh()
# Push a Refresh to Cinema
c4d.EventAdd()
# Open Octane Node Editor with the material
Octane.OpenNodeEditor(material)
def ConvertTest():
material: c4d.BaseMaterial = c4d.documents.GetActiveDocument().GetActiveMaterial()
with EasyTransaction(material) as tr:
nodes = tr.GetActiveNodes()
for node in nodes:
print(f"Node: {tr.GetName(node)}")
print(f"Default Input: {tr.GetConvertInput(node)}")
print(f"Default Output: {tr.GetConvertOutput(node)}")
print("-"*10)
import c4d
import maxon
import Renderer
from Renderer import Redshift,EasyTransaction
from pprint import pprint
def test():
material: c4d.BaseMaterial = Redshift.Material("MyMaterial")
# Use EasyTransaction to modify the graph
# 使用EasyTransaction来修改材质
with EasyTransaction(material) as tr:
std = tr.GetRootBRDF()
base_color = tr.GetPort(std, "com.redshift3d.redshift4c4d.nodes.core.standardmaterial.base color")
refl_color = tr.GetPort(std, "com.redshift3d.redshift4c4d.nodes.core.standardmaterial.refl color")
ior = tr.AddConnectShader(
"com.redshift3d.redshift4c4d.nodes.core.iortometaltints",
output_ports=["com.redshift3d.redshift4c4d.nodes.core.iortometaltints.facing", "com.redshift3d.redshift4c4d.nodes.core.iortometaltints.edgetint"],
connect_outNodes=[base_color,refl_color]
)
# Insert the material to the document
# 导入材质(来自Redshift MaterialHelper)
tr.InsertMaterial()
if __name__ == '__main__':
Renderer.ClearConsole()
create_material()
# modify_material()
# access_material_data()
# modify_octane_material()
# ConvertTest()
c4d.EventAdd()