Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
7.5 kB
3
Indexable
Never
import widget_launchers.launch_widgets as lw
import rig_factory.build.utilities.controller_utilities as cut
import rig_factory.build.utilities.build_utilities as but
import rig_factory.utilities.geometry_utilities as gtl
import rig_factory.utilities.file_utilities as fut
import rig_factory.system_signals as sig

from shutil import copyfile
import sys
import os
import glob
import json

import maya.cmds as mc


def copy_parent_build(parent_asset=None, child_asset=None, project_code=None):
    """
    Run this code in Maya to copy over the parent rig build over
    :param parent_asset: Name of the parent rig asset that you want to copy from
    :type parent_asset: string

    :param child_asset: Name of the child rig asset that you want to copy to
    :type child_asset: string

    :param project_code: Name of the project, eg. AWB
    :type project_code: string
    """
    if not parent_asset:
        mc.warning('Need to define a parent asset')
        return

    # If child_asset is not defined, then use whatever asset this code is being run in
    if not child_asset:
        child_asset = os.getenv('TT_ENTNAME')

    # If project_code is not defined, then use whatever project this code is being run in
    if not project_code:
        project_code = os.getenv('TT_PROJCODE')

    # Grab the user folder
    user = os.getenv('USER')

    # Check if controller has been initialized

    controller = cut.get_controller()

    if not controller:
        lw.launch_rig_widget()
        controller = cut.get_controller()

    # Check if iRig has been launched(Need it for show code import path etc..)
    if not 'G:/Rigging/Shows/{0}'.format(project_code) in sys.path:
        from iRig_maya.toolbox import toolboxUI
        reload(toolboxUI)
        toolboxUI.launch()

    top_parent_path = glob.glob('Y:/{0}/assets/type/*/{1}'.format(project_code, parent_asset))[0]
    top_child_path = glob.glob('Y:/{0}/assets/type/*/{1}'.format(project_code, child_asset))[0]

    # Find parent rig build folder
    parent_build_directory = os.path.join(top_parent_path, 'products/rig_build')
    parent_build_versions = os.listdir(parent_build_directory)
    latest_parent_build_folder = sorted(parent_build_versions, key=lambda x: os.path.basename(x))[-1]  # Find latest
    latest_parent_build_folder = os.path.join(parent_build_directory, latest_parent_build_folder)
    child_build_directory = os.path.join(top_child_path, 'work/rig/Maya/{0}/build'.format(user))

    # Check to see if build folder exists, if not, then create a build folder
    if not os.path.exists(child_build_directory):
        os.makedirs(child_build_directory)

    # Copy over custom_proxy_data.json(Proxy shader)
    if not os.path.exists(os.path.join(latest_parent_build_folder, 'custom_proxy_data.json')):
        parent_proxy_data_path = os.path.join(top_parent_path, 'work/elems/rig_data/custom_proxy_data.json')
        child_proxy_data_path = os.path.join(top_child_path, 'work/elems/rig_data/custom_proxy_data.json')

        # If proxy data for the child already exists, then skip as we don't want to overwrite anything
        if not os.path.exists(child_proxy_data_path) and os.path.exists(parent_proxy_data_path):
            child_rig_data_path = os.path.join(top_child_path, 'work/elems/rig_data'.format(project_code, child_asset))
            if not os.path.exists(child_rig_data_path):
                os.makedirs(child_rig_data_path)
            copyfile(parent_proxy_data_path, child_proxy_data_path)

    # Copy over rest of the files from parent rig
    for file_name in os.listdir(latest_parent_build_folder):
        if file_name != 'rig_blueprint.json':
            parent_file_path = os.path.join(
                latest_parent_build_folder, file_name
            )
            child_file_path = os.path.join(
                child_build_directory, file_name
            )
            copyfile(parent_file_path, child_file_path)

    # Set build directory to parent rig build folder
    controller.build_directory = latest_parent_build_folder
    rig_blueprint_path = os.path.join(controller.build_directory, 'rig_blueprint.json')
    with open(rig_blueprint_path, mode='r') as f:
        blueprint_data = json.load(f)
    but.build(
        controller,
        controller.build_directory,
        blueprint_data
    )

    # Toggle to guide state
    but.toggle_state(controller)
    lw.launch_rig_widget()  # Launch rig widget again to update view in the UI

    # Save handle vertices data ,remove vert association info from cloth container
    handle_vertices = controller.root.get_handle_mesh_positions()

    cloth_part = controller.named_objects.get('C_Clothes_Container', None)
    if cloth_part:
        cloth_part_handle_vertices = cloth_part.get_handle_mesh_positions()
        handle_vertices = {k: v for k, v in handle_vertices.items() if k not in cloth_part_handle_vertices}

    # Delete geos and not the origin geos as we might lose vert association info if we delete them
    #     # Empty geometry paths
    controller.root.geometry_paths = []

    geometry = controller.root.geometry_group.children
    controller.schedule_objects_for_deletion(geometry)
    controller.delete_scheduled_objects()

    # Import latest geometry from child rig
    abc_directory = os.path.join(top_child_path, 'products/abc')
    abc_versions = os.listdir(abc_directory)
    parent = controller.root.geometry_group
    latest_abc = sorted(abc_versions, key=lambda x: os.path.basename(x))[-1]  # Find latest abc
    latest_abc_path = os.path.normpath(os.path.join(abc_directory, latest_abc)).replace(os.path.sep, '/')
    gtl.import_container_geometry_path(
        controller.root,
        latest_abc_path,
        parent,
        'geometry'
    )
    controller.root.geometry_paths.append(latest_abc_path)

    # Load handle vertices data from earlier
    controller.root.set_handle_mesh_positions(handle_vertices)

    # Replace utility geometry path
    utility_geometry_path = []
    for geos in controller.root.utility_geometry_paths:
        geo_names = geos.split('/')[-1]
        utility_geometry_path.append(os.path.normpath(
            os.path.join(child_build_directory, geo_names)
        ).replace(os.path.sep, '/'))
    controller.root.utility_geometry_paths = utility_geometry_path

    '''
    You can add custom code here on a per asset basis to make changes needed, for example, making adjustments
    to the position of foot pivots or deleting any extra parts that are not needed from the parent rig
    '''

    # Save work
    controller = cut.get_controller()
    but.save_work_scene(child_build_directory, controller, comment='Migration from Parent')

    # Copy over rest of the files from parent rig again cause for some reason it gets removed from saving
    for file_name in os.listdir(latest_parent_build_folder):
        if file_name != 'rig_blueprint.json':
            parent_file_path = os.path.join(
                latest_parent_build_folder, file_name
            )
            child_file_path = os.path.join(
                child_build_directory, file_name
            )
            copyfile(parent_file_path, child_file_path)

    # Update build directory
    controller.build_directory = fut.get_user_build_directory()
    sig.build_directory_changed.emit(controller.build_directory)

copy_parent_build('Hammer_Bird_A_V2')