Previous Thread  Next Thread

chat icon Help!

Vagelis Kounis

Posted: Fri Nov 16, 2012 10:47 pm
Joined: 16 Nov 2012
Posts: 4
Hi everyone! First of all i am new to python but i understand programming well. I am trying to build a script to perform some already coded operations in Blender. More specifically, i want to duplicate an object and inside a for loop move it and calculate the boolean intersection with itself. I need all the help or ideas you can give me.
Reply with quote


CoDEmanX

Posted: Sat Nov 17, 2012 1:06 pm
Joined: 05 Apr 2009
Posts: 683
perform your actions, then have a look at the Info bar in Scripting view. The called operators are listed there, and you can copy/paste (note that some operators call multiple others and you can't copy/paste that code). You can strip couple parameters and adjust the ones you wanna calculate with script.

There are, however, better ways to perform operations for most of them (called "low-level calls").

You can do e.g.

Code:
bpy.ops.object.duplicate()
bpy.ops.transform.translate(value=(0,0,2))

to duplicate and move the selected objects.

But can also do it low-level:

Code:
for ob in bpy.context.selected_objects:
    ob_new = ob.copy()
    ob_new.data = ob.data.copy()
    ob_new.location.z += 2
    bpy.context.scene.objects.link(ob_new)
bpy.context.scene.update()


in certain situations it's easier to use operators, but low-level calls a faster and mostly don't rely on a certain context.
_________________
I'm sitting, waiting, wishing, building Blender in superstition...
Reply with quote


Vagelis Kounis

Posted: Sat Nov 17, 2012 8:39 pm
Joined: 16 Nov 2012
Posts: 4
Thank you very much for replying on such sort notice. I did what you said, and i finally created a mini-program in which i am missing a line..
Code:

import bpy

bpy.ops.object.select_all(action='TOGGLE')
bpy.ops.object.select_all(action='TOGGLE')
bpy.ops.object.delete(use_global=False)

actor1 = bpy.ops.mesh.primitive_cone_add(radius1=4,depth=11)
actor2 = bpy.ops.object.duplicate()

x=0

while x<2 :
     bpy.ops.transform.translate(value=(0,0,x))
     bpy.ops.object.modifier_add(type='BOOLEAN')
     #the missing line is here and should specify which is the second object of the boolean (in this case actor1) and the type of boolean operation (i want intersection but its default)
     bpy.ops.object.modifier_apply(apply_as='DATA', modifier="Boolean")


when performing a boolean operation using the GUI it doesn't produce a result when you select the object
Reply with quote


CoDEmanX

Posted: Sun Nov 18, 2012 6:29 pm
Joined: 05 Apr 2009
Posts: 683
bpy.ops.object.select_all(action='TOGGLE') is bound to A key, but if you wanna select everything, use action='SELECT' instead


the actor vars are useless, as operators don't return object references, but sets (e.g. {'FINISHED'} or {'CANCELLED'})
Code:
actor1 = bpy.ops.mesh.primitive_cone_add(radius1=4,depth=11)
actor2 = bpy.ops.object.duplicate()


not very pythonic and you didn't post x += 1 ?!
Code:
x=0
while x<2 :

better do:
Code:
for x in range(2):
    # ...


to get a reference to the cone, we need to get the selection after creation (this is another drawback of operators, we could use references with low-level calls directly)

Code:
import bpy

bpy.ops.object.select_all(action='SELECT')
bpy.ops.object.delete(use_global=False)

bpy.ops.mesh.primitive_cone_add(radius1=4,depth=11)
actor1 = bpy.context.object


bpy.ops.object.duplicate()
bpy.ops.transform.translate(value=(0,0,1))

# actor2 is selected, modifier will be added to it
bpy.ops.object.modifier_add(type='BOOLEAN')

bpy.context.object.modifiers[-1].object = actor1

bpy.ops.object.modifier_apply(apply_as='DATA', modifier="Boolean")

_________________
I'm sitting, waiting, wishing, building Blender in superstition...
Reply with quote


Vagelis Kounis

Posted: Fri Nov 23, 2012 6:22 pm
Joined: 16 Nov 2012
Posts: 4
i had x+=1 in my code but further down. Your help was more than i could hope for. Thank you.
Reply with quote


 
Jump to:  
Powered by phpBB © 2001, 2005 phpBB Group