Skip to content

Commit

Permalink
feat: control over indentation blocks, default to four spaces
Browse files Browse the repository at this point in the history
  • Loading branch information
BrendanParmer committed Sep 7, 2024
1 parent 0c98593 commit 147467c
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 110 deletions.
53 changes: 26 additions & 27 deletions NodeToPython/compositor/operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,45 +31,44 @@ def __init__(self):
self._used_vars[name] = 0


def _create_scene(self, indent: str):
def _create_scene(self, indent_level: int):
#TODO: wrap in more general unique name util function
self._write(f"# Generate unique scene name", indent)
self._write(f"# Generate unique scene name", indent_level)
self._write(f"{BASE_NAME} = {str_to_py_str(self.compositor_name)}",
indent)
self._write(f"{END_NAME} = {BASE_NAME}", indent)
self._write(f"if bpy.data.scenes.get({END_NAME}) != None:", indent)
indent_level)
self._write(f"{END_NAME} = {BASE_NAME}", indent_level)
self._write(f"if bpy.data.scenes.get({END_NAME}) != None:", indent_level)

indent2 = f"{indent}\t"
self._write(f"{INDEX} = 1", indent2)
self._write(f"{INDEX} = 1", indent_level + 1)
self._write(f"{END_NAME} = {BASE_NAME} + f\".{{i:03d}}\"",
indent2)
indent_level + 1)
self._write(f"while bpy.data.scenes.get({END_NAME}) != None:",
indent2)
indent_level + 1)

indent3 = f"{indent}\t\t"
self._write(f"{END_NAME} = {BASE_NAME} + f\".{{{INDEX}:03d}}\"", indent3)
self._write(f"{INDEX} += 1\n", indent3)
self._write(f"{END_NAME} = {BASE_NAME} + f\".{{{INDEX}:03d}}\"",
indent_level + 2)
self._write(f"{INDEX} += 1\n", indent_level + 2)

self._write(f"{SCENE} = bpy.context.window.scene.copy()\n", indent)
self._write(f"{SCENE}.name = {END_NAME}", indent)
self._write(f"{SCENE}.use_fake_user = True", indent)
self._write(f"bpy.context.window.scene = {SCENE}", indent)
self._write(f"{SCENE} = bpy.context.window.scene.copy()\n", indent_level)
self._write(f"{SCENE}.name = {END_NAME}", indent_level)
self._write(f"{SCENE}.use_fake_user = True", indent_level)
self._write(f"bpy.context.window.scene = {SCENE}", indent_level)

def _initialize_compositor_node_tree(self, ntp_nt, nt_name):
#initialize node group
self._write(f"#initialize {nt_name} node group", self._outer)
self._write(f"def {ntp_nt.var}_node_group():", self._outer)
self._write(f"#initialize {nt_name} node group", self._outer_indent_level)
self._write(f"def {ntp_nt.var}_node_group():", self._outer_indent_level)

if ntp_nt.node_tree == self._base_node_tree:
self._write(f"{ntp_nt.var} = {SCENE}.node_tree")
self._write(f"#start with a clean node tree")
self._write(f"for {NODE} in {ntp_nt.var}.nodes:")
self._write(f"\t{ntp_nt.var}.nodes.remove({NODE})")
self._write(f"{ntp_nt.var}.nodes.remove({NODE})", self._inner_indent_level + 1)
else:
self._write((f"{ntp_nt.var} = bpy.data.node_groups.new("
f"type = \'CompositorNodeTree\', "
f"name = {str_to_py_str(nt_name)})"))
self._write("")
self._write("", 0)

# Compositor node tree settings
#TODO: might be good to make this optional
Expand Down Expand Up @@ -189,7 +188,7 @@ def _process_node_tree(self, node_tree: CompositorNodeTree):
self._write(f"return {nt_var}\n")

#create node group
self._write(f"{nt_var} = {nt_var}_node_group()\n", self._outer)
self._write(f"{nt_var} = {nt_var}_node_group()\n", self._outer_indent_level)

def execute(self, context):
if not self._setup_options(context.scene.ntp_options):
Expand All @@ -212,8 +211,8 @@ def execute(self, context):
comp_var = clean_string(self.compositor_name)

if self._mode == 'ADDON':
self._outer = "\t\t"
self._inner = "\t\t\t"
self._outer_indent_level = 2
self._inner_indent_level = 3

if not self._setup_addon_directories(context, comp_var):
return {'CANCELLED'}
Expand All @@ -224,25 +223,25 @@ def execute(self, context):
self._class_name = clean_string(self.compositor_name, lower=False)
self._init_operator(comp_var, self.compositor_name)

self._write("def execute(self, context):", "\t")
self._write("def execute(self, context):", 1)
else:
self._file = StringIO("")
if self._include_imports:
self._file.write("import bpy, mathutils\n\n")

if self.is_scene:
if self._mode == 'ADDON':
self._create_scene("\t\t")
self._create_scene(2)
elif self._mode == 'SCRIPT':
self._create_scene("")
self._create_scene(0)

node_trees_to_process = self._topological_sort(self._base_node_tree)

for node_tree in node_trees_to_process:
self._process_node_tree(node_tree)

if self._mode == 'ADDON':
self._write("return {'FINISHED'}\n", self._outer)
self._write("return {'FINISHED'}\n", self._outer_indent_level)

self._create_menu_func()
self._create_register_func()
Expand Down
26 changes: 13 additions & 13 deletions NodeToPython/geometry/operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def _process_zones(self, zone_inputs: list[GeometryNode]) -> None:
#must set defaults after paired with output
self._set_socket_defaults(zone_input)
self._set_socket_defaults(zone_output)
self._write("")
self._write("", 0)

if bpy.app.version >= (4, 0, 0):
def _set_geo_tree_properties(self, node_tree: GeometryNodeTree) -> None:
Expand All @@ -105,7 +105,7 @@ def _set_geo_tree_properties(self, node_tree: GeometryNodeTree) -> None:
for flag in tool_flags:
if hasattr(node_tree, flag) is True:
self._write(f"{nt_var}.{flag} = {getattr(node_tree, flag)}")
self._write("")
self._write("", 0)

def _process_node_tree(self, node_tree: GeometryNodeTree) -> None:
"""
Expand All @@ -119,8 +119,8 @@ def _process_node_tree(self, node_tree: GeometryNodeTree) -> None:
self._node_tree_vars[node_tree] = nt_var

#initialize node group
self._write(f"#initialize {nt_var} node group", self._outer)
self._write(f"def {nt_var}_node_group():", self._outer)
self._write(f"#initialize {nt_var} node group", self._outer_indent_level)
self._write(f"def {nt_var}_node_group():", self._outer_indent_level)
self._write(f"{nt_var} = bpy.data.node_groups.new("
f"type = \'GeometryNodeTree\', "
f"name = {str_to_py_str(node_tree.name)})\n")
Expand Down Expand Up @@ -155,19 +155,19 @@ def _process_node_tree(self, node_tree: GeometryNodeTree) -> None:
self._write(f"return {nt_var}\n")

#create node group
self._write(f"{nt_var} = {nt_var}_node_group()\n", self._outer)
self._write(f"{nt_var} = {nt_var}_node_group()\n", self._outer_indent_level)


def _apply_modifier(self, nt: GeometryNodeTree, nt_var: str):
#get object
self._write(f"{OBJECT_NAME} = bpy.context.object.name", self._outer)
self._write(f"{OBJECT} = bpy.data.objects[{OBJECT_NAME}]", self._outer)
self._write(f"{OBJECT_NAME} = bpy.context.object.name", self._outer_indent_level)
self._write(f"{OBJECT} = bpy.data.objects[{OBJECT_NAME}]", self._outer_indent_level)

#set modifier to the one we just created
mod_name = str_to_py_str(nt.name)
self._write(f"{MODIFIER} = obj.modifiers.new(name = {mod_name}, "
f"type = 'NODES')", self._outer)
self._write(f"{MODIFIER}.node_group = {nt_var}", self._outer)
f"type = 'NODES')", self._outer_indent_level)
self._write(f"{MODIFIER}.node_group = {nt_var}", self._outer_indent_level)


def execute(self, context):
Expand All @@ -181,8 +181,8 @@ def execute(self, context):
nt_var = clean_string(nt.name)

if self._mode == 'ADDON':
self._outer = "\t\t"
self._inner = "\t\t\t"
self._outer_indent_level = 2
self._inner_indent_level = 3

if not self._setup_addon_directories(context, nt_var):
return {'CANCELLED'}
Expand All @@ -192,7 +192,7 @@ def execute(self, context):
self._create_header(nt.name)
self._class_name = clean_string(nt.name, lower = False)
self._init_operator(nt_var, nt.name)
self._write("def execute(self, context):", "\t")
self._write("def execute(self, context):", 1)
else:
self._file = StringIO("")
if self._include_imports:
Expand All @@ -206,7 +206,7 @@ def execute(self, context):

if self._mode == 'ADDON':
self._apply_modifier(nt, nt_var)
self._write("return {'FINISHED'}\n", self._outer)
self._write("return {'FINISHED'}\n", self._outer_indent_level)
self._create_menu_func()
self._create_register_func()
self._create_unregister_func()
Expand Down
Loading

0 comments on commit 147467c

Please sign in to comment.