blender2.79,一些自己常用的小脚本

主要是在用blender的时候一些常用的的脚本
比如场景中很多的集体操作
比如我选择一堆的物体我想直接一起设置相同的缩放比怎么办呢?
我这里直接通过小脚本来,还有对所有的对象设置属性等等
这里就放了一些脚本,

python 有一些坑就是,缩进很坑,要小心使用的编辑器
我这里使用的是sublime Text

import bpy 
import os 


def GetSelectedObjects():
	selected_objects = [ o for o in bpy.context.scene.objects if o.select ]
	return selected_objects;



# 进行改名,大写变小写并修改前缀
def changeLowerName():
	objs = bpy.data.objects
	for (k,v) in  objs.items(): 
		if v.type == "MESH":
			oobj = objs[k]
			uname = oobj.name
			ss = uname[:2];
			if (ss == "HH_"):
				uname = "ks" + uname[2:];
			oobj.name = uname.lower();
			# print(ss);
			#oobj.name = oobj.name.lower();
	return;
# 对所有没有ks_前缀的添加前缀
def ChangeByAddName():
	objs = bpy.data.objects
	for (k,v) in  objs.items(): 
			if v.type == "MESH":
				uname = v.name
				ss = uname[:3];
				if (ss != "ks_"):
					uname = "ks_" + uname;
				v.name = uname;
				v.data.name = uname;
	return;
#统一设置父节点
def changeNameByParent(parentName):
	objs = bpy.data.objects
	uParent = objs[parentName];
	if (not uParent):
		return;
	for (k,v) in  objs.items():
		if (v.parent == uParent):
			v.data.name = v.name;
	return;
# 查询所有模型的资源地址
def findTexSource():
	objs = bpy.data.objects
	for (k,v) in  objs.items(): 
		if v.type == "MESH":
			mtl = v.active_material;
			tex = mtl.active_texture
			if (tex.type == "IMAGE"):
				print(tex.image.filepath);
				# source = tex.image.source
				# if (source.type == "FILE"):
				# print(source);


# findTexSource();

# 对所有模型设置相同的材质
def SetSameMtl(mtlName):
	uMtl = bpy.data.materials[mtlName]
	if (uMtl):
		objs = bpy.data.objects
		for (k,v) in  objs.items(): 
			if v.type == "MESH":
				v.active_material = uMtl
#打印使用的的材质有哪些
def FindNouseDifMtl(mtlName):
	objs = bpy.data.objects
	for (k,v) in  objs.items(): 
		if v.type == "MESH":
			mtl = v.active_material;
			if (mtl and mtl.name != mtlName):
				print(k, mtl.name);


# 查询不同的文理地址的材质并打印
def findDifMtl():
	testDict = {}
	objs = bpy.data.objects
	for (k,v) in  objs.items(): 
		if v.type == "MESH":
			mtl = v.active_material;
			tex = mtl.active_texture
			if (tex.type == "IMAGE"):
				testDict[tex.image.filepath] = True;

	for (k,v) in testDict.items():
		print(k);


# 查询用到指定材质的对象名字并打印
def findUseMtlMesh(mtlName):
	objs = bpy.data.objects
	for (k,v) in  objs.items(): 
		if v.type == "MESH":
			mtl = v.active_material;
			if (mtl.name != mtlName):
				print(k);

# 对所有模型对象进行缩放
def SetScaleAll(sc):
	objs = bpy.data.objects
	for (k,v) in  objs.items(): 
		if v.type == "MESH":
			v.scale = [sc,sc,sc]
# 对选择的模型设置统一的缩放比率
def SetSelectObjectsScale(sc):
	objs = GetSelectedObjects();
	for v in  objs: 
		if v.type == "MESH":
			v.scale = [sc,sc,sc]

# 设置所有模型的父节点
def SetParentAll(parentName):
	objs = bpy.data.objects
	oobj = objs[parentName];
	print(oobj)
	if (not oobj):
		return;
	print("==start==>")
	for (k,v) in  objs.items():
		if (parentName != k and v.type == "MESH" and not v.parent):
			v.parent = oobj;
# 针对一些模型设置父节点
def SetParentAll(parentName):
	objs = bpy.data.objects
	oobj = objs[parentName];
	print(oobj)
	if (not oobj):
		return;
	print("==start==>")
	for (k,v) in  objs.items():
		# uk = k[:2];
		# print("uk:"+uk)
		if (parentName != k and v.type == "MESH" and not v.parent and k[:3] == "ks_"):
			v.parent = oobj;

# 设置所有模型的z值
def SetZAll(zz):
	objs = bpy.data.objects
	print("==start==>")
	for (k,v) in  objs.items():
		if (v.type == "MESH"):
			location = v.location
			location[2] = zz;
			v.location = location;
# 对所有的模型的移动加锁
def UnlockAllLocation():
	objs = bpy.data.objects
	print("==start==>")
	for (k,v) in  objs.items():
		if (v.type == "MESH"):
			v.lock_location = [False, False, False]
	return;

# 打印选中的模型的节点名字
def UpdateSelectedObjects():
	objs = GetSelectedObjects();
	print(objs);
	print(len(objs))
	for obj in objs:
		print(obj.name);
	return;
# 设置所有选中的点的属性
def SetSelectedObjects(key,value):
	objs = GetSelectedObjects();
	for obj in objs:
		obj[key] = value;
	return;
# 设置所有选中的模型的父节点
def SetSelectedObjectsParent(parentName):
	objs = bpy.data.objects
	oobj = objs[parentName];
	objs = GetSelectedObjects();
	if (not oobj):
		print("can not find parent ")
		return;
	for obj in objs:
		obj.parent = oobj;
	return;
# 统一对对节点的名字进行修改
def FindAndCheckResetMesh():
	objs = bpy.data.objects
	for (k,v) in  objs.items():
		if (v.type == "MESH" and k[:3] == "ks_"):
			ukey = k;
			dataName = v.data.name;
			ii = ukey.find('.');
			if (dataName.find('.') > -1 and ii > -1):
				if (ii > -1):
					ukey = ukey[:ii];
				uObj = objs[ukey];
				v.data = uObj.data;
	return;

如果自己如果要拓展或者想使用可以直接

blender2.79,一些自己常用的小脚本_第1张图片

打开就可以查很多相关的api使用了

你可能感兴趣的:(blender)