The image of use is,
1. Run this code - add 'Cube' at center automatically
2. Push 'Generate' button
3. Generate eight objects around the center cube
4. Select one from them
5. Do step (2.)
inside the code
1. Add one cube at center (0, 0, 0)
2. Set eight operations as function
3. Duplicate 'Cube', and do each operation
4. Select one object from new eight objects
5. If push 'Generate' button, delete unselected objects
6. Set the pos of selected object to center(0, 0, 0)
7. Set the name of this object to 'Cube'
8. Do step (3.) again
There are two problems now.
1. How to set the pos of selected object to center
The line 64,
<CODE>
bpy.ops.transform.translate(value=(0, 0, 0), constraint_axis=(False, False, False), constraint_orientation='GLOBAL', mirror=False, proportional='DISABLED', proportional_edit_falloff='SMOOTH', proportional_size=1, snap=False, snap_target='CLOSEST', snap_point=(0, 0, 0), snap_align=False, snap_normal=(0, 0, 0), texture_space=False, release_confirm=False)
</CODE>
is, translate the object relatively. So this value=(0, 0, 0) means [no move].
I need to set the pos absolutely (0, 0, 0).
2. How to set the name of the selected object arbitrary.
I have to select the object named 'Cube', but duplicated next generation object has 'Cube.00x'.
How about it?
The all code is here.
<CODE>
# First import bpy to get access to well... everything in blender
from bpy import *
import bpy
#import math, mathutils, random
class GMP_Panel(bpy.types.Panel):
bl_label = "Generative Modeling"
bl_space_type = "VIEW_3D"
bl_region_type = "TOOLS"
def draw(self, context):
layout = self.layout
scn = bpy.context.scene
row = layout.row()
row.operator( "bpt.generate_op" )
class GMP_Operator(bpy.types.Operator):
bl_idname = "bpt.generate_op"
bl_label = "Generate"
def execute(self, context):
# Do 8 operations
clearAndSetNewObj()
operation1()
operation2()
operation3()
operation4()
operation5()
operation6()
operation7()
operation8()
return {'FINISHED'}
def register():
bpy.utils.register_class( GMP_Operator )
bpy.utils.register_class( GMP_Panel )
def unregister():
bpy.utils.register_class( GMP_Operator )
bpy.utils.register_class( GMP_Panel )
# Add first cube
bpy.ops.mesh.primitive_cube_add(view_align=False, enter_editmode=False, location=(0, 0, 0), rotation=(0, 0, 0), layers=(True, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False))
bpy.ops.object.add_named(linked=False, name="NewOrigin")
# SubSurf and Apply
bpy.ops.object.modifier_add(type='SUBSURF')
bpy.ops.object.modifier_apply(apply_as='DATA', modifier="Subsurf")
# clearAndSetNewObj
def clearAndSetNewObj():
bpy.ops.object.select_inverse()
bpy.ops.object.delete()
bpy.ops.object.select_all(action='TOGGLE')
# Move Selected Object to Center(0, 0, 0) ------------------
bpy.ops.transform.translate(value=(0, 0, 0), constraint_axis=(False, False, False), constraint_orientation='GLOBAL', mirror=False, proportional='DISABLED', proportional_edit_falloff='SMOOTH', proportional_size=1, snap=False, snap_target='CLOSEST', snap_point=(0, 0, 0), snap_align=False, snap_normal=(0, 0, 0), texture_space=False, release_confirm=False)
# op.1
def operation1():
bpy.ops.object.select_name(name="Cube", extend=False)
bpy.ops.object.duplicate_move(OBJECT_OT_duplicate={"linked":False, "mode":'TRANSLATION'}, TRANSFORM_OT_translate={"value":(-5, 5, 0), "constraint_axis":(False, False, False), "constraint_orientation":'GLOBAL', "mirror":False, "proportional":'DISABLED', "proportional_edit_falloff":'SMOOTH', "proportional_size":1, "snap":False, "snap_target":'CLOSEST', "snap_point":(0, 0, 0), "snap_align":False, "snap_normal":(0, 0, 0), "texture_space":False, "release_confirm":False})
ob = bpy.context.active_object
newGroup = ob.vertex_groups.new('vGroup')
bpy.ops.object.mode_set(mode='OBJECT')
# Lets loop through all the faces in the mesh
for f in ob.data.faces:
# See if the current face's centre is above 0.0 on the Z axis
if f.center[2] < -0.5:
# Set select to true
f.select = True
else:
f.select = False
bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.object.vertex_group_set_active(group='vGroup')
bpy.ops.object.vertex_group_assign(new=False)
bpy.ops.object.mode_set(mode='OBJECT')
# op.2
def operation2():
bpy.ops.object.select_name(name="Cube", extend=False)
bpy.ops.object.duplicate_move(OBJECT_OT_duplicate={"linked":False, "mode":'TRANSLATION'}, TRANSFORM_OT_translate={"value":(0, 5, 0), "constraint_axis":(False, False, False), "constraint_orientation":'GLOBAL', "mirror":False, "proportional":'DISABLED', "proportional_edit_falloff":'SMOOTH', "proportional_size":1, "snap":False, "snap_target":'CLOSEST', "snap_point":(0, 0, 0), "snap_align":False, "snap_normal":(0, 0, 0), "texture_space":False, "release_confirm":False})
ob = bpy.context.active_object
newGroup = ob.vertex_groups.new('vGroup')
bpy.ops.object.mode_set(mode='OBJECT')
# Lets loop through all the faces in the mesh
for f in ob.data.faces:
# See if the current face's centre is above 0.0 on the Z axis
if (f.center[0] > 0.5 or f.center[0] < -0.5):
# Set select to true
f.select = True
else:
f.select = False
bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.object.vertex_group_set_active(group='vGroup')
bpy.ops.object.vertex_group_assign(new=False)
bpy.ops.object.mode_set(mode='OBJECT')
# op.3
def operation3():
bpy.ops.object.select_name(name="Cube", extend=False)
bpy.ops.object.duplicate_move(OBJECT_OT_duplicate={"linked":False, "mode":'TRANSLATION'}, TRANSFORM_OT_translate={"value":(5, 5, 0), "constraint_axis":(False, False, False), "constraint_orientation":'GLOBAL', "mirror":False, "proportional":'DISABLED', "proportional_edit_falloff":'SMOOTH', "proportional_size":1, "snap":False, "snap_target":'CLOSEST', "snap_point":(0, 0, 0), "snap_align":False, "snap_normal":(0, 0, 0), "texture_space":False, "release_confirm":False})
# op.4
def operation4():
bpy.ops.object.select_name(name="Cube", extend=False)
bpy.ops.object.duplicate_move(OBJECT_OT_duplicate={"linked":False, "mode":'TRANSLATION'}, TRANSFORM_OT_translate={"value":(-5, 0, 0), "constraint_axis":(False, False, False), "constraint_orientation":'GLOBAL', "mirror":False, "proportional":'DISABLED', "proportional_edit_falloff":'SMOOTH', "proportional_size":1, "snap":False, "snap_target":'CLOSEST', "snap_point":(0, 0, 0), "snap_align":False, "snap_normal":(0, 0, 0), "texture_space":False, "release_confirm":False})
# op.5
def operation5():
bpy.ops.object.select_name(name="Cube", extend=False)
bpy.ops.object.duplicate_move(OBJECT_OT_duplicate={"linked":False, "mode":'TRANSLATION'}, TRANSFORM_OT_translate={"value":(5, 0, 0), "constraint_axis":(False, False, False), "constraint_orientation":'GLOBAL', "mirror":False, "proportional":'DISABLED', "proportional_edit_falloff":'SMOOTH', "proportional_size":1, "snap":False, "snap_target":'CLOSEST', "snap_point":(0, 0, 0), "snap_align":False, "snap_normal":(0, 0, 0), "texture_space":False, "release_confirm":False})
# op.6
def operation6():
bpy.ops.object.select_name(name="Cube", extend=False)
bpy.ops.object.duplicate_move(OBJECT_OT_duplicate={"linked":False, "mode":'TRANSLATION'}, TRANSFORM_OT_translate={"value":(-5, -5, 0), "constraint_axis":(False, False, False), "constraint_orientation":'GLOBAL', "mirror":False, "proportional":'DISABLED', "proportional_edit_falloff":'SMOOTH', "proportional_size":1, "snap":False, "snap_target":'CLOSEST', "snap_point":(0, 0, 0), "snap_align":False, "snap_normal":(0, 0, 0), "texture_space":False, "release_confirm":False})
# op.7
def operation7():
bpy.ops.object.select_name(name="Cube", extend=False)
bpy.ops.object.duplicate_move(OBJECT_OT_duplicate={"linked":False, "mode":'TRANSLATION'}, TRANSFORM_OT_translate={"value":(0, -5, 0), "constraint_axis":(False, False, False), "constraint_orientation":'GLOBAL', "mirror":False, "proportional":'DISABLED', "proportional_edit_falloff":'SMOOTH', "proportional_size":1, "snap":False, "snap_target":'CLOSEST', "snap_point":(0, 0, 0), "snap_align":False, "snap_normal":(0, 0, 0), "texture_space":False, "release_confirm":False})
# op.8
def operation8():
bpy.ops.object.select_name(name="Cube", extend=False)
bpy.ops.object.duplicate_move(OBJECT_OT_duplicate={"linked":False, "mode":'TRANSLATION'}, TRANSFORM_OT_translate={"value":(5, -5, 0), "constraint_axis":(False, False, False), "constraint_orientation":'GLOBAL', "mirror":False, "proportional":'DISABLED', "proportional_edit_falloff":'SMOOTH', "proportional_size":1, "snap":False, "snap_target":'CLOSEST', "snap_point":(0, 0, 0), "snap_align":False, "snap_normal":(0, 0, 0), "texture_space":False, "release_confirm":False})
bpy.ops.object.modifier_add(type='SUBSURF')
bpy.ops.object.modifier_apply(apply_as='DATA', modifier="Subsurf")
if __name__ == '__main__':
register()
</CODE>
No comments:
Post a Comment