1#!/pxrpythonsubst
2#
3# Copyright 2020 Pixar
4#
5# Licensed under the Apache License, Version 2.0 (the "Apache License")
6# with the following modification; you may not use this file except in
7# compliance with the Apache License and the following modification to it:
8# Section 6. Trademarks. is deleted and replaced with:
9#
10# 6. Trademarks. This License does not grant permission to use the trade
11#    names, trademarks, service marks, or product names of the Licensor
12#    and its affiliates, except as required to comply with Section 4(c) of
13#    the License and to reproduce the content of the NOTICE file.
14#
15# You may obtain a copy of the Apache License at
16#
17#     http://www.apache.org/licenses/LICENSE-2.0
18#
19# Unless required by applicable law or agreed to in writing, software
20# distributed under the Apache License with the above modification is
21# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
22# KIND, either express or implied. See the Apache License for the specific
23# language governing permissions and limitations under the Apache License.
24#
25from __future__ import print_function
26
27from pxr.UsdAppUtils.complexityArgs import RefinementComplexities
28from pxr.Usdviewq.common import RenderModes, Usd, UsdGeom
29from pxr import Vt, Gf, Sdf
30
31# Remove any unwanted visuals from the view, and enable autoClip
32def _modifySettings(appController):
33    appController._dataModel.viewSettings.showBBoxes = False
34    appController._dataModel.viewSettings.showHUD = False
35    appController._dataModel.viewSettings.autoComputeClippingPlanes = True
36
37# Set the complexity and refresh the view.
38def _setComplexity(appController, complexity):
39    appController._dataModel.viewSettings.complexity = complexity
40    appController._stageView.updateView()
41
42def _setRenderMode(appController, renderMode):
43    appController._dataModel.viewSettings.renderMode = renderMode
44    appController._stageView.updateView()
45
46def _CreatePrimvar(prim, primvarName, primvarType, interpToken, value):
47    pv = prim.CreatePrimvar(primvarName, primvarType, interpToken)
48    if not pv:
49        print("Failed to create primvar", str(primvarName))
50        return
51    pv.Set(value)
52
53def _RemovePrimvar(prim, primvarName):
54    # XXX: The UsdGeom API doesn't have a DestroyPrimvar method yet.
55    prim.RemoveProperty('primvars:' + str(primvarName))
56
57def _testAddRemovePrimvarUsedByMaterial(appController):
58    _setRenderMode(appController, RenderModes.SMOOTH_SHADED)
59    appController._takeShot("start.png")
60    stage = appController._dataModel.stage
61    prim = stage.GetPrimAtPath("/Scene/Geom/Plane")
62    mesh = UsdGeom.Imageable(prim)
63    if not mesh:
64        print("Prim", str(prim.GetPath()), "is not imageable.")
65        return
66
67    # The preview surface material has a primvar reader node with the
68    # varname 'myColor' that has a fallback value of blue.
69    # XXX Adding the primvar results in a resync currently in usdImaging, due to
70    # limitations in Hydra Storm. This will change in the near future.
71
72    # Adding the primvar expected by the material should result in the authored
73    # value (green) being used instead of the fallback (blue)
74    _CreatePrimvar(mesh, 'myColor', Sdf.ValueTypeNames.Float3, 'constant',
75        Gf.Vec3f(0.0, 1.0, 0.0))
76    appController._takeShot("add_fallback_primvar_smooth.png")
77
78    # Change the display mode to ensure repr switching in Storm picks up the
79    # primvar
80    _setRenderMode(appController, RenderModes.FLAT_SHADED)
81    appController._takeShot("add_fallback_primvar_flat.png")
82
83    # XXX Removing the primvar results in a resync in usdImaging, due to
84    # limitations in Hydra Storm. This will change in the near future.
85    # Removing the primvar should result in the fallback value being used (blue)
86    _RemovePrimvar(prim, 'myColor')
87    appController._takeShot("remove_fallback_primvar_flat.png")
88
89    _setRenderMode(appController, RenderModes.WIREFRAME_ON_SURFACE)
90    appController._takeShot("remove_fallback_primvar_wireOnSurf.png")
91
92def _testAddRemovePrimvarNotUsedByMaterial(appController):
93    _setRenderMode(appController, RenderModes.SMOOTH_SHADED)
94
95    stage = appController._dataModel.stage
96    prim = stage.GetPrimAtPath("/Scene/Geom/Plane")
97    mesh = UsdGeom.Imageable(prim)
98    if not mesh:
99        print("Prim", str(prim.GetPath()), "is not imageable.")
100        return
101
102    # XXX Adding the primvar results in a resync currently in usdImaging, due to
103    # limitations in Hydra Storm. This will change in the near future.
104    # The primvars below aren't used by the material. If primvar filtering is
105    # enabled, they will be ignored and not "make it" to the backend.
106    _CreatePrimvar(mesh, 'foo', Sdf.ValueTypeNames.Float, 'constant', 0.1)
107    _CreatePrimvar(mesh, 'bar', Sdf.ValueTypeNames.Vector3fArray, 'vertex',
108        Vt.Vec3fArray(8))
109    appController._takeShot("add_unused_primvars.png")
110
111    _RemovePrimvar(prim, 'foo')
112    _RemovePrimvar(prim, 'bar')
113    appController._takeShot("remove_unused_primvars.png")
114
115# This test adds and removes primvars that are used/unused by the material.
116def testUsdviewInputFunction(appController):
117    _modifySettings(appController)
118    _testAddRemovePrimvarUsedByMaterial(appController)
119    _testAddRemovePrimvarNotUsedByMaterial(appController)
120