2
$\begingroup$

I am new to programming. Since Blender, I communicate with the PostgeSQL database. The purpose of the code is:

1 / Browse the table "works" and find new date_works for each object. 2 / affect the material to each object based on the date. 3 / if an object is in the group 'work', we find the parent. If date_works of the child is higher than that of the mother, we hide the child object.

This is the point 3, which does not. What do you think of my code? What is wrong? Thank you.

#Find the most recent work in the table "works"       
    for  works ,recentW in db.prepare("SELECT mesh_ works,max(date_ works) FROM  works GROUP BY mesh_ works"):    
       year=str(recentW.year)
       mat_M= bpy.data.materials.get(year)
       mesh=bpy.data.meshes["%s"% works]
       for obj in bpy.data.objects:
        if (obj.data==mesh):
                 c=obj                      
       # materials allocation in accordance with the date found. (example: finding dates 2015 => material it affects the "2015")
       c.active_material=mat_M
       # ensure that these objects are visible
       c.hide=False          
    # hide child objects whose work dates predate those of parents
       #if it is a child, 
       if c.name in bpy.data.groups["Child"].objects:
           #find parent
           parent=c.parent
           #search in the database of the most recent date of the parent
           recentParent=db.prepare("SELECT MAX(date_ works) FROM  works WHERE id_ works='%s'"%parent)
           #if the date of the parent is more recent than that of the child while the child is masked.
           for recent in recentParent:                      
                     if(recent[0]!=None):
                       yearParent=str(recent[0].year)                              
                       if(yearParent>=year):
                         c.hide=True
    return {'FINISHED'} 
$\endgroup$
3
  • 1
    $\begingroup$ Welcome to Blender SE! It helps to provide a minimal working example instead of just dumping a bunch of code for someone else to debug. Maybe it's even possible to cut this down into one line of code while still preserving the bug. $\endgroup$
    – Garrett
    Feb 10, 2015 at 18:45
  • $\begingroup$ Things like if (obj.data==mesh): should be if obj.type == 'MESH': . My advice would be to add print statements to what the script does and if it doesn't behave as you want add more prints of variable to find out why. If you want answer here, isolate part 4, or give it a try on stackoverflow. $\endgroup$
    – stacker
    Feb 10, 2015 at 19:17
  • $\begingroup$ I did not put "print" in the code I posted for short. my problem is that I always recent[0] = None $\endgroup$ Feb 10, 2015 at 19:38

1 Answer 1

0
$\begingroup$

Syntactically something like this should work (I striped the DB code):

import bpy

mat_M= bpy.data.materials.get('2015')
year='2014'
for obj in bpy.data.objects:
    c = None
    print(obj)
    if obj.type=='MESH':
        c=obj
    if c == None:
        continue                      
    c.active_material=mat_M
    c.hide=False          
    if c.name in bpy.data.groups["Child"].objects:
        parent=c.parent
        recentParent = [[2014],[2015]]
        for recent in recentParent:                      
            if recent[0]!=None:
                yearParent=str(recent)
                if yearParent>=year:
                    c.hide=True

for recent in recentParent iterates over the result of the db query, try your exact statement in a UI for your SQL server. If it doesn't return any data you need to fix that first. DB connections are better ask on stackoverflow. Note that there is a space in id_ works

$\endgroup$
2
  • $\begingroup$ everything works except the query returns nothing. Perhaps this is the expession ('%s'"%parent) is not correct. yes I correct id_works space.thank you $\endgroup$ Feb 11, 2015 at 8:48
  • $\begingroup$ it's good I found my problem. it was necessary to parent = c.parent.name thank you $\endgroup$ Feb 11, 2015 at 13:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .