瀏覽代碼

Add a bulk obj importer to the add-on

The importer reads a directory of .zip files and imports each one to its own collection.
master
Fen Dweller 3 年之前
父節點
當前提交
1d30ea03e2
共有 2 個檔案被更改,包括 76 行新增3 行删除
  1. +55
    -2
      scripts/blender/addons/macrovision/ops.py
  2. +21
    -1
      scripts/blender/addons/macrovision/ui.py

+ 55
- 2
scripts/blender/addons/macrovision/ops.py 查看文件

@@ -1,5 +1,6 @@
import bpy

from bpy.props import StringProperty
from bpy_extras.io_utils import ImportHelper
from mathutils import Vector, Euler, Color, Matrix
import json
import pathlib
@@ -7,6 +8,9 @@ import os
from math import pi
import random
import bmesh
import glob
import zipfile
import tempfile

VIEW_DATA = {
"FRONT": [0, 1, 2, "Front"],
@@ -17,9 +21,22 @@ VIEW_DATA = {
"BACK": [2, 1, 2, "Back"],
"TOP": [0, 0, 1, "Top"],
"BOTTOM": [0, 2, 1, "Bottom"],

"BOTTOM_FLIPPED": [2, 2, 1, "Bottom Flipped"],
}

# Taken from https://blender.stackexchange.com/questions/127403/change-active-collection
# This is the only way to get a LayerCollection.

def recurLayerCollection(layerColl, collName):
found = None
if (layerColl.name == collName):
return layerColl
for layer in layerColl.children:
found = recurLayerCollection(layer, collName)
if found:
return found

def get_bounds(objects, camera_transform):
xl = []
yl = []
@@ -468,8 +485,44 @@ class MVExport(bpy.types.Operator):

return {'FINISHED'}

class MVImportObj(bpy.types.Operator, ImportHelper):
bl_idname = "mv.import_obj"
bl_label = "Import from a directory of .zip files containing .obj files"
filepath: StringProperty(subtype="FILE_PATH")
filename_ext = "."
use_filter_folder = True

def execute(self, context: bpy.context):
dir = pathlib.Path(self.filepath)
glob_pattern = dir.joinpath("*.zip").__str__()
for file in glob.glob(glob_pattern):
zip = zipfile.ZipFile(file)
tmpdir = pathlib.Path(tempfile.mkdtemp())
zip.extractall(tmpdir.__str__())
obj_glob_pattern = tmpdir.joinpath("*.obj").__str__()
obj = glob.glob(obj_glob_pattern)[0]
obj_path = pathlib.Path(obj)
name = obj_path.with_suffix("").name
if name in bpy.data.collections:
continue

c = bpy.data.collections.new(name)
bpy.data.collections["Macrovision"].children.link(c)
layer_collection = recurLayerCollection(bpy.context.view_layer.layer_collection, name)
bpy.context.view_layer.active_layer_collection = layer_collection

bpy.ops.import_scene.obj(filepath=obj_path.__str__())

return {'FINISHED'}
def invoke(self, context, event):
context.window_manager.fileselect_add(self)
return {'RUNNING_MODAL'}

clses = [
MVExport,
MVAssignMaterials,
MVConfigCollection
MVConfigCollection,
MVImportObj
]

+ 21
- 1
scripts/blender/addons/macrovision/ui.py 查看文件

@@ -128,10 +128,30 @@ class MVCollectionPanel(bpy.types.Panel):
row.prop(context.collection.mv_entity_volume, "base")
row.prop(context.collection.mv_entity_volume, "power")

class MVImportPanel(bpy.types.Panel):
bl_idname="OBJECT_PT_MV_import_menu"
bl_label="Import"
bl_space_type="VIEW_3D"
bl_region_type="UI"
bl_category = "Macrovision"

@classmethod
def poll(cls, context):
return True

def draw(self, context):
layout = self.layout

box = layout.box()
box.label(text="Setup")

box.operator("mv.import_obj")

clses = [
MV_UL_ViewList,
MV_Views_List_Add,
MV_Views_List_Delete,
MVScenePanel,
MVCollectionPanel
MVCollectionPanel,
MVImportPanel
]

Loading…
取消
儲存