Skip to content

Commit 7a73ee5

Browse files
committed
Custom mesh and Custm instance attributes.
1 parent 666493a commit 7a73ee5

4 files changed

Lines changed: 83 additions & 5 deletions

File tree

BEngine-Py/bengine/BERunNodes.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import bpy
22

3-
# from unicodedata import decimal
4-
53
# try:
64
# from .Utils import BEUtils
75
# except:
@@ -11,8 +9,6 @@
119
# SCRIPT_DIR = PACKAGE_PARENT
1210
# sys.path.append(str(SCRIPT_DIR) + "/Utils")
1311

14-
# import BEUtils
15-
1612
from .Utils import BEUtils
1713
from . import BESettings
1814
from .BEStartParams import StartParams

BEngine-Py/bengine/BESettings.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@
1919
# Out Variables
2020
KEYPARAM_BE_INSTANCE = "BE_Instance"
2121

22+
CUSTOM_ATTRIBUTES = "CustomAttributes"
23+
CUSTOM_FLOAT_ATTRIBS = "CustomFloatAttributes"
24+
CUSTOM_VECTOR_ATTRIBS = "CustomVectorAttributes"
25+
CUSTOM_COLOR_ATTRIBS = "CustomColorAttributes"
26+
2227
class EngineType(Enum):
2328
Unity = 0
2429
Unreal = 1

BEngine-Py/bengine/Utils/BEUtils.py

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,12 @@ def RecordObjectOutputToJSON(objects_sets_dict, the_object, is_instance: bool,
539539
inst_pos, inst_rot, inst_scale = the_object.matrix_world.decompose()
540540
js_object_data["Transforms"].append((tuple(inst_pos), tuple(inst_rot.to_euler('XYZ')), tuple(inst_scale)))
541541

542+
# Get Custom Attributes
543+
custom_attribs_dict = GetCustomAttribs("bei_", mesh.attributes)
544+
if custom_attribs_dict:
545+
js_object_data[BESettings.CUSTOM_ATTRIBUTES] = custom_attribs_dict
546+
547+
542548
# Get Mesh or BENGINE_INSTANCE
543549
if obj_type == "MESH":
544550
# Get BENGINE_INSTANCE Value
@@ -549,7 +555,7 @@ def RecordObjectOutputToJSON(objects_sets_dict, the_object, is_instance: bool,
549555
be_inst_id = mesh.attributes[BESettings.BENGINE_INSTANCE].data[0].value
550556

551557
if type(be_inst_id) is int:
552-
js_object_data[BESettings.KEYPARAM_BE_INSTANCE] = be_inst_id
558+
js_object_data[BESettings.OUT_BE_INSTANCE] = be_inst_id
553559
else:
554560
print(BESettings.BENGINE_INSTANCE + " Attribute is not Integer!!!")
555561

@@ -568,6 +574,51 @@ def RecordObjectOutputToJSON(objects_sets_dict, the_object, is_instance: bool,
568574
js_object_data["Mesh"] = meshes_tmp_list.index(mesh)
569575

570576

577+
# Get Custom Instance Attributes
578+
def GetCustomAttribs(custom_attrib_name: str, attributes):
579+
custom_attribs_dict = {}
580+
581+
float_custom_attribs = {}
582+
vector_custom_attribs = {}
583+
color_custom_attribs = {}
584+
585+
for attrib_name in attributes.keys():
586+
if attrib_name.startswith(custom_attrib_name):
587+
attrib = attributes[attrib_name].data[0]
588+
589+
GetCustomAttrib(attrib, float_custom_attribs, vector_custom_attribs, color_custom_attribs, attrib_name)
590+
591+
# Add Custom Attributes
592+
if float_custom_attribs or vector_custom_attribs or color_custom_attribs:
593+
if float_custom_attribs:
594+
custom_attribs_dict[BESettings.CUSTOM_FLOAT_ATTRIBS] = float_custom_attribs
595+
596+
if vector_custom_attribs:
597+
custom_attribs_dict[BESettings.CUSTOM_VECTOR_ATTRIBS] = vector_custom_attribs
598+
599+
if color_custom_attribs:
600+
custom_attribs_dict[BESettings.CUSTOM_COLOR_ATTRIBS] = color_custom_attribs
601+
602+
return custom_attribs_dict
603+
604+
605+
def GetCustomAttrib(attrib, float_attribs: dict, vector_attribs: dict, color_attribs: dict, attrib_name: str):
606+
attrib_type = type(attrib)
607+
attrib_val = None
608+
609+
if (attrib_type is bpy.types.FloatVectorAttributeValue):
610+
attrib_val = attrib.vector
611+
vector_attribs[attrib_name] = tuple(attrib_val)
612+
613+
elif (attrib_type is bpy.types.FloatColorAttributeValue or attrib_type is bpy.types.ByteColorAttributeValue):
614+
attrib_val = attrib.color
615+
color_attribs[attrib_name] = tuple(attrib_val)
616+
617+
else:
618+
attrib_val = attrib.value
619+
float_attribs[attrib_name] = attrib_val
620+
621+
571622
def GetBlenderOutputs(context, process_objs: list, engine_type: EngineType, is_GN: bool):
572623

573624
depsgraph = context.evaluated_depsgraph_get()

BEngine-Py/bengine/Utils/BEUtilsGeo.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
from .. import BESettings
99
from ..BESettings import EngineType
1010

11+
from . import BEUtils
12+
1113

1214
def CreateEmptyMesh():
1315
empty_mesh_data = bpy.data.meshes.new('BESubMesh')
@@ -273,6 +275,11 @@ def MeshToJSONData(mesh, engine_type: EngineType):
273275
uvs_dict = {}
274276
np_col_attrib = None
275277

278+
# Custom Attributes
279+
float_custom_attribs = {}
280+
vector_custom_attribs = {}
281+
color_custom_attribs = {}
282+
276283
# Get Attributes
277284
for attrib_name in mesh.attributes.keys():
278285

@@ -333,9 +340,28 @@ def MeshToJSONData(mesh, engine_type: EngineType):
333340
else:
334341
print("Attribute " + attrib_name + " must be FACE domain!!!")
335342

343+
# Custom Mesh Attribute
344+
elif attrib_name.startswith("bem_"):
345+
BEUtils.GetCustomAttrib(parsed_attr.data[0], float_custom_attribs, vector_custom_attribs, color_custom_attribs, attrib_name)
346+
336347
if uvs_dict:
337348
mesh_dict["UVs"] = uvs_dict
338349

350+
# Add Custom Attributes
351+
if float_custom_attribs or vector_custom_attribs or color_custom_attribs:
352+
attribs = {}
353+
354+
if float_custom_attribs:
355+
attribs[BESettings.CUSTOM_FLOAT_ATTRIBS] = float_custom_attribs
356+
357+
if vector_custom_attribs:
358+
attribs[BESettings.CUSTOM_VECTOR_ATTRIBS] = vector_custom_attribs
359+
360+
if color_custom_attribs:
361+
attribs[BESettings.CUSTOM_COLOR_ATTRIBS] = color_custom_attribs
362+
363+
mesh_dict[BESettings.CUSTOM_ATTRIBUTES] = attribs
364+
339365
return mesh_dict
340366

341367

0 commit comments

Comments
 (0)