1
$\begingroup$

I tried my script in the latest version 3.5.1 and 2.93.4 (and I even asked a friend to try it on the latest version too) and my script is not working like it should be and I can't figure out why.

Here is my script (sorry if it's messy) :

import bpy

# create metaball for every frame
number_mb = 1

for i in range (number_mb):
    bpy.ops.object.metaball_add(location=(0, 0, 0), scale=(1.5, 1.5, 1.5))
    bpy.context.object.scale.x = 1.503
    bpy.context.object.scale.y = 1.503
    bpy.context.object.scale.z = 1.503
    bpy.context.object.data.resolution = 0.1
    bpy.context.object.data.render_resolution = 0.1
    material = bpy.data.materials['hypercube']
    bpy.context.object.data.materials.append(material)



    # get a refrence to the currently active object
    # metaball = bpy.context.active_object

    # metaball in particle system
    bpy.data.particles["ParticleSettings"].instance_object = bpy.data.objects["Mball"]
    bpy.data.particles["ParticleSettings.005"].instance_object = bpy.data.objects["Mball"]

    # change keyframe
    bpy.context.scene.frame_current = i + 150
    
    # turn metaball into mesh 
    bpy.ops.object.convert(target='MESH')

    # metaball boolean modifier
    bpy.ops.object.modifier_add(type='BOOLEAN')
    bpy.context.object.modifiers["Boolean"].operation = 'INTERSECT'
    bpy.context.object.modifiers["Boolean"].object = bpy.data.objects["collision"]
    bpy.context.object.modifiers["Boolean"].solver = 'FAST'
    bpy.ops.object.modifier_apply(modifier="Boolean")
    
     #   copy location
    bpy.ops.object.constraint_add(type='COPY_LOCATION')
    bpy.context.object.constraints["Copy Location"].target = bpy.data.objects["grabby"]
    bpy.ops.object.constraint_add(type='COPY_SCALE')
    bpy.context.object.constraints["Copy Scale"].target = bpy.data.objects["grabby"]


    # metaball subdivision 1
#    bpy.ops.object.modifier_add(type='SUBSURF')
#    bpy.context.object.modifiers["Subdivision"].levels = 1
#    bpy.context.object.modifiers["Subdivision"].render_levels = 1
    
    
    # metaball hide/unhide 
    
    bpy.context.active_object.hide_viewport = False
    bpy.context.active_object.keyframe_insert("hide_viewport", frame = i + 150) 
    bpy.context.active_object.hide_viewport = True
    bpy.context.active_object.keyframe_insert("hide_viewport", frame = i + 151)
    bpy.context.active_object.hide_viewport = True
    bpy.context.active_object.keyframe_insert("hide_viewport", frame = 0)
    
    bpy.context.active_object.hide_render = False
    bpy.context.active_object.keyframe_insert("hide_render", frame = i + 150)
    bpy.context.active_object.hide_viewport = True
    bpy.context.active_object.keyframe_insert("hide_render", frame = i + 151)
    bpy.context.active_object.hide_viewport = True
    bpy.context.active_object.keyframe_insert("hide_render", frame = 0)

Only the last part where I hide/unhide is important but I'm still sharing the full thing. Basically at frame 0 I hide in the preview and the render the frame, at frame 150 I unhide it, then at frame 151 I hide it again. It works fine in the preview but not the render for some ungodly reason.

If anyone is interested in helping me here's a blend file (I took the script from my full project and made it run in a new blend file with some essential objects). You only need to hit run script beforehand.

In preview frame 150 and 151 Preview frame 150

Preview frame 151

In render frame 150 and 151 Render frame 150 Render frame 151

$\endgroup$
2
  • $\begingroup$ Please in future edit a closed post instead of deleting it. $\endgroup$ Commented Jun 7, 2023 at 7:28
  • $\begingroup$ That's interesting. In the closed question, you posted correct working code, but here it seems you did not change bpy.context.active_object.hide_viewport = True lines to hide_render after copy pasting. $\endgroup$ Commented Jun 7, 2023 at 9:33

2 Answers 2

3
$\begingroup$

It seems you copy-pasted your code:

bpy.context.active_object.hide_viewport = False
bpy.context.active_object.keyframe_insert("hide_viewport", frame = i + 150) 
bpy.context.active_object.hide_viewport = True
bpy.context.active_object.keyframe_insert("hide_viewport", frame = i + 151)
bpy.context.active_object.hide_viewport = True
bpy.context.active_object.keyframe_insert("hide_viewport", frame = 0)

In order to also hide in renders, but then you only replaced 4 out of 6 occurences of hide_viewport with hide_render.

Unfortunately while Blender has an option to automatically replace a phrase with another:

It doesn't have a common option "In Selection". Therefore in order to easily replace all occurences in a given text, by using "Replace All" option, ✲ CtrlX, ✲ CtrlV cut-paste it to another text block or a text editor of your choice, ✲ CtrlH replace text there, and copy-paste it back to your editor.

The fixed code becomes:

bpy.context.active_object.hide_render = False
bpy.context.active_object.keyframe_insert("hide_render", frame = i + 150) 
bpy.context.active_object.hide_render = True
bpy.context.active_object.keyframe_insert("hide_render", frame = i + 151)
bpy.context.active_object.hide_render = True
bpy.context.active_object.keyframe_insert("hide_render", frame = 0)

Even better, consider reducing the duplication of your code:

def keyframe(*, target, frame, state):
    target.hide_viewport = state
    target.keyframe_insert("hide_viewport", frame=frame)
    target.hide_render = state
    target.keyframe_insert("hide_render", frame=frame)
    
ob = bpy.context.active_object
keyframe(target=ob, frame=i+150, state=False)
keyframe(target=ob, frame=i+151, state=True)
keyframe(target=ob, frame=0, state=True)
$\endgroup$
-2
$\begingroup$

AI Icon Warning, Potential AI generated content⚠️

This post is suspected to have been generated with the help of an artificial intelligence, chat bot, or other language model tools without proper attribution.

There is reason to believe this post was not written by a human due to its structure, and grammatical construction, and has been marked by our community.
Current AI chat bots are a language models, that means they are like a parrot, they can mimic human language well, but they are built to write seemingly logical, seemingly factual grammatically valid text, rather than actually be correct, like a human would.
The veracity of the following content cannot be ascertained. See the Concerns regarding AI generated content.

Well Bing rambles on a bit, not the clearest wording but to summarize I believe the TLDR is in the last section, this line:

bpy.context.active_object.hide_viewport = True

Should be changed to:

bpy.context.active_object.hide_render = True

And this line:

bpy.context.active_object.hide_viewport = False

Should be changed to:

bpy.context.active_object.hide_render = False

Or alternatively you could use:

bpy.context.active_object.hide_set (True)

According to Bing: "It seems that the problem is that you are using the hide_viewport property to hide and unhide objects in render, which is not the correct way to do it. The hide_viewport property only affects the visibility of objects in the viewport, not in the render. To hide and unhide objects in render, the you should use the hide_render property instead.

The solution is to modify the code to use hide_render instead of hide_viewport. For example, instead of:

bpy.data.objects ["Cube"].hide_viewport = True

You should write:

bpy.data.objects ["Cube"].hide_render = True

This will hide the cube object in render, but not in the viewport. To unhide it, you can set the hide_render property to False.

Alternatively, you can also use the hide_set() method to hide and unhide objects in both viewport and render. For example, instead of:

bpy.data.objects ["Cube"].hide_viewport = True

You can write:

bpy.data.objects ["Cube"].hide_set (True)

This will hide the cube object in both viewport and render. To unhide it, you can pass False as an argument to the hide_set() method."

References:

How to hide a certain object in blender 2.8?

Hiding objects with scripting - unpredictable behaviour

$\endgroup$
3
  • $\begingroup$ not a good comment. bing ignored whatever you input in it $\endgroup$
    – Hyde
    Commented Jun 7, 2023 at 4:43
  • $\begingroup$ i am dumb as hell $\endgroup$
    – Hyde
    Commented Jun 7, 2023 at 4:46
  • $\begingroup$ I think the answer would be fine as is without the added AI generated ramblings (which by the way are false, hide_set will only hide the object in the viewport, please always double check when you're using AI generated content). Although the other answer by Markus is IMO of higher overall quality. $\endgroup$
    – Gorgious
    Commented Jun 7, 2023 at 9:45

You must log in to answer this question.

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