Autodesk / maya-usd

A common USD (Universal Scene Description) plugin for Autodesk Maya
770 stars 201 forks source link

Visibility not respected for instanced prims #522

Closed eddiehoyle closed 4 years ago

eddiehoyle commented 4 years ago

Describe the bug Changing instanced prims visibility state to "invisible" isn't respected, draws prim at origin instead.

Steps to reproduce

  1. Import to Maya a layer that contains a prim with instanceable = True and which references some imageable data.
  2. Via the UsdGeom.Imageable API, set the visibility attribute on the prim to "inherited" or "invisible"
  3. Observe the prim being redrawn at the origin when instanced = True and visibility is set from "inherited" to "invisible"
  4. If the prim is not instanced, the visibility works as expected

Here's a snippet:

from maya import cmds
from pxr import Sdf, UsdGeom

layer_ref = Sdf.Layer.CreateAnonymous()
layer_ref.ImportFromString("""#sdf 1.4.2
(
    defaultPrim = "root"
)

def Xform "root"
{
    def Mesh "plane_M_geo"
    {
        float3[] extent = [(-5, 0, -5), (5, 0, 5)]
        int[] faceVertexCounts = [4]
        int[] faceVertexIndices = [0, 1, 3, 2]
        normal3f[] normals = [(0, 1, 0)]
        point3f[] points = [(-5, 0, 5), (5, 0, 5), (-5, 0, -5), (5, 0, -5)]
    }
}""")
layer_ref.Export("/tmp/ref.usda")

layer_src = Sdf.Layer.CreateAnonymous()
layer_src.ImportFromString("""#sdf 1.4.2
(
    defaultPrim = "root"
)

def Xform "root" (
    instanceable = true
    kind = "component"
    prepend references = @/tmp/ref.usda@

)
{
    double3 xformOp:translate = (-10, 0, 0)
    uniform token[] xformOpOrder = ["xformOp:translate"]
}""")
layer_src.Export("/tmp/src.usda")

# Pseudocode for importing "/tmp/src.usda" via Proxy Shape 
# cmds.loadPlugin("...") 
# cmds.usdMaya_CreateUsdProxyShape(...)
# proxy = usdMaya.getProxy(...)
stage = proxy.getUsdStage()
prim = stage.GetPrimAtPath("/root")
vis = UsdGeom.Imageable(prim).GetVisibilityAttr()

Run these next blocks separately and repeatedly:

# Works
prim.SetInstanceable(False)
vis.Set("inherited" if vis.Get() == "invisible" else "invisible")

# Weird
prim.SetInstanceable(True)
vis.Set("inherited" if vis.Get() == "invisible" else "invisible")

Apologies for the import pseudocode, I'm unfamiliar with the exact import commands

Expected behavior Visibility state of instanced prims should be respected by the viewport.

Attachments AL_USDMaya-visibilityInstances1

Note: The locator represents world origin.

Specs (if applicable):

murphyeoin commented 4 years ago

So the problem is basically that the VP2 subscene API makes the following assumption:

The problem is that when the instance becomes invisible, HD reports that there are no instances, which maya interprets as “please render as a single object”. The problem is here:

https://github.com/Autodesk/maya-usd/blob/dev/lib/mayaUsd/render/vp2RenderDelegate/mesh.cpp#L1493

Things I’ve tried:

I’ve attempted to use the MPxSubSceneOverride (via the drawScene variable) to removeAllInstances for the rprim. This sadly fails because when there are no instances, since VP2 has been treating it as a single object.

If there are no instances found, set the world matrix for the instance to ZERO (i.e. scale the instance out of existence). This actually works in the above repro, however since single objects in the usd scene also have no instance transforms, this stops them displaying.

I attempted to remove the render item from the override, however this doesn’t appear to be possible whilst in the middle of a scene update.

I’m currently seeing if I can remap from the rprim back to the prim, to see if I can differentiate between a normal prim and an instanced prim with no visible instances. I might then be able to scale it out of existence as a temporary work around.

kxl-adsk commented 4 years ago

@murphyeoin You mentioned trouble to prevent rendering of a single object and some tricks to prevent it from being rendered, like ZERO matrix. The typical way we achieve this is by disabling render items - see https://help.autodesk.com/view/MAYAUL/2020/ENU/?guid=Maya_SDK_MERGED_cpp_ref_class_m_h_w_render_1_1_m_render_item_html

williamkrick commented 4 years ago

@murphyeoin I believe the changes in https://github.com/Autodesk/maya-usd/pull/570 will fix this issue. Can you test to confirm?

murphyeoin commented 4 years ago

Fantastic - thank you @williamkrick and @kxl-adsk - we will test right away!

eddiehoyle commented 4 years ago

Hi @williamkrick, confirming the changes in #570 fixed this. Thanks for your help!