Ok. So, I'm very new to Python, and I'm not very skilled with Blender either. I do have some C# experience, if it helps.
I'm working on a module/add-on that takes OBJs made with World Machine and puts them in place in the Blender scene. I was trying to work with heightmaps, but seams never matched up. I've done some manual pulling around, and I know these can line up perfectly.
My problem is two fold:
A) The filename for each OBJ describes the location. They all have the naming format of "object_x000_y000.obj" Basically, where the numbers are, are how many units to the left or up I want to move the obj. But extracting this information from the file name makes NO Sense to me in python. I struggled with this in C#, but I can do it after some work. But python is like Greek to me right now. Any Documentation to help me chew up strings would be nice.
B) I'm struggling with understanding how to affect each individual object gets imported anyway. I want to rename and move each object. I wouldn't mind learning how to change the size for future reference also. I'm trying to avoid using an array, instead I want to work each import at a time per iteration in the loop. I would do this in C# by making a method I would loop, and return the "finished" object, but I don't get how to do this in Python.
Below is the entire script for reference, including comments that describe my problem:
import bpy
import os
from bpy.props import(StringProperty,
PointerProperty,
)
from bpy.types import(Panel,
Operator,
AddonPreferences,
PropertyGroup,
)
class MySettings(PropertyGroup):
path = StringProperty(
name="",
description ="Path to Directory",
default ="",
maxlen=1024,
subtype='DIR_PATH')
class WMImporterPanel(Panel):
bl_idname = "OBJECT_PT_WMImporter"
bl_category = "WorldMachine Importer"
bl_label = "WM Importer"
bl_space_type = 'VIEW_3D'
bl_region_type = 'TOOLS'
bl_context = 'objectmode'
def draw(self, context):
layout = self.layout
col = layout.column(align=True)
scn = context.scene
#Selecting Directory/Folder path
col.label(text="Directory of Files")
col.separator()
dirpath = col.prop(scn.my_tool, "path", text="")
#Button to perform import of meshes
col.separator()
col.operator("wmimport.importmeshes", text = "Import Meshes")
class OBJECT_OT_ImportButton(Operator):
bl_idname = "wmimport.importmeshes"
bl_label ="Import Meshes"
def execute(self,context):
scn = context.scene
dirpath = scn.my_tool.path
#print(dirpath)
file_list = os.listdir(dirpath)
obj_list = [item for item in file_list if item[-3:] == 'obj']
#for file in obj_list:
# print (file)
# Detect Naming Convention
# Need Help
# I want to pull the first file and rip the format from it.
# Files come in the format of <filename>_x000_y000.obj
# I want to use the filename to name each object imported, so if I called it Island
# the object in blender will be called something like Island(X,Y)
# where X = number following x in filename, and Y likewise.
for item in obj_list:
#Detect Location
# Need Help
#I want to get the X and Y value in the filename, so I can use that to
#increment the object in corrosponding directions.
fullpath = os.path.join(dirpath,item)
bpy.ops.import_scene.obj(filepath=fullpath, axis_forward='Y', axis_up='Z')
# Should I assign this obj to a variable?
# newObj = bpy.ops.import_scene.obj(filepath=fullpath, axis_forward='Y', axis_up='Z')
return{'FINISHED'}
def register():
bpy.utils.register_module(__name__)
bpy.types.Scene.my_tool = PointerProperty(type=MySettings)
def unregister():
bpy.utils.unregister_module(__name__)
del bpy.types.Scene.my_tool
if __name__ == "__main__":
register()