1
$\begingroup$

I wrote a script and I have been focusing on the hologram button I made. I used the selected operator but the button only works when I select the object and then run the script

import bpy
mat = bpy.data.materials.new(name = "Hologram")
mat.diffuse_color = (0,0.519,0.800)
mat.specular_intensity = 0
mat.emit = 1
mat.use_transparency = True
mat.alpha = 0.503
ob = bpy.context.active_object
data = ob.data
def item_type_lst(self,context):
    types={obj.type:obj.type for obj in context.scene.objects}
    return [(obj,obj,"")for obj in types]


def item_type_obj(self,context):
    return[(obj.name,obj.name,"") for obj in context.scene.objects if obj.type==context.scene.myEnumitem]

class SimpleOperator(bpy.types.Operator):
    bl_idname="dh.simple_opt"
    bl_label="Simple Operator"
    def execute(self,context):
        if len(ob.data.materials):
            ob.data.materials[0] = mat
        else:
           ob.data.materials.append(mat)
        ob.modifiers.new("part", type = 'PARTICLE_SYSTEM')
        part = ob.particle_systems[0]
        settings = part.settings
        settings.frame_start = 4
        settings.name = "Hologram"
        settings.frame_end = 34
        settings.lifetime = 250
        settings.count = len(data.polygons)
        settings.normal_factor = 0
        settings.physics_type = 'NO'
        settings.render_type = 'NONE'
        settings.effector_weights.gravity = 0
        settings.effector_weights.force = 0
        D = bpy.data
        test = "Hologram"
        bpy.ops.texture.new()
        text =bpy.data.textures[len(bpy.data.textures)-1]
        part = bpy.data.particles[test]
        tex = bpy.data.textures.new(test, 'BLEND')
        bpy.data.textures[test].use_flip_axis = 'VERTICAL'
        slot = part.texture_slots.add()
        slot.texture = tex

        return{'FINISHED'}

class View3dPanel():    
    bl_space_type="VIEW_3D"
    bl_region_type="TOOLS"
    bl_category="Tech"


class PanelA(View3dPanel,bpy.types.Panel):
    bl_label="Holograms"   


    def draw(self,context):
        layout=self.layout
        layout.operator(operator = "dh.simple_opt",text = "Hologram",icon = "OUTLINER_DATA_LAMP")
        self.layout.split()
        self.layout.split()
        self.layout.split()


        col=layout.column(align = True)

        col.label(text = "Mesh Objects")
        col.operator(operator = "mesh.primitive_cube_add",text = "Cube",icon = "MESH_CUBE")
        col.operator(operator = "mesh.primitive_cone_add",text = "Cone",icon = "MESH_CONE")
        row=col.row(align = True)
        row.operator(operator = "mesh.primitive_monkey_add",text = "Monkey",icon = "MESH_MONKEY")
        row.operator(operator = "mesh.primitive_torus_add",text = "Torus",icon = "MESH_TORUS")
        col.operator(operator = "mesh.primitive_cylinder_add",text = "Cylinder",icon = "MESH_CYLINDER")



class PanelB(View3dPanel,bpy.types.Panel):
    bl_label="Panel B"

    def draw(self,context):
        layout=self.layout
        layout=layout.box()
        layout.label(text = "object list",icon = "SORTALPHA")
        col=layout.column(align = False)
        col.prop(data = context.scene,property = "myEnumitem")
        col.prop(data = context.scene,property = "myEnumitem_obj")




bpy.types.Scene.myEnumitem=bpy.props.EnumProperty(name ="Obj Type",items=item_type_lst)
bpy.types.Scene.myEnumitem_obj=bpy.props.EnumProperty(name ="Object",items=item_type_obj)


if __name__=='__main__':
    bpy.utils.register_module(__name__)

Thanks in advance

$\endgroup$
2
  • $\begingroup$ Hi bunbot, could you please edit your code. In the blender text editor you can select all (ctrl-A), tab to the right (tab), copy (ctrl-v) paste here and formatting will be as is in blender.. Tab back one to the left, in blender text editor, with shift-tab. $\endgroup$ – batFINGER Jan 11 '17 at 6:29
  • $\begingroup$ I tried to fix it. Hope it was better. $\endgroup$ – user32399 Jan 11 '17 at 13:01
0
$\begingroup$

You defined ob and mat in global scope. If the script was run without a context object, ie context.active_object is None then you will get errors.

Added a poll method. If there is not context object the operator wont poll. Set C.scene.objects.active = None in the python console and the button will be disabled.

Made a small change to your script to define within the operator and to list callbacks to use self, since this is the scene as it is defined as a property on the Scene type object.

import bpy

def item_type_lst(self, context):
    types = {obj.type: obj.type for obj in self.objects}
    return [(obj, obj, "") for obj in types]


def item_type_obj(self, context):
    return[(obj.name, obj.name, "") for obj in self.objects if obj.type == self.myEnumitem]

class SimpleOperator(bpy.types.Operator):
    bl_idname = "dh.simple_opt"
    bl_label = "Simple Operator"

    @classmethod
    def poll(cls, context):
        return context.active_object is not None

    def execute(self, context):

        mat = bpy.data.materials.new(name="Hologram")
        mat.diffuse_color = (0, 0.519, 0.800)
        mat.specular_intensity = 0
        mat.emit = 1
        mat.use_transparency = True
        mat.alpha = 0.503
        ob = context.active_object
        data = ob.data
        if len(ob.data.materials):
            ob.data.materials[0] = mat
        else:
            ob.data.materials.append(mat)
        ob.modifiers.new("part", type='PARTICLE_SYSTEM')
        part = ob.particle_systems[0]
        settings = part.settings
        settings.frame_start = 4
        settings.name = "Hologram"
        settings.frame_end = 34
        settings.lifetime = 250
        settings.count = len(data.polygons)
        settings.normal_factor = 0
        settings.physics_type = 'NO'
        settings.render_type = 'NONE'
        settings.effector_weights.gravity = 0
        settings.effector_weights.force = 0
        D = bpy.data
        test = "Hologram"
        bpy.ops.texture.new()
        text = bpy.data.textures[-1]
        part = bpy.data.particles[test]
        tex = bpy.data.textures.new(test, 'BLEND')
        bpy.data.textures[test].use_flip_axis = 'VERTICAL'
        slot = part.texture_slots.add()
        slot.texture = tex

        return{'FINISHED'}

class View3dPanel():
    bl_space_type = "VIEW_3D"
    bl_region_type = "TOOLS"
    bl_category = "Tech"


class PanelA(View3dPanel, bpy.types.Panel):
    bl_label = "Holograms"

    def draw(self, context):
        layout = self.layout
        layout.operator(operator="dh.simple_opt", text="Hologram", icon='OUTLINER_DATA_LAMP')
        self.layout.split()
        self.layout.split()
        self.layout.split()

        col = layout.column(align=True)

        col.label(text="Mesh Objects")
        col.operator(operator="mesh.primitive_cube_add", text="Cube", icon="MESH_CUBE")
        col.operator(operator="mesh.primitive_cone_add", text="Cone", icon='MESH_CONE')
        row = col.row(align=True)
        row.operator(operator="mesh.primitive_monkey_add", text="Monkey", icon='MESH_MONKEY')
        row.operator(operator="mesh.primitive_torus_add", text="Torus", icon="MESH_TORUS")
        col.operator(operator="mesh.primitive_cylinder_add", text="Cylinder", icon="MESH_CYLINDER")


class PanelB(View3dPanel, bpy.types.Panel):
    bl_label = "Panel B"

    def draw(self, context):
        layout = self.layout
        layout = layout.box()
        layout.label(text="object list", icon='SORTALPHA')
        col = layout.column(align=False)
        col.prop(data=context.scene, property="myEnumitem")
        col.prop(data=context.scene, property="myEnumitem_obj")

def register():
    bpy.types.Scene.myEnumitem = bpy.props.EnumProperty(name="Obj Type", items=item_type_lst)
    bpy.types.Scene.myEnumitem_obj = bpy.props.EnumProperty(name="Object", items=item_type_obj)
    bpy.utils.register_module(__name__)

def unregister():
    bpy.utils.unregister_module(__name__)

if __name__ == '__main__':
    register()
$\endgroup$
1
  • $\begingroup$ Thanks, it's always helpful to have a second pair of eyes. $\endgroup$ – user32399 Jan 11 '17 at 15:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy