Skip to content

Commit

Permalink
Allow NodeTrees to be reused in the pre_export phase.
Browse files Browse the repository at this point in the history
Logic trees can be shared, so it is conceivable that a logic tree might
be pre-exported more than once. They can already be created by artists
and referenced from multiple Advanced Logic modifiers, so this is good
for consistency.
  • Loading branch information
Hoikas committed Jun 24, 2024
1 parent 778caad commit 1cf33ba
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 14 deletions.
8 changes: 6 additions & 2 deletions korman/exporter/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -510,8 +510,12 @@ def _(temporary, parent):

@handle_temporary.register(bpy.types.NodeTree)
def _(temporary, parent):
self.exit_stack.enter_context(TemporaryObject(temporary, bpy.data.node_groups.remove))
log_msg(f"'{parent.name}' generated NodeTree '{temporary.name}'")
# NodeTrees are reuseable, so make sure we haven't already encountered it.
if not temporary.name in self.want_node_trees:
self.exit_stack.enter_context(TemporaryObject(temporary, bpy.data.node_groups.remove))
log_msg(f"'{parent.name}' generated NodeTree '{temporary.name}'")
else:
log_msg(f"'{parent.name}' reused NodeTree '{temporary.name}'")
if temporary.bl_idname == "PlasmaNodeTree":
parent_so = self.mgr.find_create_object(plSceneObject, bl=parent)
self.want_node_trees[temporary.name].add((parent, parent_so))
Expand Down
28 changes: 16 additions & 12 deletions korman/properties/modifiers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,20 +181,24 @@ def requires_actor(self):

class PlasmaModifierLogicWiz:
def convert_logic(self, bo, **kwargs):
"""Creates, converts, and returns an unmanaged NodeTree for this logic wizard. If the wizard
fails during conversion, the temporary tree is deleted for you. However, on success, you
are responsible for removing the tree from Blender, if applicable."""
"""Attempts to look up an already existing logic tree matching the name provided and returns
it, if found. If not, creates, converts, and returns an unmanaged NodeTree for this wizard.
If the wizard fails during conversion, the temporary tree is deleted for you. However, on
success, you are responsible for removing the tree from Blender, if applicable."""
name = kwargs.pop("name", self.key_name)
assert not "tree" in kwargs
tree = bpy.data.node_groups.new(name, "PlasmaNodeTree")
kwargs["tree"] = tree
try:
self.logicwiz(bo, **kwargs)
except:
bpy.data.node_groups.remove(tree)
raise
else:
return tree

node_groups = bpy.data.node_groups
tree = node_groups.get(name)
if tree is None:
tree = node_groups.new(name, "PlasmaNodeTree")
kwargs["tree"] = tree
try:
self.logicwiz(bo, **kwargs)
except:
bpy.data.node_groups.remove(tree)
raise
return tree

def _create_python_file_node(self, tree, filename: str, attributes: Dict[str, Any]) -> bpy.types.Node:
pfm_node = tree.nodes.new("PlasmaPythonFileNode")
Expand Down

0 comments on commit 1cf33ba

Please sign in to comment.