Help!

An open space where people who use Blender in an academic environment can share progress and the typical problems and needs of academic environments.

Moderators: jesterKing, stiv

Vagelis Kounis
Posts: 4
Joined: Fri Nov 16, 2012 6:04 pm

Help!

Postby Vagelis Kounis » Fri Nov 16, 2012 10:47 pm

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.

CoDEmanX
Posts: 894
Joined: Sun Apr 05, 2009 7:42 pm
Location: Germany

Postby CoDEmanX » Sat Nov 17, 2012 1:06 pm

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: Select all

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: Select all

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...

Vagelis Kounis
Posts: 4
Joined: Fri Nov 16, 2012 6:04 pm

Postby Vagelis Kounis » Sat Nov 17, 2012 8:39 pm

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: Select all

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

CoDEmanX
Posts: 894
Joined: Sun Apr 05, 2009 7:42 pm
Location: Germany

Postby CoDEmanX » Sun Nov 18, 2012 6:29 pm

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: Select all

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: Select all

x=0
while x<2 :

better do:

Code: Select all

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: Select all

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...

Vagelis Kounis
Posts: 4
Joined: Fri Nov 16, 2012 6:04 pm

Postby Vagelis Kounis » Fri Nov 23, 2012 6:22 pm

i had x+=1 in my code but further down. Your help was more than i could hope for. Thank you.

GunnarG
Posts: 1
Joined: Thu Aug 29, 2013 4:35 pm

Postby GunnarG » Thu Aug 29, 2013 4:58 pm

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


Thanks, this tip solved a major Blender headache for me.

I had a problem roughly as follows (Blender 2.65):

Code: Select all

myObj = ...create object...
copy1 = ...call bpy.ops.object.duplicate() on myObj...
...change location.x/y and name of copy1
copy2 = ...call duplicate() on myObj...

On the [second duplicate() call, my changes to the first copy ("copy1" above) were reverted (e.g. its position was reset to the original myObj position).

I don't know Blender internals well enough to say for sure if the behavior of duplicate() is indeed correct in this case, but it does seem rather weird.

Luckily, using myObj.copy() instead, as you proposed, worked as expected.


Return to “Academic & Research”

Who is online

Users browsing this forum: No registered users and 2 guests