def path_to_shot(self, parents, item):
        import traceback

        import ftrack

        path = []
        for p in parents:
            path.append(p.getName())
        path.reverse()

        # Setup parent.
        parent = parents[0]
        if "--" in item.name():
            name_split = item.name().split("--")

            if len(name_split) == 2:
                try:
                    copy_path = list(path)
                    copy_path.append(name_split[0])
                    parent = ftrack.getSequence(copy_path)
                except:
                    self.log.error(traceback.format_exc())
                    parent = parents[0].createSequence(name_split[0])

            if len(name_split) == 3:
                try:
                    copy_path = list(path)
                    copy_path.append(name_split[0])
                    parent = ftrack.getSequence(copy_path)
                except:
                    self.log.error(traceback.format_exc())
                    parent = parents[0].createEpisode(name_split[0])

                parents = [parent] + parents

                try:
                    copy_path.append(name_split[1])
                    parent = ftrack.getSequence(copy_path)
                except:
                    self.log.error(traceback.format_exc())
                    parent = parents[0].createSequence(name_split[1])

        return copy_path, parent
    def path_to_shot(self, parents, item):
        import traceback

        import ftrack

        path = []
        for p in parents:
            path.append(p.getName())
        path.reverse()

        # Setup parent.
        parent = parents[0]
        if "--" in item.name():
            name_split = item.name().split("--")

            if len(name_split) == 2:
                try:
                    copy_path = list(path)
                    copy_path.append(name_split[0])
                    parent = ftrack.getSequence(copy_path)
                except:
                    self.log.error(traceback.format_exc())
                    parent = parents[0].createSequence(name_split[0])

            if len(name_split) == 3:
                try:
                    copy_path = list(path)
                    copy_path.append(name_split[0])
                    parent = ftrack.getSequence(copy_path)
                except:
                    self.log.error(traceback.format_exc())
                    parent = parents[0].createEpisode(name_split[0])

                parents = [parent] + parents

                try:
                    copy_path.append(name_split[1])
                    parent = ftrack.getSequence(copy_path)
                except:
                    self.log.error(traceback.format_exc())
                    parent = parents[0].createSequence(name_split[1])

        return copy_path, parent
Example #3
0
    def create_from_structure(self, parent, structure, projectFolder,
                              tmpFolder):
        """Create *structure* under *parent*."""
        level = structure[0]
        children = structure[1:]
        object_type = level['object_type']

        for data in level['data']:
            if object_type == 'sequence':
                try:
                    new_object = parent.createSequence(data)
                except Exception:
                    logging.info(
                        'Sequence {0} already exists. Doing nothing'.format(
                            data))
                    path = self.getPath(parent, data)
                    new_object = ftrack.getSequence(path)
                projectFolder = os.path.join(projectFolder, data)

            if object_type == 'shot':
                try:
                    new_object = parent.createShot(data)
                    tmpFolder = os.path.join(projectFolder, data)
                    self.createImgFolders(tmpFolder)
                except Exception:
                    logging.info(
                        'Shot {0} already exists. Doing nothing'.format(data))
                    path = self.getPath(parent, data)
                    new_object = ftrack.getShot(path)

            if object_type == 'task':
                taskType = ftrack.TaskType(id=data['typeid'])
                try:
                    new_object = parent.createTask(
                        TASK_TYPE_LOOKUP[data['typeid']], taskType)
                    new_object.set(data)
                except Exception:
                    logging.info(
                        'Task {0} already exists. Doing nothing'.format(
                            TASK_TYPE_LOOKUP[data['typeid']]))
                    new_object = ''

            logging.debug('Created {new_object} on parent {parent}'.format(
                parent=parent, new_object=new_object))
            if children:
                self.create_from_structure(new_object, children, projectFolder,
                                           tmpFolder)
Example #4
0
    def process(self, instance, context):

        # skipping if not launched from ftrack
        if not context.has_data('ftrackData'):
            return

        ftrack_data = context.data('ftrackData')
        task = ftrack.Task(ftrack_data['Task']['id'])
        parents = task.getParents()
        item = instance[0]

        path = []
        for p in parents:
            path.append(p.getName())

        # setup parent
        parent = parents[0]
        if '--' in item.name():
            name_split = item.name().split('--')
            if len(name_split) == 2:
                try:
                    copy_path = list(path)
                    copy_path.append(name_split[0])
                    parent = ftrack.getSequence(copy_path)
                except:
                    parent = parents[0].createSequence(name_split[0])
            if len(name_split) == 3:
                try:
                    copy_path = list(path)
                    copy_path.append(name_split[0])
                    parent = ftrack.getSequence(copy_path)
                except:
                    parent = parents[0].createEpisode(name_split[0])

                parents = [parent] + parents

                try:
                    copy_path.append(name_split[1])
                    parent = ftrack.getSequence(copy_path)
                except:
                    parent = parents[0].createSequence(name_split[1])

        # creating shot
        shot_name = item.name()
        duration = item.sourceOut() - item.sourceIn()
        duration = abs(int(round((abs(duration) + 1) / item.playbackSpeed())))

        if '--' in item.name():
            shot_name = item.name().split('--')[-1]

        shot = None
        try:
            shot = parent.createShot(shot_name)

            path = self.get_path(shot, context)

            instance.set_data('ftrackId', value=shot.getId())

            if not os.path.exists(os.path.dirname(path)):
                os.makedirs(os.path.dirname(path))

            item.sequence().writeAudioToFile(path, item.timelineIn(),
                                             item.timelineOut())

            msg = 'Creating new shot with name'
            msg += ' "%s"' % item.name()
            self.log.info(msg)

            instance.data['ftrackShot'] = shot
        except:
            path = []
            try:
                for p in reversed(parent.getParents()):
                    path.append(p.getName())
            except:
                pass
            path.append(parent.getName())
            path.append(shot_name)
            shot = ftrack.getShot(path)

            instance.set_data('ftrackId', value=shot.getId())

            path = self.get_path(shot, context)

            if not os.path.exists(os.path.dirname(path)):
                os.makedirs(os.path.dirname(path))

            item.sequence().writeAudioToFile(path, item.timelineIn(),
                                             item.timelineOut())

            instance.data['ftrackShot'] = shot

        # assign attributes to shot
        shot.set('fstart', value=1)
        shot.set('fend', value=duration)
        shot.set('handles', value=instance.data['handles'])

        # generate thumbnail
        nukeWriter = hiero.core.nuke.ScriptWriter()

        root_node = hiero.core.nuke.RootNode(1, 1)
        nukeWriter.addNode(root_node)

        handles = instance.data['handles']

        item.addToNukeScript(script=nukeWriter,
                             firstFrame=1,
                             includeRetimes=True,
                             retimeMethod='Frame',
                             startHandle=handles,
                             endHandle=handles)

        input_path = item.source().mediaSource().fileinfos()[0].filename()
        output_path = os.path.splitext(input_path)[0]
        output_path += '_thumbnail.png'
        output_path = os.path.join(tempfile.gettempdir(),
                                   os.path.basename(output_path))

        fmt = hiero.core.Format(150, 100, 1, 'thumbnail')
        fmt.addToNukeScript(script=nukeWriter)

        write_node = hiero.core.nuke.WriteNode(output_path)
        write_node.setKnob('file_type', 'png')
        nukeWriter.addNode(write_node)

        script_path = output_path.replace('.png', '.nk')
        nukeWriter.writeToDisk(script_path)
        logFileName = output_path.replace('.png', '.log')
        process = hiero.core.nuke.executeNukeScript(script_path,
                                                    open(logFileName, 'w'))

        while process.poll() is None:
            time.sleep(0.5)

        if os.path.exists(output_path):
            self.log.info("Thumbnail rendered successfully!")

            # creating thumbnails
            thumb = shot.createThumbnail(output_path)
            for t in shot.getTasks():
                t.set('thumbid', value=thumb.get('entityId'))
        else:
            self.log.error("Thumbnail failed to render")

        # clean up
        """
    def process(self, instance, context):

        # skipping if not launched from ftrack
        if not context.has_data('ftrackData'):
            return

        ftrack_data = context.data('ftrackData')
        task = ftrack.Task(ftrack_data['Task']['id'])
        parents = task.getParents()
        item = instance[0]

        path = []
        for p in parents:
            path.append(p.getName())

        # setup parent
        parent = parents[0]
        if '--' in item.name():
            name_split = item.name().split('--')
            if len(name_split) == 2:
                try:
                    copy_path = list(path)
                    copy_path.append(name_split[0])
                    parent = ftrack.getSequence(copy_path)
                except:
                    parent = parents[0].createSequence(name_split[0])
            if len(name_split) == 3:
                try:
                    copy_path = list(path)
                    copy_path.append(name_split[0])
                    parent = ftrack.getSequence(copy_path)
                except:
                    parent = parents[0].createEpisode(name_split[0])

                parents = [parent] + parents

                try:
                    copy_path.append(name_split[1])
                    parent = ftrack.getSequence(copy_path)
                except:
                    parent = parents[0].createSequence(name_split[1])

        # creating shot
        shot_name = item.name()
        duration = item.sourceOut() - item.sourceIn()
        duration = abs(int(round((abs(duration) + 1) / item.playbackSpeed())))

        if '--' in item.name():
            shot_name = item.name().split('--')[-1]

        shot = None
        try:
            shot = parent.createShot(shot_name)

            path = self.get_path(shot, context)

            instance.set_data('ftrackId', value=shot.getId())

            if not os.path.exists(os.path.dirname(path)):
                os.makedirs(os.path.dirname(path))

            item.sequence().writeAudioToFile(path, item.timelineIn(),
                                             item.timelineOut())

            msg = 'Creating new shot with name'
            msg += ' "%s"' % item.name()
            self.log.info(msg)

            instance.data['ftrackShot'] = shot
        except:
            path = []
            try:
                for p in reversed(parent.getParents()):
                    path.append(p.getName())
            except:
                pass
            path.append(parent.getName())
            path.append(shot_name)
            shot = ftrack.getShot(path)

            instance.set_data('ftrackId', value=shot.getId())

            path = self.get_path(shot, context)

            if not os.path.exists(os.path.dirname(path)):
                os.makedirs(os.path.dirname(path))

            item.sequence().writeAudioToFile(path, item.timelineIn(),
                                             item.timelineOut())

            instance.data['ftrackShot'] = shot

        # assign attributes to shot
        shot.set('fstart', value=1)
        shot.set('fend', value=duration)
        shot.set('handles', value=instance.data['handles'])

        # generate thumbnail
        nukeWriter = hiero.core.nuke.ScriptWriter()

        root_node = hiero.core.nuke.RootNode(1, 1)
        nukeWriter.addNode(root_node)

        handles = instance.data['handles']

        item.addToNukeScript(script=nukeWriter, firstFrame=1,
                             includeRetimes=True, retimeMethod='Frame',
                             startHandle=handles, endHandle=handles)

        input_path = item.source().mediaSource().fileinfos()[0].filename()
        output_path = os.path.splitext(input_path)[0]
        output_path += '_thumbnail.png'
        output_path = os.path.join(tempfile.gettempdir(),
                                   os.path.basename(output_path))

        fmt = hiero.core.Format(150, 100, 1, 'thumbnail')
        fmt.addToNukeScript(script=nukeWriter)

        write_node = hiero.core.nuke.WriteNode(output_path)
        write_node.setKnob('file_type', 'png')
        nukeWriter.addNode(write_node)

        script_path = output_path.replace('.png', '.nk')
        nukeWriter.writeToDisk(script_path)
        logFileName = output_path.replace('.png', '.log')
        process = hiero.core.nuke.executeNukeScript(script_path,
                                                    open(logFileName, 'w'))

        while process.poll() is None:
            time.sleep(0.5)

        if os.path.exists(output_path):
            self.log.info("Thumbnail rendered successfully!")

            # creating thumbnails
            thumb = shot.createThumbnail(output_path)
            for t in shot.getTasks():
                t.set('thumbid', value=thumb.get('entityId'))
        else:
            self.log.error("Thumbnail failed to render")

        # clean up
        """
    def process(self, instance, context):

        # skipping if not launched from ftrack
        if not context.has_data('ftrackData'):
            return

        ftrack_data = context.data('ftrackData')
        task = ftrack.Task(ftrack_data['Task']['id'])
        parents = task.getParents()
        item = instance[0]

        path = []
        for p in parents:
            path.append(p.getName())

        # setup parent
        parent = parents[0]
        if '--' in item.name():
            name_split = item.name().split('--')
            if len(name_split) == 2:
                try:
                    copy_path = list(path)
                    copy_path.append(name_split[0])
                    parent = ftrack.getSequence(copy_path)
                except:
                    parent = parents[0].createSequence(name_split[0])
            if len(name_split) == 3:
                try:
                    copy_path = list(path)
                    copy_path.append(name_split[0])
                    parent = ftrack.getSequence(copy_path)
                except:
                    parent = parents[0].createEpisode(name_split[0])

                parents = [parent] + parents

                try:
                    copy_path.append(name_split[1])
                    parent = ftrack.getSequence(copy_path)
                except:
                    parent = parents[0].createSequence(name_split[1])

        self.log.info(parent)
        self.log.info(parents[0])

        # creating shot
        shot_name = item.name()
        duration = item.sourceOut() - item.sourceIn() + 1
        if '--' in item.name():
            shot_name = item.name().split('--')[-1]

        tasks = []

        try:
            shot = parent.createShot(shot_name)

            shot.set('fstart', value=1)
            shot.set('fend', value=duration)

            path = self.get_path(shot, context)

            instance.set_data('ftrackId', value=shot.getId())

            if not os.path.exists(os.path.dirname(path)):
                os.makedirs(os.path.dirname(path))

            item.sequence().writeAudioToFile(path, item.timelineIn(),
                                                    item.timelineOut())

            msg = 'Creating new shot with name'
            msg += ' "%s"' % item.name()
            self.log.info(msg)
        except:
            path = []
            try:
                for p in reversed(parent.getParents()):
                    path.append(p.getName())
            except:
                pass
            path.append(parent.getName())
            path.append(shot_name)
            shot = ftrack.getShot(path)

            instance.set_data('ftrackId', value=shot.getId())

            shot.set('fstart', value=1)
            shot.set('fend', value=duration)

            path = self.get_path(shot, context)

            if not os.path.exists(os.path.dirname(path)):
                os.makedirs(os.path.dirname(path))

            item.sequence().writeAudioToFile(path, item.timelineIn(),
                                                    item.timelineOut())

        d = os.path.dirname
        tools_path = d(d(d(d(d(d(inspect.getfile(inspect.currentframe())))))))
        exe = os.path.join(tools_path, 'ffmpeg', 'bin', 'ffmpeg.exe')
        input_path = item.source().mediaSource().fileinfos()[0].filename()
        ext = os.path.splitext(input_path)[1]
        output_path = os.path.splitext(input_path)[0]
        output_path += '_thumbnail.png'
        output_path = os.path.join(tempfile.gettempdir(),
                                    os.path.basename(output_path))
        input_cmd = ''
        fps = item.sequence().framerate().toFloat()

        if ext == '.mov':
            arg = ' scale=-1:108'
            input_cmd = ' -vf' + arg + ' -vframes' + ' 1'
        else:
            arg = ' scale=-1:108'
            if os.path.splitext(input_path)[1] == '.exr':
                arg += ',lutrgb=r=gammaval(0.45454545):'
                arg += 'g=gammaval(0.45454545):'
                arg += 'b=gammaval(0.45454545)'
            input_cmd = ' -vf' + arg

        tc = self.frames_to_timecode(item.sourceIn(), fps)
        cmd = exe + ' -ss '+ tc +' -i "' + input_path + '" ' + input_cmd
        cmd += ' -y "' + output_path + '"'
        self.log.info(cmd)
        subprocess.call(cmd)

        # creating thumbnails
        thumb = shot.createThumbnail(output_path)
        for t in shot.getTasks():
            t.set('thumbid', value=thumb.get('entityId'))

        if os.path.exists(output_path):
            os.remove(output_path)