Commit f64c8e07 authored by frankie's avatar frankie 💬

adding second generation script

parent b2a99afa
import os,re,subprocess
import time
RUN_GENERATION = True
RUN_RENDER = True
RUN_LEGS = False
blend_exec = '/opt/blender-2.79b-linux-glibc219-x86_64/./blender'
template_path = 'generate.blend'
generation_script = 'scripts/main.py'
template_path = os.path.abspath( 'generate.blend' )
generation_script = os.path.abspath( 'scripts/main.py' )
max_run = 1
current_run = 0
......@@ -16,7 +20,7 @@ def launch_generation():
print( "\t####################################################")
print( "\t############# starting run " + str(current_run) + '/' + str(max_run) + " #############" )
print( "\t####################################################")
p1 = subprocess.Popen( [ blend_exec, os.path.abspath( template_path ), '-P', os.path.abspath( generation_script ) ] )
p1 = subprocess.Popen( [ blend_exec, template_path, '-P', generation_script ] )
p1.wait()
total_run_time = time.time() - previous_run_time
previous_run_time = time.time()
......@@ -25,8 +29,9 @@ def launch_generation():
print( "\t####################################################")
current_run += 1
for i in range( 0, max_run ):
launch_generation()
if RUN_GENERATION:
for i in range( 0, max_run ):
launch_generation()
'''
# usefull to modify the frame gaps specified in 'render-file.py'
......@@ -35,10 +40,10 @@ if modify_render_files:
p1.wait()
'''
if launch_render:
if RUN_RENDER:
p1 = subprocess.Popen( [ 'python', 'runner_01_render.py' ] )
p1.wait()
if launch_render:
if RUN_LEGS:
p1 = subprocess.Popen( [ 'python', 'runner_05_legs.py' ] )
p1.wait()
\ No newline at end of file
import os, re, subprocess, colorsys
from time import gmtime, strftime
from PIL import Image,ImageFilter
blend_exec = '/opt/blender-2.79-linux-glibc219-x86_64/./blender'
weight_baker = os.path.abspath( 'scripts/weight_baking.py' )
hotfolder = '../generated'
folder_suffix_flag = '_done'
needle = 'render-file.py'
diffuse_needle = '_diffuse.png'
def boost_contrast( input_path, output_path = None ):
im = Image.open( input_path )
ld = im.load()
width, height = im.size
for y in range(height):
for x in range(width):
r,g,b = ld[x,y]
if r + g + b == 0:
continue
higher = 0
value = r
if g > value:
higher = 1
value = g
if b > value:
higher = 2
value = b
mult = 255.0 / ld[x,y][higher]
ld[x,y] = ( int(r*mult), int(g*mult), int(b*mult) )
'''
h,s,v = colorsys.rgb_to_hsv(r/255., g/255., b/255.)
h = (h + -90.0/360.0) % 1.0
s = s**0.65
r,g,b = colorsys.hsv_to_rgb(h, s, 1)
ld[x,y] = (int( v * 255 ),int( v * 255 ),int( v * 255 ))
ld[x,y] = (int( s * 255 ),int( s * 255 ),int( s * 255 ))
ld[x,y] = (int(r * 255.9999), int(g * 255.9999), int(b * 255.9999))
'''
im = im.filter( ImageFilter.BLUR )
out = output_path
if out == None:
out = input_path + '.png'
im.save( out,"PNG" )
def launch_rendering( path ):
files = os.listdir( path )
oktogo = False
diffuse_needle_found = False
blend_file = None
for f in files:
if f == needle:
oktogo = True
elif f[-len(diffuse_needle):] == diffuse_needle:
diffuse_needle_found = True
elif f[-6:] == '.blend':
blend_file = f
if oktogo == True:
print( "############# starting model generation ", path, "#############" )
p1 = subprocess.Popen( [ 'python', path + '/' + needle ] )
p1.wait()
print( "############# starting diffuse generation ", path, "#############" )
if not diffuse_needle_found and blend_file is not None:
print( "processing " + path + ' > ' + blend_file )
p1 = subprocess.Popen( [ blend_exec, os.path.join( path, blend_file ), '-P', weight_baker ] )
p1.wait()
# seeking diffuse
diffuse = None
files = [f for f in os.listdir(path)]
for f in files:
if f[-len(diffuse_needle):] == diffuse_needle:
diffuse = f
break
if diffuse is not None:
absp = os.path.join( path, diffuse )
boost_contrast( absp, absp ) # overwrite
os.rename( path, path + folder_suffix_flag )
folders = sorted([os.path.join(hotfolder, o) for o in os.listdir(hotfolder)
if os.path.isdir(os.path.join(hotfolder,o))])
for f in folders:
# double check
if os.path.isdir(f) is True:
if f[len(f)-len(folder_suffix_flag):] != folder_suffix_flag:
launch_rendering( f )
\ No newline at end of file
import bpy
import mathutils
'''
reads all quaternions of current pose and print in console as a dictionnary
dictionnary keys: bone name
dictionnary values: tuple with quaternion values
@param
name of the armature
'''
def print_pose( arma_name ):
print( "************** print_pose() start **************" )
arma_obj = bpy.data.objects[ arma_name ]
arma = arma_obj.pose
current_pose = "{"
bcount = 0
for bone in arma.bones:
if bcount > 0:
current_pose += ", "
current_pose += "\'" + bone.name + "\':("
current_pose += str( bone.rotation_quaternion[0] ) + ", "
current_pose += str( bone.rotation_quaternion[1] ) + ", "
current_pose += str( bone.rotation_quaternion[2] ) + ", "
current_pose += str( bone.rotation_quaternion[3] )
current_pose += ")"
bcount += 1
current_pose += "}"
print( current_pose )
print( "************** print_pose() end **************" )
'''
loads a specific pose on an armature
@param
name of the armature
dictionnay: key = bone name, value = tuple with quaternion values
'''
def load_pose( arma_name, pose ):
arma_obj = bpy.data.objects[ arma_name ]
arma = arma_obj.pose
bpy.context.scene.objects.active = arma_obj
bpy.ops.object.select_all(action='DESELECT')
bpy.ops.object.mode_set(mode='OBJECT')
bpy.ops.object.select_all(action='DESELECT')
#bpy.ops.object.select_pattern(pattern=arma_name)
bpy.ops.object.mode_set(mode='POSE')
for bone in arma.bones:
quat = mathutils.Quaternion( pose[ bone.name ] )
#print( bone, quat )
bone.rotation_quaternion = quat
bpy.ops.object.mode_set(mode='OBJECT')
bpy.ops.object.select_all(action='DESELECT')
def load_slerp_poses( arma_name, pose0, pose1, alpha ):
arma_obj = bpy.data.objects[ arma_name ]
arma = arma_obj.pose
pose = {}
for bone in arma.bones:
quat0 = mathutils.Quaternion( pose0[ bone.name ] )
quat1 = mathutils.Quaternion( pose1[ bone.name ] )
q = quat0.slerp( quat1, alpha )
pose[ bone.name ] = ( q[0], q[1], q[2], q[3] )
load_pose( arma_name, pose )
def load_random_pose( arma_name ):
cid0 = int( mathutils.noise.random() * ( len( pose_list ) - 1 ) )
cid1 = int( mathutils.noise.random() * ( len( pose_list ) - 1 ) )
alpha = mathutils.noise.random()
print( "mixing pose", cid0, "with", cid1, ", alpha:", alpha )
load_slerp_poses( arma_name, pose_list[ cid0 ], pose_list[ cid1 ], alpha )
pose_list = []
# initial
pose_list.append( {'root':(1.0, 0.0, 0.0, 0.0), 'hip_L':(1.0, 0.0, 0.0, 0.0), 'upperleg_L':(1.0, 0.0, 0.0, 0.0), 'leg_L':(1.0, 0.0, 0.0, 0.0), 'hip_R':(1.0, 0.0, 0.0, 0.0), 'upperleg_R':(1.0, 0.0, 0.0, 0.0), 'leg_R':(1.0, 0.0, 0.0, 0.0)} )
# tailleur
pose_list.append( {'root':(1.0, 0.0, 0.0, 0.0), 'hip_L':(1.0, 0.0, 0.0, 0.0), 'upperleg_L':(0.853783130645752, 0.366218239068985, -0.340086966753006, 0.14587566256523132), 'leg_L':(0.7087972164154053, -0.7054123878479004, -1.767170942912344e-07, -7.462822537718239e-08), 'hip_R':(1.0, 0.0, 0.0, 0.0), 'upperleg_R':(0.8362533450126648, 0.39717304706573486, 0.34150615334510803, -0.16219620406627655), 'leg_R':(0.5366555452346802, -0.8438015580177307, 8.917520233353571e-08, 1.448607633847132e-07)} )
# wide open
pose_list.append( {'root':(1.0, 0.0, 0.0, 0.0), 'hip_L':(1.0, 0.0, 0.0, 0.0), 'upperleg_L':(0.8886080384254456, 0.12094464898109436, -0.028764626011252403, 0.441498339176178), 'leg_L':(0.970582127571106, -0.24077068269252777, 3.070562470952609e-08, 2.7107310174301347e-08), 'hip_R':(1.0, 0.0, 0.0, 0.0), 'upperleg_R':(0.9203392863273621, 0.14903244376182556, 0.08222314715385437, -0.3521423935890198), 'leg_R':(0.9756578803062439, -0.21929827332496643, -7.636468346561287e-09, 1.431837937104774e-08)} )
# accroupi
pose_list.append( {'root':(1.0, 0.0, 0.0, 0.0), 'hip_L':(1.0, 0.0, 0.0, 0.0), 'upperleg_L':(0.9175531268119812, -0.32363080978393555, 0.07683564722537994, 0.21784336864948273), 'leg_L':(0.3376120924949646, -0.9412853717803955, 2.551666682393261e-07, -4.7171346295726835e-07), 'hip_R':(1.0, 0.0, 0.0, 0.0), 'upperleg_R':(0.9188348054885864, -0.3350473642349243, 0.006266191601753235, -0.20844024419784546), 'leg_R':(0.30799275636672974, -0.9513887166976929, -1.5425885635522718e-07, 2.6720127266344207e-07)} )
# jambe gauche croisée ouverte
pose_list.append( {'root':(1.0, 0.0, 0.0, 0.0), 'hip_L':(1.0, 0.0, 0.0, 0.0), 'upperleg_L':(0.7443562746047974, 0.20229847729206085, -0.5997703075408936, 0.21280235052108765), 'leg_L':(0.5829612612724304, -0.8124999403953552, -2.003662729066491e-07, 2.2044488900974102e-08), 'hip_R':(1.0, 0.0, 0.0, 0.0), 'upperleg_R':(0.9687809348106384, -0.1321210265159607, 0.20785640180110931, -0.02834717556834221), 'leg_R':(0.9775136113166809, -0.2108728289604187, -7.621973274751781e-09, 3.417979144160199e-08)} )
# jambe droite croisée ouverte
pose_list.append( {'root':(1.0, 0.0, 0.0, 0.0), 'hip_L':(1.0, 0.0, 0.0, 0.0), 'upperleg_L':(0.991597056388855, -0.051413580775260925, -0.09881721436977386, 0.06578522175550461), 'leg_L':(0.8825993537902832, -0.4701261818408966, -7.017109737716964e-08, -6.680762965061149e-08), 'hip_R':(1.0, 0.0, 0.0, 0.0), 'upperleg_R':(0.7513235211372375, 0.2894059121608734, 0.5897215604782104, -0.06313477456569672), 'leg_R':(0.6219395995140076, -0.7830652594566345, 7.786730549241838e-08, -3.593877551111291e-08)} )
# jambe gauche croisée fermée
pose_list.append( {'root':(1.0, 0.0, 0.0, 0.0), 'hip_L':(1.0, 0.0, 0.0, 0.0), 'upperleg_L':(0.9754602313041687, 0.07017459720373154, -0.09620121866464615, -0.1851980835199356), 'leg_L':(0.8756988644599915, -0.4828576147556305, -9.087131758178657e-08, -3.9033391630027836e-08), 'hip_R':(1.0, 0.0, 0.0, 0.0), 'upperleg_R':(0.9802179932594299, -0.10994869470596313, 0.08077140152454376, 0.1433885097503662), 'leg_R':(0.9827722311019897, -0.18482141196727753, -2.3691193451469417e-10, 3.0561672303974774e-08)} )
# jambe droite croisée fermée
pose_list.append( {'root':(1.0, 0.0, 0.0, 0.0), 'hip_L':(1.0, 0.0, 0.0, 0.0), 'upperleg_L':(0.9830724596977234, -0.11871244758367538, 0.01673079840838909, -0.13854987919330597), 'leg_L':(0.9777296781539917, -0.20986871421337128, -3.52438362938301e-08, -1.1787633980020473e-08), 'hip_R':(1.0, 0.0, 0.0, 0.0), 'upperleg_R':(0.97410649061203, 0.04138699173927307, 0.1591317057609558, 0.15517999231815338), 'leg_R':(0.9522908926010132, -0.30519193410873413, 1.0268800920698595e-08, 3.3236080554388536e-08)} )
####### development #######
# print_pose( "butt_armature" )
# reset to default
load_pose( "butt_armature", pose_list[ 0 ] )
cid0 = int( mathutils.noise.random() * ( len( pose_list ) - 1 ) )
cid1 = int( mathutils.noise.random() * ( len( pose_list ) - 1 ) )
alpha = mathutils.noise.random()
print( "mixing pose", cid0, "with", cid1, ", alpha:", alpha )
# load_slerp_poses( "butt_armature", pose_list[ cid0 ], pose_list[ cid1 ], alpha )
# load_pose( "butt_armature", pose_list[ int( mathutils.noise.random() * ( len( pose_list ) - 1 ) ) ] )
\ No newline at end of file
This diff is collapsed.
import os
hotfolder = '../generated'
folder_suffix_flag = '_done'
needle = 'render-file.py'
search_string = 'frame_step = 1'
replace_string = 'frame_step = 200'
folders = sorted([os.path.join(hotfolder, o) for o in os.listdir(hotfolder)
if os.path.isdir(os.path.join(hotfolder,o))])
found = 0
for f in folders:
if f[-len(folder_suffix_flag):] != folder_suffix_flag:
files = os.listdir( f )
for fi in files:
if fi == needle:
found += 1
filedata = ''
with open( os.path.join( hotfolder, f, fi ), 'r') as pyf :
filedata = pyf.read()
filedata = filedata.replace( search_string, replace_string )
pyf.close()
with open( os.path.join( hotfolder, f, fi ), 'w') as pyf:
pyf.write(filedata)
pyf.close()
\ No newline at end of file
import bpy
import colorsys
import os
wname = 'cube_diffuse'
wwidth = 1024
wheight = 1024
do_bake = True
bpy.data.objects['floor'].location[2] = -100
if not wname in bpy.data.images:
bpy.ops.image.new(name=wname, width=wwidth, height=wheight, color=(0.0, 0.0, 0.0, 1.0), alpha=False, generated_type='BLANK', float=True)
wmap = bpy.data.images[wname]
wmap.filepath = '//' + os.path.basename(bpy.data.filepath)[:-6] + '_diffuse.png'
print( (wwidth*wheight), len( wmap.pixels ), len( wmap.pixels ) / (wwidth*wheight) )
'''
newpixels = [0.0] * len( wmap.pixels )
pcount = len( wmap.pixels )
for y in range( 0, wheight ):
for x in range( 0, wwidth ):
pixid = x + y * wwidth
rid = ( pixid * 4 )
gid = ( pixid * 4 ) + 1
bid = ( pixid * 4 ) + 2
aid = ( pixid * 4 ) + 3
newpixels[ rid ] = x / wwidth
newpixels[ gid ] = y / wheight
newpixels[ aid ] = 1.0
wmap.pixels = newpixels
'''
if do_bake:
cube = bpy.data.objects['cube']
material = cube.material_slots[0].material
#bpy.ops.object.select_pattern(pattern="cube")
#bpy.context.scene.objects.active = cube
#bpy.context.active_object = cube
#cube.active_material_index = 0
#bpy.ops.node.add_node(type="ShaderNodeTexImage", use_transform=True)
nodes = material.node_tree.nodes
node = nodes.new('ShaderNodeTexImage')
node.name = 'bake_target'
node.image = wmap
for i in range(0, 20):
if i == 1:
bpy.context.scene.layers[i] = True
else:
bpy.context.scene.layers[i] = False
bpy.context.scene.frame_current = 0
cube.select = True
bpy.context.scene.objects.active = cube
bpy.ops.object.bake(type='DIFFUSE')
wmap.save()
quit()
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment