Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed Duplicate and Input nodes creating objects only once in a for-loop #109

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion helper.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import re
import bpy
from mathutils import Vector

Expand Down Expand Up @@ -188,4 +189,20 @@ def selection_type_to_string(sel_type):
if "FACE" in sel_type:
out.append("Face")

return " + ".join(out)
return " + ".join(out)

re_trailing_number = re.compile(r'\.(\d{3})$')
def strip_trailing_number(s):
""" Strip trailing number such as ".001". """
m = re_trailing_number.match(s)
return s[0:-4] if m else s

def unique_name(base_name):
""" Generate unique object name in `bpy.data.objects`. """
base_name = strip_trailing_number(base_name)
count = 1
name = base_name
while bpy.data.objects.get(name):
name = "%s.%03d" % (base_name, count)
count += 1
return name
44 changes: 34 additions & 10 deletions nodes/_base/node_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

from bpy.props import PointerProperty, StringProperty, BoolProperty
from .._base.node_base import ScNode
from ...helper import focus_on_object, remove_object
from ...helper import focus_on_object, remove_object, print_log, unique_name

class ScInputNode(ScNode):
in_name: StringProperty(default="Object", update=ScNode.update_value)
# in_uv: BoolProperty(default=True, update=ScNode.update_value)
out_mesh: PointerProperty(type=bpy.types.Object)
generated_objects: StringProperty(default="[]")

def init(self, context):
self.node_executable = True
Expand All @@ -16,20 +16,44 @@ def init(self, context):
# self.inputs.new("ScNodeSocketBool", "Generate UVs").init("in_uv")
self.outputs.new("ScNodeSocketObject", "Object")

def copy(self, node):
super().copy(node)
self.generated_objects = "[]"

def error_condition(self):
return (
self.inputs["Name"].default_value == ""
)

def pre_execute(self):
focus_on_object(self.out_mesh)
remove_object(self.out_mesh)
# Clear generated objects
# Check "first_time" to prevent unexpected deletion inside for-loop
if (self.first_time):
for obj in eval(self.generated_objects):
# When obj is already deleted, remove_object() raises some exception.
try:
remove_object(eval(obj))
except:
print_log(self.name, None, "pre_execute",
"Invalid object: " + repr(obj))
self.generated_objects = "[]"

def post_execute(self):
# WORKAROUND
# Normally, Blender automatically makes the name of the object unique.
# But we lose the old object because the name is overwritten by the
# new object. We need to keep the old object's name and manually give
# the new object a new unique name.
name = unique_name(self.inputs["Name"].default_value)
out_mesh = bpy.context.active_object
out_mesh.name = name
if (out_mesh.data):
out_mesh.data.name = out_mesh.name

tmp = eval(self.generated_objects)
tmp.append(repr(out_mesh))
self.generated_objects = repr(tmp)

out = {}
self.out_mesh = bpy.context.active_object
self.out_mesh.name = self.inputs["Name"].default_value
if (self.out_mesh.data):
self.out_mesh.data.name = self.out_mesh.name
out["Object"] = self.out_mesh
return out
out["Object"] = out_mesh
return out
28 changes: 24 additions & 4 deletions nodes/object_operators/ScDuplicateObject.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,47 @@
import bpy

from bpy.props import FloatProperty
from bpy.props import FloatProperty, StringProperty
from bpy.types import Node
from .._base.node_base import ScNode
from .._base.node_operator import ScObjectOperatorNode
from ...helper import remove_object
from ...helper import remove_object, print_log

class ScDuplicateObject(Node, ScObjectOperatorNode):
bl_idname = "ScDuplicateObject"
bl_label = "Duplicate Object"

generated_objects: StringProperty(default="[]")

def init(self, context):
super().init(context)
self.outputs.new("ScNodeSocketObject", "Duplicate Object")

def copy(self, node):
super().copy(node)
self.generated_objects = "[]"

def pre_execute(self):
remove_object(self.outputs["Duplicate Object"].default_value)
# Check first_time to prevent unexpected deletion inside for-loop
if (self.first_time):
for obj in eval(self.generated_objects):
# When obj is already deleted, remove_object() raises some exception.
try:
remove_object(eval(obj))
except:
print_log(self.name, None, "pre_execute",
"Invalid object: " + repr(obj))
self.generated_objects = "[]"
super().pre_execute()

def functionality(self):
bpy.ops.object.duplicate()

def post_execute(self):
# Store duplicated object to delete it before re-execute this node.
tmp = eval(self.generated_objects)
tmp.append(repr(bpy.context.active_object))
self.generated_objects = repr(tmp)

out = super().post_execute()
out["Duplicate Object"] = bpy.context.active_object
return out
return out