Skip to content

vray_proxies

Tools for loading looks to vray proxies.

assign_vrayproxy_shaders(vrayproxy, assignments)

Assign shaders to content of Vray Proxy.

This will create shader overrides on Vray Proxy to assign shaders to its content.

Todo

Allow to optimize and assign a single shader to multiple shapes at once or maybe even set it to the highest available path?

Parameters:

Name Type Description Default
vrayproxy str

Name of Vray Proxy

required
assignments dict

Mapping of shader assignments.

required

Returns:

Type Description

None

Source code in client/ayon_maya/tools/mayalookassigner/vray_proxies.py
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
def assign_vrayproxy_shaders(vrayproxy, assignments):
    # type: (str, dict) -> None
    """Assign shaders to content of Vray Proxy.

    This will create shader overrides on Vray Proxy to assign shaders to its
    content.

    Todo:
        Allow to optimize and assign a single shader to multiple shapes at
        once or maybe even set it to the highest available path?

    Args:
        vrayproxy (str): Name of Vray Proxy
        assignments (dict): Mapping of shader assignments.

    Returns:
        None

    """
    # Clear all current shader assignments
    plug = vrayproxy + ".shaders"
    num = cmds.getAttr(plug, size=True)
    for i in reversed(range(num)):
        cmds.removeMultiInstance("{}[{}]".format(plug, i), b=True)

    # Create new assignment overrides
    index = 0
    for material, paths in assignments.items():
        for path in paths:
            plug = "{}.shaders[{}]".format(vrayproxy, index)
            cmds.setAttr(plug + ".shadersNames", path, type="string")
            cmds.connectAttr(material + ".outColor",
                             plug + ".shadersConnections", force=True)
            index += 1

vrayproxy_assign_look(vrayproxy, product_name='lookMain')

Assign look to vray proxy.

Parameters:

Name Type Description Default
vrayproxy str

Name of vrayproxy to apply look to.

required
product_name str

Name of look product.

'lookMain'

Returns:

Type Description

None

Source code in client/ayon_maya/tools/mayalookassigner/vray_proxies.py
 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
def vrayproxy_assign_look(vrayproxy, product_name="lookMain"):
    # type: (str, str) -> None
    """Assign look to vray proxy.

    Args:
        vrayproxy (str): Name of vrayproxy to apply look to.
        product_name (str): Name of look product.

    Returns:
        None

    """
    path = cmds.getAttr(vrayproxy + ".fileName")

    nodes_by_id = get_alembic_ids_cache(path)
    if not nodes_by_id:
        log.warning("Alembic file has no cbId attributes: %s" % path)
        return

    # Group by asset id so we run over the look per asset
    node_ids_by_asset_id = defaultdict(set)
    for node_id in nodes_by_id:
        folder_id = node_id.split(":", 1)[0]
        node_ids_by_asset_id[folder_id].add(node_id)

    project_name = get_current_project_name()
    for folder_id, node_ids in node_ids_by_asset_id.items():

        # Get latest look version
        version_entity = ayon_api.get_last_version_by_product_name(
            project_name,
            product_name,
            folder_id,
            fields={"id"}
        )
        if not version_entity:
            print("Didn't find last version for product name {}".format(
                product_name
            ))
            continue
        version_id = version_entity["id"]

        relationships = lib.get_look_relationships(version_id)
        shadernodes, _ = lib.load_look(version_id)

        # Get only the node ids and paths related to this asset
        # And get the shader edits the look supplies
        asset_nodes_by_id = {
            node_id: nodes_by_id[node_id] for node_id in node_ids
        }
        edits = list(
            maya_lib.iter_shader_edits(
                relationships, shadernodes, asset_nodes_by_id
            )
        )

        # Create assignments
        assignments = {}
        for edit in edits:
            if edit["action"] == "assign":
                nodes = edit["nodes"]
                shader = edit["shader"]
                if not cmds.ls(shader, type="shadingEngine"):
                    print("Skipping non-shader: %s" % shader)
                    continue

                inputs = cmds.listConnections(
                    shader + ".surfaceShader", source=True)
                if not inputs:
                    print("Shading engine missing material: %s" % shader)

                # Strip off component assignments
                for i, node in enumerate(nodes):
                    if "." in node:
                        log.warning(
                            ("Converting face assignment to full object "
                             "assignment. This conversion can be lossy: "
                             "{}").format(node))
                        nodes[i] = node.split(".")[0]

                material = inputs[0]
                assignments[material] = nodes

        assign_vrayproxy_shaders(vrayproxy, assignments)