Ejemplo n.º 1
0
class Dialog(Q.Widgets.Dialog):
    def __init__(self, path=None):
        super(Dialog, self).__init__()
        self._node = None
        self._path = path
        self._setup_ui()

    def _setup_ui(self):
        self.setWindowTitle("Copy Publish to Work Area")

        self.setLayout(Q.VBoxLayout())

        self._workspace = workspace = self._path or cmds.workspace(
            q=True, rootDirectory=True)
        self._model, self._picker = picker_presets.publishes_from_path(
            workspace)
        self._model.register_node_type(ScenePickerNode)
        self._picker.setMaximumHeight(400)
        self._picker.nodeChanged.connect(self._on_node_changed)
        self._picker.setColumnWidths([200] * 10)
        self.layout().addWidget(self._picker)

        self._namer = SceneNameWidget(dict(workspace=workspace))
        self.layout().addWidget(self._namer)

        button_layout = Q.HBoxLayout()
        self.layout().addLayout(button_layout)

        self._cancel_button = Q.PushButton("Cancel")
        self._cancel_button.clicked.connect(self._on_cancel_pressed)
        button_layout.addWidget(self._cancel_button)

        button_layout.addStretch()

        self._copy_button = Q.PushButton("Copy")
        self._copy_button.setEnabled(False)
        self._copy_button.clicked.connect(self._on_copy_pressed)
        button_layout.addWidget(self._copy_button)

        self._open_button = Q.PushButton("Copy and Open")
        self._open_button.setEnabled(False)
        self._open_button.clicked.connect(self._on_open_pressed)
        button_layout.addWidget(self._open_button)

        self._preview = Preview()
        self._picker.setPreviewWidget(self._preview)
        self._picker.updatePreviewWidget.connect(self._on_update_preview)

    def _on_node_changed(self, node):

        self._node = node
        self._enable = 'PublishEvent' in node.state

        # Button only works when there is a publish.
        self._copy_button.setEnabled(self._enable)
        self._open_button.setEnabled(self._enable)

        if self._enable:
            self._task_path = self._model.sgfs.path_for_entity(
                node.state['Task'])

            publish = node.state.get('PublishEvent')

            # TODO: Strip this with the scene_name(r)'s tools, when they exist.
            detail = publish['code']
            step_name = publish.fetch('link.Task.step.Step.short_name')
            if detail.lower().startswith(step_name.lower()):
                detail = detail[len(step_name):].lstrip('_')

            self._namer._namer.detail = detail
            self._namer._namer.extension = ext = os.path.splitext(
                publish['sg_path'])[1]

            basename = self._namer._namer.get_basename()
            self._namer._namer.revision = utils.get_next_revision(
                os.path.join(self._workspace, 'scenes'),
                os.path.splitext(basename)[0],
                ext,
                1,
            )

            self._namer.namer_updated()
            self._namer.update_preview()

    def _on_update_preview(self, index):
        node = self._model.node_from_index(index)
        entity = node.state['PublishEvent']
        self._preview.update(entity)

    def _on_cancel_pressed(self):
        self.hide()

    def _on_open_pressed(self):
        self._on_copy_pressed(open_=True)

    def _on_copy_pressed(self, state=None, open_=False):

        src_path = self._node.state.get('maya_scene')
        if not src_path:
            publish = self._node.state['PublishEvent']
            src_path = publish.fetch('sg_path')

        dst_path = self._namer._namer.get_path()

        # TODO: Do this with shutil
        subprocess.check_call(['cp', src_path, dst_path])
        subprocess.check_call(['chmod', 'a+w', dst_path])

        self.hide()

        if open_:

            # Make sure they want to proceed if there are changes to the file.
            if cmds.file(q=True, modified=True):
                res = Q.MessageBox.warning(
                    self, "Unsaved Changes",
                    "Would you like to save your changes before opening the copied file?",
                    Q.MessageBox.Save | Q.MessageBox.No | Q.MessageBox.Cancel,
                    Q.MessageBox.Save)
                if res & Q.MessageBox.Cancel:
                    return
                if res & Q.MessageBox.Save:
                    cmds.file(save=True)

            cmds.file(dst_path, open=True, force=True)