-
Notifications
You must be signed in to change notification settings - Fork 2
/
create_pbr_material_with_textures.py
62 lines (49 loc) · 2 KB
/
create_pbr_material_with_textures.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
# DO NOT EDIT THIS FILE DIRECTLY. This is generated from a literate program
# with the VSCode extension Literate Programming (jesterking.literate).
#
# These literate programs are Rhino 3D scripts explaining how scripting works
# in Rhino 3D with Python.
#
# Read the literate programs at:
# https://jesterking.github.io/literate
#
# This code is part of the repository:
# https://github.com/jesterKing/rhipy
#
# The Literate Programming extension repository is at:
# https://github.com/jesterKing/literate
#
# Licensed under Apache 2.0
# See https://github.com/jesterKing/rhipy/blob/master/LICENSE
#
# Copyright (C) Nathan 'jesterKing' Letwory
#
import Rhino as rh
import Rhino.Render as rr
import Rhino.DocObjects as rd
import scriptcontext as sc
bitmap_texture_type_guid = rr.ContentUuids.BitmapTextureType
pbr_material_type_guid = rr.ContentUuids.PhysicallyBasedMaterialType
bmtex = rr.RenderContentType.NewContentFromTypeId(bitmap_texture_type_guid)
bmtex.Filename = "C:\\Users\\Nathan\\Pictures\\uvtester.png"
simtex = bmtex.SimulatedTexture(rr.RenderTexture.TextureGeneration.Allow)
# first create an empty PBR material
pbr_rm = rr.RenderContentType.NewContentFromTypeId(pbr_material_type_guid)
# to get to a Rhino.DocObjects.PhysicallyBasedMaterial we need to simulate the
# render material first.
sim = pbr_rm.SimulatedMaterial(rr.RenderTexture.TextureGeneration.Allow)
# from the simulated material we can get the Rhino.DocObjects.PhysicallyBasedMaterial
pbr = sim.PhysicallyBased
# now we have an instance of a type that has all the API you need to set the PBR
# properties. For simple glass we set color to white, opacity to 0 and opacity
# IOR to 1.52
pbr.Opacity = 0.0
pbr.OpacityIOR = 1.52
pbr.BaseColor = rh.Display.Color4f.White
pbr.SetTexture(simtex.Texture(), rd.TextureType.PBR_BaseColor)
# convert it back to RenderMaterial
new_pbr = rr.RenderMaterial.FromMaterial(pbr.Material, sc.doc)
# Set a good name
new_pbr.Name = "My Own PBR Glass"
# Add it to the document
sc.doc.RenderMaterials.Add(new_pbr)