Ozbend
(Ozbend)
February 20, 2018, 1:04pm
1
Hi! I have a good idea little script, but he is doesn’t works good.
What it does: by clicking on button it loads a model obj from a folder, assigns materials by names, takes a few pictures (depends on the number of cameras), saves it to a folder.
Who can fix it?
Attachments
main.zip (1.63 KB)
renderhjs
(renderhjs)
February 22, 2018, 8:24pm
2
Took the liberty to embed the code here, use the CODE tags to embed similar to the QUOTE tags
import os;import bpy;
import time;
import sys;
from time import sleep;
###################################################################
#### Set Image Size
IMAGE_SIZE = 512;
###################################################################
#### Set Models (OBJ) Directory
MODEL_DIRECTORY = 'C:/Blender_proj/jewelers/models/';
###################################################################
#### Set IMAGE (JPG) Directory
IMAGE_DIRECTORY = 'C:/Blender_proj/jewelers/images/';
###################################################################
### Get The Materials From The Scene
mMetalsMaterials = [];
mGamMaterials = [];
### ADD Metal Materials
#mMetalsMaterials.append(bpy.data.materials.get("Metal_B"));
mMetalsMaterials.append(bpy.data.materials.get("Metal_Gold"));
mMetalsMaterials.append(bpy.data.materials.get("Metal_Silver"));
### ADD Gam Materials
mGamMaterials.append(bpy.data.materials.get("Gem_1"));
#mGamMaterials.append(bpy.data.materials.get("Gem_2"));
mGamMaterials.append(bpy.data.materials.get("Gem_3"));
##################################################################
############# DO NOT CHANGE THE CODE FROM HERE ##################
##################################################################
def setMesh(pObj,pVector):
#pObj.scale = (0.1, 0.1, 0.1);
pObj.rotation_euler = pVector;
# pObj.location = (0, 0, 1.2);
##################################################
def setModel(pVector):
for aObj in mAll:
setMesh(aObj,pVector);
##################################################
mBaseRotation = (1,0,0);
mCameras = [];
for aObj in bpy.data.objects:
if aObj.type == 'CAMERA':
mCameras.append(aObj);
def render(pPath):
bpy.data.scenes['Scene'].render.filepath = pPath;
bpy.context.scene.render.resolution_x = IMAGE_SIZE;
bpy.context.scene.render.resolution_y = IMAGE_SIZE;
bpy.ops.render.render(write_still=True);
##################################################
# Assign Materials to object
def setMaterials(pObj,pMat):
if pObj.data.materials:
pObj.data.materials[0] = pMat;
else:
pObj.data.materials.append(pMat);
##################################################
def cleanObject():
for aObj in bpy.data.objects:
if aObj.type == 'MESH' :
if ((aObj.name[0] == "M") or (aObj.name[0] == "G") or (aObj.name[0] == "T")):
aObj.select = True;
else:
aObj.select = False;
bpy.ops.object.delete();
##################################################
def showProgressBar(pName):
global mSubModelIndex;
mSubModelIndex = mSubModelIndex + 1;
#sys.stdout.write("Start Rendering " + str(mTotal) +"
");
sys.stdout.write("Model " + str(mCurrentModelIndex) + " / " + str(mTotal) + ": Rendering " + str(mSubModelIndex) + " / " + str(mModelTotal)+ " name: "+ pName +"
");
sys.stdout.flush();
def renderGamMesh(pIndex,pName,pMetals,pGams):
if(pIndex >= len(pGams)):
#for i in range(len(mCameras)):
#sceneKey = bpy.data.scenes.keys()[0];
#bpy.data.screens[sceneKey].camera = mCameras[i];
#mCameras[i].select = True;
setModel((1,0,0));
render(IMAGE_DIRECTORY + pName + '_Ang'+ str(1)+'.jpg');
setModel((1,0.8,0.1));
render(IMAGE_DIRECTORY + pName + '_Ang'+ str(2)+'.jpg');
showProgressBar(pName);
return;
for i in range(len(mGamMaterials)):
setMaterials(pGams[pIndex], mGamMaterials[i]);
renderGamMesh(pIndex+1,pName+"-"+ str(i),pMetals,pGams);
##################################################
def renderMetalMesh(pIndex,pName,pMetals,pGams):
if(pIndex >= len(pMetals) ):
renderGamMesh(0,pName + "_Gam",pMetals,pGams);
return;
for i in range(len(mMetalsMaterials)):
setMaterials(pMetals[pIndex], mMetalsMaterials[i]);
aName = pMetals[pIndex].name;
global mTexts;
for t in range(len(mTexts[aName])):
setMaterials( mTexts[aName][t] , mMetalsMaterials[i]);
renderMetalMesh(pIndex+1,pName+"-"+ str(i),pMetals,pGams);
##################################################
def renderItem(pName):
aModelUrl = MODEL_DIRECTORY + pName + ".obj";
cleanObject();
imported_object = bpy.ops.import_scene.obj(filepath=aModelUrl, use_smooth_groups=False, use_split_groups=True);
global mTexts;
global mMetals;
global mGams;
global mAll;
mTexts = {};
mMetals = [];
mGams = [];
mAll = [];
for aObj in bpy.data.objects:
if aObj.type == 'MESH':
if (aObj.name[0] == "M"):
mTexts[aObj.name] = [];
mMetals.append(aObj);
mAll.append(aObj);
if (aObj.name[0] == "G"):
mAll.append(aObj);
mGams.append(aObj);
global mModelTotal;
mModelTotal = pow(len(mGamMaterials), len(mGams))*pow(len(mMetalsMaterials),len(mMetals));
for aObj in bpy.data.objects:
if aObj.type == 'MESH':
if (aObj.name[0] == "T"):
aName = aObj.name[4:];
mAll.append(aObj);
mTexts[aName].append(aObj);
print("aName = " + aName)
renderMetalMesh(0,pName + "_Met",mMetals,mGams);
listing = os.listdir(MODEL_DIRECTORY);
mTotal = len(listing);
mCurrentModelIndex = 0;
mSubModelIndex = 0
mModelTotal = 0;
mTexts = 0;
os.system("cls");
for entry in listing:
mCurrentModelIndex = 0;
aFileName = entry[:-4];
renderItem(aFileName);
sys.stdout.write("DONE
");
sys.stdout.flush();