Creating planes and assigning them information from a table?

Hey, guys!

I got stuck trying to do a very simple change in the following script and I’m wondering if anyone could help me out a bit!

In a nutshell, original script imports a file containing table of objects and their information (location, rotation etc.) for already existing objects in the Blender scene and distribute them within 3D space.

What I want to change here is for that script to create planes and redistribute those in the scene instead, per every item in the imported ymap table.

.

entity

  • refers to the objects that are usually already in the scene before the script crunches the ymap information table. With the change I’m trying to make, those entities (planes) should be freshly created instead accordingly to the ymap table instead!

.

class SOLLUMZ_OT_import_ymap(SOLLUMZ_OT_base, bpy.types.Operator, ImportHelper):
    """Imports .ymap.xml file exported from codewalker"""
    bl_idname = "sollumz.importymap"
    bl_label = "Import ymap.xml"
    filename_ext = ".ymap.xml"
    bl_action = "Import a YMAP"
    bl_showtime = True
    bl_update_view = True

    filter_glob: bpy.props.StringProperty(
        default="*.ymap.xml",
        options={"HIDDEN"},
        maxlen=255,
    )

    def apply_entity_properties(self, obj, entity):
        obj.entity_properties.archetype_name = entity.archetype_name
        obj.entity_properties.flags = entity.flags
        obj.entity_properties.guid = entity.guid
        obj.entity_properties.parent_index = entity.parent_index
        obj.entity_properties.lod_dist = entity.lod_dist
        obj.entity_properties.child_lod_dist = entity.child_lod_dist
        obj.entity_properties.lod_level = "sollumz_" + entity.lod_level.lower()
        obj.entity_properties.num_children = entity.num_children
        obj.entity_properties.priority_level = "sollumz_" + entity.priority_level.lower()
        obj.entity_properties.ambient_occlusion_multiplier = entity.ambient_occlusion_multiplier
        obj.entity_properties.artificial_ambient_occlusion = entity.artificial_ambient_occlusion
        obj.entity_properties.tint_value = entity.tint_value

        if entity.type != "CMloInstanceDef":
            # Entities in YMAPs need rotation inverted
            entity.rotation.invert()
        obj.matrix_world = entity.rotation.to_matrix().to_4x4()
        obj.location = entity.position
        obj.scale = Vector(
            (entity.scale_xy, entity.scale_xy, entity.scale_z))

    def run(self, context):


        try:
            ymap = YMAP.from_xml_file(self.filepath)
            found = False
            if ymap.entities: # does the ymap table contain object information?

       ######################### Where the change takes place

                bpy.ops.mesh.primitive_plane_add(enter_editmode=False, align='WORLD', location=(0, 0, 0), scale=(1, 1, 1))


            #### It is also important to import all it's properties below

				#bpy.context.object.name = (obj, entity.archetype_name)
				#bpy.context.active_object.apply_entity_properties(obj, entity)


            else:
                self.error(f"{self.filepath} contains no entities to import!")
                return False
        except:
            self.error(f"Error during import: {traceback.format_exc()}")
            return False

Thank you for the reading!

What isn’t working? At a glance, your code looks correct

I just realised what else it’s missing.

It only checks if it has any entities inside table.

if ymap.entities: # does the ymap table contain object information?

But it needs to be going through the whole table and pick the entity info one by one. This is how I’d normally do it in Lua:

for k,v in pairs(ymap.entities) do	
				#"create a plane in the scene"

				#bpy.context.object.name = (obj, entity.archetype_name)
				#bpy.context.active_object.apply_entity_properties(obj, entity)
			end

I have some Lua experience, but Python is entirely new for me.