Example #1
0
    def build(self, obj, factories: StructFactory, imported_structs: OldNewNames):
        prop = BPYProperty(obj, self.name)

        # this is structure (pointer property)
        if isinstance(self._struct, dict):
            pointer_type = BPYPointers[self._struct["type"]]
            old_obj_name = self._struct["value"]
            if pointer_type == BPYPointers.NODE:
                new_name = imported_structs[(StrTypes.get_type(pointer_type), obj.id_data.name, old_obj_name)]
                # this should work in case obj is a node or socket
                # but in other cases probably extra information should be kept in the property structure
                data_block = obj.id_data.nodes[new_name]
            else:
                new_name = imported_structs[(StrTypes.get_type(pointer_type), '', old_obj_name)]
                data_block = pointer_type.collection[new_name]
            setattr(obj, self.name, data_block)

        # this is collection property
        elif prop.type == 'COLLECTION':
            self._set_collection_values(obj, factories, imported_structs)

        # this is property
        elif prop.is_valid:
            prop.value = self._struct

        # this is attribute
        else:
            setattr(obj, self.name, self._struct)
Example #2
0
 def _add_node_properties(self, node: SverchCustomTreeNode):
     """adds non default node properties"""
     for prop_name in node.keys():
         prop = BPYProperty(node, prop_name)
         if self._is_property_to_export(prop):
             if prop.type == 'COLLECTION':
                 # protection from storing default values
                 self._structure["params"][
                     prop_name] = prop.filter_collection_values()
             else:
                 self._structure["params"][prop.name] = prop.value
Example #3
0
    def export(self, node, factories: StructFactory, dependencies) -> dict:
        # add_mandatory_attributes
        self._struct['bl_idname'] = node.bl_idname
        self._struct["attributes"]['location'] = recursive_framed_location_finder(node, node.location[:])

        _set_optional(self._struct["attributes"], 'height', node.height, node.height != 100.0)
        _set_optional(self._struct["attributes"], 'width', node.width, node.width != 140.0)
        _set_optional(self._struct["attributes"], "label", node.label)
        _set_optional(self._struct["attributes"], "hide", node.hide)
        _set_optional(self._struct["attributes"], "use_custom_color", node.use_custom_color)
        _set_optional(self._struct["attributes"], "color", node.color[:], node.use_custom_color)
        if node.parent:  # the node is inside of a frame node
            prop = BPYProperty(node, "parent")
            raw_struct = factories.prop("parent", self.logger).export(prop, factories, dependencies)
            self._struct["attributes"]["parent"] = raw_struct
        else:
            del self._struct["attributes"]["parent"]

        # add non default node properties
        for prop_name in node.keys():
            prop = BPYProperty(node, prop_name)
            if prop.is_valid and prop.is_to_save:
                raw_struct = factories.prop(prop.name, self.logger).export(prop, factories, dependencies)
                if raw_struct is not None:
                    self._struct["properties"][prop.name] = raw_struct

        _set_optional(self._struct, "properties", self._struct["properties"])

        # all sockets should be kept in a file because it's possible to create UI
        # where sockets would be defined by pressing buttons for example like in the node group interface.
        # there is no sense of exporting information about sockets of group input and output nodes
        # they are totally controlled by Blender update system.
        if node.bl_idname not in ['NodeGroupInput', 'NodeGroupOutput']:
            for socket in node.inputs:
                raw_struct = factories.sock(socket.identifier, self.logger).export(socket, factories, dependencies)
                self._struct["inputs"][socket.identifier] = raw_struct

            for socket in node.outputs:
                raw_struct = factories.sock(socket.identifier, self.logger).export(socket, factories, dependencies)
                self._struct["outputs"][socket.identifier] = raw_struct

        _set_optional(self._struct, "inputs", self._struct["inputs"])
        _set_optional(self._struct, "outputs", self._struct["outputs"])

        if hasattr(node, 'save_to_json'):
            node.save_to_json(self._struct["advanced_properties"])

        _set_optional(self._struct, "advanced_properties", self._struct["advanced_properties"])

        return self._struct
Example #4
0
    def export(self, tree, factories: StructFactory, dependencies) -> dict:
        for node in tree.nodes:
            raw_struct = factories.node(node.name, self.logger).export(node, factories, dependencies)
            self._struct['nodes'][node.name] = raw_struct

        for link in _ordered_links(tree):
            self._struct["links"].append(factories.link(None, self.logger).export(link, factories, dependencies))

        for socket in tree.inputs:
            raw_struct = factories.interface(socket.name, self.logger).export(socket, factories, dependencies)
            self._struct["inputs"][socket.identifier] = raw_struct

        for socket in tree.outputs:
            raw_struct = factories.interface(socket.name, self.logger).export(socket, factories, dependencies)
            self._struct["outputs"][socket.identifier] = raw_struct

        for prop_name in tree.keys():
            prop = BPYProperty(tree, prop_name)
            if prop.is_valid and prop.is_to_save:
                raw_struct = factories.prop(prop.name, self.logger).export(prop, factories, dependencies)
                if raw_struct is not None:
                    self._struct["properties"][prop.name] = raw_struct

        self._struct["bl_idname"] = tree.bl_idname

        if not self._struct["properties"]:
            del self._struct["properties"]

        return self._struct
Example #5
0
 def _add_socket_properties(self, node: SverchCustomTreeNode):
     """
     add non default input socket properties
     output sockets does not have anything worth exporting
     """
     for i, sock in enumerate(node.inputs):
         sock_props = dict()
         for prop_name in sock.keys():
             prop = BPYProperty(sock, prop_name)
             if self._is_property_to_export(prop):
                 if prop.type == 'COLLECTION':
                     sock_props[prop_name] = prop.filter_collection_values()
                 else:
                     sock_props[prop.name] = prop.value
         if sock_props:
             self._structure['custom_socket_props'][str(i)] = sock_props
Example #6
0
    def import_node(self, apply_attributes: bool = True):
        """Reads node structure and apply settings to node"""
        if apply_attributes:
            for attr_name, attr_value in self._node_attributes():
                with self._fails_log.add_fail(
                        "Setting node attribute",
                        f'Tree: {self._node.id_data.name}, Node: {self._node.name}, attr: {attr_name}'):
                    setattr(self._node, attr_name, attr_value)

        for prop_name, prop_value in self._node_properties():
            if prop_name in {"all_props", "cls_dict", "monad"}:
                return  # this properties for monads which are applied in another place
            with self._fails_log.add_fail(
                    "Setting node property",
                    f'Tree: {self._node.id_data.name}, Node: {self._node.name}, prop: {prop_name}'):
                prop = BPYProperty(self._node, prop_name)
                if prop.is_valid:  # some files can have outdated properties which should be filtered
                    prop.value = prop_value

        # this block is before applying socket properties because some nodes can generate them in load method
        if hasattr(self._node, 'load_from_json'):
            with self._fails_log.add_fail(
                    "Setting advance node properties",
                    f'Tree: {self._node.id_data.name}, Node: {self._node.name}'):
                self._node.load_from_json(self._structure, self._import_version)

        for sock_index, prop_name, prop_value in self._input_socket_properties():
            with self._fails_log.add_fail(
                    "Setting socket property",
                    f'Tree: {self._node.id_data.name}, Node: {self._node.name}, prop: {prop_name}'):
                socket = self._node.inputs[sock_index]
                prop = BPYProperty(socket, prop_name)
                if prop.is_valid:
                    prop.value = prop_value
Example #7
0
    def export(self, socket, factories, dependencies):
        self._struct['bl_idname'] = socket.bl_idname
        self._struct['name'] = socket.name

        _set_optional(self._struct["attributes"], 'hide_value', socket.hide_value)
        _set_optional(self._struct, "attributes", self._struct["attributes"])

        for prop_name in socket.keys():
            prop = BPYProperty(socket, prop_name)
            if prop.is_valid and prop.is_to_save:
                raw_struct = factories.prop(prop.name, self.logger).export(prop, factories, dependencies)
                if raw_struct is not None:
                    self._struct["properties"][prop.name] = raw_struct
        _set_optional(self._struct, "properties", self._struct["properties"])

        return self._struct