13 Commits

Author SHA1 Message Date
40756b0178 create document and bump version 2020-08-08 11:35:32 +08:00
af05f2ae21 fully finish bm import/export function 2020-08-07 14:17:13 +08:00
034f1e55aa update export module into bm 1.1 2020-08-07 10:41:50 +08:00
da4f07f37a add smooth feature 2020-08-06 14:43:31 +08:00
d90d05170b last edit 2020-08-06 12:17:06 +08:00
f61c3e1cef fix edit mode switch crash 2020-08-06 11:41:52 +08:00
bc13386f55 update mesh algo. but it still have bugs 2020-08-06 11:17:29 +08:00
72fe37040c add some prefs and finish bm import (no debug) 2020-08-05 16:19:38 +08:00
dcfc397421 fix fatal face export error 2020-07-23 11:21:11 +08:00
5b2314dbf8 move a settings' location 2020-07-20 16:14:39 +08:00
0dc526535c move some codes and add poll function for rail uv 2020-07-20 10:13:18 +08:00
6c00b2cb7b fix typo 2020-07-19 15:13:55 +08:00
654ce39a02 add pref and align tools 2020-07-19 15:05:43 +08:00
36 changed files with 994 additions and 225 deletions

2
.gitattributes vendored Normal file
View File

@ -0,0 +1,2 @@
# our generated mesh should be save as binary
*.bin binary

View File

@ -1,9 +1,54 @@
# Ballance Blender Helper
This is a Blender plugin which is served for Ballance mapping.
[中文版本](README_ZH.md)
WIP. Only BM export function is finished. But it is enough. Following function will be added in the future and it just like the decoration of BM export. Because once we have BM export, every map can be converted from Blender to Virtools.
## Brief introduction
BM file spec can be seen in [there](https://github.com/yyc12345/gist/tree/master/BMFileSpec)
This is a Blender plugin which is served for Ballance mapping in Blender.
Support criteria: Only support the lastest **LTS** version.
Currently, it only contain fundamental functions. More useful features will be added in future.
## Technical infomation
Used BM file spec can be found in [there](https://github.com/yyc12345/gist/blob/master/BMFileSpec/BMSpec_ZH.md)(Chinese only).
Used tools chain principle and the file format located in `meshes` can be found in [there](https://github.com/yyc12345/gist/blob/master/BMFileSpec/YYCToolsChainSpec_ZH.md)(Chinese only).
This plugin will continuously support Blender lastest **LTS** version. This plugin will migrate to new version when the new LTS version released. Currently, it based on Blender 2.83.x.
## Function introduction
### Plugin settings
* External texture folder: Please fill in the Texture directory of Ballance, the plugin will call the external texture file from this directory (the texture file originally with Ballance)
* No component collection: Objects in this collection will be forced to be set as non-Component. If left blank, this function will be shutdown.
* Temp texture folder: used to cache texture files extracted from BM files. Please arrange a directory that will not be automatically cleaned up. Since Blender will continue to read texture files from this directory, it cannot be emptied at will. And it also does not allow files with the same name to exist, that is, if I import two BMs for two maps, and there are two files with the same name but different images in the two BMs, the later files will overwrite the previous files , And in turn caused a texture error when the first blender document was opened again. For solving this problem, please refer to the subsequent BM import / export
### BM import / export
For import, in order to prevent texture errors, the best way is to force packaging once. After successfully importing the BM, choose to pack all into the blend file, and then clear the directory where the Temp texture folder is located, and then click Unpack to file if necessary, this operation will re-depend the textures in the texture library under the project folder.
For export, you can choose to export a collection or an object (Export mode), and specify the target (Export target).
It should be noted that once the BM is exported, all the faces in the file will be converted to triangular faces, please make a backup in advance. And it is recommended to use a flat collection structure, do not put a collections within another collection, which may cause some unnecessary problems.
### Ballance 3D
Ballance 3D is a set of light tools related to 3D operations, which can be found in the upper right corner of the 3D view.
#### Super Align
Provide 3ds Max like align tools. Current active will be seen as reference object. All selected objects(except active object) will be seen as operating object (So you can select multiple objects to align to the reference object).
#### Create Rail UV
Create UV for rails. You should select the object which you want add rail like UV to. Then, click this menu. Before doing this, you need make sure all selected object have at least 1 UV map (If it have more than 1 UV map, only the first UV map will be changed).
## Install
Put `ballance_blender_plugin` into Blender's plugin folder, `scripts/addons_contrib`. Then enable this plugin in Blender's preferences (Don't forget to configure this plugin's settings).
## Dev plan
* Add elements in Add menu.
* The assisted tools for creating custom floor in Blender (for example: add UV for floor).

54
README_ZH.md Normal file
View File

@ -0,0 +1,54 @@
# Ballance Blender Helper
[English version](README.md)
## 简介
这是一个用于Blender的插件其主要是服务于Ballance制图。
目前仅仅包含比较基本的功能,其余的更多有用的功能将在未来版本中进行开发
## 技术信息
使用的BM文件标准可以在[这里](https://github.com/yyc12345/gist/blob/master/BMFileSpec/BMSpec_ZH.md)查找
使用的制图链标准以及`meshes`文件夹下的文件的格式可以在[这里](https://github.com/yyc12345/gist/blob/master/BMFileSpec/YYCToolsChainSpec_ZH.md)查找
支持Blender的原则是支持当前最新的 **LTS** 版本在最新的LTS版本释出之后会花一些时间迁移插件。当前插件基于2.83.x版本
## 功能介绍
### 插件设置
* External texture folder请填写为Ballance的Texture目录插件将从此目录下调用外置贴图文件即Ballance原本带有的贴图文件
* No component collection处于此集合中的物体将被强制指定为非Component。如果留空则表示不需要这个功能。
* Temp texture folder用于缓存从BM文件中提取的贴图文件请安排一个平时不会被自动清理的目录。由于Blender会持续从这个目录读取贴图文件因此不能随意清空。并且其也不允许同名文件存在即如果我为2个地图分别导入两个BM这两个BM中存在贴图文件名相同但图像不同的两个文件那么后来的文件将会覆盖前面的文件并进而导致前者导入后的文档再次打开时出现贴图错误。关于解决这个问题的方法请参考后续的BM导入导出
### BM导入导出
对于导入而言为了防止贴图出错最好的方法是强制打包一次。在导入BM成功之后选择全部打包到blend文件然后清空Temp texture folder所在目录然后如果有需要可以再点击解包到文件将贴图重新依赖到工程文件夹下的贴图库内。
对于导出可以选择导出一个集合或者是一个物体Export mode并给定对象Export target即可。
需要注意的是一旦导出BM文件中所有的面将全部转换为三角形面请做好备份。并且建议使用平铺的集合结构不要在集合内嵌套集合可能会导致一些不必要的问题。
### Ballance 3D
Ballance 3D是一套简单的用于制图3D相关的轻型工具集合可以在3D视图右上角找到。
#### Super Align
提供一种类似于3ds Max的对齐方式。当前活动物体将被设为参照对象当前选中的所有物体如果参照也被选中则去掉参照对象将被视为操作对象因此可以选择多个物体一起对齐到参照对象
#### Create Rail UV
为地图中的钢轨创建UV你需要先选中需要添加类似钢轨UV的物体然后点击这个按钮以创建。在创建之前需要保证选中物体在右侧属性列表中至少有一个UV若有多个UV则会只操作第一个
## 安装
`ballance_blender_plugin`直接复制到Blender插件目录`scripts/addons_contrib`内即可。然后在Blender偏好设置中启用即可记得配置插件设置
## 后续开发计划
* 直接从添加菜单中添加机关
* 在Blender中创建自定义路面的辅助工具例如辅助添加路面UV等

View File

@ -2,10 +2,13 @@ bl_info={
"name":"Ballance Blender Plugin",
"description":"Ballance mapping tools for Blender",
"author":"yyc12345",
"version":(0,1),
"version":(1,0),
"blender":(2,83,0),
"category":"Object",
"support":"TESTING"
"support":"TESTING",
"warning": "Please read document before using this plugin.",
"wiki_url": "https://github.com/yyc12345/BallanceBlenderHelper",
"tracker_url": "https://github.com/yyc12345/BallanceBlenderHelper/issues"
}
# ============================================= import system
@ -21,93 +24,40 @@ if "bpy" in locals():
importlib.reload(utils)
if "config" in locals():
importlib.reload(config)
from . import config, utils, bm_import_export, floor_rail_uv
# ============================================= func block
class ImportBM(bpy.types.Operator, bpy_extras.io_utils.ImportHelper):
"""Load a Ballance Map File"""
bl_idname = "import_scene.bm"
bl_label = "Import BM"
bl_options = {'PRESET', 'UNDO'}
filename_ext = ".bm"
def execute(self, context):
bm_import_export.import_bm(context, self.filepath)
return {'FINISHED'}
class ExportBM(bpy.types.Operator, bpy_extras.io_utils.ExportHelper):
"""Save a Ballance Map File"""
bl_idname = "export_scene.bm"
bl_label = 'Export BM'
bl_options = {'PRESET'}
filename_ext = ".bm"
export_mode: bpy.props.EnumProperty(
name="Export mode",
items=(('COLLECTION', "Selected collection", "Export the selected collection"),
('OBJECT', "Selected objects", "Export the selected objects"),
),
)
export_target: bpy.props.StringProperty(
name="Export target",
description="Which one will be exported",
)
no_component_suffix: bpy.props.StringProperty(
name="No component suffix",
description="The object which have this suffix will not be saved as component.",
)
def execute(self, context):
bm_import_export.export_bm(context, self.filepath, self.export_mode, self.export_target, self.no_component_suffix)
return {'FINISHED'}
if "preferences" in locals():
importlib.reload(preferences)
if "super_align" in locals():
importlib.reload(super_align)
from . import config, utils, bm_import_export, floor_rail_uv, preferences, super_align
# ============================================= menu system
class RailUVOperator(bpy.types.Operator):
"""Create a UV for rail"""
bl_idname = "ballance.rail_uv"
bl_label = "Create Rail UV"
bl_options = {'UNDO'}
def execute(self, context):
floor_rail_uv.create_rail_uv()
return {'FINISHED'}
class FloorUVOperator(bpy.types.Operator):
"""Virtoolize the UV of floor"""
bl_idname = "ballance.floor_uv"
bl_label = "Virtoolize floor UV"
bl_options = {'UNDO'}
def execute(self, context):
floor_rail_uv.virtoolize_floor_uv()
return {'FINISHED'}
class ThreeDViewerMenu(bpy.types.Menu):
"""Ballance related 3D operator"""
bl_label = "Ballance 3D"
bl_idname = "OBJECT_MT_ballance3d_menu"
def draw(self, context):
layout = self.layout
layout.operator("ballance.super_align")
layout.operator("ballance.rail_uv")
layout.operator("ballance.floor_uv")
# ============================================= blender call system
classes = (
ImportBM,
ExportBM,
RailUVOperator,
FloorUVOperator,
preferences.BallanceBlenderPluginPreferences,
bm_import_export.ImportBM,
bm_import_export.ExportBM,
floor_rail_uv.RailUVOperator,
super_align.SuperAlignOperator,
ThreeDViewerMenu
)
def menu_func_bm_import(self, context):
self.layout.operator(ImportBM.bl_idname, text="Ballance Map (.bm)")
self.layout.operator(bm_import_export.ImportBM.bl_idname, text="Ballance Map (.bm)")
def menu_func_bm_export(self, context):
self.layout.operator(ExportBM.bl_idname, text="Ballance Map (.bm)")
self.layout.operator(bm_import_export.ExportBM.bl_idname, text="Ballance Map (.bm)")
def menu_func_ballance_3d(self, context):
layout = self.layout
layout.menu(ThreeDViewerMenu.bl_idname)

View File

@ -2,15 +2,302 @@ import bpy,bmesh,bpy_extras,mathutils
import pathlib,zipfile,time,os,tempfile,math
import struct,shutil
from bpy_extras import io_utils,node_shader_utils
from bpy_extras.io_utils import unpack_list
from bpy_extras.image_utils import load_image
from . import utils, config
bm_current_version = 10
class ImportBM(bpy.types.Operator, bpy_extras.io_utils.ImportHelper):
"""Load a Ballance Map File (BM file spec 1.0)"""
bl_idname = "import_scene.bm"
bl_label = "Import BM "
bl_options = {'PRESET', 'UNDO'}
filename_ext = ".bm"
def import_bm(context,filepath):
# todo: finish this
pass
@classmethod
def poll(self, context):
prefs = bpy.context.preferences.addons[__package__].preferences
return (os.path.isdir(prefs.temp_texture_folder) and os.path.isdir(prefs.external_folder))
def execute(self, context):
prefs = bpy.context.preferences.addons[__package__].preferences
import_bm(context, self.filepath, prefs.external_folder, prefs.temp_texture_folder)
return {'FINISHED'}
class ExportBM(bpy.types.Operator, bpy_extras.io_utils.ExportHelper):
"""Save a Ballance Map File (BM file spec 1.0)"""
bl_idname = "export_scene.bm"
bl_label = 'Export BM'
bl_options = {'PRESET'}
filename_ext = ".bm"
def export_bm(context,filepath,export_mode, export_target, no_component_suffix):
export_mode: bpy.props.EnumProperty(
name="Export mode",
items=(('COLLECTION', "Selected collection", "Export the selected collection"),
('OBJECT', "Selected objects", "Export the selected objects"),
),
)
export_target: bpy.props.StringProperty(
name="Export target",
description="Which one will be exported",
)
def execute(self, context):
export_bm(context, self.filepath, self.export_mode, self.export_target)
return {'FINISHED'}
# ========================================== method
bm_current_version = 11
def import_bm(context,filepath,externalTexture,blenderTempFolder):
# ============================================ alloc a temp folder
tempFolderObj = tempfile.TemporaryDirectory()
tempFolder = tempFolderObj.name
# debug
# print(tempFolder)
tempTextureFolder = os.path.join(tempFolder, "Texture")
prefs = bpy.context.preferences.addons[__package__].preferences
blenderTempTextureFolder = prefs.temp_texture_folder
externalTextureFolder = prefs.external_folder
with zipfile.ZipFile(filepath, 'r', zipfile.ZIP_DEFLATED, 9) as zipObj:
zipObj.extractall(tempFolder)
# index.bm
findex = open(os.path.join(tempFolder, "index.bm"), "rb")
# judge version first
gotten_version = read_uint32(findex)
if (gotten_version != bm_current_version):
utils.ShowMessageBox("Unsupported BM spec. Expect: {} Gotten: {}".format(bm_current_version, gotten_version), "Unsupported BM spec", 'WARNING')
findex.close()
tempFolderObj.cleanup()
return
objectList = []
meshList = []
materialList = []
textureList = []
while len(peek_stream(findex)) != 0:
index_name = read_string(findex)
index_type = read_uint8(findex)
index_offset = read_uint64(findex)
blockCache = info_block_helper(index_name, index_offset)
if index_type == info_bm_type.OBJECT:
objectList.append(blockCache)
elif index_type == info_bm_type.MESH:
meshList.append(blockCache)
elif index_type == info_bm_type.MATERIAL:
materialList.append(blockCache)
elif index_type == info_bm_type.TEXTURE:
textureList.append(blockCache)
else:
pass
findex.close()
# texture.bm
ftexture = open(os.path.join(tempFolder, "texture.bm"), "rb")
for item in textureList:
ftexture.seek(item.offset, os.SEEK_SET)
texture_filename = read_string(ftexture)
texture_isExternal = read_bool(ftexture)
if texture_isExternal:
txur = load_image(texture_filename, externalTextureFolder)
item.blenderData = txur
else:
# not external. copy temp file into blender temp. then use it.
# try copy. if fail, don't need to do more
try:
shutil.copy(os.path.join(tempTextureFolder, texture_filename), os.path.join(blenderTempTextureFolder, texture_filename))
except:
pass
txur = load_image(texture_filename, blenderTempTextureFolder)
item.blenderData = txur
txur.name = item.name
ftexture.close()
# material.bm
fmaterial = open(os.path.join(tempFolder, "material.bm"), "rb")
for item in materialList:
fmaterial.seek(item.offset, os.SEEK_SET)
# read data
material_colAmbient = read_3vector(fmaterial)
material_colDiffuse = read_3vector(fmaterial)
material_colSpecular = read_3vector(fmaterial)
material_colEmissive = read_3vector(fmaterial)
material_specularPower = read_float(fmaterial)
material_useTexture = read_bool(fmaterial)
material_texture = read_uint32(fmaterial)
# create basic material
m = bpy.data.materials.new(item.name)
m.use_nodes=True
for node in m.node_tree.nodes:
m.node_tree.nodes.remove(node)
bnode=m.node_tree.nodes.new(type="ShaderNodeBsdfPrincipled")
mnode=m.node_tree.nodes.new(type="ShaderNodeOutputMaterial")
m.node_tree.links.new(bnode.outputs[0],mnode.inputs[0])
m.metallic = sum(material_colAmbient) / 3
m.diffuse_color = [i for i in material_colDiffuse] + [1]
m.specular_color = material_colSpecular
m.specular_intensity = material_specularPower
# create a texture
if material_useTexture:
inode=m.node_tree.nodes.new(type="ShaderNodeTexImage")
inode.image=textureList[material_texture].blenderData
m.node_tree.links.new(inode.outputs[0],bnode.inputs[0])
# write custom property
m['virtools-ambient'] = material_colAmbient
m['virtools-diffuse'] = material_colDiffuse
m['virtools-specular'] = material_colSpecular
m['virtools-emissive'] = material_colEmissive
m['virtools-power'] = material_specularPower
item.blenderData = m
fmaterial.close()
# mesh.bm
fmesh = open(os.path.join(tempFolder, "mesh.bm"), "rb")
vList=[]
vtList=[]
vnList=[]
faceList=[]
materialSolt = []
for item in meshList:
fmesh.seek(item.offset, os.SEEK_SET)
# create real mesh
mesh = bpy.data.meshes.new(item.name)
vList.clear()
vtList.clear()
vnList.clear()
faceList.clear()
materialSolt.clear()
# in first read, store all data into list
listCount = read_uint32(fmesh)
for i in range(listCount):
cache = read_3vector(fmesh)
# switch yz
vList.append((cache[0], cache[2], cache[1]))
listCount = read_uint32(fmesh)
for i in range(listCount):
cache = read_2vector(fmesh)
# reverse v
vtList.append((cache[0], -cache[1]))
listCount = read_uint32(fmesh)
for i in range(listCount):
cache = read_3vector(fmesh)
# switch yz
vnList.append((cache[0], cache[2], cache[1]))
listCount = read_uint32(fmesh)
for i in range(listCount):
faceData = read_face(fmesh)
mesh_useMaterial = read_bool(fmesh)
mesh_materialIndex = read_uint32(fmesh)
if mesh_useMaterial:
neededMaterial = materialList[mesh_materialIndex].blenderData
if neededMaterial in materialSolt:
neededIndex = materialSolt.index(neededMaterial)
else:
neededIndex = len(materialSolt)
materialSolt.append(neededMaterial)
else:
neededIndex = -1
# we need invert triangle sort
faceList.append((
faceData[6], faceData[7], faceData[8],
faceData[3], faceData[4], faceData[5],
faceData[0], faceData[1], faceData[2],
neededIndex
))
# and then we need add material solt for this mesh
for mat in materialSolt:
mesh.materials.append(mat)
# then, we need add correspond count for vertices
mesh.vertices.add(len(vList))
mesh.loops.add(len(faceList)*3) # triangle face confirm
mesh.polygons.add(len(faceList))
mesh.uv_layers.new(do_init=False)
mesh.create_normals_split()
# add vertices data
mesh.vertices.foreach_set("co", unpack_list(vList))
mesh.loops.foreach_set("vertex_index", unpack_list(flat_vertices_index(faceList)))
mesh.loops.foreach_set("normal", unpack_list(flat_vertices_normal(faceList, vnList)))
mesh.uv_layers[0].data.foreach_set("uv", unpack_list(flat_vertices_uv(faceList, vtList)))
for i in range(len(faceList)):
mesh.polygons[i].loop_start = i * 3
mesh.polygons[i].loop_total = 3
if faceList[i][9] != -1:
mesh.polygons[i].material_index = faceList[i][9]
mesh.polygons[i].use_smooth = True
mesh.validate(clean_customdata=False)
mesh.update(calc_edges=False, calc_edges_loose=False)
# add into item using
item.blenderData = mesh
fmesh.close()
# object
fobject = open(os.path.join(tempFolder, "object.bm"), "rb")
# we need get needed collection first
view_layer = context.view_layer
collection = view_layer.active_layer_collection.collection
if prefs.no_component_collection == "":
forcedCollection = None
else:
try:
forcedCollection = bpy.data.collections[prefs.no_component_collection]
except:
forcedCollection = bpy.data.collections.new(prefs.no_component_collection)
view_layer.active_layer_collection.collection.children.link(forcedCollection)
# start process it
for item in objectList:
fobject.seek(item.offset, os.SEEK_SET)
# read data
object_isComponent = read_bool(fobject)
object_isForcedNoComponent = read_bool(fobject)
object_isHidden = read_bool(fobject)
object_worldMatrix = read_worldMaterix(fobject)
object_meshIndex = read_uint32(fobject)
# got mesh first
if object_isComponent:
neededMesh = load_component(object_meshIndex)
else:
neededMesh = meshList[object_meshIndex].blenderData
# create real object
obj = bpy.data.objects.new(item.name, neededMesh)
if (not object_isComponent) and object_isForcedNoComponent and (forcedCollection is not None):
forcedCollection.objects.link(obj)
else:
collection.objects.link(obj)
obj.matrix_world = object_worldMatrix
obj.hide_set(object_isHidden)
fobject.close()
view_layer.update()
tempFolderObj.cleanup()
def export_bm(context,filepath,export_mode, export_target):
# ============================================ alloc a temp folder
tempFolderObj = tempfile.TemporaryDirectory()
tempFolder = tempFolderObj.name
@ -18,27 +305,23 @@ def export_bm(context,filepath,export_mode, export_target, no_component_suffix):
# tempFolder = "G:\\ziptest"
tempTextureFolder = os.path.join(tempFolder, "Texture")
os.makedirs(tempTextureFolder)
prefs = bpy.context.preferences.addons[__package__].preferences
# ============================================ find export target
# ============================================ find export target. don't need judge them in there. just collect them
if export_mode== "COLLECTION":
objectList = bpy.data.collections[export_target].objects
else:
objectList = [bpy.data.objects[export_target]]
needSuffixChecker = no_component_suffix != ""
componentObj = set()
for obj in objectList:
if needSuffixChecker and obj.name.endwith(no_component_suffix):
pass # meshObjList.add(obj)
else:
if is_component(obj.name):
componentObj.add(obj)
else:
pass # meshObjList.add(obj)
# try get forcedCollection
try:
forcedCollection = bpy.data.collections[prefs.no_component_collection]
except:
forcedCollection = None
# ============================================ export
finfo = open(os.path.join(tempFolder, "index.bm"), "wb")
finfo.write(struct.pack("I", bm_current_version))
write_uint32(finfo, bm_current_version)
# ====================== export object
fobject = open(os.path.join(tempFolder, "object.bm"), "wb")
@ -49,15 +332,22 @@ def export_bm(context,filepath,export_mode, export_target, no_component_suffix):
# only export mesh object
if obj.type != 'MESH':
continue
varis_component = obj in componentObj
# clean no mesh object
currentMesh = obj.data
if currentMesh == None:
if currentMesh is None:
continue
# judge component
object_isComponent = is_component(obj.name)
object_isForcedNoComponent = False
if (forcedCollection is not None) and (obj.name in forcedCollection.objects):
# change it to forced no component
object_isComponent = False
object_isForcedNoComponent = True
# triangle first and then group
if not varis_component:
if not object_isComponent:
if currentMesh not in meshSet:
mesh_triangulate(currentMesh)
meshSet.add(currentMesh)
@ -66,19 +356,24 @@ def export_bm(context,filepath,export_mode, export_target, no_component_suffix):
meshCount += 1
else:
meshId = meshList.index(currentMesh)
else:
meshId = get_component_id(obj.name)
# get visibility
object_isHidden = not obj.visible_get()
# write finfo first
write_string(finfo, obj.name)
write_int(finfo, info_bm_type.OBJECT)
write_long(finfo, fobject.tell())
write_uint8(finfo, info_bm_type.OBJECT)
write_uint64(finfo, fobject.tell())
# write fobject
write_int(fobject, 1 if varis_component else 0)
write_bool(fobject, object_isComponent)
write_bool(fobject, object_isForcedNoComponent)
print(object_isHidden)
write_bool(fobject, object_isHidden)
write_worldMatrix(fobject, obj.matrix_world)
if varis_component:
write_int(fobject, get_component_id(obj.name))
else:
write_int(fobject, meshId)
write_uint32(fobject, meshId)
fobject.close()
@ -91,13 +386,13 @@ def export_bm(context,filepath,export_mode, export_target, no_component_suffix):
# write finfo first
write_string(finfo, mesh.name)
write_int(finfo, info_bm_type.MESH)
write_long(finfo, fmesh.tell())
write_uint8(finfo, info_bm_type.MESH)
write_uint64(finfo, fmesh.tell())
# write fmesh
# vertices
vecList = mesh.vertices[:]
write_int(fmesh, len(vecList))
write_uint32(fmesh, len(vecList))
for vec in vecList:
#swap yz
write_3vector(fmesh,vec.co[0],vec.co[2],vec.co[1])
@ -105,7 +400,7 @@ def export_bm(context,filepath,export_mode, export_target, no_component_suffix):
# uv
face_index_pairs = [(face, index) for index, face in enumerate(mesh.polygons)]
uv_layer = mesh.uv_layers.active.data[:]
write_int(fmesh, len(face_index_pairs) * 3)
write_uint32(fmesh, len(face_index_pairs) * 3)
for f, f_index in face_index_pairs:
# it should be triangle face, otherwise throw a error
if (f.loop_total != 3):
@ -117,7 +412,7 @@ def export_bm(context,filepath,export_mode, export_target, no_component_suffix):
write_2vector(fmesh, uv[0], -uv[1])
# normals
write_int(fmesh, len(face_index_pairs) * 3)
write_uint32(fmesh, len(face_index_pairs) * 3)
for f, f_index in face_index_pairs:
# no need to check triangle again
for loop_index in range(f.loop_start, f.loop_start + f.loop_total):
@ -134,7 +429,7 @@ def export_bm(context,filepath,export_mode, export_target, no_component_suffix):
materialSet.add(mat)
materialList.append(mat)
write_int(fmesh, len(face_index_pairs))
write_uint32(fmesh, len(face_index_pairs))
vtIndex = []
vnIndex = []
vIndex = []
@ -163,8 +458,8 @@ def export_bm(context,filepath,export_mode, export_target, no_component_suffix):
vIndex[0], vtIndex[0], vnIndex[0])
# set used material
write_int(fmesh, 0 if noMaterial else 1)
write_int(fmesh, usedMat)
write_bool(fmesh, not noMaterial)
write_uint32(fmesh, usedMat)
mesh.free_normals_split()
@ -175,24 +470,32 @@ def export_bm(context,filepath,export_mode, export_target, no_component_suffix):
textureSet = set()
textureList = []
textureCount = 0
for material in materialList:
# write finfo first
write_string(finfo, material.name)
write_int(finfo, info_bm_type.MATERIAL)
write_long(finfo, fmaterial.tell())
write_uint8(finfo, info_bm_type.MATERIAL)
write_uint64(finfo, fmaterial.tell())
# write basic color
# try get original written data
material_colAmbient = try_get_custom_property(material, 'virtools-ambient')
material_colDiffuse = try_get_custom_property(material, 'virtools-diffuse')
material_colSpecular = try_get_custom_property(material, 'virtools-specular')
material_colEmissive = try_get_custom_property(material, 'virtools-emissive')
material_specularPower = try_get_custom_property(material, 'virtools-power')
# get basic color
mat_wrap = node_shader_utils.PrincipledBSDFWrapper(material)
if mat_wrap:
use_mirror = mat_wrap.metallic != 0.0
if use_mirror:
write_3vector(fmaterial, mat_wrap.metallic, mat_wrap.metallic, mat_wrap.metallic)
material_colAmbient = set_value_when_none(material_colAmbient, (mat_wrap.metallic, mat_wrap.metallic, mat_wrap.metallic))
else:
write_3vector(fmaterial, 1, 1, 1)
write_3vector(fmaterial, mat_wrap.base_color[0], mat_wrap.base_color[1], mat_wrap.base_color[2])
write_3vector(fmaterial, mat_wrap.specular, mat_wrap.specular, mat_wrap.specular)
material_colAmbient = set_value_when_none(material_colAmbient, (1.0, 1.0, 1.0))
material_colDiffuse = set_value_when_none(material_colDiffuse, (mat_wrap.base_color[0], mat_wrap.base_color[1], mat_wrap.base_color[2]))
material_colSpecular = set_value_when_none(material_colSpecular, (mat_wrap.specular, mat_wrap.specular, mat_wrap.specular))
material_colEmissive = set_value_when_none(material_colEmissive, mat_wrap.emission_color[:3])
material_specularPower = set_value_when_none(material_specularPower, 0.0)
# confirm texture
tex_wrap = getattr(mat_wrap, "base_color_texture", None)
if tex_wrap:
@ -207,24 +510,35 @@ def export_bm(context,filepath,export_mode, export_target, no_component_suffix):
else:
currentTexture = textureList.index(image)
write_int(fmaterial, 1)
write_int(fmaterial, currentTexture)
material_useTexture = True
material_texture = currentTexture
else:
# no texture
write_int(fmaterial, 0)
write_int(fmaterial, 0)
material_useTexture = False
material_texture = 0
else:
# no texture
write_int(fmaterial, 0)
write_int(fmaterial, 0)
material_useTexture = False
material_texture = 0
else:
# no Principled BSDF. write garbage
write_3vector(fmaterial, 0.8, 0.8, 0.8)
write_3vector(fmaterial, 0.8, 0.8, 0.8)
write_3vector(fmaterial, 0.8, 0.8, 0.8)
write_int(fmaterial, 0)
write_int(fmaterial, 0)
material_colAmbient = set_value_when_none(material_colAmbient, (0.8, 0.8, 0.8))
material_colDiffuse = set_value_when_none(material_colDiffuse, (0.8, 0.8, 0.8))
material_colSpecular = set_value_when_none(material_colSpecular, (0.8, 0.8, 0.8))
material_colEmissive = set_value_when_none(material_colEmissive, (0.8, 0.8, 0.8))
material_specularPower = set_value_when_none(material_specularPower, 0.0)
material_useTexture = False
material_texture = 0
write_color(fmaterial, material_colAmbient)
write_color(fmaterial, material_colDiffuse)
write_color(fmaterial, material_colSpecular)
write_color(fmaterial, material_colEmissive)
write_float(fmaterial, material_specularPower)
write_bool(fmaterial, material_useTexture)
write_uint32(fmaterial, material_texture)
fmaterial.close()
@ -236,8 +550,8 @@ def export_bm(context,filepath,export_mode, export_target, no_component_suffix):
for texture in textureList:
# write finfo first
write_string(finfo, texture.name)
write_int(finfo, info_bm_type.TEXTURE)
write_long(finfo, ftexture.tell())
write_uint8(finfo, info_bm_type.TEXTURE)
write_uint64(finfo, ftexture.tell())
# confirm internal
texture_filepath = io_utils.path_reference(texture.filepath, source_dir, tempTextureFolder,
@ -245,10 +559,10 @@ def export_bm(context,filepath,export_mode, export_target, no_component_suffix):
filename = os.path.basename(texture_filepath)
write_string(ftexture, filename)
if (is_external_texture(filename)):
write_int(ftexture, 1)
write_bool(ftexture, True)
else:
# copy internal texture, if this file is copied, do not copy it again
write_int(ftexture, 0)
write_bool(ftexture, False)
if filename not in existed_texture:
shutil.copy(texture_filepath, os.path.join(tempTextureFolder, filename))
existed_texture.add(filename)
@ -271,17 +585,126 @@ def export_bm(context,filepath,export_mode, export_target, no_component_suffix):
# ======================================================================================= export / import assistant
# shared
class info_bm_type():
OBJECT = 0
MESH = 1
MATERIAL = 2
TEXTURE = 3
# import
class info_block_helper():
def __init__(self, name, offset):
self.name = name
self.offset = offset
self.blenderData = None
def load_component(component_id):
# get file first
compName = config.component_list[component_id]
selectedFile = os.path.join(
os.path.dirname(__file__),
'meshes',
compName + '.bin'
)
# read file. please note this sector is sync with import_bm's mesh's code. when something change, please change each other.
fmesh = open(selectedFile, 'rb')
# create real mesh, we don't need to consider name. blender will solve duplicated name
mesh = bpy.data.meshes.new('mesh_' + compName)
vList = []
vnList = []
faceList = []
# in first read, store all data into list
listCount = read_uint32(fmesh)
for i in range(listCount):
cache = read_3vector(fmesh)
# switch yz
vList.append((cache[0], cache[2], cache[1]))
listCount = read_uint32(fmesh)
for i in range(listCount):
cache = read_3vector(fmesh)
# switch yz
vnList.append((cache[0], cache[2], cache[1]))
listCount = read_uint32(fmesh)
for i in range(listCount):
faceData = read_component_face(fmesh)
# we need invert triangle sort
faceList.append((
faceData[4], faceData[5],
faceData[2], faceData[3],
faceData[0], faceData[1]
))
# then, we need add correspond count for vertices
mesh.vertices.add(len(vList))
mesh.loops.add(len(faceList)*3) # triangle face confirm
mesh.polygons.add(len(faceList))
mesh.create_normals_split()
# add vertices data
mesh.vertices.foreach_set("co", unpack_list(vList))
mesh.loops.foreach_set("vertex_index", unpack_list(flat_component_vertices_index(faceList)))
mesh.loops.foreach_set("normal", unpack_list(flat_component_vertices_normal(faceList, vnList)))
for i in range(len(faceList)):
mesh.polygons[i].loop_start = i * 3
mesh.polygons[i].loop_total = 3
mesh.polygons[i].use_smooth = True
mesh.validate(clean_customdata=False)
mesh.update(calc_edges=False, calc_edges_loose=False)
fmesh.close()
return mesh
def flat_vertices_index(faceList):
for item in faceList:
yield (item[0], )
yield (item[3], )
yield (item[6], )
def flat_vertices_normal(faceList, vnList):
for item in faceList:
yield vnList[item[2]]
yield vnList[item[5]]
yield vnList[item[8]]
def flat_vertices_uv(faceList, vtList):
for item in faceList:
yield vtList[item[1]]
yield vtList[item[4]]
yield vtList[item[7]]
def flat_component_vertices_index(faceList):
for item in faceList:
yield (item[0], )
yield (item[2], )
yield (item[4], )
def flat_component_vertices_normal(faceList, vnList):
for item in faceList:
yield vnList[item[1]]
yield vnList[item[3]]
yield vnList[item[5]]
# export
def is_component(name):
return get_component_id(name) != -1
def get_component_id(name):
return -1 # todo: finish this, -1 mean not a component
for ind, comp in enumerate(config.component_list):
if name.startswith(comp):
return ind
return -1
def is_external_texture(name):
if name in config.external_texture_list:
@ -296,19 +719,92 @@ def mesh_triangulate(me):
bm.to_mesh(me)
bm.free()
def try_get_custom_property(obj, field):
try:
return obj[field]
except:
return None
def set_value_when_none(obj, newValue):
if obj is None:
return newValue
else:
return obj
# ======================================================================================= file io assistant
# import
def peek_stream(fs):
res = fs.read(1)
fs.seek(-1, os.SEEK_CUR)
return res
def read_float(fs):
return struct.unpack("f", fs.read(4))[0]
def read_uint8(fs):
return struct.unpack("B", fs.read(1))[0]
def read_uint32(fs):
return struct.unpack("I", fs.read(4))[0]
def read_uint64(fs):
return struct.unpack("Q", fs.read(8))[0]
def read_string(fs):
count = read_uint32(fs)
return fs.read(count*4).decode("utf_32_le")
def read_bool(fs):
return read_uint8(fs) != 0
def read_worldMaterix(fs):
p = struct.unpack("ffffffffffffffff", fs.read(4*4*4))
res = mathutils.Matrix((
(p[0], p[2], p[1], p[3]),
(p[8], p[10], p[9], p[11]),
(p[4], p[6], p[5], p[7]),
(p[12], p[14], p[13], p[15])))
return res.transposed()
def read_3vector(fs):
return struct.unpack("fff", fs.read(3*4))
def read_2vector(fs):
return struct.unpack("ff", fs.read(2*4))
def read_face(fs):
return struct.unpack("IIIIIIIII", fs.read(4*9))
def read_component_face(fs):
return struct.unpack("IIIIII", fs.read(4*6))
# export
def write_string(fs,str):
count=len(str)
write_int(fs,count)
write_uint32(fs,count)
fs.write(str.encode("utf_32_le"))
def write_int(fs,num):
def write_uint8(fs,num):
fs.write(struct.pack("B", num))
def write_uint32(fs,num):
fs.write(struct.pack("I", num))
def write_long(fs,num):
def write_uint64(fs,num):
fs.write(struct.pack("Q", num))
def write_bool(fs,boolean):
if boolean:
write_uint8(fs, 1)
else:
write_uint8(fs, 0)
def write_float(fs,fl):
fs.write(struct.pack("f", fl))
def write_worldMatrix(fs, matt):
mat = matt.transposed()
fs.write(struct.pack("ffffffffffffffff",
@ -320,9 +816,12 @@ def write_worldMatrix(fs, matt):
def write_3vector(fs, x, y ,z):
fs.write(struct.pack("fff", x, y ,z))
def write_color(fs, colors):
write_3vector(fs, colors[0], colors[1], colors[2])
def write_2vector(fs, u, v):
fs.write(struct.pack("ff", u, v))
def write_face(fs, v1, vn1, vt1, v2, vn2, vt2, v3, vn3, vt3):
fs.write(struct.pack("IIIIIIIII", v1, vn1, vt1, v2, vn2, vt2, v3, vn3, vt3))
def write_face(fs, v1, vt1, vn1, v2, vt2, vn2, v3, vt3, vn3):
fs.write(struct.pack("IIIIIIIII", v1, vt1, vn1, v2, vt2, vn2, v3, vt3, vn3))

View File

@ -1,85 +1,113 @@
external_texture_list = set([
'Ball_LightningSphere1.bmp',
'Ball_LightningSphere2.bmp',
'Ball_LightningSphere3.bmp',
'Ball_Paper.bmp',
'Ball_Stone.bmp',
'Ball_Wood.bmp',
'Brick.bmp',
'Button01_deselect.tga',
'Button01_select.tga',
'Button01_special.tga',
'Column_beige.bmp',
'Column_beige_fade.tga',
'Column_blue.bmp',
'Cursor.tga',
'Dome.bmp',
'DomeEnvironment.bmp',
'DomeShadow.tga',
'ExtraBall.bmp',
'ExtraParticle.bmp',
'E_Holzbeschlag.bmp',
'FloorGlow.bmp',
'Floor_Side.bmp',
'Floor_Top_Border.bmp',
'Floor_Top_Borderless.bmp',
'Floor_Top_Checkpoint.bmp',
'Floor_Top_Flat.bmp',
'Floor_Top_Profil.bmp',
'Floor_Top_ProfilFlat.bmp',
'Font_1.tga',
'Gravitylogo_intro.bmp',
'HardShadow.bmp',
'Laterne_Glas.bmp',
'Laterne_Schatten.tga',
'Laterne_Verlauf.tga',
'Logo.bmp',
'Metal_stained.bmp',
'Misc_Ufo.bmp',
'Misc_UFO_Flash.bmp',
'Modul03_Floor.bmp',
'Modul03_Wall.bmp',
'Modul11_13_Wood.bmp',
'Modul11_Wood.bmp',
'Modul15.bmp',
'Modul16.bmp',
'Modul18.bmp',
'Modul18_Gitter.tga',
'Modul30_d_Seiten.bmp',
'Particle_Flames.bmp',
'Particle_Smoke.bmp',
'PE_Bal_balloons.bmp',
'PE_Bal_platform.bmp',
'PE_Ufo_env.bmp',
'Pfeil.tga',
'P_Extra_Life_Oil.bmp',
'P_Extra_Life_Particle.bmp',
'P_Extra_Life_Shadow.bmp',
'Rail_Environment.bmp',
'sandsack.bmp',
'SkyLayer.bmp',
'Sky_Vortex.bmp',
'Stick_Bottom.tga',
'Stick_Stripes.bmp',
'Target.bmp',
'Tower_Roof.bmp',
'Trafo_Environment.bmp',
'Trafo_FlashField.bmp',
'Trafo_Shadow_Big.tga',
'Tut_Pfeil01.tga',
'Tut_Pfeil_Hoch.tga',
'Wolken_intro.tga',
'Wood_Metal.bmp',
'Wood_MetalStripes.bmp',
'Wood_Misc.bmp',
'Wood_Nailed.bmp',
'Wood_Old.bmp',
'Wood_Panel.bmp',
'Wood_Plain.bmp',
'Wood_Plain2.bmp',
'Wood_Raft.bmp'
"atari.avi",
"atari.bmp",
"Ball_LightningSphere1.bmp",
"Ball_LightningSphere2.bmp",
"Ball_LightningSphere3.bmp",
"Ball_Paper.bmp",
"Ball_Stone.bmp",
"Ball_Wood.bmp",
"Brick.bmp",
"Button01_deselect.tga",
"Button01_select.tga",
"Button01_special.tga",
"Column_beige.bmp",
"Column_beige_fade.tga",
"Column_blue.bmp",
"Cursor.tga",
"Dome.bmp",
"DomeEnvironment.bmp",
"DomeShadow.tga",
"ExtraBall.bmp",
"ExtraParticle.bmp",
"E_Holzbeschlag.bmp",
"FloorGlow.bmp",
"Floor_Side.bmp",
"Floor_Top_Border.bmp",
"Floor_Top_Borderless.bmp",
"Floor_Top_Checkpoint.bmp",
"Floor_Top_Flat.bmp",
"Floor_Top_Profil.bmp",
"Floor_Top_ProfilFlat.bmp",
"Font_1.tga",
"Gravitylogo_intro.bmp",
"HardShadow.bmp",
"Laterne_Glas.bmp",
"Laterne_Schatten.tga",
"Laterne_Verlauf.tga",
"Logo.bmp",
"Metal_stained.bmp",
"Misc_Ufo.bmp",
"Misc_UFO_Flash.bmp",
"Modul03_Floor.bmp",
"Modul03_Wall.bmp",
"Modul11_13_Wood.bmp",
"Modul11_Wood.bmp",
"Modul15.bmp",
"Modul16.bmp",
"Modul18.bmp",
"Modul18_Gitter.tga",
"Modul30_d_Seiten.bmp",
"Particle_Flames.bmp",
"Particle_Smoke.bmp",
"PE_Bal_balloons.bmp",
"PE_Bal_platform.bmp",
"PE_Ufo_env.bmp",
"Pfeil.tga",
"P_Extra_Life_Oil.bmp",
"P_Extra_Life_Particle.bmp",
"P_Extra_Life_Shadow.bmp",
"Rail_Environment.bmp",
"sandsack.bmp",
"SkyLayer.bmp",
"Sky_Vortex.bmp",
"Stick_Bottom.tga",
"Stick_Stripes.bmp",
"Target.bmp",
"Tower_Roof.bmp",
"Trafo_Environment.bmp",
"Trafo_FlashField.bmp",
"Trafo_Shadow_Big.tga",
"Tut_Pfeil01.tga",
"Tut_Pfeil_Hoch.tga",
"Wolken_intro.tga",
"Wood_Metal.bmp",
"Wood_MetalStripes.bmp",
"Wood_Misc.bmp",
"Wood_Nailed.bmp",
"Wood_Old.bmp",
"Wood_Panel.bmp",
"Wood_Plain.bmp",
"Wood_Plain2.bmp",
"Wood_Raft.bmp"
])
component_list = [
"P_Extra_Life",
"P_Extra_Point",
"P_Trafo_Paper",
"P_Trafo_Stone",
"P_Trafo_Wood",
"P_Ball_Paper",
"P_Ball_Stone",
"P_Ball_Wood",
"P_Box",
"P_Dome",
"P_Modul_01",
"P_Modul_03",
"P_Modul_08",
"P_Modul_17",
"P_Modul_18",
"P_Modul_19",
"P_Modul_25",
"P_Modul_26",
"P_Modul_29",
"P_Modul_30",
"P_Modul_34",
"P_Modul_37",
"P_Modul_41",
"PC_TwoFlames",
"PE_Balloon",
"PR_Resetpoint",
"PS_FourFlames"
]

View File

@ -1,15 +1,46 @@
import bpy,bmesh
from . import utils
def create_rail_uv():
meshList = []
class RailUVOperator(bpy.types.Operator):
"""Create a UV for rail"""
bl_idname = "ballance.rail_uv"
bl_label = "Create Rail UV"
bl_options = {'UNDO'}
@classmethod
def poll(self, context):
return check_rail_target()
def execute(self, context):
create_rail_uv()
return {'FINISHED'}
# ====================== method
def check_rail_target():
for obj in bpy.context.selected_objects:
if obj.type != 'MESH':
continue
if obj.mode != 'OBJECT':
continue
if obj.data.uv_layers.active.data == None:
utils.ShowMessageBox("You should create a UV layer for this object firstly. Then execute this operator.", "No UV layer", 'ERROR')
return
continue
return True
return False
def create_rail_uv():
meshList = []
ignoredObj = []
for obj in bpy.context.selected_objects:
if obj.type != 'MESH':
ignoredObj.append(obj.name)
continue
if obj.mode != 'OBJECT':
ignoredObj.append(obj.name)
continue
if obj.data.uv_layers.active.data == None:
ignoredObj.append(obj.name)
continue
meshList.append(obj.data)
@ -22,6 +53,9 @@ def create_rail_uv():
uv_layer[loop_index].uv[0] = 0 # vecList[index].co[0]
uv_layer[loop_index].uv[1] = 1 # vecList[index].co[1]
if len(ignoredObj) != 0:
utils.ShowMessageBox("Following objects are not processed due to they are not suit for this function now: " + ', '.join(ignoredObj), "No processed object", 'WARNING')
def virtoolize_floor_uv():
pass

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -0,0 +1,29 @@
import bpy
class BallanceBlenderPluginPreferences(bpy.types.AddonPreferences):
bl_idname = __package__
external_folder: bpy.props.StringProperty(
name="External texture folder",
description="The Ballance texture folder which will be used buy this plugin to get external texture.",
)
no_component_collection: bpy.props.StringProperty(
name="No component collection",
description="(Import) The object which stored in this collectiion will not be saved as component. (Export) All forced no component objects will be stored in this collection",
)
temp_texture_folder: bpy.props.StringProperty(
name="Temp texture folder",
description="The folder which will temporarily store the textures which are extracted from bm. Due to system temp folder will be deleted after decoding of bm, so this path should not be blank.",
)
def draw(self, context):
layout = self.layout
row = layout.row()
col = row.column()
col.prop(self, "external_folder")
col.prop(self, "no_component_collection")
col.prop(self, "temp_texture_folder")

View File

@ -0,0 +1,128 @@
import bpy,mathutils
from . import utils
class SuperAlignOperator(bpy.types.Operator):
"""Align object with 3ds Max way"""
bl_idname = "ballance.super_align"
bl_label = "Super Align"
bl_options = {'UNDO'}
align_x: bpy.props.BoolProperty(name="X position")
align_y: bpy.props.BoolProperty(name="Y position")
align_z: bpy.props.BoolProperty(name="Z position")
current_references: bpy.props.EnumProperty(
name="Current",
items=(('MIN', "Min", ""),
('CENTER', "Center (bound box)", ""),
('POINT', "Center (axis)", ""),
('MAX', "Max", "")
),
)
target_references: bpy.props.EnumProperty(
name="Target",
items=(('MIN', "Min", ""),
('CENTER', "Center (bound box)", ""),
('POINT', "Center (axis)", ""),
('MAX', "Max", "")
),
)
@classmethod
def poll(self, context):
return check_align_target()
def execute(self, context):
align_object(self.align_x, self.align_y, self.align_z, self.current_references, self.target_references)
return {'FINISHED'}
def invoke(self, context, event):
wm = context.window_manager
return wm.invoke_props_dialog(self)
def draw(self, context):
layout = self.layout
col = layout.column()
col.label(text="Align axis")
row = col.row()
row.prop(self, "align_x")
row.prop(self, "align_y")
row.prop(self, "align_z")
col.prop(self, "current_references")
col.prop(self, "target_references")
# ============================== method
def check_align_target():
if bpy.context.active_object is None:
return False
selected = bpy.context.selected_objects[:]
length = len(selected)
if bpy.context.active_object in selected:
length -= 1
if length == 0:
return False
return True
def align_object(use_x, use_y, use_z, currentMode, targetMode):
if not (use_x or use_y or use_z):
return
# calc active object data
currentObj = bpy.context.active_object
currentObjBbox = [currentObj.matrix_world @ mathutils.Vector(corner) for corner in currentObj.bound_box]
currentObjRef = provideObjRefPoint(currentObj, currentObjBbox, currentMode)
# calc target
targetObjList = bpy.context.selected_objects[:]
if currentObj in targetObjList:
targetObjList.remove(currentObj)
# process each obj
for targetObj in targetObjList:
targetObjBbox = [targetObj.matrix_world @ mathutils.Vector(corner) for corner in targetObj.bound_box]
targetObjRef = provideObjRefPoint(targetObj, targetObjBbox, targetMode)
if use_x:
targetObj.location.x += currentObjRef.x - targetObjRef.x
if use_y:
targetObj.location.y += currentObjRef.y - targetObjRef.y
if use_z:
targetObj.location.z += currentObjRef.z - targetObjRef.z
def provideObjRefPoint(obj, vecList, mode):
refPoint = mathutils.Vector((0, 0, 0))
if (mode == 'MIN'):
refPoint.x = min([vec.x for vec in vecList])
refPoint.y = min([vec.y for vec in vecList])
refPoint.z = min([vec.z for vec in vecList])
elif (mode == 'MAX'):
refPoint.x = max([vec.x for vec in vecList])
refPoint.y = max([vec.y for vec in vecList])
refPoint.z = max([vec.z for vec in vecList])
elif (mode == 'CENTER'):
maxVecCache = mathutils.Vector((0, 0, 0))
minVecCache = mathutils.Vector((0, 0, 0))
minVecCache.x = min([vec.x for vec in vecList])
minVecCache.y = min([vec.y for vec in vecList])
minVecCache.z = min([vec.z for vec in vecList])
maxVecCache.x = max([vec.x for vec in vecList])
maxVecCache.y = max([vec.y for vec in vecList])
maxVecCache.z = max([vec.z for vec in vecList])
refPoint.x = (maxVecCache.x + minVecCache.x) / 2
refPoint.y = (maxVecCache.y + minVecCache.y) / 2
refPoint.z = (maxVecCache.z + minVecCache.z) / 2
else:
refPoint.x = obj.location.x
refPoint.y = obj.location.y
refPoint.z = obj.location.z
return refPoint