fixed api_WIP, minor cleanup in other nodes

This commit is contained in:
martinaCodes
2022-09-04 23:36:30 +02:00
parent 8b8024ba7e
commit 7c2162f054
7 changed files with 73 additions and 87 deletions
+10 -20
View File
@@ -26,7 +26,11 @@ from sverchok.node_tree import SverchCustomTreeNode
from sverchok.data_structure import updateNode
def update_usecase(self, context):
print("API - running update usecase!")
module_usecase = self.get_module_usecase()
if module_usecase:
self.generate_node(*module_usecase)
class SvIfcTooltip(bpy.types.Operator):
@@ -43,25 +47,13 @@ class SvIfcTooltip(bpy.types.Operator):
class SvIfcApi(bpy.types.Node, SverchCustomTreeNode, ifcsverchok.helper.SvIfcCore):
def update_usecase(self, context):
try:
module_usecase = self.get_module_usecase()
if module_usecase:
self.generate_node(*module_usecase)
except:
raise Exception(
f"Couldn't run generate_node(). Module usecase: {module_usecase}"
)
bl_idname = "SvIfcApi"
bl_label = "IFC API"
tooltip: StringProperty(name="Tooltip")
usecase: StringProperty(update=updateNode)
usecase: StringProperty(name="Usecase", update=update_usecase)
def sv_init(self, context):
input_socket = self.inputs.new("SvStringsSocket", "usecase").use_prop = True
input_socket.tooltip = "ifcopenshell.api usecase, written like 'module.usecase' \n E.g.: 'project.create_file'"
self.inputs.new("SvStringsSocket", "usecase").prop_name = "usecase"
self.outputs.new("SvVerticesSocket", "file")
def draw_buttons(self, context, layout):
@@ -72,9 +64,8 @@ class SvIfcApi(bpy.types.Node, SverchCustomTreeNode, ifcsverchok.helper.SvIfcCor
print("process")
module_usecase = self.get_module_usecase()
if module_usecase:
self.generate_node(*module_usecase)
self.sv_input_names = [i.name for i in self.inputs]
super().process() # super().process() uses zip_long_repeat() which doesn't take objects like bmesh
super().process()
def get_module_usecase(self):
usecase = self.inputs["usecase"].sv_get()[0][0]
@@ -85,7 +76,7 @@ class SvIfcApi(bpy.types.Node, SverchCustomTreeNode, ifcsverchok.helper.SvIfcCor
try:
node_data = ifcopenshell.api.extract_docs(module, usecase)
except:
raise Exception("Node not yet implemented:", module, usecase)
print("Node not yet implemented:", module, usecase)
return
while len(self.inputs) > 1:
self.inputs.remove(self.inputs[-1])
@@ -93,7 +84,7 @@ class SvIfcApi(bpy.types.Node, SverchCustomTreeNode, ifcsverchok.helper.SvIfcCor
self.tooltip = ""
for name, data in node_data["inputs"].items():
setattr(SvIfcApi, name, StringProperty(name=name, update=updateNode))
self.inputs.new("SvStringsSocket", name).use_prop = True
self.inputs.new("SvStringsSocket", name).prop_name = name
if "default" in data:
self.tooltip = f"{name} ({data['default']}): {data['description']}\n"
else:
@@ -107,7 +98,6 @@ class SvIfcApi(bpy.types.Node, SverchCustomTreeNode, ifcsverchok.helper.SvIfcCor
self.outputs["file"].sv_set([ifcopenshell.api.run(usecase, **settings)])
def register():
bpy.utils.register_class(SvIfcTooltip)
bpy.utils.register_class(SvIfcApi)
+60 -31
View File
@@ -28,7 +28,6 @@ import importlib
class SvIfcTooltip(bpy.types.Operator):
bl_idname = "node.sv_ifc_tooltip"
bl_label = "IFC Info"
@@ -44,37 +43,48 @@ class SvIfcTooltip(bpy.types.Operator):
class SvIfcApiWIP(bpy.types.Node, SverchCustomTreeNode, ifcsverchok.helper.SvIfcCore):
def update_usecase(self, context):
# update is not getting run rn
try:
module_usecase = self.get_module_usecase()
if module_usecase:
self.generate_node(*module_usecase)
except:
raise Exception(
f"Couldn't run generate_node(). Module usecase: {module_usecase}"
)
# def update_usecase(self, context):
# # update_usecase is not getting run rn
# module_usecase = self.get_module_usecase()
# if module_usecase:
# self.generate_node(*module_usecase)
bl_idname = "SvIfcApiWIP"
bl_label = "IFC API WIP"
tooltip: StringProperty(name="Tooltip")
usecase: StringProperty(update=updateNode)
usecase: StringProperty(name="usecase", update=updateNode)
current_usecase: StringProperty(name="current_usecase")
def sv_init(self, context):
self.inputs.new("SvStringsSocket", "usecase").use_prop = True
# input_socket.tooltip = "ifcopenshell.api usecase, written like 'module.usecase' \n E.g.: 'project.create_file'"
self.inputs.new("SvStringsSocket", "usecase").prop_name = "usecase"
#input_socket.tooltip = "ifcopenshell.api usecase, written like 'module.usecase' \n E.g.: 'project.create_file'"
self.outputs.new("SvVerticesSocket", "file")
def draw_buttons(self, context, layout):
op = layout.operator("node.sv_ifc_tooltip", text="", icon="QUESTION", emboss=False)
op.tooltip = self.tooltip
op = layout.operator("node.sv_ifc_tooltip", text="", icon="QUESTION", emboss=False).tooltip = "ifcopenshell.api usecase, written like 'module.usecase' \n E.g.: 'project.create_file"
# op.tooltip = self.tooltip
def process(self):
print("process")
module_usecase = self.get_module_usecase()
self.sv_input_names = ["usecase"]
module_usecase = self.inputs["usecase"].sv_get()[0][0]
if module_usecase:
self.generate_node(*module_usecase)
try:
module_usecase = self.get_module_usecase()
except:
raise Exception(
f"Couldn't run generate_node(). Module usecase: {module_usecase}"
)
if '.'.join(module_usecase) != self.current_usecase:
self.generate_node(*module_usecase)
try:
for i in range(0, len(self.inputs)):
input = self.inputs[i].sv_get()
except:
# This occurs when a blender save file is reloaded
self.generate_node(*module_usecase)
self.sv_input_names = [i.name for i in self.inputs]
super().process() # super().process() uses zip_long_repeat() which doesn't take objects like bmesh
@@ -85,26 +95,45 @@ class SvIfcApiWIP(bpy.types.Node, SverchCustomTreeNode, ifcsverchok.helper.SvIfc
def generate_node(self, module, usecase):
importlib.import_module(f"ifcopenshell.api.{module}.{usecase}")
init_func = getattr(getattr(ifcopenshell.api, module), usecase).Usecase.__init__
if "settings" in init_func.__code__.co_varnames:
args = list(init_func.__code__.co_consts[3])
args.insert(0, 'file')
else:
args = ['file']
local_module = getattr(getattr(ifcopenshell.api, module), usecase)
node_inputs = {}
if hasattr(local_module.Usecase(local_module), "file"):
node_inputs["file"] = None
node_inputs.update(getattr(local_module.Usecase(local_module), "settings", {}))
# print("node inputs: ", node_inputs)
while len(self.inputs) > 1:
self.inputs.remove(self.inputs[-1])
if node_inputs:
self.tooltip = ""
for name, data in node_inputs.items():
setattr(SvIfcApiWIP, name, StringProperty(name=name))
self.inputs.new("SvStringsSocket", name).prop_name = name
if data is not None:
self.tooltip = f"{name} ({data}): {data}\n"
else:
self.tooltip = f"{name}: None\n"
self.tooltip = self.tooltip.strip()
self.current_usecase = '.'.join([module, usecase])
for name in args:
# print("name type", type(name))
setattr(SvIfcApiWIP, str(name), StringProperty(name=str(name), update=updateNode))
self.inputs.new("SvStringsSocket", str(name))
def process_ifc(self, usecase, *setting_values):
if usecase:
if usecase and setting_values:
settings = dict(zip(self.sv_input_names[1:], setting_values))
settings = {k: v for k, v in settings.items() if v != ""}
self.outputs["file"].sv_set([ifcopenshell.api.run(usecase, **settings)])
try:
if "file" in settings:
file = settings["file"]
settings.pop("file")
self.outputs["file"].sv_set([ifcopenshell.api.run(usecase, file, **settings)])
else:
self.outputs["file"].sv_set([ifcopenshell.api.run(usecase, **settings)])
except:
raise Exception(f"Couldn't run usecase.")
+1 -14
View File
@@ -49,24 +49,13 @@ class SvIfcBMeshToIfcGeo(bpy.types.Node, SverchCustomTreeNode, ifcsverchok.helpe
self.outputs.new("SvVerticesSocket", "representation")
def draw_buttons(self, context, layout):
op = layout.operator("node.sv_ifc_tooltip", text="", icon="QUESTION", emboss=False).tooltip = "Description?"
op = layout.operator("node.sv_ifc_tooltip", text="", icon="QUESTION", emboss=False).tooltip = "Blender mesh to Ifc Geometric Representation"
#op.tooltip = self.tooltip
def process(self):
print("Running process...")
# file
file = self.inputs["file"].sv_get()[0][0]
# blender mesh
blender_object = self.inputs["blender_object"].sv_get()[0]
print("blender object: ", blender_object)
#geometry
geometry = blender_object.data
print("geometry: ", geometry)
#run process_ifc
self.process_ifc(file, blender_object, geometry)
def process_ifc(self, file, blender_object, geometry):
@@ -75,7 +64,6 @@ class SvIfcBMeshToIfcGeo(bpy.types.Node, SverchCustomTreeNode, ifcsverchok.helpe
for context in file.by_type("IFCGEOMETRICREPRESENTATIONCONTEXT", include_subtypes=True):
if context.get_info()["ContextType"] == "Model" and context.get_info()["ContextIdentifier"] == "Body":
repr_context= context
print("pop", context)
elif context.get_info()["ContextType"] == "Model":
repr_context= context
except:
@@ -84,7 +72,6 @@ class SvIfcBMeshToIfcGeo(bpy.types.Node, SverchCustomTreeNode, ifcsverchok.helpe
)
representation = [ifcopenshell.api.run("geometry.add_representation", file, should_run_listeners=False,blender_object = blender_object, geometry=geometry, context = repr_context)]
print("representation: ", representation)
try:
self.outputs["file"].sv_set([[file]])
self.outputs["representation"].sv_set([representation])
+1 -3
View File
@@ -61,10 +61,9 @@ class SvIfcCreateEntity(bpy.types.Node, SverchCustomTreeNode, ifcsverchok.helper
for i in range(0, self.entity_schema.attribute_count()):
self.sv_input_names.append(self.entity_schema.attribute_by_index(i).name())
self.process()
super().process()
def generate_inputs(self, ifc_class):
# print("generate_inputs...")
while len(self.inputs) > 2:
self.inputs.remove(self.inputs[-1])
for i in range(0, self.entity_schema.attribute_count()):
@@ -78,7 +77,6 @@ class SvIfcCreateEntity(bpy.types.Node, SverchCustomTreeNode, ifcsverchok.helper
for i in range(0, len(entity)):
try:
value = attributes[i]
# print("value (attribute[i]): ", value, "|value type: ", type(value))
if isinstance(value, str) and value == "":
value = None
except:
@@ -49,7 +49,6 @@ class SvIfcCreateProject(bpy.types.Node, SverchCustomTreeNode, ifcsverchok.helpe
#project name
project_name = self.inputs["project_name"].sv_get()[0][0]
print("project name:", project_name)
self.process_ifc(file, project_name)
+1 -15
View File
@@ -59,22 +59,8 @@ class SvIfcCreateShape(bpy.types.Node, SverchCustomTreeNode, ifcsverchok.helper.
def process(self):
self.sv_input_names = ["file", "entity"]
super().process()
def process_helper(self):
print("Hello from helper")
sv_inputs_nested = []
for name in self.sv_input_names:
sv_inputs_nested.append(self.inputs[name].sv_get())
print(24*"#","\n", "sv_input: ", self.inputs[name].sv_get(), "input type: ", type(self.inputs[name].sv_get()), "\n", "#"*24)
for sv_input_nested in zip_long_repeat(*sv_inputs_nested):
for sv_input in zip_long_repeat(*sv_input_nested):
sv_input = list(sv_input)
print(24*"#","\n", "sv_input post zip_long_repeat: ", sv_input, "input type: ", type(sv_input), "\n", "#"*24)
self.process_ifc(*sv_input)
def process_ifc(self, file, entity):
print("process ifc...")
print("entity: ", entity)
try:
if not entity.is_a("IfcProduct"):
return
@@ -88,7 +74,7 @@ class SvIfcCreateShape(bpy.types.Node, SverchCustomTreeNode, ifcsverchok.helper.
obj = bpy.data.objects.new("IFC Element", mesh)
bpy.context.scene.collection.objects.link(obj)
except:
print("Entity could not be converted into a shape", entity)
raise Exception("Entity could not be converted into a shape. Entity: {}".format(entity))
def register():
@@ -64,13 +64,10 @@ class SvIfcQuickProjectSetup(bpy.types.Node, SverchCustomTreeNode, ifcsverchok.h
def process(self):
self.sv_input_names = [i.name for i in self.inputs]
print("inputnames: ", self.sv_input_names)
super().process()
def process_ifc(self, *setting_values):
print("setting values: ", setting_values)
settings = dict(zip(self.sv_input_names, setting_values))
print("settings: ", settings)
settings = {k: v for k, v in settings.items() if v != ""}
file = template.create(
filename=settings['filename'],