blender ,通过具有固定名称+变量的脚本分配纹理
blender , assign a texture via script with fixed name + variable
这是我的问题。该脚本在 Blender 2.9 中运行良好,直到我将该随机变量放入名称中。基本上我需要通过脚本 link 为不同的文件和材料添加不同的纹理。问题是我有 label 21.jpg, label 34.jpg , label 345.jpg 。那个“随机变量”是一个占位符,我需要放在那里才能成功加载标签纹理,无论名称中的随机数是什么?提前致谢
import bpy
import os
from random import *
from time import gmtime, strftime
import xml.etree.ElementTree as ET
from bpy.types import Operator
from bpy.props import FloatVectorProperty,StringProperty, IntProperty, BoolProperty, EnumProperty
os.getcwd()
def remap_texture_images():
color_img = bpy.data.images['color']
color_img.filepath = bpy.path.relpath("//" + "label" + random_variable + ".jpg")
remap_texture_images()
你可以试试:
import os
import glob
import bpy
def remap_texture_images():
color_img = bpy.data.images['color']
list_of_images = glob.glob('*.jpg')
if list_of_images:
image_name = list_of_images[0]
color_img.filepath = bpy.path.relpath(os.path.join(os.getcwd(), image_name))
else:
print("No image with jpg extension in this directory.")
remap_texture_images()
还没有测试过,但它应该可以工作:
def get_image_paths():
from pathlib import Path
return list(Path("path/to/thousands/of/folders").rglob("*.jpg"))
def remap_texture_images(paths):
from random import choice
color_img = bpy.data.images["color"]
color_img.filepath = bpy.path.relpath(str(choice(paths)))
remap_texture_images(get_image_paths())
这假设您的图像都是 JPG,并存储在名为 label
的相对文件夹中。
它还假定您想要随机 select 并应用文件夹中的任何图像,而不考虑重复。也就是说,多个color_img
可能有相同的filepath
.
import os
import glob
import bpy
def remap_texture_images():
os.chdir("insert_absolute_path_you_start_looking_for_images_here_from_here")
path = os.getcwd()
color_img = bpy.data.images['color']
list_of_images = glob.glob('*.jpg')
print (list_of_images)
print (path)
if list_of_images:
image_name = list_of_images[0]
color_img.filepath = bpy.path.relpath(os.path.join(os.getcwd(), image_name))
else:
print("No image with jpg extension in this directory.")
remap_texture_images()
感谢大家的帮助。 @jma 一切都很完美,但我怀疑 blender 用作工作目录,它是自己的临时目录,所以 glob.glob 会在那里扫描,这就是打印路径调试行的原因。基本上我要做的就是添加一行 os.chdir ,以确保从我需要的文件夹开始
这是我的问题。该脚本在 Blender 2.9 中运行良好,直到我将该随机变量放入名称中。基本上我需要通过脚本 link 为不同的文件和材料添加不同的纹理。问题是我有 label 21.jpg, label 34.jpg , label 345.jpg 。那个“随机变量”是一个占位符,我需要放在那里才能成功加载标签纹理,无论名称中的随机数是什么?提前致谢
import bpy
import os
from random import *
from time import gmtime, strftime
import xml.etree.ElementTree as ET
from bpy.types import Operator
from bpy.props import FloatVectorProperty,StringProperty, IntProperty, BoolProperty, EnumProperty
os.getcwd()
def remap_texture_images():
color_img = bpy.data.images['color']
color_img.filepath = bpy.path.relpath("//" + "label" + random_variable + ".jpg")
remap_texture_images()
你可以试试:
import os
import glob
import bpy
def remap_texture_images():
color_img = bpy.data.images['color']
list_of_images = glob.glob('*.jpg')
if list_of_images:
image_name = list_of_images[0]
color_img.filepath = bpy.path.relpath(os.path.join(os.getcwd(), image_name))
else:
print("No image with jpg extension in this directory.")
remap_texture_images()
还没有测试过,但它应该可以工作:
def get_image_paths():
from pathlib import Path
return list(Path("path/to/thousands/of/folders").rglob("*.jpg"))
def remap_texture_images(paths):
from random import choice
color_img = bpy.data.images["color"]
color_img.filepath = bpy.path.relpath(str(choice(paths)))
remap_texture_images(get_image_paths())
这假设您的图像都是 JPG,并存储在名为 label
的相对文件夹中。
它还假定您想要随机 select 并应用文件夹中的任何图像,而不考虑重复。也就是说,多个color_img
可能有相同的filepath
.
import os
import glob
import bpy
def remap_texture_images():
os.chdir("insert_absolute_path_you_start_looking_for_images_here_from_here")
path = os.getcwd()
color_img = bpy.data.images['color']
list_of_images = glob.glob('*.jpg')
print (list_of_images)
print (path)
if list_of_images:
image_name = list_of_images[0]
color_img.filepath = bpy.path.relpath(os.path.join(os.getcwd(), image_name))
else:
print("No image with jpg extension in this directory.")
remap_texture_images()
感谢大家的帮助。 @jma 一切都很完美,但我怀疑 blender 用作工作目录,它是自己的临时目录,所以 glob.glob 会在那里扫描,这就是打印路径调试行的原因。基本上我要做的就是添加一行 os.chdir ,以确保从我需要的文件夹开始